blob: edf0938611056118c450bf08bf48fe0d438a2d77 [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
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivity2ce49532010-07-26 14:37:46 +030049#define ByteOp (1<<16) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivity2ce49532010-07-26 14:37:46 +030051#define ImplicitOps (1<<17) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<17) /* Register operand. */
53#define DstMem (3<<17) /* Memory operand. */
54#define DstAcc (4<<17) /* Destination Accumulator */
55#define DstDI (5<<17) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<17) /* 64bit memory operand */
57#define DstMask (7<<17)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Avi Kivity2ce49532010-07-26 14:37:46 +030085#define GroupMask 0x0f /* Group number stored in bits 0:3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086/* Misc flags */
Avi Kivity047a4812010-07-26 14:37:47 +030087#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020088#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020089#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030090#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091/* Source 2 operand type */
92#define Src2None (0<<29)
93#define Src2CL (1<<29)
94#define Src2ImmByte (2<<29)
95#define Src2One (3<<29)
96#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Avi Kivityea9ef042010-07-29 15:11:34 +030098#define X2(x) x, x
99#define X3(x) X2(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +0300100#define X4(x) X2(x), X2(x)
Avi Kivityea9ef042010-07-29 15:11:34 +0300101#define X5(x) X4(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +0300102#define X6(x) X4(x), X2(x)
103#define X7(x) X4(x), X3(x)
104#define X8(x) X4(x), X4(x)
105#define X16(x) X8(x), X8(x)
106
Avi Kivity43bb19c2008-01-18 12:46:50 +0200107enum {
Avi Kivitye071edd2010-07-26 14:37:51 +0300108 Group1, Group1A, Group3, Group4, Group5, Group7, Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200109};
110
Avi Kivityd65b1de2010-07-29 15:11:35 +0300111struct opcode {
112 u32 flags;
113};
114
Avi Kivityfd853312010-07-29 15:11:36 +0300115#define D(_y) { .flags = (_y) }
116#define N D(0)
117
Avi Kivity42a1c522010-07-29 15:11:37 +0300118static struct opcode group_table[] = {
119 [Group1*8] =
120 X7(D(Lock)), N,
121 [Group1A*8] =
122 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
123 [Group3*8] =
124 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
125 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
126 X4(D(Undefined)),
127 [Group4*8] =
128 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
129 N, N, N, N, N, N,
130 [Group5*8] =
131 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
132 D(SrcMem | ModRM | Stack), N,
133 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
134 D(SrcMem | ModRM | Stack), N,
135 [Group7*8] =
136 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
137 D(SrcNone | ModRM | DstMem | Mov), N,
138 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
139 [Group8*8] =
140 N, N, N, N,
141 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
142 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
143 [Group9*8] =
144 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
145};
146
147static struct opcode group2_table[] = {
148 [Group7*8] =
149 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
150 D(SrcNone | ModRM | DstMem | Mov), N,
151 D(SrcMem16 | ModRM | Mov | Priv), N,
152 [Group9*8] =
153 N, N, N, N, N, N, N, N,
154};
155
Avi Kivityd65b1de2010-07-29 15:11:35 +0300156static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800157 /* 0x00 - 0x07 */
Avi Kivityfd853312010-07-29 15:11:36 +0300158 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
159 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
160 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
161 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0x08 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300163 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
164 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
165 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
166 D(ImplicitOps | Stack | No64), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 /* 0x10 - 0x17 */
Avi Kivityfd853312010-07-29 15:11:36 +0300168 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
169 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
170 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
171 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 /* 0x18 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300173 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
174 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
175 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
176 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 /* 0x20 - 0x27 */
Avi Kivityfd853312010-07-29 15:11:36 +0300178 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
179 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
180 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181 /* 0x28 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300182 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
183 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
184 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0x30 - 0x37 */
Avi Kivityfd853312010-07-29 15:11:36 +0300186 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
187 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
188 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0x38 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300190 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
191 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
192 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
193 N, N,
Avi Kivity749358a2010-07-26 14:37:40 +0300194 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300195 X16(D(DstReg)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300196 /* 0x50 - 0x57 */
Avi Kivityfd853312010-07-29 15:11:36 +0300197 X8(D(SrcReg | Stack)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300198 /* 0x58 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300199 X8(D(DstReg | Stack)),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700200 /* 0x60 - 0x67 */
Avi Kivityfd853312010-07-29 15:11:36 +0300201 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
202 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
203 N, N, N, N,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700204 /* 0x68 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300205 D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N,
206 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
207 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300208 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300209 X16(D(SrcImmByte)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210 /* 0x80 - 0x87 */
Avi Kivityfd853312010-07-29 15:11:36 +0300211 D(ByteOp | DstMem | SrcImm | ModRM | Group | Group1),
212 D(DstMem | SrcImm | ModRM | Group | Group1),
213 D(ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1),
214 D(DstMem | SrcImmByte | ModRM | Group | Group1),
215 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
216 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0x88 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300218 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
219 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
220 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
221 D(ImplicitOps | SrcMem16 | ModRM), D(Group | Group1A),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300222 /* 0x90 - 0x97 */
Avi Kivityfd853312010-07-29 15:11:36 +0300223 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300224 /* 0x98 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300225 N, N, D(SrcImmFAddr | No64), N,
226 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300228 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
229 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
230 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
231 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300233 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
234 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
235 D(ByteOp | DstDI | String), D(DstDI | String),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300236 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300237 X8(D(ByteOp | DstReg | SrcImm | Mov)),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300238 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300239 X8(D(DstReg | SrcImm | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 /* 0xC0 - 0xC7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300241 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
242 N, D(ImplicitOps | Stack), N, N,
243 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0xC8 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300245 N, N, N, D(ImplicitOps | Stack),
246 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247 /* 0xD0 - 0xD7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300248 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
249 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
250 N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 /* 0xD8 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300252 N, N, N, N, N, N, N, N,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300253 /* 0xE0 - 0xE7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300254 N, N, N, N,
255 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
256 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Nitin A Kamble098c9372007-08-19 11:00:36 +0300257 /* 0xE8 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300258 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
259 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
260 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
261 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xF0 - 0xF7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300263 N, N, N, N,
264 D(ImplicitOps | Priv), D(ImplicitOps), D(ByteOp | Group | Group3), D(Group | Group3),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xF8 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300266 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
267 D(ImplicitOps), D(ImplicitOps), D(Group | Group4), D(Group | Group5),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268};
269
Avi Kivityd65b1de2010-07-29 15:11:35 +0300270static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 /* 0x00 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300272 N, D(Group | GroupDual | Group7), N, N,
273 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
274 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
275 N, D(ImplicitOps | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 /* 0x10 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300277 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 /* 0x20 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300279 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
280 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
281 N, N, N, N,
282 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 /* 0x30 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300284 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
285 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
286 N, N, N, N, N, N, N, N,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300287 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300288 X16(D(DstReg | SrcMem | ModRM | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 /* 0x50 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300290 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 /* 0x60 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300292 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300294 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 /* 0x80 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300296 X16(D(SrcImm)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 /* 0x90 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300298 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300300 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
301 N, D(DstMem | SrcReg | ModRM | BitOp),
302 D(DstMem | SrcReg | Src2ImmByte | ModRM),
303 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300305 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
306 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
307 D(DstMem | SrcReg | Src2ImmByte | ModRM),
308 D(DstMem | SrcReg | Src2CL | ModRM),
309 D(ModRM), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300311 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
312 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
313 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
314 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300316 N, N,
317 D(Group | Group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
318 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
319 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320 /* 0xC0 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300321 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
322 N, N, N, D(Group | GroupDual | Group9),
323 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 /* 0xD0 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300325 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 /* 0xE0 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300327 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 /* 0xF0 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300329 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330};
331
Avi Kivityfd853312010-07-29 15:11:36 +0300332#undef D
333#undef N
334
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200336#define EFLG_ID (1<<21)
337#define EFLG_VIP (1<<20)
338#define EFLG_VIF (1<<19)
339#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200340#define EFLG_VM (1<<17)
341#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200342#define EFLG_IOPL (3<<12)
343#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344#define EFLG_OF (1<<11)
345#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200346#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200347#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348#define EFLG_SF (1<<7)
349#define EFLG_ZF (1<<6)
350#define EFLG_AF (1<<4)
351#define EFLG_PF (1<<2)
352#define EFLG_CF (1<<0)
353
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300354#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
355#define EFLG_RESERVED_ONE_MASK 2
356
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357/*
358 * Instruction emulation:
359 * Most instructions are emulated directly via a fragment of inline assembly
360 * code. This allows us to save/restore EFLAGS and thus very easily pick up
361 * any modified flags.
362 */
363
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800364#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365#define _LO32 "k" /* force 32-bit operand */
366#define _STK "%%rsp" /* stack pointer */
367#elif defined(__i386__)
368#define _LO32 "" /* force 32-bit operand */
369#define _STK "%%esp" /* stack pointer */
370#endif
371
372/*
373 * These EFLAGS bits are restored from saved value during emulation, and
374 * any changes are written back to the saved value after emulation.
375 */
376#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
377
378/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200379#define _PRE_EFLAGS(_sav, _msk, _tmp) \
380 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
381 "movl %"_sav",%"_LO32 _tmp"; " \
382 "push %"_tmp"; " \
383 "push %"_tmp"; " \
384 "movl %"_msk",%"_LO32 _tmp"; " \
385 "andl %"_LO32 _tmp",("_STK"); " \
386 "pushf; " \
387 "notl %"_LO32 _tmp"; " \
388 "andl %"_LO32 _tmp",("_STK"); " \
389 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
390 "pop %"_tmp"; " \
391 "orl %"_LO32 _tmp",("_STK"); " \
392 "popf; " \
393 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394
395/* After executing instruction: write-back necessary bits in EFLAGS. */
396#define _POST_EFLAGS(_sav, _msk, _tmp) \
397 /* _sav |= EFLAGS & _msk; */ \
398 "pushf; " \
399 "pop %"_tmp"; " \
400 "andl %"_msk",%"_LO32 _tmp"; " \
401 "orl %"_LO32 _tmp",%"_sav"; "
402
Avi Kivitydda96d82008-11-26 15:14:10 +0200403#ifdef CONFIG_X86_64
404#define ON64(x) x
405#else
406#define ON64(x)
407#endif
408
Avi Kivity6b7ad612008-11-26 15:30:45 +0200409#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
410 do { \
411 __asm__ __volatile__ ( \
412 _PRE_EFLAGS("0", "4", "2") \
413 _op _suffix " %"_x"3,%1; " \
414 _POST_EFLAGS("0", "4", "2") \
415 : "=m" (_eflags), "=m" ((_dst).val), \
416 "=&r" (_tmp) \
417 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200418 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200419
420
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421/* Raw emulation: instruction has two explicit operands. */
422#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200423 do { \
424 unsigned long _tmp; \
425 \
426 switch ((_dst).bytes) { \
427 case 2: \
428 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
429 break; \
430 case 4: \
431 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
432 break; \
433 case 8: \
434 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
435 break; \
436 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 } while (0)
438
439#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
440 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200441 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200444 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 break; \
446 default: \
447 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
448 _wx, _wy, _lx, _ly, _qx, _qy); \
449 break; \
450 } \
451 } while (0)
452
453/* Source operand is byte-sized and may be restricted to just %cl. */
454#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
455 __emulate_2op(_op, _src, _dst, _eflags, \
456 "b", "c", "b", "c", "b", "c", "b", "c")
457
458/* Source operand is byte, word, long or quad sized. */
459#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
460 __emulate_2op(_op, _src, _dst, _eflags, \
461 "b", "q", "w", "r", _LO32, "r", "", "r")
462
463/* Source operand is word, long or quad sized. */
464#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
465 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
466 "w", "r", _LO32, "r", "", "r")
467
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100468/* Instruction has three operands and one operand is stored in ECX register */
469#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
470 do { \
471 unsigned long _tmp; \
472 _type _clv = (_cl).val; \
473 _type _srcv = (_src).val; \
474 _type _dstv = (_dst).val; \
475 \
476 __asm__ __volatile__ ( \
477 _PRE_EFLAGS("0", "5", "2") \
478 _op _suffix " %4,%1 \n" \
479 _POST_EFLAGS("0", "5", "2") \
480 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
481 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
482 ); \
483 \
484 (_cl).val = (unsigned long) _clv; \
485 (_src).val = (unsigned long) _srcv; \
486 (_dst).val = (unsigned long) _dstv; \
487 } while (0)
488
489#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
490 do { \
491 switch ((_dst).bytes) { \
492 case 2: \
493 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
494 "w", unsigned short); \
495 break; \
496 case 4: \
497 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
498 "l", unsigned int); \
499 break; \
500 case 8: \
501 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
502 "q", unsigned long)); \
503 break; \
504 } \
505 } while (0)
506
Avi Kivitydda96d82008-11-26 15:14:10 +0200507#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 do { \
509 unsigned long _tmp; \
510 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200511 __asm__ __volatile__ ( \
512 _PRE_EFLAGS("0", "3", "2") \
513 _op _suffix " %1; " \
514 _POST_EFLAGS("0", "3", "2") \
515 : "=m" (_eflags), "+m" ((_dst).val), \
516 "=&r" (_tmp) \
517 : "i" (EFLAGS_MASK)); \
518 } while (0)
519
520/* Instruction has only one explicit operand (no source operand). */
521#define emulate_1op(_op, _dst, _eflags) \
522 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400523 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200524 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
525 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
526 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
527 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 } \
529 } while (0)
530
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531/* Fetch next part of the instruction being emulated. */
532#define insn_fetch(_type, _size, _eip) \
533({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200534 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200535 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 goto done; \
537 (_eip) += (_size); \
538 (_type)_x; \
539})
540
Gleb Natapov414e6272010-04-28 19:15:26 +0300541#define insn_fetch_arr(_arr, _size, _eip) \
542({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
543 if (rc != X86EMUL_CONTINUE) \
544 goto done; \
545 (_eip) += (_size); \
546})
547
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800548static inline unsigned long ad_mask(struct decode_cache *c)
549{
550 return (1UL << (c->ad_bytes << 3)) - 1;
551}
552
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800554static inline unsigned long
555address_mask(struct decode_cache *c, unsigned long reg)
556{
557 if (c->ad_bytes == sizeof(unsigned long))
558 return reg;
559 else
560 return reg & ad_mask(c);
561}
562
563static inline unsigned long
564register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
565{
566 return base + address_mask(c, reg);
567}
568
Harvey Harrison7a9572752008-02-19 07:40:41 -0800569static inline void
570register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
571{
572 if (c->ad_bytes == sizeof(unsigned long))
573 *reg += inc;
574 else
575 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
576}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577
Harvey Harrison7a9572752008-02-19 07:40:41 -0800578static inline void jmp_rel(struct decode_cache *c, int rel)
579{
580 register_address_increment(c, &c->eip, rel);
581}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300582
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300583static void set_seg_override(struct decode_cache *c, int seg)
584{
585 c->has_seg_override = true;
586 c->seg_override = seg;
587}
588
Gleb Natapov79168fd2010-04-28 19:15:30 +0300589static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
590 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300591{
592 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
593 return 0;
594
Gleb Natapov79168fd2010-04-28 19:15:30 +0300595 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300596}
597
598static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300599 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300600 struct decode_cache *c)
601{
602 if (!c->has_seg_override)
603 return 0;
604
Gleb Natapov79168fd2010-04-28 19:15:30 +0300605 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606}
607
Gleb Natapov79168fd2010-04-28 19:15:30 +0300608static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
609 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300610{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300611 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300612}
613
Gleb Natapov79168fd2010-04-28 19:15:30 +0300614static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
615 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300616{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300617 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300618}
619
Gleb Natapov54b84862010-04-28 19:15:44 +0300620static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
621 u32 error, bool valid)
622{
623 ctxt->exception = vec;
624 ctxt->error_code = error;
625 ctxt->error_code_valid = valid;
626 ctxt->restart = false;
627}
628
629static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
630{
631 emulate_exception(ctxt, GP_VECTOR, err, true);
632}
633
634static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
635 int err)
636{
637 ctxt->cr2 = addr;
638 emulate_exception(ctxt, PF_VECTOR, err, true);
639}
640
641static void emulate_ud(struct x86_emulate_ctxt *ctxt)
642{
643 emulate_exception(ctxt, UD_VECTOR, 0, false);
644}
645
646static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
647{
648 emulate_exception(ctxt, TS_VECTOR, err, true);
649}
650
Avi Kivity62266862007-11-20 13:15:52 +0200651static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
652 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300653 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200654{
655 struct fetch_cache *fc = &ctxt->decode.fetch;
656 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300657 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200658
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300659 if (eip == fc->end) {
660 cur_size = fc->end - fc->start;
661 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
662 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
663 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900664 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200665 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300666 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200667 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300668 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900669 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200670}
671
672static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
673 struct x86_emulate_ops *ops,
674 unsigned long eip, void *dest, unsigned size)
675{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900676 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200677
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200678 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200679 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200680 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200681 while (size--) {
682 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900683 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200684 return rc;
685 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900686 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200687}
688
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000689/*
690 * Given the 'reg' portion of a ModRM byte, and a register block, return a
691 * pointer into the block that addresses the relevant register.
692 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
693 */
694static void *decode_register(u8 modrm_reg, unsigned long *regs,
695 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696{
697 void *p;
698
699 p = &regs[modrm_reg];
700 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
701 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
702 return p;
703}
704
705static int read_descriptor(struct x86_emulate_ctxt *ctxt,
706 struct x86_emulate_ops *ops,
707 void *ptr,
708 u16 *size, unsigned long *address, int op_bytes)
709{
710 int rc;
711
712 if (op_bytes == 2)
713 op_bytes = 3;
714 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300715 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200716 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900717 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300719 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200720 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 return rc;
722}
723
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300724static int test_cc(unsigned int condition, unsigned int flags)
725{
726 int rc = 0;
727
728 switch ((condition & 15) >> 1) {
729 case 0: /* o */
730 rc |= (flags & EFLG_OF);
731 break;
732 case 1: /* b/c/nae */
733 rc |= (flags & EFLG_CF);
734 break;
735 case 2: /* z/e */
736 rc |= (flags & EFLG_ZF);
737 break;
738 case 3: /* be/na */
739 rc |= (flags & (EFLG_CF|EFLG_ZF));
740 break;
741 case 4: /* s */
742 rc |= (flags & EFLG_SF);
743 break;
744 case 5: /* p/pe */
745 rc |= (flags & EFLG_PF);
746 break;
747 case 7: /* le/ng */
748 rc |= (flags & EFLG_ZF);
749 /* fall through */
750 case 6: /* l/nge */
751 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
752 break;
753 }
754
755 /* Odd condition identifiers (lsb == 1) have inverted sense. */
756 return (!!rc ^ (condition & 1));
757}
758
Avi Kivity3c118e22007-10-31 10:27:04 +0200759static void decode_register_operand(struct operand *op,
760 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200761 int inhibit_bytereg)
762{
Avi Kivity33615aa2007-10-31 11:15:56 +0200763 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200764 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200765
766 if (!(c->d & ModRM))
767 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200768 op->type = OP_REG;
769 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200770 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200771 op->val = *(u8 *)op->ptr;
772 op->bytes = 1;
773 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200774 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200775 op->bytes = c->op_bytes;
776 switch (op->bytes) {
777 case 2:
778 op->val = *(u16 *)op->ptr;
779 break;
780 case 4:
781 op->val = *(u32 *)op->ptr;
782 break;
783 case 8:
784 op->val = *(u64 *) op->ptr;
785 break;
786 }
787 }
788 op->orig_val = op->val;
789}
790
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791static int decode_modrm(struct x86_emulate_ctxt *ctxt,
792 struct x86_emulate_ops *ops)
793{
794 struct decode_cache *c = &ctxt->decode;
795 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700796 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900797 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200798
799 if (c->rex_prefix) {
800 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
801 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
802 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
803 }
804
805 c->modrm = insn_fetch(u8, 1, c->eip);
806 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
807 c->modrm_reg |= (c->modrm & 0x38) >> 3;
808 c->modrm_rm |= (c->modrm & 0x07);
809 c->modrm_ea = 0;
810 c->use_modrm_ea = 1;
811
812 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300813 c->modrm_ptr = decode_register(c->modrm_rm,
814 c->regs, c->d & ByteOp);
815 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200816 return rc;
817 }
818
819 if (c->ad_bytes == 2) {
820 unsigned bx = c->regs[VCPU_REGS_RBX];
821 unsigned bp = c->regs[VCPU_REGS_RBP];
822 unsigned si = c->regs[VCPU_REGS_RSI];
823 unsigned di = c->regs[VCPU_REGS_RDI];
824
825 /* 16-bit ModR/M decode. */
826 switch (c->modrm_mod) {
827 case 0:
828 if (c->modrm_rm == 6)
829 c->modrm_ea += insn_fetch(u16, 2, c->eip);
830 break;
831 case 1:
832 c->modrm_ea += insn_fetch(s8, 1, c->eip);
833 break;
834 case 2:
835 c->modrm_ea += insn_fetch(u16, 2, c->eip);
836 break;
837 }
838 switch (c->modrm_rm) {
839 case 0:
840 c->modrm_ea += bx + si;
841 break;
842 case 1:
843 c->modrm_ea += bx + di;
844 break;
845 case 2:
846 c->modrm_ea += bp + si;
847 break;
848 case 3:
849 c->modrm_ea += bp + di;
850 break;
851 case 4:
852 c->modrm_ea += si;
853 break;
854 case 5:
855 c->modrm_ea += di;
856 break;
857 case 6:
858 if (c->modrm_mod != 0)
859 c->modrm_ea += bp;
860 break;
861 case 7:
862 c->modrm_ea += bx;
863 break;
864 }
865 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
866 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300867 if (!c->has_seg_override)
868 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200869 c->modrm_ea = (u16)c->modrm_ea;
870 } else {
871 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700872 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 sib = insn_fetch(u8, 1, c->eip);
874 index_reg |= (sib >> 3) & 7;
875 base_reg |= sib & 7;
876 scale = sib >> 6;
877
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700878 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
879 c->modrm_ea += insn_fetch(s32, 4, c->eip);
880 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700882 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200883 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700884 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
885 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700886 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700887 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889 switch (c->modrm_mod) {
890 case 0:
891 if (c->modrm_rm == 5)
892 c->modrm_ea += insn_fetch(s32, 4, c->eip);
893 break;
894 case 1:
895 c->modrm_ea += insn_fetch(s8, 1, c->eip);
896 break;
897 case 2:
898 c->modrm_ea += insn_fetch(s32, 4, c->eip);
899 break;
900 }
901 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902done:
903 return rc;
904}
905
906static int decode_abs(struct x86_emulate_ctxt *ctxt,
907 struct x86_emulate_ops *ops)
908{
909 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900910 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200911
912 switch (c->ad_bytes) {
913 case 2:
914 c->modrm_ea = insn_fetch(u16, 2, c->eip);
915 break;
916 case 4:
917 c->modrm_ea = insn_fetch(u32, 4, c->eip);
918 break;
919 case 8:
920 c->modrm_ea = insn_fetch(u64, 8, c->eip);
921 break;
922 }
923done:
924 return rc;
925}
926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200928x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900931 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 int mode = ctxt->mode;
Avi Kivity52811d72010-07-26 14:37:48 +0300933 int def_op_bytes, def_ad_bytes, group, dual;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935
Gleb Natapov5cd21912010-03-18 15:20:26 +0200936 /* we cannot decode insn before we complete previous rep insn */
937 WARN_ON(ctxt->restart);
938
Gleb Natapov063db062010-03-18 15:20:06 +0200939 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300940 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300941 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942
943 switch (mode) {
944 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200945 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200947 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 break;
949 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200950 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800952#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200954 def_op_bytes = 4;
955 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 break;
957#endif
958 default:
959 return -1;
960 }
961
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200962 c->op_bytes = def_op_bytes;
963 c->ad_bytes = def_ad_bytes;
964
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200966 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 /* switch between 2/4 bytes */
970 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 break;
972 case 0x67: /* address-size override */
973 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200974 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200975 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200977 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200978 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300981 case 0x2e: /* CS override */
982 case 0x36: /* SS override */
983 case 0x3e: /* DS override */
984 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 break;
986 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300988 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200990 case 0x40 ... 0x4f: /* REX */
991 if (mode != X86EMUL_MODE_PROT64)
992 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200993 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200994 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200998 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100999 c->rep_prefix = REPNE_PREFIX;
1000 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001002 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 default:
1005 goto done_prefixes;
1006 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001007
1008 /* Any legacy prefix after a REX prefix nullifies its effect. */
1009
Avi Kivity33615aa2007-10-31 11:15:56 +02001010 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011 }
1012
1013done_prefixes:
1014
1015 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001016 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001017 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
1020 /* Opcode byte(s). */
Avi Kivityd65b1de2010-07-29 15:11:35 +03001021 c->d = opcode_table[c->b].flags;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001022 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001024 if (c->b == 0x0f) {
1025 c->twobyte = 1;
1026 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivityd65b1de2010-07-29 15:11:35 +03001027 c->d = twobyte_table[c->b].flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001029 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Avi Kivitye09d0822008-01-18 12:38:59 +02001031 if (c->d & Group) {
1032 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001033 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001034 c->modrm = insn_fetch(u8, 1, c->eip);
1035 --c->eip;
1036
1037 group = (group << 3) + ((c->modrm >> 3) & 7);
Avi Kivity52811d72010-07-26 14:37:48 +03001038 c->d &= ~(Group | GroupDual | GroupMask);
1039 if (dual && (c->modrm >> 6) == 3)
Avi Kivityd65b1de2010-07-29 15:11:35 +03001040 c->d |= group2_table[group].flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001041 else
Avi Kivityd65b1de2010-07-29 15:11:35 +03001042 c->d |= group_table[group].flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001043 }
1044
1045 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001046 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001047 DPRINTF("Cannot emulate %02x\n", c->b);
1048 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 }
1050
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001051 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1052 c->op_bytes = 8;
1053
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001055 if (c->d & ModRM)
1056 rc = decode_modrm(ctxt, ops);
1057 else if (c->d & MemAbs)
1058 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001059 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001060 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001062 if (!c->has_seg_override)
1063 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001064
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001065 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001066 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001067
1068 if (c->ad_bytes != 8)
1069 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001070
1071 if (c->rip_relative)
1072 c->modrm_ea += c->eip;
1073
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 /*
1075 * Decode and fetch the source operand: register, memory
1076 * or immediate.
1077 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001078 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 case SrcNone:
1080 break;
1081 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001082 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 break;
1084 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001085 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 goto srcmem_common;
1087 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001088 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 goto srcmem_common;
1090 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 c->src.bytes = (c->d & ByteOp) ? 1 :
1092 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001093 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001094 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001095 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001096 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001097 /*
1098 * For instructions with a ModR/M byte, switch to register
1099 * access if Mod = 3.
1100 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001101 if ((c->d & ModRM) && c->modrm_mod == 3) {
1102 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001103 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001104 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001105 break;
1106 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001107 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001108 c->src.ptr = (unsigned long *)c->modrm_ea;
1109 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 break;
1111 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001112 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 c->src.type = OP_IMM;
1114 c->src.ptr = (unsigned long *)c->eip;
1115 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1116 if (c->src.bytes == 8)
1117 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 break;
1123 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001124 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 break;
1126 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001127 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 break;
1129 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001130 if ((c->d & SrcMask) == SrcImmU) {
1131 switch (c->src.bytes) {
1132 case 1:
1133 c->src.val &= 0xff;
1134 break;
1135 case 2:
1136 c->src.val &= 0xffff;
1137 break;
1138 case 4:
1139 c->src.val &= 0xffffffff;
1140 break;
1141 }
1142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 break;
1144 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001145 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001146 c->src.type = OP_IMM;
1147 c->src.ptr = (unsigned long *)c->eip;
1148 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001149 if ((c->d & SrcMask) == SrcImmByte)
1150 c->src.val = insn_fetch(s8, 1, c->eip);
1151 else
1152 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001154 case SrcAcc:
1155 c->src.type = OP_REG;
1156 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1157 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1158 switch (c->src.bytes) {
1159 case 1:
1160 c->src.val = *(u8 *)c->src.ptr;
1161 break;
1162 case 2:
1163 c->src.val = *(u16 *)c->src.ptr;
1164 break;
1165 case 4:
1166 c->src.val = *(u32 *)c->src.ptr;
1167 break;
1168 case 8:
1169 c->src.val = *(u64 *)c->src.ptr;
1170 break;
1171 }
1172 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001173 case SrcOne:
1174 c->src.bytes = 1;
1175 c->src.val = 1;
1176 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001177 case SrcSI:
1178 c->src.type = OP_MEM;
1179 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1180 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001181 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001182 c->regs[VCPU_REGS_RSI]);
1183 c->src.val = 0;
1184 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001185 case SrcImmFAddr:
1186 c->src.type = OP_IMM;
1187 c->src.ptr = (unsigned long *)c->eip;
1188 c->src.bytes = c->op_bytes + 2;
1189 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1190 break;
1191 case SrcMemFAddr:
1192 c->src.type = OP_MEM;
1193 c->src.ptr = (unsigned long *)c->modrm_ea;
1194 c->src.bytes = c->op_bytes + 2;
1195 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 }
1197
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001198 /*
1199 * Decode and fetch the second source operand: register, memory
1200 * or immediate.
1201 */
1202 switch (c->d & Src2Mask) {
1203 case Src2None:
1204 break;
1205 case Src2CL:
1206 c->src2.bytes = 1;
1207 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1208 break;
1209 case Src2ImmByte:
1210 c->src2.type = OP_IMM;
1211 c->src2.ptr = (unsigned long *)c->eip;
1212 c->src2.bytes = 1;
1213 c->src2.val = insn_fetch(u8, 1, c->eip);
1214 break;
1215 case Src2One:
1216 c->src2.bytes = 1;
1217 c->src2.val = 1;
1218 break;
1219 }
1220
Avi Kivity038e51d2007-01-22 20:40:40 -08001221 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001222 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001223 case ImplicitOps:
1224 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001225 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001226 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001227 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001228 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001229 break;
1230 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001231 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001232 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001233 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001234 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001235 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001236 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001237 break;
1238 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001239 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001240 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001241 if ((c->d & DstMask) == DstMem64)
1242 c->dst.bytes = 8;
1243 else
1244 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001245 c->dst.val = 0;
1246 if (c->d & BitOp) {
1247 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1248
1249 c->dst.ptr = (void *)c->dst.ptr +
1250 (c->src.val & mask) / 8;
1251 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001252 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001253 case DstAcc:
1254 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001255 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001256 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001257 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001258 case 1:
1259 c->dst.val = *(u8 *)c->dst.ptr;
1260 break;
1261 case 2:
1262 c->dst.val = *(u16 *)c->dst.ptr;
1263 break;
1264 case 4:
1265 c->dst.val = *(u32 *)c->dst.ptr;
1266 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001267 case 8:
1268 c->dst.val = *(u64 *)c->dst.ptr;
1269 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001270 }
1271 c->dst.orig_val = c->dst.val;
1272 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001273 case DstDI:
1274 c->dst.type = OP_MEM;
1275 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1276 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001277 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001278 c->regs[VCPU_REGS_RDI]);
1279 c->dst.val = 0;
1280 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001281 }
1282
1283done:
1284 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1285}
1286
Gleb Natapov9de41572010-04-28 19:15:22 +03001287static int read_emulated(struct x86_emulate_ctxt *ctxt,
1288 struct x86_emulate_ops *ops,
1289 unsigned long addr, void *dest, unsigned size)
1290{
1291 int rc;
1292 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001293 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001294
1295 while (size) {
1296 int n = min(size, 8u);
1297 size -= n;
1298 if (mc->pos < mc->end)
1299 goto read_cached;
1300
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001301 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1302 ctxt->vcpu);
1303 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001304 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001305 if (rc != X86EMUL_CONTINUE)
1306 return rc;
1307 mc->end += n;
1308
1309 read_cached:
1310 memcpy(dest, mc->data + mc->pos, n);
1311 mc->pos += n;
1312 dest += n;
1313 addr += n;
1314 }
1315 return X86EMUL_CONTINUE;
1316}
1317
Gleb Natapov7b262e92010-03-18 15:20:27 +02001318static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1319 struct x86_emulate_ops *ops,
1320 unsigned int size, unsigned short port,
1321 void *dest)
1322{
1323 struct read_cache *rc = &ctxt->decode.io_read;
1324
1325 if (rc->pos == rc->end) { /* refill pio read ahead */
1326 struct decode_cache *c = &ctxt->decode;
1327 unsigned int in_page, n;
1328 unsigned int count = c->rep_prefix ?
1329 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1330 in_page = (ctxt->eflags & EFLG_DF) ?
1331 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1332 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1333 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1334 count);
1335 if (n == 0)
1336 n = 1;
1337 rc->pos = rc->end = 0;
1338 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1339 return 0;
1340 rc->end = n * size;
1341 }
1342
1343 memcpy(dest, rc->data + rc->pos, size);
1344 rc->pos += size;
1345 return 1;
1346}
1347
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001348static u32 desc_limit_scaled(struct desc_struct *desc)
1349{
1350 u32 limit = get_desc_limit(desc);
1351
1352 return desc->g ? (limit << 12) | 0xfff : limit;
1353}
1354
1355static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1356 struct x86_emulate_ops *ops,
1357 u16 selector, struct desc_ptr *dt)
1358{
1359 if (selector & 1 << 2) {
1360 struct desc_struct desc;
1361 memset (dt, 0, sizeof *dt);
1362 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1363 return;
1364
1365 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1366 dt->address = get_desc_base(&desc);
1367 } else
1368 ops->get_gdt(dt, ctxt->vcpu);
1369}
1370
1371/* allowed just for 8 bytes segments */
1372static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1373 struct x86_emulate_ops *ops,
1374 u16 selector, struct desc_struct *desc)
1375{
1376 struct desc_ptr dt;
1377 u16 index = selector >> 3;
1378 int ret;
1379 u32 err;
1380 ulong addr;
1381
1382 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1383
1384 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001385 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386 return X86EMUL_PROPAGATE_FAULT;
1387 }
1388 addr = dt.address + index * 8;
1389 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1390 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001391 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392
1393 return ret;
1394}
1395
1396/* allowed just for 8 bytes segments */
1397static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1398 struct x86_emulate_ops *ops,
1399 u16 selector, struct desc_struct *desc)
1400{
1401 struct desc_ptr dt;
1402 u16 index = selector >> 3;
1403 u32 err;
1404 ulong addr;
1405 int ret;
1406
1407 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1408
1409 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001410 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411 return X86EMUL_PROPAGATE_FAULT;
1412 }
1413
1414 addr = dt.address + index * 8;
1415 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1416 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001417 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418
1419 return ret;
1420}
1421
1422static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1423 struct x86_emulate_ops *ops,
1424 u16 selector, int seg)
1425{
1426 struct desc_struct seg_desc;
1427 u8 dpl, rpl, cpl;
1428 unsigned err_vec = GP_VECTOR;
1429 u32 err_code = 0;
1430 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1431 int ret;
1432
1433 memset(&seg_desc, 0, sizeof seg_desc);
1434
1435 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1436 || ctxt->mode == X86EMUL_MODE_REAL) {
1437 /* set real mode segment descriptor */
1438 set_desc_base(&seg_desc, selector << 4);
1439 set_desc_limit(&seg_desc, 0xffff);
1440 seg_desc.type = 3;
1441 seg_desc.p = 1;
1442 seg_desc.s = 1;
1443 goto load;
1444 }
1445
1446 /* NULL selector is not valid for TR, CS and SS */
1447 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1448 && null_selector)
1449 goto exception;
1450
1451 /* TR should be in GDT only */
1452 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1453 goto exception;
1454
1455 if (null_selector) /* for NULL selector skip all following checks */
1456 goto load;
1457
1458 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1459 if (ret != X86EMUL_CONTINUE)
1460 return ret;
1461
1462 err_code = selector & 0xfffc;
1463 err_vec = GP_VECTOR;
1464
1465 /* can't load system descriptor into segment selecor */
1466 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1467 goto exception;
1468
1469 if (!seg_desc.p) {
1470 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1471 goto exception;
1472 }
1473
1474 rpl = selector & 3;
1475 dpl = seg_desc.dpl;
1476 cpl = ops->cpl(ctxt->vcpu);
1477
1478 switch (seg) {
1479 case VCPU_SREG_SS:
1480 /*
1481 * segment is not a writable data segment or segment
1482 * selector's RPL != CPL or segment selector's RPL != CPL
1483 */
1484 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1485 goto exception;
1486 break;
1487 case VCPU_SREG_CS:
1488 if (!(seg_desc.type & 8))
1489 goto exception;
1490
1491 if (seg_desc.type & 4) {
1492 /* conforming */
1493 if (dpl > cpl)
1494 goto exception;
1495 } else {
1496 /* nonconforming */
1497 if (rpl > cpl || dpl != cpl)
1498 goto exception;
1499 }
1500 /* CS(RPL) <- CPL */
1501 selector = (selector & 0xfffc) | cpl;
1502 break;
1503 case VCPU_SREG_TR:
1504 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1505 goto exception;
1506 break;
1507 case VCPU_SREG_LDTR:
1508 if (seg_desc.s || seg_desc.type != 2)
1509 goto exception;
1510 break;
1511 default: /* DS, ES, FS, or GS */
1512 /*
1513 * segment is not a data or readable code segment or
1514 * ((segment is a data or nonconforming code segment)
1515 * and (both RPL and CPL > DPL))
1516 */
1517 if ((seg_desc.type & 0xa) == 0x8 ||
1518 (((seg_desc.type & 0xc) != 0xc) &&
1519 (rpl > dpl && cpl > dpl)))
1520 goto exception;
1521 break;
1522 }
1523
1524 if (seg_desc.s) {
1525 /* mark segment as accessed */
1526 seg_desc.type |= 1;
1527 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1528 if (ret != X86EMUL_CONTINUE)
1529 return ret;
1530 }
1531load:
1532 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1533 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1534 return X86EMUL_CONTINUE;
1535exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001536 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 return X86EMUL_PROPAGATE_FAULT;
1538}
1539
Wei Yongjunc37eda12010-06-15 09:03:33 +08001540static inline int writeback(struct x86_emulate_ctxt *ctxt,
1541 struct x86_emulate_ops *ops)
1542{
1543 int rc;
1544 struct decode_cache *c = &ctxt->decode;
1545 u32 err;
1546
1547 switch (c->dst.type) {
1548 case OP_REG:
1549 /* The 4-byte case *is* correct:
1550 * in 64-bit mode we zero-extend.
1551 */
1552 switch (c->dst.bytes) {
1553 case 1:
1554 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1555 break;
1556 case 2:
1557 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1558 break;
1559 case 4:
1560 *c->dst.ptr = (u32)c->dst.val;
1561 break; /* 64b: zero-ext */
1562 case 8:
1563 *c->dst.ptr = c->dst.val;
1564 break;
1565 }
1566 break;
1567 case OP_MEM:
1568 if (c->lock_prefix)
1569 rc = ops->cmpxchg_emulated(
1570 (unsigned long)c->dst.ptr,
1571 &c->dst.orig_val,
1572 &c->dst.val,
1573 c->dst.bytes,
1574 &err,
1575 ctxt->vcpu);
1576 else
1577 rc = ops->write_emulated(
1578 (unsigned long)c->dst.ptr,
1579 &c->dst.val,
1580 c->dst.bytes,
1581 &err,
1582 ctxt->vcpu);
1583 if (rc == X86EMUL_PROPAGATE_FAULT)
1584 emulate_pf(ctxt,
1585 (unsigned long)c->dst.ptr, err);
1586 if (rc != X86EMUL_CONTINUE)
1587 return rc;
1588 break;
1589 case OP_NONE:
1590 /* no writeback */
1591 break;
1592 default:
1593 break;
1594 }
1595 return X86EMUL_CONTINUE;
1596}
1597
Gleb Natapov79168fd2010-04-28 19:15:30 +03001598static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1599 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001600{
1601 struct decode_cache *c = &ctxt->decode;
1602
1603 c->dst.type = OP_MEM;
1604 c->dst.bytes = c->op_bytes;
1605 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001606 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001608 c->regs[VCPU_REGS_RSP]);
1609}
1610
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001611static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001612 struct x86_emulate_ops *ops,
1613 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614{
1615 struct decode_cache *c = &ctxt->decode;
1616 int rc;
1617
Gleb Natapov79168fd2010-04-28 19:15:30 +03001618 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001619 c->regs[VCPU_REGS_RSP]),
1620 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001621 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001622 return rc;
1623
Avi Kivity350f69d2009-01-05 11:12:40 +02001624 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001625 return rc;
1626}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001627
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001628static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1629 struct x86_emulate_ops *ops,
1630 void *dest, int len)
1631{
1632 int rc;
1633 unsigned long val, change_mask;
1634 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001635 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001636
1637 rc = emulate_pop(ctxt, ops, &val, len);
1638 if (rc != X86EMUL_CONTINUE)
1639 return rc;
1640
1641 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1642 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1643
1644 switch(ctxt->mode) {
1645 case X86EMUL_MODE_PROT64:
1646 case X86EMUL_MODE_PROT32:
1647 case X86EMUL_MODE_PROT16:
1648 if (cpl == 0)
1649 change_mask |= EFLG_IOPL;
1650 if (cpl <= iopl)
1651 change_mask |= EFLG_IF;
1652 break;
1653 case X86EMUL_MODE_VM86:
1654 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001655 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001656 return X86EMUL_PROPAGATE_FAULT;
1657 }
1658 change_mask |= EFLG_IF;
1659 break;
1660 default: /* real mode */
1661 change_mask |= (EFLG_IOPL | EFLG_IF);
1662 break;
1663 }
1664
1665 *(unsigned long *)dest =
1666 (ctxt->eflags & ~change_mask) | (val & change_mask);
1667
1668 return rc;
1669}
1670
Gleb Natapov79168fd2010-04-28 19:15:30 +03001671static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1672 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001673{
1674 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001675
Gleb Natapov79168fd2010-04-28 19:15:30 +03001676 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001677
Gleb Natapov79168fd2010-04-28 19:15:30 +03001678 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001679}
1680
1681static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1682 struct x86_emulate_ops *ops, int seg)
1683{
1684 struct decode_cache *c = &ctxt->decode;
1685 unsigned long selector;
1686 int rc;
1687
1688 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001689 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001690 return rc;
1691
Gleb Natapov2e873022010-03-18 15:20:18 +02001692 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001693 return rc;
1694}
1695
Wei Yongjunc37eda12010-06-15 09:03:33 +08001696static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001697 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001698{
1699 struct decode_cache *c = &ctxt->decode;
1700 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001701 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001702 int reg = VCPU_REGS_RAX;
1703
1704 while (reg <= VCPU_REGS_RDI) {
1705 (reg == VCPU_REGS_RSP) ?
1706 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1707
Gleb Natapov79168fd2010-04-28 19:15:30 +03001708 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001709
1710 rc = writeback(ctxt, ops);
1711 if (rc != X86EMUL_CONTINUE)
1712 return rc;
1713
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001714 ++reg;
1715 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001716
1717 /* Disable writeback. */
1718 c->dst.type = OP_NONE;
1719
1720 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001721}
1722
1723static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1724 struct x86_emulate_ops *ops)
1725{
1726 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001727 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001728 int reg = VCPU_REGS_RDI;
1729
1730 while (reg >= VCPU_REGS_RAX) {
1731 if (reg == VCPU_REGS_RSP) {
1732 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1733 c->op_bytes);
1734 --reg;
1735 }
1736
1737 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001738 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001739 break;
1740 --reg;
1741 }
1742 return rc;
1743}
1744
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001745static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1746 struct x86_emulate_ops *ops)
1747{
1748 struct decode_cache *c = &ctxt->decode;
1749 int rc = X86EMUL_CONTINUE;
1750 unsigned long temp_eip = 0;
1751 unsigned long temp_eflags = 0;
1752 unsigned long cs = 0;
1753 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1754 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1755 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1756 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1757
1758 /* TODO: Add stack limit check */
1759
1760 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1761
1762 if (rc != X86EMUL_CONTINUE)
1763 return rc;
1764
1765 if (temp_eip & ~0xffff) {
1766 emulate_gp(ctxt, 0);
1767 return X86EMUL_PROPAGATE_FAULT;
1768 }
1769
1770 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1771
1772 if (rc != X86EMUL_CONTINUE)
1773 return rc;
1774
1775 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1776
1777 if (rc != X86EMUL_CONTINUE)
1778 return rc;
1779
1780 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1781
1782 if (rc != X86EMUL_CONTINUE)
1783 return rc;
1784
1785 c->eip = temp_eip;
1786
1787
1788 if (c->op_bytes == 4)
1789 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1790 else if (c->op_bytes == 2) {
1791 ctxt->eflags &= ~0xffff;
1792 ctxt->eflags |= temp_eflags;
1793 }
1794
1795 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1796 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1797
1798 return rc;
1799}
1800
1801static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1802 struct x86_emulate_ops* ops)
1803{
1804 switch(ctxt->mode) {
1805 case X86EMUL_MODE_REAL:
1806 return emulate_iret_real(ctxt, ops);
1807 case X86EMUL_MODE_VM86:
1808 case X86EMUL_MODE_PROT16:
1809 case X86EMUL_MODE_PROT32:
1810 case X86EMUL_MODE_PROT64:
1811 default:
1812 /* iret from protected mode unimplemented yet */
1813 return X86EMUL_UNHANDLEABLE;
1814 }
1815}
1816
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001817static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1818 struct x86_emulate_ops *ops)
1819{
1820 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001821
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001822 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823}
1824
Laurent Vivier05f086f2007-09-24 11:10:55 +02001825static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001826{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001827 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001828 switch (c->modrm_reg) {
1829 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001830 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001831 break;
1832 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001833 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001834 break;
1835 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001836 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001837 break;
1838 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001839 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001840 break;
1841 case 4: /* sal/shl */
1842 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001843 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844 break;
1845 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001846 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001847 break;
1848 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001849 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001850 break;
1851 }
1852}
1853
1854static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001855 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856{
1857 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001858
1859 switch (c->modrm_reg) {
1860 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001861 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001862 break;
1863 case 2: /* not */
1864 c->dst.val = ~c->dst.val;
1865 break;
1866 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001867 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001868 break;
1869 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001870 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001872 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873}
1874
1875static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001876 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877{
1878 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001879
1880 switch (c->modrm_reg) {
1881 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001882 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883 break;
1884 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001885 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001887 case 2: /* call near abs */ {
1888 long int old_eip;
1889 old_eip = c->eip;
1890 c->eip = c->src.val;
1891 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001892 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001893 break;
1894 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001895 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001896 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897 break;
1898 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001902 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001903}
1904
1905static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001906 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907{
1908 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001909 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001910
1911 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1912 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001913 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1914 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001915 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001916 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001917 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1918 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001919
Laurent Vivier05f086f2007-09-24 11:10:55 +02001920 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001921 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001922 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923}
1924
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001925static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1926 struct x86_emulate_ops *ops)
1927{
1928 struct decode_cache *c = &ctxt->decode;
1929 int rc;
1930 unsigned long cs;
1931
1932 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001933 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001934 return rc;
1935 if (c->op_bytes == 4)
1936 c->eip = (u32)c->eip;
1937 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001938 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001939 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001940 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001941 return rc;
1942}
1943
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001944static inline void
1945setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001946 struct x86_emulate_ops *ops, struct desc_struct *cs,
1947 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001948{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001949 memset(cs, 0, sizeof(struct desc_struct));
1950 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1951 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001952
1953 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001954 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001955 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001956 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001957 cs->type = 0x0b; /* Read, Execute, Accessed */
1958 cs->s = 1;
1959 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001960 cs->p = 1;
1961 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001962
Gleb Natapov79168fd2010-04-28 19:15:30 +03001963 set_desc_base(ss, 0); /* flat segment */
1964 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001965 ss->g = 1; /* 4kb granularity */
1966 ss->s = 1;
1967 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001968 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001969 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001970 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001971}
1972
1973static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001974emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001975{
1976 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001977 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001978 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001979 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001980
1981 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001982 if (ctxt->mode == X86EMUL_MODE_REAL ||
1983 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001984 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001985 return X86EMUL_PROPAGATE_FAULT;
1986 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001987
Gleb Natapov79168fd2010-04-28 19:15:30 +03001988 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001989
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001990 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001992 cs_sel = (u16)(msr_data & 0xfffc);
1993 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001994
1995 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001997 cs.l = 1;
1998 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2000 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2001 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2002 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002003
2004 c->regs[VCPU_REGS_RCX] = c->eip;
2005 if (is_long_mode(ctxt->vcpu)) {
2006#ifdef CONFIG_X86_64
2007 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
2008
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002009 ops->get_msr(ctxt->vcpu,
2010 ctxt->mode == X86EMUL_MODE_PROT64 ?
2011 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002012 c->eip = msr_data;
2013
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002014 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002015 ctxt->eflags &= ~(msr_data | EFLG_RF);
2016#endif
2017 } else {
2018 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002019 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002020 c->eip = (u32)msr_data;
2021
2022 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2023 }
2024
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002025 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002026}
2027
Andre Przywara8c604352009-06-18 12:56:01 +02002028static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002029emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002030{
2031 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002032 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002033 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002034 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002035
Gleb Natapova0044752010-02-10 14:21:31 +02002036 /* inject #GP if in real mode */
2037 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002038 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002039 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002040 }
2041
2042 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2043 * Therefore, we inject an #UD.
2044 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002045 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002046 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002047 return X86EMUL_PROPAGATE_FAULT;
2048 }
Andre Przywara8c604352009-06-18 12:56:01 +02002049
Gleb Natapov79168fd2010-04-28 19:15:30 +03002050 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002051
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002052 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002053 switch (ctxt->mode) {
2054 case X86EMUL_MODE_PROT32:
2055 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002056 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002057 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002058 }
2059 break;
2060 case X86EMUL_MODE_PROT64:
2061 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002062 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002063 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002064 }
2065 break;
2066 }
2067
2068 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 cs_sel = (u16)msr_data;
2070 cs_sel &= ~SELECTOR_RPL_MASK;
2071 ss_sel = cs_sel + 8;
2072 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002073 if (ctxt->mode == X86EMUL_MODE_PROT64
2074 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002075 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002076 cs.l = 1;
2077 }
2078
Gleb Natapov79168fd2010-04-28 19:15:30 +03002079 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2080 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2081 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2082 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002083
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002084 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002085 c->eip = msr_data;
2086
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002087 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002088 c->regs[VCPU_REGS_RSP] = msr_data;
2089
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002090 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002091}
2092
Andre Przywara4668f052009-06-18 12:56:02 +02002093static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002094emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002095{
2096 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002097 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002098 u64 msr_data;
2099 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002100 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002101
Gleb Natapova0044752010-02-10 14:21:31 +02002102 /* inject #GP if in real mode or Virtual 8086 mode */
2103 if (ctxt->mode == X86EMUL_MODE_REAL ||
2104 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002105 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002106 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002107 }
2108
Gleb Natapov79168fd2010-04-28 19:15:30 +03002109 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002110
2111 if ((c->rex_prefix & 0x8) != 0x0)
2112 usermode = X86EMUL_MODE_PROT64;
2113 else
2114 usermode = X86EMUL_MODE_PROT32;
2115
2116 cs.dpl = 3;
2117 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002118 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002119 switch (usermode) {
2120 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002121 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002122 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002123 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002124 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002125 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002126 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002127 break;
2128 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002129 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002130 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002131 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002132 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002133 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002134 ss_sel = cs_sel + 8;
2135 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002136 cs.l = 1;
2137 break;
2138 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002139 cs_sel |= SELECTOR_RPL_MASK;
2140 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002141
Gleb Natapov79168fd2010-04-28 19:15:30 +03002142 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2143 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2144 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2145 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002146
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002147 c->eip = c->regs[VCPU_REGS_RDX];
2148 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002149
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002150 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002151}
2152
Gleb Natapov9c537242010-03-18 15:20:05 +02002153static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2154 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002155{
2156 int iopl;
2157 if (ctxt->mode == X86EMUL_MODE_REAL)
2158 return false;
2159 if (ctxt->mode == X86EMUL_MODE_VM86)
2160 return true;
2161 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002162 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002163}
2164
2165static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2166 struct x86_emulate_ops *ops,
2167 u16 port, u16 len)
2168{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002169 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002170 int r;
2171 u16 io_bitmap_ptr;
2172 u8 perm, bit_idx = port & 0x7;
2173 unsigned mask = (1 << len) - 1;
2174
Gleb Natapov79168fd2010-04-28 19:15:30 +03002175 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2176 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002177 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002179 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002180 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2181 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002182 if (r != X86EMUL_CONTINUE)
2183 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002184 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002185 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002186 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2187 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002188 if (r != X86EMUL_CONTINUE)
2189 return false;
2190 if ((perm >> bit_idx) & mask)
2191 return false;
2192 return true;
2193}
2194
2195static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2196 struct x86_emulate_ops *ops,
2197 u16 port, u16 len)
2198{
Gleb Natapov9c537242010-03-18 15:20:05 +02002199 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002200 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2201 return false;
2202 return true;
2203}
2204
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2206 struct x86_emulate_ops *ops,
2207 struct tss_segment_16 *tss)
2208{
2209 struct decode_cache *c = &ctxt->decode;
2210
2211 tss->ip = c->eip;
2212 tss->flag = ctxt->eflags;
2213 tss->ax = c->regs[VCPU_REGS_RAX];
2214 tss->cx = c->regs[VCPU_REGS_RCX];
2215 tss->dx = c->regs[VCPU_REGS_RDX];
2216 tss->bx = c->regs[VCPU_REGS_RBX];
2217 tss->sp = c->regs[VCPU_REGS_RSP];
2218 tss->bp = c->regs[VCPU_REGS_RBP];
2219 tss->si = c->regs[VCPU_REGS_RSI];
2220 tss->di = c->regs[VCPU_REGS_RDI];
2221
2222 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2223 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2224 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2225 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2226 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2227}
2228
2229static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2230 struct x86_emulate_ops *ops,
2231 struct tss_segment_16 *tss)
2232{
2233 struct decode_cache *c = &ctxt->decode;
2234 int ret;
2235
2236 c->eip = tss->ip;
2237 ctxt->eflags = tss->flag | 2;
2238 c->regs[VCPU_REGS_RAX] = tss->ax;
2239 c->regs[VCPU_REGS_RCX] = tss->cx;
2240 c->regs[VCPU_REGS_RDX] = tss->dx;
2241 c->regs[VCPU_REGS_RBX] = tss->bx;
2242 c->regs[VCPU_REGS_RSP] = tss->sp;
2243 c->regs[VCPU_REGS_RBP] = tss->bp;
2244 c->regs[VCPU_REGS_RSI] = tss->si;
2245 c->regs[VCPU_REGS_RDI] = tss->di;
2246
2247 /*
2248 * SDM says that segment selectors are loaded before segment
2249 * descriptors
2250 */
2251 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2252 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2253 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2254 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2255 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2256
2257 /*
2258 * Now load segment descriptors. If fault happenes at this stage
2259 * it is handled in a context of new task
2260 */
2261 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2262 if (ret != X86EMUL_CONTINUE)
2263 return ret;
2264 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2265 if (ret != X86EMUL_CONTINUE)
2266 return ret;
2267 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2268 if (ret != X86EMUL_CONTINUE)
2269 return ret;
2270 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2271 if (ret != X86EMUL_CONTINUE)
2272 return ret;
2273 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2274 if (ret != X86EMUL_CONTINUE)
2275 return ret;
2276
2277 return X86EMUL_CONTINUE;
2278}
2279
2280static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2281 struct x86_emulate_ops *ops,
2282 u16 tss_selector, u16 old_tss_sel,
2283 ulong old_tss_base, struct desc_struct *new_desc)
2284{
2285 struct tss_segment_16 tss_seg;
2286 int ret;
2287 u32 err, new_tss_base = get_desc_base(new_desc);
2288
2289 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2290 &err);
2291 if (ret == X86EMUL_PROPAGATE_FAULT) {
2292 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002293 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294 return ret;
2295 }
2296
2297 save_state_to_tss16(ctxt, ops, &tss_seg);
2298
2299 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2300 &err);
2301 if (ret == X86EMUL_PROPAGATE_FAULT) {
2302 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002303 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304 return ret;
2305 }
2306
2307 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2308 &err);
2309 if (ret == X86EMUL_PROPAGATE_FAULT) {
2310 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002311 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312 return ret;
2313 }
2314
2315 if (old_tss_sel != 0xffff) {
2316 tss_seg.prev_task_link = old_tss_sel;
2317
2318 ret = ops->write_std(new_tss_base,
2319 &tss_seg.prev_task_link,
2320 sizeof tss_seg.prev_task_link,
2321 ctxt->vcpu, &err);
2322 if (ret == X86EMUL_PROPAGATE_FAULT) {
2323 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002324 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325 return ret;
2326 }
2327 }
2328
2329 return load_state_from_tss16(ctxt, ops, &tss_seg);
2330}
2331
2332static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2333 struct x86_emulate_ops *ops,
2334 struct tss_segment_32 *tss)
2335{
2336 struct decode_cache *c = &ctxt->decode;
2337
2338 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2339 tss->eip = c->eip;
2340 tss->eflags = ctxt->eflags;
2341 tss->eax = c->regs[VCPU_REGS_RAX];
2342 tss->ecx = c->regs[VCPU_REGS_RCX];
2343 tss->edx = c->regs[VCPU_REGS_RDX];
2344 tss->ebx = c->regs[VCPU_REGS_RBX];
2345 tss->esp = c->regs[VCPU_REGS_RSP];
2346 tss->ebp = c->regs[VCPU_REGS_RBP];
2347 tss->esi = c->regs[VCPU_REGS_RSI];
2348 tss->edi = c->regs[VCPU_REGS_RDI];
2349
2350 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2351 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2352 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2353 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2354 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2355 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2356 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2357}
2358
2359static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2360 struct x86_emulate_ops *ops,
2361 struct tss_segment_32 *tss)
2362{
2363 struct decode_cache *c = &ctxt->decode;
2364 int ret;
2365
Gleb Natapov0f122442010-04-28 19:15:31 +03002366 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002367 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002368 return X86EMUL_PROPAGATE_FAULT;
2369 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002370 c->eip = tss->eip;
2371 ctxt->eflags = tss->eflags | 2;
2372 c->regs[VCPU_REGS_RAX] = tss->eax;
2373 c->regs[VCPU_REGS_RCX] = tss->ecx;
2374 c->regs[VCPU_REGS_RDX] = tss->edx;
2375 c->regs[VCPU_REGS_RBX] = tss->ebx;
2376 c->regs[VCPU_REGS_RSP] = tss->esp;
2377 c->regs[VCPU_REGS_RBP] = tss->ebp;
2378 c->regs[VCPU_REGS_RSI] = tss->esi;
2379 c->regs[VCPU_REGS_RDI] = tss->edi;
2380
2381 /*
2382 * SDM says that segment selectors are loaded before segment
2383 * descriptors
2384 */
2385 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2386 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2387 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2388 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2389 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2390 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2391 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2392
2393 /*
2394 * Now load segment descriptors. If fault happenes at this stage
2395 * it is handled in a context of new task
2396 */
2397 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2398 if (ret != X86EMUL_CONTINUE)
2399 return ret;
2400 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2401 if (ret != X86EMUL_CONTINUE)
2402 return ret;
2403 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2404 if (ret != X86EMUL_CONTINUE)
2405 return ret;
2406 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2407 if (ret != X86EMUL_CONTINUE)
2408 return ret;
2409 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2410 if (ret != X86EMUL_CONTINUE)
2411 return ret;
2412 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2413 if (ret != X86EMUL_CONTINUE)
2414 return ret;
2415 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2416 if (ret != X86EMUL_CONTINUE)
2417 return ret;
2418
2419 return X86EMUL_CONTINUE;
2420}
2421
2422static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2423 struct x86_emulate_ops *ops,
2424 u16 tss_selector, u16 old_tss_sel,
2425 ulong old_tss_base, struct desc_struct *new_desc)
2426{
2427 struct tss_segment_32 tss_seg;
2428 int ret;
2429 u32 err, new_tss_base = get_desc_base(new_desc);
2430
2431 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2432 &err);
2433 if (ret == X86EMUL_PROPAGATE_FAULT) {
2434 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002435 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 return ret;
2437 }
2438
2439 save_state_to_tss32(ctxt, ops, &tss_seg);
2440
2441 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2442 &err);
2443 if (ret == X86EMUL_PROPAGATE_FAULT) {
2444 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002445 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446 return ret;
2447 }
2448
2449 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2450 &err);
2451 if (ret == X86EMUL_PROPAGATE_FAULT) {
2452 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002453 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454 return ret;
2455 }
2456
2457 if (old_tss_sel != 0xffff) {
2458 tss_seg.prev_task_link = old_tss_sel;
2459
2460 ret = ops->write_std(new_tss_base,
2461 &tss_seg.prev_task_link,
2462 sizeof tss_seg.prev_task_link,
2463 ctxt->vcpu, &err);
2464 if (ret == X86EMUL_PROPAGATE_FAULT) {
2465 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002466 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467 return ret;
2468 }
2469 }
2470
2471 return load_state_from_tss32(ctxt, ops, &tss_seg);
2472}
2473
2474static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002475 struct x86_emulate_ops *ops,
2476 u16 tss_selector, int reason,
2477 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478{
2479 struct desc_struct curr_tss_desc, next_tss_desc;
2480 int ret;
2481 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2482 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002483 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002484 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002485
2486 /* FIXME: old_tss_base == ~0 ? */
2487
2488 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2489 if (ret != X86EMUL_CONTINUE)
2490 return ret;
2491 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2492 if (ret != X86EMUL_CONTINUE)
2493 return ret;
2494
2495 /* FIXME: check that next_tss_desc is tss */
2496
2497 if (reason != TASK_SWITCH_IRET) {
2498 if ((tss_selector & 3) > next_tss_desc.dpl ||
2499 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002500 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501 return X86EMUL_PROPAGATE_FAULT;
2502 }
2503 }
2504
Gleb Natapovceffb452010-03-18 15:20:19 +02002505 desc_limit = desc_limit_scaled(&next_tss_desc);
2506 if (!next_tss_desc.p ||
2507 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2508 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002509 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510 return X86EMUL_PROPAGATE_FAULT;
2511 }
2512
2513 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2514 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2515 write_segment_descriptor(ctxt, ops, old_tss_sel,
2516 &curr_tss_desc);
2517 }
2518
2519 if (reason == TASK_SWITCH_IRET)
2520 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2521
2522 /* set back link to prev task only if NT bit is set in eflags
2523 note that old_tss_sel is not used afetr this point */
2524 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2525 old_tss_sel = 0xffff;
2526
2527 if (next_tss_desc.type & 8)
2528 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2529 old_tss_base, &next_tss_desc);
2530 else
2531 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2532 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002533 if (ret != X86EMUL_CONTINUE)
2534 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535
2536 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2537 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2538
2539 if (reason != TASK_SWITCH_IRET) {
2540 next_tss_desc.type |= (1 << 1); /* set busy flag */
2541 write_segment_descriptor(ctxt, ops, tss_selector,
2542 &next_tss_desc);
2543 }
2544
2545 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2546 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2547 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2548
Jan Kiszkae269fb22010-04-14 15:51:09 +02002549 if (has_error_code) {
2550 struct decode_cache *c = &ctxt->decode;
2551
2552 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2553 c->lock_prefix = 0;
2554 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002555 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002556 }
2557
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 return ret;
2559}
2560
2561int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2562 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002563 u16 tss_selector, int reason,
2564 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565{
2566 struct decode_cache *c = &ctxt->decode;
2567 int rc;
2568
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002570 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571
Jan Kiszkae269fb22010-04-14 15:51:09 +02002572 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2573 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574
2575 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002576 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002577 if (rc == X86EMUL_CONTINUE)
2578 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 }
2580
Gleb Natapov19d04432010-04-15 12:29:50 +03002581 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582}
2583
Gleb Natapova682e352010-03-18 15:20:21 +02002584static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002585 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002586{
2587 struct decode_cache *c = &ctxt->decode;
2588 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2589
Gleb Natapovd9271122010-03-18 15:20:22 +02002590 register_address_increment(c, &c->regs[reg], df * op->bytes);
2591 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002592}
2593
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002594int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002595x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002596{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002597 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002598 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002599 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002600 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002601
Gleb Natapov9de41572010-04-28 19:15:22 +03002602 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002603
Gleb Natapov11616242010-02-11 14:43:14 +02002604 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002605 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002606 goto done;
2607 }
2608
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002609 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002610 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002611 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002612 goto done;
2613 }
2614
Gleb Natapove92805a2010-02-10 14:21:35 +02002615 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002616 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002617 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002618 goto done;
2619 }
2620
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002621 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002622 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002623 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002624 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002625 string_done:
2626 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002627 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002628 goto done;
2629 }
2630 /* The second termination condition only applies for REPE
2631 * and REPNE. Test if the repeat string operation prefix is
2632 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2633 * corresponding termination condition according to:
2634 * - if REPE/REPZ and ZF = 0 then done
2635 * - if REPNE/REPNZ and ZF = 1 then done
2636 */
2637 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002638 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002639 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002640 ((ctxt->eflags & EFLG_ZF) == 0))
2641 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002642 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002643 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2644 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002645 }
Gleb Natapov063db062010-03-18 15:20:06 +02002646 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002647 }
2648
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002649 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002650 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002651 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002652 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002653 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002654 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002655 }
2656
Gleb Natapove35b7b92010-02-25 16:36:42 +02002657 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002658 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2659 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002660 if (rc != X86EMUL_CONTINUE)
2661 goto done;
2662 }
2663
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002664 if ((c->d & DstMask) == ImplicitOps)
2665 goto special_insn;
2666
2667
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002668 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2669 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002670 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2671 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002672 if (rc != X86EMUL_CONTINUE)
2673 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002674 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002675 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002676
Avi Kivity018a98d2007-11-27 19:30:56 +02002677special_insn:
2678
Laurent Viviere4e03de2007-09-18 11:52:50 +02002679 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 goto twobyte_insn;
2681
Laurent Viviere4e03de2007-09-18 11:52:50 +02002682 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 case 0x00 ... 0x05:
2684 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002685 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002687 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002688 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002689 break;
2690 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002691 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002692 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002693 goto done;
2694 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 case 0x08 ... 0x0d:
2696 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002697 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002699 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002700 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 case 0x10 ... 0x15:
2703 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002704 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002706 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002707 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002708 break;
2709 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002710 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002711 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002712 goto done;
2713 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 case 0x18 ... 0x1d:
2715 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002716 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002718 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002719 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002720 break;
2721 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002722 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002723 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002724 goto done;
2725 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002726 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002728 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 break;
2730 case 0x28 ... 0x2d:
2731 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002732 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 break;
2734 case 0x30 ... 0x35:
2735 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002736 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 break;
2738 case 0x38 ... 0x3d:
2739 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002740 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002742 case 0x40 ... 0x47: /* inc r16/r32 */
2743 emulate_1op("inc", c->dst, ctxt->eflags);
2744 break;
2745 case 0x48 ... 0x4f: /* dec r16/r32 */
2746 emulate_1op("dec", c->dst, ctxt->eflags);
2747 break;
2748 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002749 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002750 break;
2751 case 0x58 ... 0x5f: /* pop reg */
2752 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002753 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002754 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002755 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002756 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002757 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002758 rc = emulate_pusha(ctxt, ops);
2759 if (rc != X86EMUL_CONTINUE)
2760 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002761 break;
2762 case 0x61: /* popa */
2763 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002764 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002765 goto done;
2766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002768 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002770 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002772 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002773 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002774 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002775 break;
2776 case 0x6c: /* insb */
2777 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002778 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002779 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002780 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002781 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002782 goto done;
2783 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002784 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2785 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002786 goto done; /* IO is needed, skip writeback */
2787 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002788 case 0x6e: /* outsb */
2789 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002790 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002791 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002792 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002793 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002794 goto done;
2795 }
Gleb Natapov79729952010-03-18 15:20:24 +02002796 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2797 &c->src.val, 1, ctxt->vcpu);
2798
2799 c->dst.type = OP_NONE; /* nothing to writeback */
2800 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002801 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002802 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002803 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002804 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002806 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 case 0:
2808 goto add;
2809 case 1:
2810 goto or;
2811 case 2:
2812 goto adc;
2813 case 3:
2814 goto sbb;
2815 case 4:
2816 goto and;
2817 case 5:
2818 goto sub;
2819 case 6:
2820 goto xor;
2821 case 7:
2822 goto cmp;
2823 }
2824 break;
2825 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002826 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002827 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 break;
2829 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002830 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002832 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002834 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 break;
2836 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002837 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 break;
2839 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002840 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 break; /* 64b reg: zero-extend */
2842 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002843 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 break;
2845 }
2846 /*
2847 * Write back the memory destination with implicit LOCK
2848 * prefix.
2849 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002850 c->dst.val = c->src.val;
2851 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002854 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002855 case 0x8c: /* mov r/m, sreg */
2856 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002857 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002858 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002859 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002860 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002861 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002862 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002863 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002864 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002865 case 0x8e: { /* mov seg, r/m16 */
2866 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002867
2868 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002869
Gleb Natapovc6975182010-02-18 12:15:01 +02002870 if (c->modrm_reg == VCPU_SREG_CS ||
2871 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002872 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002873 goto done;
2874 }
2875
Glauber Costa310b5d32009-05-12 16:21:06 -04002876 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002877 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002878
Gleb Natapov2e873022010-03-18 15:20:18 +02002879 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002880
2881 c->dst.type = OP_NONE; /* Disable writeback. */
2882 break;
2883 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002885 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002886 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002889 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002890 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2891 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002892 break;
2893 }
2894 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002895 c->src.type = OP_REG;
2896 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002897 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2898 c->src.val = *(c->src.ptr);
2899 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002900 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002901 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002902 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002903 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002904 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002905 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002906 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002907 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002908 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2909 if (rc != X86EMUL_CONTINUE)
2910 goto done;
2911 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002912 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002914 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002916 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002917 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002918 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002919 case 0xa8 ... 0xa9: /* test ax, imm */
2920 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002922 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 break;
2924 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002925 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 case 0xae ... 0xaf: /* scas */
2927 DPRINTF("Urk! I don't handle SCAS.\n");
2928 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002929 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002930 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002931 case 0xc0 ... 0xc1:
2932 emulate_grp2(ctxt);
2933 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002934 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002935 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002936 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002937 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002938 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002939 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2940 mov:
2941 c->dst.val = c->src.val;
2942 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002943 case 0xcb: /* ret far */
2944 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002945 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002946 goto done;
2947 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002948 case 0xcf: /* iret */
2949 rc = emulate_iret(ctxt, ops);
2950
2951 if (rc != X86EMUL_CONTINUE)
2952 goto done;
2953 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002954 case 0xd0 ... 0xd1: /* Grp2 */
2955 c->src.val = 1;
2956 emulate_grp2(ctxt);
2957 break;
2958 case 0xd2 ... 0xd3: /* Grp2 */
2959 c->src.val = c->regs[VCPU_REGS_RCX];
2960 emulate_grp2(ctxt);
2961 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002962 case 0xe4: /* inb */
2963 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002964 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002965 case 0xe6: /* outb */
2966 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002967 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002968 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002969 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002970 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002971 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002972 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002973 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002974 }
2975 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002976 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002977 case 0xea: { /* jmp far */
2978 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002979 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002980 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2981
2982 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002983 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002984
Gleb Natapov414e6272010-04-28 19:15:26 +03002985 c->eip = 0;
2986 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002987 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002988 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002989 case 0xeb:
2990 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002991 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002992 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002993 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002994 case 0xec: /* in al,dx */
2995 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002996 c->src.val = c->regs[VCPU_REGS_RDX];
2997 do_io_in:
2998 c->dst.bytes = min(c->dst.bytes, 4u);
2999 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003000 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003001 goto done;
3002 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003003 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3004 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003005 goto done; /* IO is needed */
3006 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003007 case 0xee: /* out dx,al */
3008 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003009 c->src.val = c->regs[VCPU_REGS_RDX];
3010 do_io_out:
3011 c->dst.bytes = min(c->dst.bytes, 4u);
3012 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003013 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003014 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003015 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003016 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3017 ctxt->vcpu);
3018 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003019 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003020 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003021 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003022 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003023 case 0xf5: /* cmc */
3024 /* complement carry flag from eflags reg */
3025 ctxt->eflags ^= EFLG_CF;
3026 c->dst.type = OP_NONE; /* Disable writeback. */
3027 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003028 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003029 if (!emulate_grp3(ctxt, ops))
3030 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003031 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003032 case 0xf8: /* clc */
3033 ctxt->eflags &= ~EFLG_CF;
3034 c->dst.type = OP_NONE; /* Disable writeback. */
3035 break;
3036 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003037 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003038 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003039 goto done;
3040 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003041 ctxt->eflags &= ~X86_EFLAGS_IF;
3042 c->dst.type = OP_NONE; /* Disable writeback. */
3043 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003044 break;
3045 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003046 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003047 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003048 goto done;
3049 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003050 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003051 ctxt->eflags |= X86_EFLAGS_IF;
3052 c->dst.type = OP_NONE; /* Disable writeback. */
3053 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003054 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003055 case 0xfc: /* cld */
3056 ctxt->eflags &= ~EFLG_DF;
3057 c->dst.type = OP_NONE; /* Disable writeback. */
3058 break;
3059 case 0xfd: /* std */
3060 ctxt->eflags |= EFLG_DF;
3061 c->dst.type = OP_NONE; /* Disable writeback. */
3062 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003063 case 0xfe: /* Grp4 */
3064 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003065 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003066 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003067 goto done;
3068 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003069 case 0xff: /* Grp5 */
3070 if (c->modrm_reg == 5)
3071 goto jump_far;
3072 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003073 default:
3074 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003076
3077writeback:
3078 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003079 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003080 goto done;
3081
Gleb Natapov5cd21912010-03-18 15:20:26 +02003082 /*
3083 * restore dst type in case the decoding will be reused
3084 * (happens for string instruction )
3085 */
3086 c->dst.type = saved_dst_type;
3087
Gleb Natapova682e352010-03-18 15:20:21 +02003088 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003089 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3090 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003091
3092 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003093 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3094 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003095
Gleb Natapov5cd21912010-03-18 15:20:26 +02003096 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003097 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003098 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003099 /*
3100 * Re-enter guest when pio read ahead buffer is empty or,
3101 * if it is not used, after each 1024 iteration.
3102 */
3103 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3104 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003105 ctxt->restart = false;
3106 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003107 /*
3108 * reset read cache here in case string instruction is restared
3109 * without decoding
3110 */
3111 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003112 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003113
3114done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003115 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116
3117twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003118 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003120 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 u16 size;
3122 unsigned long address;
3123
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003124 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003125 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003126 goto cannot_emulate;
3127
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003128 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003129 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003130 goto done;
3131
Avi Kivity33e38852008-05-21 15:34:25 +03003132 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003133 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003134 /* Disable writeback. */
3135 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003136 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003138 rc = read_descriptor(ctxt, ops, c->src.ptr,
3139 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003140 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 goto done;
3142 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003143 /* Disable writeback. */
3144 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003146 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003147 if (c->modrm_mod == 3) {
3148 switch (c->modrm_rm) {
3149 case 1:
3150 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003151 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003152 goto done;
3153 break;
3154 default:
3155 goto cannot_emulate;
3156 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003157 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003158 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003159 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003160 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003161 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003162 goto done;
3163 realmode_lidt(ctxt->vcpu, size, address);
3164 }
Avi Kivity16286d02008-04-14 14:40:50 +03003165 /* Disable writeback. */
3166 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 break;
3168 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003169 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003170 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 break;
3172 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003173 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3174 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003175 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003177 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003178 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003179 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003181 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003182 /* Disable writeback. */
3183 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184 break;
3185 default:
3186 goto cannot_emulate;
3187 }
3188 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003189 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003190 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003191 if (rc != X86EMUL_CONTINUE)
3192 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003193 else
3194 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003195 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003196 case 0x06:
3197 emulate_clts(ctxt->vcpu);
3198 c->dst.type = OP_NONE;
3199 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003200 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003201 kvm_emulate_wbinvd(ctxt->vcpu);
3202 c->dst.type = OP_NONE;
3203 break;
3204 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003205 case 0x0d: /* GrpP (prefetch) */
3206 case 0x18: /* Grp16 (prefetch/nop) */
3207 c->dst.type = OP_NONE;
3208 break;
3209 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003210 switch (c->modrm_reg) {
3211 case 1:
3212 case 5 ... 7:
3213 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003214 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003215 goto done;
3216 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003217 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003218 c->dst.type = OP_NONE; /* no writeback */
3219 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003221 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3222 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003223 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003224 goto done;
3225 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003226 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003227 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003229 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003230 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003231 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003232 goto done;
3233 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003234 c->dst.type = OP_NONE;
3235 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003237 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3238 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003239 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003240 goto done;
3241 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003242
Gleb Natapov338dbc92010-04-28 19:15:32 +03003243 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3244 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3245 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3246 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003247 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003248 goto done;
3249 }
3250
Laurent Viviera01af5e2007-09-24 11:10:56 +02003251 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003253 case 0x30:
3254 /* wrmsr */
3255 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3256 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003257 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003258 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003259 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003260 }
3261 rc = X86EMUL_CONTINUE;
3262 c->dst.type = OP_NONE;
3263 break;
3264 case 0x32:
3265 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003266 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003267 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003268 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003269 } else {
3270 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3271 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3272 }
3273 rc = X86EMUL_CONTINUE;
3274 c->dst.type = OP_NONE;
3275 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003276 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003277 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003278 if (rc != X86EMUL_CONTINUE)
3279 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003280 else
3281 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003282 break;
3283 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003284 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003285 if (rc != X86EMUL_CONTINUE)
3286 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003287 else
3288 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003289 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003291 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003292 if (!test_cc(c->b, ctxt->eflags))
3293 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003295 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003296 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003297 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003298 c->dst.type = OP_NONE;
3299 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003300 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003301 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003302 break;
3303 case 0xa1: /* pop fs */
3304 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003305 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003306 goto done;
3307 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003308 case 0xa3:
3309 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003310 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003311 /* only subword offset */
3312 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003313 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003314 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003315 case 0xa4: /* shld imm8, r, r/m */
3316 case 0xa5: /* shld cl, r, r/m */
3317 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3318 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003319 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003320 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003321 break;
3322 case 0xa9: /* pop gs */
3323 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003324 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003325 goto done;
3326 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003327 case 0xab:
3328 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003329 /* only subword offset */
3330 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003331 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003332 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003333 case 0xac: /* shrd imm8, r, r/m */
3334 case 0xad: /* shrd cl, r, r/m */
3335 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3336 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003337 case 0xae: /* clflush */
3338 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339 case 0xb0 ... 0xb1: /* cmpxchg */
3340 /*
3341 * Save real source value, then compare EAX against
3342 * destination.
3343 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003344 c->src.orig_val = c->src.val;
3345 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003346 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3347 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003349 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 } else {
3351 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003352 c->dst.type = OP_REG;
3353 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 }
3355 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 case 0xb3:
3357 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003358 /* only subword offset */
3359 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003360 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003363 c->dst.bytes = c->op_bytes;
3364 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3365 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003368 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 case 0:
3370 goto bt;
3371 case 1:
3372 goto bts;
3373 case 2:
3374 goto btr;
3375 case 3:
3376 goto btc;
3377 }
3378 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003379 case 0xbb:
3380 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003381 /* only subword offset */
3382 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003383 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003386 c->dst.bytes = c->op_bytes;
3387 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3388 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003390 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003391 c->dst.bytes = c->op_bytes;
3392 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3393 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003394 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003396 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003397 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003398 goto done;
3399 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003400 default:
3401 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402 }
3403 goto writeback;
3404
3405cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003406 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 return -1;
3408}