blob: 618fdc8c8d06e3b879e042e80b17b5d31f9f23b5 [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 Kivity99880c52010-07-29 15:11:41 +0300108 NoGrp, 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;
Avi Kivity120df892010-07-29 15:11:39 +0300113 union {
114 struct opcode *group;
115 struct group_dual *gdual;
116 } u;
117};
118
119struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300122};
123
Avi Kivityfd853312010-07-29 15:11:36 +0300124#define D(_y) { .flags = (_y) }
125#define N D(0)
Avi Kivity120df892010-07-29 15:11:39 +0300126#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
127#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
Avi Kivityfd853312010-07-29 15:11:36 +0300128
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300129static struct opcode group1[] = {
130 X7(D(Lock)), N
131};
132
Avi Kivity99880c52010-07-29 15:11:41 +0300133static struct opcode group1A[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300134 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
Avi Kivity99880c52010-07-29 15:11:41 +0300135};
136
137static struct opcode group_table[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300138 [Group3*8] =
139 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
140 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
141 X4(D(Undefined)),
142 [Group4*8] =
143 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
144 N, N, N, N, N, N,
145 [Group5*8] =
146 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
147 D(SrcMem | ModRM | Stack), N,
148 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
149 D(SrcMem | ModRM | Stack), N,
150 [Group7*8] =
151 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
152 D(SrcNone | ModRM | DstMem | Mov), N,
153 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
154 [Group8*8] =
155 N, N, N, N,
156 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
157 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
158 [Group9*8] =
159 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
160};
161
162static struct opcode group2_table[] = {
163 [Group7*8] =
164 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
165 D(SrcNone | ModRM | DstMem | Mov), N,
166 D(SrcMem16 | ModRM | Mov | Priv), N,
167 [Group9*8] =
168 N, N, N, N, N, N, N, N,
169};
170
Avi Kivityd65b1de2010-07-29 15:11:35 +0300171static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 /* 0x00 - 0x07 */
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 /* 0x08 - 0x0F */
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 | SrcImm), D(DstAcc | SrcImm),
181 D(ImplicitOps | Stack | No64), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0x10 - 0x17 */
Avi Kivityfd853312010-07-29 15:11:36 +0300183 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
184 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
185 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
186 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 /* 0x18 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300188 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
189 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
190 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
191 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0x20 - 0x27 */
Avi Kivityfd853312010-07-29 15:11:36 +0300193 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
194 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
195 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0x28 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300197 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
198 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
199 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0x30 - 0x37 */
Avi Kivityfd853312010-07-29 15:11:36 +0300201 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
202 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
203 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204 /* 0x38 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300205 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
206 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
207 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
208 N, N,
Avi Kivity749358a2010-07-26 14:37:40 +0300209 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300210 X16(D(DstReg)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300211 /* 0x50 - 0x57 */
Avi Kivityfd853312010-07-29 15:11:36 +0300212 X8(D(SrcReg | Stack)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300213 /* 0x58 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300214 X8(D(DstReg | Stack)),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700215 /* 0x60 - 0x67 */
Avi Kivityfd853312010-07-29 15:11:36 +0300216 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
217 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
218 N, N, N, N,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700219 /* 0x68 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300220 D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N,
221 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
222 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300223 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300224 X16(D(SrcImmByte)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 /* 0x80 - 0x87 */
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300226 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
227 G(DstMem | SrcImm | ModRM | Group, group1),
228 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
229 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivityfd853312010-07-29 15:11:36 +0300230 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
231 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0x88 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300233 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
234 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
235 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
Avi Kivity99880c52010-07-29 15:11:41 +0300236 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300237 /* 0x90 - 0x97 */
Avi Kivityfd853312010-07-29 15:11:36 +0300238 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300239 /* 0x98 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300240 N, N, D(SrcImmFAddr | No64), N,
241 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300243 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
244 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
245 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
246 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300248 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
249 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
250 D(ByteOp | DstDI | String), D(DstDI | String),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300251 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300252 X8(D(ByteOp | DstReg | SrcImm | Mov)),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300253 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300254 X8(D(DstReg | SrcImm | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 /* 0xC0 - 0xC7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300256 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
257 N, D(ImplicitOps | Stack), N, N,
258 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259 /* 0xC8 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300260 N, N, N, D(ImplicitOps | Stack),
261 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xD0 - 0xD7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300263 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
264 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
265 N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266 /* 0xD8 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300267 N, N, N, N, N, N, N, N,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300268 /* 0xE0 - 0xE7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300269 N, N, N, N,
270 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
271 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Nitin A Kamble098c9372007-08-19 11:00:36 +0300272 /* 0xE8 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300273 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
274 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
275 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
276 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 /* 0xF0 - 0xF7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300278 N, N, N, N,
279 D(ImplicitOps | Priv), D(ImplicitOps), D(ByteOp | Group | Group3), D(Group | Group3),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 /* 0xF8 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300281 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
282 D(ImplicitOps), D(ImplicitOps), D(Group | Group4), D(Group | Group5),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283};
284
Avi Kivityd65b1de2010-07-29 15:11:35 +0300285static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 /* 0x00 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300287 N, D(Group | GroupDual | Group7), N, N,
288 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
289 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
290 N, D(ImplicitOps | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 /* 0x10 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300292 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 -0800293 /* 0x20 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300294 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
295 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
296 N, N, N, N,
297 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 /* 0x30 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300299 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
300 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
301 N, N, N, N, N, N, N, N,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300302 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300303 X16(D(DstReg | SrcMem | ModRM | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 /* 0x50 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300305 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306 /* 0x60 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300307 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300309 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310 /* 0x80 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300311 X16(D(SrcImm)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312 /* 0x90 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300313 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300315 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
316 N, D(DstMem | SrcReg | ModRM | BitOp),
317 D(DstMem | SrcReg | Src2ImmByte | ModRM),
318 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300320 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
321 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
322 D(DstMem | SrcReg | Src2ImmByte | ModRM),
323 D(DstMem | SrcReg | Src2CL | ModRM),
324 D(ModRM), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300326 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
327 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
328 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
329 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300331 N, N,
332 D(Group | Group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
333 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
334 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335 /* 0xC0 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300336 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
337 N, N, N, D(Group | GroupDual | Group9),
338 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339 /* 0xD0 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300340 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341 /* 0xE0 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300342 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343 /* 0xF0 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300344 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345};
346
Avi Kivityfd853312010-07-29 15:11:36 +0300347#undef D
348#undef N
Avi Kivity120df892010-07-29 15:11:39 +0300349#undef G
350#undef GD
Avi Kivityfd853312010-07-29 15:11:36 +0300351
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200353#define EFLG_ID (1<<21)
354#define EFLG_VIP (1<<20)
355#define EFLG_VIF (1<<19)
356#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200357#define EFLG_VM (1<<17)
358#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200359#define EFLG_IOPL (3<<12)
360#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361#define EFLG_OF (1<<11)
362#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200363#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200364#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365#define EFLG_SF (1<<7)
366#define EFLG_ZF (1<<6)
367#define EFLG_AF (1<<4)
368#define EFLG_PF (1<<2)
369#define EFLG_CF (1<<0)
370
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300371#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
372#define EFLG_RESERVED_ONE_MASK 2
373
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374/*
375 * Instruction emulation:
376 * Most instructions are emulated directly via a fragment of inline assembly
377 * code. This allows us to save/restore EFLAGS and thus very easily pick up
378 * any modified flags.
379 */
380
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800381#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382#define _LO32 "k" /* force 32-bit operand */
383#define _STK "%%rsp" /* stack pointer */
384#elif defined(__i386__)
385#define _LO32 "" /* force 32-bit operand */
386#define _STK "%%esp" /* stack pointer */
387#endif
388
389/*
390 * These EFLAGS bits are restored from saved value during emulation, and
391 * any changes are written back to the saved value after emulation.
392 */
393#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
394
395/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200396#define _PRE_EFLAGS(_sav, _msk, _tmp) \
397 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
398 "movl %"_sav",%"_LO32 _tmp"; " \
399 "push %"_tmp"; " \
400 "push %"_tmp"; " \
401 "movl %"_msk",%"_LO32 _tmp"; " \
402 "andl %"_LO32 _tmp",("_STK"); " \
403 "pushf; " \
404 "notl %"_LO32 _tmp"; " \
405 "andl %"_LO32 _tmp",("_STK"); " \
406 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
407 "pop %"_tmp"; " \
408 "orl %"_LO32 _tmp",("_STK"); " \
409 "popf; " \
410 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
412/* After executing instruction: write-back necessary bits in EFLAGS. */
413#define _POST_EFLAGS(_sav, _msk, _tmp) \
414 /* _sav |= EFLAGS & _msk; */ \
415 "pushf; " \
416 "pop %"_tmp"; " \
417 "andl %"_msk",%"_LO32 _tmp"; " \
418 "orl %"_LO32 _tmp",%"_sav"; "
419
Avi Kivitydda96d82008-11-26 15:14:10 +0200420#ifdef CONFIG_X86_64
421#define ON64(x) x
422#else
423#define ON64(x)
424#endif
425
Avi Kivity6b7ad612008-11-26 15:30:45 +0200426#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
427 do { \
428 __asm__ __volatile__ ( \
429 _PRE_EFLAGS("0", "4", "2") \
430 _op _suffix " %"_x"3,%1; " \
431 _POST_EFLAGS("0", "4", "2") \
432 : "=m" (_eflags), "=m" ((_dst).val), \
433 "=&r" (_tmp) \
434 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200435 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200436
437
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438/* Raw emulation: instruction has two explicit operands. */
439#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200440 do { \
441 unsigned long _tmp; \
442 \
443 switch ((_dst).bytes) { \
444 case 2: \
445 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
446 break; \
447 case 4: \
448 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
449 break; \
450 case 8: \
451 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
452 break; \
453 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 } while (0)
455
456#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
457 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200458 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400459 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200461 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 break; \
463 default: \
464 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
465 _wx, _wy, _lx, _ly, _qx, _qy); \
466 break; \
467 } \
468 } while (0)
469
470/* Source operand is byte-sized and may be restricted to just %cl. */
471#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
472 __emulate_2op(_op, _src, _dst, _eflags, \
473 "b", "c", "b", "c", "b", "c", "b", "c")
474
475/* Source operand is byte, word, long or quad sized. */
476#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
477 __emulate_2op(_op, _src, _dst, _eflags, \
478 "b", "q", "w", "r", _LO32, "r", "", "r")
479
480/* Source operand is word, long or quad sized. */
481#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
482 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
483 "w", "r", _LO32, "r", "", "r")
484
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100485/* Instruction has three operands and one operand is stored in ECX register */
486#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
487 do { \
488 unsigned long _tmp; \
489 _type _clv = (_cl).val; \
490 _type _srcv = (_src).val; \
491 _type _dstv = (_dst).val; \
492 \
493 __asm__ __volatile__ ( \
494 _PRE_EFLAGS("0", "5", "2") \
495 _op _suffix " %4,%1 \n" \
496 _POST_EFLAGS("0", "5", "2") \
497 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
498 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
499 ); \
500 \
501 (_cl).val = (unsigned long) _clv; \
502 (_src).val = (unsigned long) _srcv; \
503 (_dst).val = (unsigned long) _dstv; \
504 } while (0)
505
506#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
507 do { \
508 switch ((_dst).bytes) { \
509 case 2: \
510 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
511 "w", unsigned short); \
512 break; \
513 case 4: \
514 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
515 "l", unsigned int); \
516 break; \
517 case 8: \
518 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
519 "q", unsigned long)); \
520 break; \
521 } \
522 } while (0)
523
Avi Kivitydda96d82008-11-26 15:14:10 +0200524#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525 do { \
526 unsigned long _tmp; \
527 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200528 __asm__ __volatile__ ( \
529 _PRE_EFLAGS("0", "3", "2") \
530 _op _suffix " %1; " \
531 _POST_EFLAGS("0", "3", "2") \
532 : "=m" (_eflags), "+m" ((_dst).val), \
533 "=&r" (_tmp) \
534 : "i" (EFLAGS_MASK)); \
535 } while (0)
536
537/* Instruction has only one explicit operand (no source operand). */
538#define emulate_1op(_op, _dst, _eflags) \
539 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400540 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200541 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
542 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
543 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
544 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 } \
546 } while (0)
547
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548/* Fetch next part of the instruction being emulated. */
549#define insn_fetch(_type, _size, _eip) \
550({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200551 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200552 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 goto done; \
554 (_eip) += (_size); \
555 (_type)_x; \
556})
557
Gleb Natapov414e6272010-04-28 19:15:26 +0300558#define insn_fetch_arr(_arr, _size, _eip) \
559({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
560 if (rc != X86EMUL_CONTINUE) \
561 goto done; \
562 (_eip) += (_size); \
563})
564
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800565static inline unsigned long ad_mask(struct decode_cache *c)
566{
567 return (1UL << (c->ad_bytes << 3)) - 1;
568}
569
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800571static inline unsigned long
572address_mask(struct decode_cache *c, unsigned long reg)
573{
574 if (c->ad_bytes == sizeof(unsigned long))
575 return reg;
576 else
577 return reg & ad_mask(c);
578}
579
580static inline unsigned long
581register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
582{
583 return base + address_mask(c, reg);
584}
585
Harvey Harrison7a9572752008-02-19 07:40:41 -0800586static inline void
587register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
588{
589 if (c->ad_bytes == sizeof(unsigned long))
590 *reg += inc;
591 else
592 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
593}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594
Harvey Harrison7a9572752008-02-19 07:40:41 -0800595static inline void jmp_rel(struct decode_cache *c, int rel)
596{
597 register_address_increment(c, &c->eip, rel);
598}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300599
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300600static void set_seg_override(struct decode_cache *c, int seg)
601{
602 c->has_seg_override = true;
603 c->seg_override = seg;
604}
605
Gleb Natapov79168fd2010-04-28 19:15:30 +0300606static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
607 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300608{
609 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
610 return 0;
611
Gleb Natapov79168fd2010-04-28 19:15:30 +0300612 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300613}
614
615static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300616 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300617 struct decode_cache *c)
618{
619 if (!c->has_seg_override)
620 return 0;
621
Gleb Natapov79168fd2010-04-28 19:15:30 +0300622 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300623}
624
Gleb Natapov79168fd2010-04-28 19:15:30 +0300625static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
626 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300627{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300628 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300629}
630
Gleb Natapov79168fd2010-04-28 19:15:30 +0300631static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
632 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300633{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300634 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300635}
636
Gleb Natapov54b84862010-04-28 19:15:44 +0300637static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
638 u32 error, bool valid)
639{
640 ctxt->exception = vec;
641 ctxt->error_code = error;
642 ctxt->error_code_valid = valid;
643 ctxt->restart = false;
644}
645
646static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
647{
648 emulate_exception(ctxt, GP_VECTOR, err, true);
649}
650
651static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
652 int err)
653{
654 ctxt->cr2 = addr;
655 emulate_exception(ctxt, PF_VECTOR, err, true);
656}
657
658static void emulate_ud(struct x86_emulate_ctxt *ctxt)
659{
660 emulate_exception(ctxt, UD_VECTOR, 0, false);
661}
662
663static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
664{
665 emulate_exception(ctxt, TS_VECTOR, err, true);
666}
667
Avi Kivity62266862007-11-20 13:15:52 +0200668static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
669 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300670 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200671{
672 struct fetch_cache *fc = &ctxt->decode.fetch;
673 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300674 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200675
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300676 if (eip == fc->end) {
677 cur_size = fc->end - fc->start;
678 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
679 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
680 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900681 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200682 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300683 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200684 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300685 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900686 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200687}
688
689static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
690 struct x86_emulate_ops *ops,
691 unsigned long eip, void *dest, unsigned size)
692{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900693 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200694
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200695 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200696 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200697 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200698 while (size--) {
699 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900700 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200701 return rc;
702 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900703 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200704}
705
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000706/*
707 * Given the 'reg' portion of a ModRM byte, and a register block, return a
708 * pointer into the block that addresses the relevant register.
709 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
710 */
711static void *decode_register(u8 modrm_reg, unsigned long *regs,
712 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713{
714 void *p;
715
716 p = &regs[modrm_reg];
717 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
718 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
719 return p;
720}
721
722static int read_descriptor(struct x86_emulate_ctxt *ctxt,
723 struct x86_emulate_ops *ops,
724 void *ptr,
725 u16 *size, unsigned long *address, int op_bytes)
726{
727 int rc;
728
729 if (op_bytes == 2)
730 op_bytes = 3;
731 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300732 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200733 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900734 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300736 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200737 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 return rc;
739}
740
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300741static int test_cc(unsigned int condition, unsigned int flags)
742{
743 int rc = 0;
744
745 switch ((condition & 15) >> 1) {
746 case 0: /* o */
747 rc |= (flags & EFLG_OF);
748 break;
749 case 1: /* b/c/nae */
750 rc |= (flags & EFLG_CF);
751 break;
752 case 2: /* z/e */
753 rc |= (flags & EFLG_ZF);
754 break;
755 case 3: /* be/na */
756 rc |= (flags & (EFLG_CF|EFLG_ZF));
757 break;
758 case 4: /* s */
759 rc |= (flags & EFLG_SF);
760 break;
761 case 5: /* p/pe */
762 rc |= (flags & EFLG_PF);
763 break;
764 case 7: /* le/ng */
765 rc |= (flags & EFLG_ZF);
766 /* fall through */
767 case 6: /* l/nge */
768 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
769 break;
770 }
771
772 /* Odd condition identifiers (lsb == 1) have inverted sense. */
773 return (!!rc ^ (condition & 1));
774}
775
Avi Kivity3c118e22007-10-31 10:27:04 +0200776static void decode_register_operand(struct operand *op,
777 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200778 int inhibit_bytereg)
779{
Avi Kivity33615aa2007-10-31 11:15:56 +0200780 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200781 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200782
783 if (!(c->d & ModRM))
784 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200785 op->type = OP_REG;
786 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200787 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200788 op->val = *(u8 *)op->ptr;
789 op->bytes = 1;
790 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200791 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200792 op->bytes = c->op_bytes;
793 switch (op->bytes) {
794 case 2:
795 op->val = *(u16 *)op->ptr;
796 break;
797 case 4:
798 op->val = *(u32 *)op->ptr;
799 break;
800 case 8:
801 op->val = *(u64 *) op->ptr;
802 break;
803 }
804 }
805 op->orig_val = op->val;
806}
807
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808static int decode_modrm(struct x86_emulate_ctxt *ctxt,
809 struct x86_emulate_ops *ops)
810{
811 struct decode_cache *c = &ctxt->decode;
812 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700813 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900814 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200815
816 if (c->rex_prefix) {
817 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
818 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
819 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
820 }
821
822 c->modrm = insn_fetch(u8, 1, c->eip);
823 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
824 c->modrm_reg |= (c->modrm & 0x38) >> 3;
825 c->modrm_rm |= (c->modrm & 0x07);
826 c->modrm_ea = 0;
827 c->use_modrm_ea = 1;
828
829 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300830 c->modrm_ptr = decode_register(c->modrm_rm,
831 c->regs, c->d & ByteOp);
832 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200833 return rc;
834 }
835
836 if (c->ad_bytes == 2) {
837 unsigned bx = c->regs[VCPU_REGS_RBX];
838 unsigned bp = c->regs[VCPU_REGS_RBP];
839 unsigned si = c->regs[VCPU_REGS_RSI];
840 unsigned di = c->regs[VCPU_REGS_RDI];
841
842 /* 16-bit ModR/M decode. */
843 switch (c->modrm_mod) {
844 case 0:
845 if (c->modrm_rm == 6)
846 c->modrm_ea += insn_fetch(u16, 2, c->eip);
847 break;
848 case 1:
849 c->modrm_ea += insn_fetch(s8, 1, c->eip);
850 break;
851 case 2:
852 c->modrm_ea += insn_fetch(u16, 2, c->eip);
853 break;
854 }
855 switch (c->modrm_rm) {
856 case 0:
857 c->modrm_ea += bx + si;
858 break;
859 case 1:
860 c->modrm_ea += bx + di;
861 break;
862 case 2:
863 c->modrm_ea += bp + si;
864 break;
865 case 3:
866 c->modrm_ea += bp + di;
867 break;
868 case 4:
869 c->modrm_ea += si;
870 break;
871 case 5:
872 c->modrm_ea += di;
873 break;
874 case 6:
875 if (c->modrm_mod != 0)
876 c->modrm_ea += bp;
877 break;
878 case 7:
879 c->modrm_ea += bx;
880 break;
881 }
882 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
883 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300884 if (!c->has_seg_override)
885 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886 c->modrm_ea = (u16)c->modrm_ea;
887 } else {
888 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700889 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890 sib = insn_fetch(u8, 1, c->eip);
891 index_reg |= (sib >> 3) & 7;
892 base_reg |= sib & 7;
893 scale = sib >> 6;
894
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700895 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
896 c->modrm_ea += insn_fetch(s32, 4, c->eip);
897 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200898 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700899 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200900 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700901 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
902 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700903 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700904 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200905 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906 switch (c->modrm_mod) {
907 case 0:
908 if (c->modrm_rm == 5)
909 c->modrm_ea += insn_fetch(s32, 4, c->eip);
910 break;
911 case 1:
912 c->modrm_ea += insn_fetch(s8, 1, c->eip);
913 break;
914 case 2:
915 c->modrm_ea += insn_fetch(s32, 4, c->eip);
916 break;
917 }
918 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200919done:
920 return rc;
921}
922
923static int decode_abs(struct x86_emulate_ctxt *ctxt,
924 struct x86_emulate_ops *ops)
925{
926 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900927 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200928
929 switch (c->ad_bytes) {
930 case 2:
931 c->modrm_ea = insn_fetch(u16, 2, c->eip);
932 break;
933 case 4:
934 c->modrm_ea = insn_fetch(u32, 4, c->eip);
935 break;
936 case 8:
937 c->modrm_ea = insn_fetch(u64, 8, c->eip);
938 break;
939 }
940done:
941 return rc;
942}
943
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200945x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200947 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900948 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 int mode = ctxt->mode;
Avi Kivity120df892010-07-29 15:11:39 +0300950 int def_op_bytes, def_ad_bytes, group, dual, goffset;
951 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952
Gleb Natapov5cd21912010-03-18 15:20:26 +0200953 /* we cannot decode insn before we complete previous rep insn */
954 WARN_ON(ctxt->restart);
955
Gleb Natapov063db062010-03-18 15:20:06 +0200956 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300957 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300958 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959
960 switch (mode) {
961 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200962 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200964 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 break;
966 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800969#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200971 def_op_bytes = 4;
972 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 break;
974#endif
975 default:
976 return -1;
977 }
978
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200979 c->op_bytes = def_op_bytes;
980 c->ad_bytes = def_ad_bytes;
981
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200983 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200984 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200986 /* switch between 2/4 bytes */
987 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 break;
989 case 0x67: /* address-size override */
990 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200991 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200992 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200994 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200995 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300998 case 0x2e: /* CS override */
999 case 0x36: /* SS override */
1000 case 0x3e: /* DS override */
1001 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 break;
1003 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001005 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001007 case 0x40 ... 0x4f: /* REX */
1008 if (mode != X86EMUL_MODE_PROT64)
1009 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001010 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001011 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001015 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001016 c->rep_prefix = REPNE_PREFIX;
1017 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001019 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 default:
1022 goto done_prefixes;
1023 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001024
1025 /* Any legacy prefix after a REX prefix nullifies its effect. */
1026
Avi Kivity33615aa2007-10-31 11:15:56 +02001027 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 }
1029
1030done_prefixes:
1031
1032 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001033 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001034 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036
1037 /* Opcode byte(s). */
Avi Kivity120df892010-07-29 15:11:39 +03001038 opcode = opcode_table[c->b];
1039 if (opcode.flags == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 if (c->b == 0x0f) {
1042 c->twobyte = 1;
1043 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivity120df892010-07-29 15:11:39 +03001044 opcode = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001046 }
Avi Kivity120df892010-07-29 15:11:39 +03001047 c->d = opcode.flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048
Avi Kivitye09d0822008-01-18 12:38:59 +02001049 if (c->d & Group) {
1050 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001051 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001052 c->modrm = insn_fetch(u8, 1, c->eip);
1053 --c->eip;
1054
Avi Kivity120df892010-07-29 15:11:39 +03001055 if (group) {
1056 g_mod012 = g_mod3 = &group_table[group * 8];
1057 if (c->d & GroupDual)
1058 g_mod3 = &group2_table[group * 8];
1059 } else {
1060 if (c->d & GroupDual) {
1061 g_mod012 = opcode.u.gdual->mod012;
1062 g_mod3 = opcode.u.gdual->mod3;
1063 } else
1064 g_mod012 = g_mod3 = opcode.u.group;
1065 }
1066
Avi Kivity52811d72010-07-26 14:37:48 +03001067 c->d &= ~(Group | GroupDual | GroupMask);
Avi Kivity120df892010-07-29 15:11:39 +03001068
1069 goffset = (c->modrm >> 3) & 7;
1070
1071 if ((c->modrm >> 6) == 3)
1072 opcode = g_mod3[goffset];
Avi Kivitye09d0822008-01-18 12:38:59 +02001073 else
Avi Kivity120df892010-07-29 15:11:39 +03001074 opcode = g_mod012[goffset];
1075 c->d |= opcode.flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001076 }
1077
1078 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001079 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001080 DPRINTF("Cannot emulate %02x\n", c->b);
1081 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 }
1083
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001084 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1085 c->op_bytes = 8;
1086
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001088 if (c->d & ModRM)
1089 rc = decode_modrm(ctxt, ops);
1090 else if (c->d & MemAbs)
1091 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001092 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001093 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001095 if (!c->has_seg_override)
1096 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001097
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001098 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001099 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001100
1101 if (c->ad_bytes != 8)
1102 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001103
1104 if (c->rip_relative)
1105 c->modrm_ea += c->eip;
1106
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 /*
1108 * Decode and fetch the source operand: register, memory
1109 * or immediate.
1110 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 case SrcNone:
1113 break;
1114 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001115 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 break;
1117 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001118 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 goto srcmem_common;
1120 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 goto srcmem_common;
1123 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001124 c->src.bytes = (c->d & ByteOp) ? 1 :
1125 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001126 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001127 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001128 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001129 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001130 /*
1131 * For instructions with a ModR/M byte, switch to register
1132 * access if Mod = 3.
1133 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001134 if ((c->d & ModRM) && c->modrm_mod == 3) {
1135 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001136 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001137 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001138 break;
1139 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001140 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001141 c->src.ptr = (unsigned long *)c->modrm_ea;
1142 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 break;
1144 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001145 case SrcImmU:
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 = (c->d & ByteOp) ? 1 : c->op_bytes;
1149 if (c->src.bytes == 8)
1150 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001152 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001154 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 break;
1156 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001157 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 break;
1159 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001160 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 break;
1162 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001163 if ((c->d & SrcMask) == SrcImmU) {
1164 switch (c->src.bytes) {
1165 case 1:
1166 c->src.val &= 0xff;
1167 break;
1168 case 2:
1169 c->src.val &= 0xffff;
1170 break;
1171 case 4:
1172 c->src.val &= 0xffffffff;
1173 break;
1174 }
1175 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 break;
1177 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001178 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001179 c->src.type = OP_IMM;
1180 c->src.ptr = (unsigned long *)c->eip;
1181 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001182 if ((c->d & SrcMask) == SrcImmByte)
1183 c->src.val = insn_fetch(s8, 1, c->eip);
1184 else
1185 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001187 case SrcAcc:
1188 c->src.type = OP_REG;
1189 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1190 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1191 switch (c->src.bytes) {
1192 case 1:
1193 c->src.val = *(u8 *)c->src.ptr;
1194 break;
1195 case 2:
1196 c->src.val = *(u16 *)c->src.ptr;
1197 break;
1198 case 4:
1199 c->src.val = *(u32 *)c->src.ptr;
1200 break;
1201 case 8:
1202 c->src.val = *(u64 *)c->src.ptr;
1203 break;
1204 }
1205 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001206 case SrcOne:
1207 c->src.bytes = 1;
1208 c->src.val = 1;
1209 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001210 case SrcSI:
1211 c->src.type = OP_MEM;
1212 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1213 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001214 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001215 c->regs[VCPU_REGS_RSI]);
1216 c->src.val = 0;
1217 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001218 case SrcImmFAddr:
1219 c->src.type = OP_IMM;
1220 c->src.ptr = (unsigned long *)c->eip;
1221 c->src.bytes = c->op_bytes + 2;
1222 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1223 break;
1224 case SrcMemFAddr:
1225 c->src.type = OP_MEM;
1226 c->src.ptr = (unsigned long *)c->modrm_ea;
1227 c->src.bytes = c->op_bytes + 2;
1228 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 }
1230
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001231 /*
1232 * Decode and fetch the second source operand: register, memory
1233 * or immediate.
1234 */
1235 switch (c->d & Src2Mask) {
1236 case Src2None:
1237 break;
1238 case Src2CL:
1239 c->src2.bytes = 1;
1240 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1241 break;
1242 case Src2ImmByte:
1243 c->src2.type = OP_IMM;
1244 c->src2.ptr = (unsigned long *)c->eip;
1245 c->src2.bytes = 1;
1246 c->src2.val = insn_fetch(u8, 1, c->eip);
1247 break;
1248 case Src2One:
1249 c->src2.bytes = 1;
1250 c->src2.val = 1;
1251 break;
1252 }
1253
Avi Kivity038e51d2007-01-22 20:40:40 -08001254 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001255 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001256 case ImplicitOps:
1257 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001258 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001259 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001260 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001261 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001262 break;
1263 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001264 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001265 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001266 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001267 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001268 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001269 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001270 break;
1271 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001272 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001273 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001274 if ((c->d & DstMask) == DstMem64)
1275 c->dst.bytes = 8;
1276 else
1277 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001278 c->dst.val = 0;
1279 if (c->d & BitOp) {
1280 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1281
1282 c->dst.ptr = (void *)c->dst.ptr +
1283 (c->src.val & mask) / 8;
1284 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001285 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001286 case DstAcc:
1287 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001288 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001289 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001290 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001291 case 1:
1292 c->dst.val = *(u8 *)c->dst.ptr;
1293 break;
1294 case 2:
1295 c->dst.val = *(u16 *)c->dst.ptr;
1296 break;
1297 case 4:
1298 c->dst.val = *(u32 *)c->dst.ptr;
1299 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001300 case 8:
1301 c->dst.val = *(u64 *)c->dst.ptr;
1302 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001303 }
1304 c->dst.orig_val = c->dst.val;
1305 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001306 case DstDI:
1307 c->dst.type = OP_MEM;
1308 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1309 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001310 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001311 c->regs[VCPU_REGS_RDI]);
1312 c->dst.val = 0;
1313 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001314 }
1315
1316done:
1317 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1318}
1319
Gleb Natapov9de41572010-04-28 19:15:22 +03001320static int read_emulated(struct x86_emulate_ctxt *ctxt,
1321 struct x86_emulate_ops *ops,
1322 unsigned long addr, void *dest, unsigned size)
1323{
1324 int rc;
1325 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001326 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001327
1328 while (size) {
1329 int n = min(size, 8u);
1330 size -= n;
1331 if (mc->pos < mc->end)
1332 goto read_cached;
1333
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001334 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1335 ctxt->vcpu);
1336 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001337 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001338 if (rc != X86EMUL_CONTINUE)
1339 return rc;
1340 mc->end += n;
1341
1342 read_cached:
1343 memcpy(dest, mc->data + mc->pos, n);
1344 mc->pos += n;
1345 dest += n;
1346 addr += n;
1347 }
1348 return X86EMUL_CONTINUE;
1349}
1350
Gleb Natapov7b262e92010-03-18 15:20:27 +02001351static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1352 struct x86_emulate_ops *ops,
1353 unsigned int size, unsigned short port,
1354 void *dest)
1355{
1356 struct read_cache *rc = &ctxt->decode.io_read;
1357
1358 if (rc->pos == rc->end) { /* refill pio read ahead */
1359 struct decode_cache *c = &ctxt->decode;
1360 unsigned int in_page, n;
1361 unsigned int count = c->rep_prefix ?
1362 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1363 in_page = (ctxt->eflags & EFLG_DF) ?
1364 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1365 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1366 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1367 count);
1368 if (n == 0)
1369 n = 1;
1370 rc->pos = rc->end = 0;
1371 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1372 return 0;
1373 rc->end = n * size;
1374 }
1375
1376 memcpy(dest, rc->data + rc->pos, size);
1377 rc->pos += size;
1378 return 1;
1379}
1380
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001381static u32 desc_limit_scaled(struct desc_struct *desc)
1382{
1383 u32 limit = get_desc_limit(desc);
1384
1385 return desc->g ? (limit << 12) | 0xfff : limit;
1386}
1387
1388static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1389 struct x86_emulate_ops *ops,
1390 u16 selector, struct desc_ptr *dt)
1391{
1392 if (selector & 1 << 2) {
1393 struct desc_struct desc;
1394 memset (dt, 0, sizeof *dt);
1395 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1396 return;
1397
1398 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1399 dt->address = get_desc_base(&desc);
1400 } else
1401 ops->get_gdt(dt, ctxt->vcpu);
1402}
1403
1404/* allowed just for 8 bytes segments */
1405static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1406 struct x86_emulate_ops *ops,
1407 u16 selector, struct desc_struct *desc)
1408{
1409 struct desc_ptr dt;
1410 u16 index = selector >> 3;
1411 int ret;
1412 u32 err;
1413 ulong addr;
1414
1415 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1416
1417 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001418 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419 return X86EMUL_PROPAGATE_FAULT;
1420 }
1421 addr = dt.address + index * 8;
1422 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1423 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001424 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001425
1426 return ret;
1427}
1428
1429/* allowed just for 8 bytes segments */
1430static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1431 struct x86_emulate_ops *ops,
1432 u16 selector, struct desc_struct *desc)
1433{
1434 struct desc_ptr dt;
1435 u16 index = selector >> 3;
1436 u32 err;
1437 ulong addr;
1438 int ret;
1439
1440 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1441
1442 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001443 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444 return X86EMUL_PROPAGATE_FAULT;
1445 }
1446
1447 addr = dt.address + index * 8;
1448 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1449 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001450 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451
1452 return ret;
1453}
1454
1455static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1456 struct x86_emulate_ops *ops,
1457 u16 selector, int seg)
1458{
1459 struct desc_struct seg_desc;
1460 u8 dpl, rpl, cpl;
1461 unsigned err_vec = GP_VECTOR;
1462 u32 err_code = 0;
1463 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1464 int ret;
1465
1466 memset(&seg_desc, 0, sizeof seg_desc);
1467
1468 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1469 || ctxt->mode == X86EMUL_MODE_REAL) {
1470 /* set real mode segment descriptor */
1471 set_desc_base(&seg_desc, selector << 4);
1472 set_desc_limit(&seg_desc, 0xffff);
1473 seg_desc.type = 3;
1474 seg_desc.p = 1;
1475 seg_desc.s = 1;
1476 goto load;
1477 }
1478
1479 /* NULL selector is not valid for TR, CS and SS */
1480 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1481 && null_selector)
1482 goto exception;
1483
1484 /* TR should be in GDT only */
1485 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1486 goto exception;
1487
1488 if (null_selector) /* for NULL selector skip all following checks */
1489 goto load;
1490
1491 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1492 if (ret != X86EMUL_CONTINUE)
1493 return ret;
1494
1495 err_code = selector & 0xfffc;
1496 err_vec = GP_VECTOR;
1497
1498 /* can't load system descriptor into segment selecor */
1499 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1500 goto exception;
1501
1502 if (!seg_desc.p) {
1503 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1504 goto exception;
1505 }
1506
1507 rpl = selector & 3;
1508 dpl = seg_desc.dpl;
1509 cpl = ops->cpl(ctxt->vcpu);
1510
1511 switch (seg) {
1512 case VCPU_SREG_SS:
1513 /*
1514 * segment is not a writable data segment or segment
1515 * selector's RPL != CPL or segment selector's RPL != CPL
1516 */
1517 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1518 goto exception;
1519 break;
1520 case VCPU_SREG_CS:
1521 if (!(seg_desc.type & 8))
1522 goto exception;
1523
1524 if (seg_desc.type & 4) {
1525 /* conforming */
1526 if (dpl > cpl)
1527 goto exception;
1528 } else {
1529 /* nonconforming */
1530 if (rpl > cpl || dpl != cpl)
1531 goto exception;
1532 }
1533 /* CS(RPL) <- CPL */
1534 selector = (selector & 0xfffc) | cpl;
1535 break;
1536 case VCPU_SREG_TR:
1537 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1538 goto exception;
1539 break;
1540 case VCPU_SREG_LDTR:
1541 if (seg_desc.s || seg_desc.type != 2)
1542 goto exception;
1543 break;
1544 default: /* DS, ES, FS, or GS */
1545 /*
1546 * segment is not a data or readable code segment or
1547 * ((segment is a data or nonconforming code segment)
1548 * and (both RPL and CPL > DPL))
1549 */
1550 if ((seg_desc.type & 0xa) == 0x8 ||
1551 (((seg_desc.type & 0xc) != 0xc) &&
1552 (rpl > dpl && cpl > dpl)))
1553 goto exception;
1554 break;
1555 }
1556
1557 if (seg_desc.s) {
1558 /* mark segment as accessed */
1559 seg_desc.type |= 1;
1560 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1561 if (ret != X86EMUL_CONTINUE)
1562 return ret;
1563 }
1564load:
1565 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1566 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1567 return X86EMUL_CONTINUE;
1568exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001569 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570 return X86EMUL_PROPAGATE_FAULT;
1571}
1572
Wei Yongjunc37eda12010-06-15 09:03:33 +08001573static inline int writeback(struct x86_emulate_ctxt *ctxt,
1574 struct x86_emulate_ops *ops)
1575{
1576 int rc;
1577 struct decode_cache *c = &ctxt->decode;
1578 u32 err;
1579
1580 switch (c->dst.type) {
1581 case OP_REG:
1582 /* The 4-byte case *is* correct:
1583 * in 64-bit mode we zero-extend.
1584 */
1585 switch (c->dst.bytes) {
1586 case 1:
1587 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1588 break;
1589 case 2:
1590 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1591 break;
1592 case 4:
1593 *c->dst.ptr = (u32)c->dst.val;
1594 break; /* 64b: zero-ext */
1595 case 8:
1596 *c->dst.ptr = c->dst.val;
1597 break;
1598 }
1599 break;
1600 case OP_MEM:
1601 if (c->lock_prefix)
1602 rc = ops->cmpxchg_emulated(
1603 (unsigned long)c->dst.ptr,
1604 &c->dst.orig_val,
1605 &c->dst.val,
1606 c->dst.bytes,
1607 &err,
1608 ctxt->vcpu);
1609 else
1610 rc = ops->write_emulated(
1611 (unsigned long)c->dst.ptr,
1612 &c->dst.val,
1613 c->dst.bytes,
1614 &err,
1615 ctxt->vcpu);
1616 if (rc == X86EMUL_PROPAGATE_FAULT)
1617 emulate_pf(ctxt,
1618 (unsigned long)c->dst.ptr, err);
1619 if (rc != X86EMUL_CONTINUE)
1620 return rc;
1621 break;
1622 case OP_NONE:
1623 /* no writeback */
1624 break;
1625 default:
1626 break;
1627 }
1628 return X86EMUL_CONTINUE;
1629}
1630
Gleb Natapov79168fd2010-04-28 19:15:30 +03001631static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1632 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001633{
1634 struct decode_cache *c = &ctxt->decode;
1635
1636 c->dst.type = OP_MEM;
1637 c->dst.bytes = c->op_bytes;
1638 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001639 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001640 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641 c->regs[VCPU_REGS_RSP]);
1642}
1643
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001644static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001645 struct x86_emulate_ops *ops,
1646 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647{
1648 struct decode_cache *c = &ctxt->decode;
1649 int rc;
1650
Gleb Natapov79168fd2010-04-28 19:15:30 +03001651 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001652 c->regs[VCPU_REGS_RSP]),
1653 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001654 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001655 return rc;
1656
Avi Kivity350f69d2009-01-05 11:12:40 +02001657 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001658 return rc;
1659}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001660
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001661static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1662 struct x86_emulate_ops *ops,
1663 void *dest, int len)
1664{
1665 int rc;
1666 unsigned long val, change_mask;
1667 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001668 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001669
1670 rc = emulate_pop(ctxt, ops, &val, len);
1671 if (rc != X86EMUL_CONTINUE)
1672 return rc;
1673
1674 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1675 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1676
1677 switch(ctxt->mode) {
1678 case X86EMUL_MODE_PROT64:
1679 case X86EMUL_MODE_PROT32:
1680 case X86EMUL_MODE_PROT16:
1681 if (cpl == 0)
1682 change_mask |= EFLG_IOPL;
1683 if (cpl <= iopl)
1684 change_mask |= EFLG_IF;
1685 break;
1686 case X86EMUL_MODE_VM86:
1687 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001688 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001689 return X86EMUL_PROPAGATE_FAULT;
1690 }
1691 change_mask |= EFLG_IF;
1692 break;
1693 default: /* real mode */
1694 change_mask |= (EFLG_IOPL | EFLG_IF);
1695 break;
1696 }
1697
1698 *(unsigned long *)dest =
1699 (ctxt->eflags & ~change_mask) | (val & change_mask);
1700
1701 return rc;
1702}
1703
Gleb Natapov79168fd2010-04-28 19:15:30 +03001704static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1705 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001706{
1707 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001708
Gleb Natapov79168fd2010-04-28 19:15:30 +03001709 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001710
Gleb Natapov79168fd2010-04-28 19:15:30 +03001711 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001712}
1713
1714static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1715 struct x86_emulate_ops *ops, int seg)
1716{
1717 struct decode_cache *c = &ctxt->decode;
1718 unsigned long selector;
1719 int rc;
1720
1721 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001722 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001723 return rc;
1724
Gleb Natapov2e873022010-03-18 15:20:18 +02001725 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001726 return rc;
1727}
1728
Wei Yongjunc37eda12010-06-15 09:03:33 +08001729static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001730 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001731{
1732 struct decode_cache *c = &ctxt->decode;
1733 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001735 int reg = VCPU_REGS_RAX;
1736
1737 while (reg <= VCPU_REGS_RDI) {
1738 (reg == VCPU_REGS_RSP) ?
1739 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1740
Gleb Natapov79168fd2010-04-28 19:15:30 +03001741 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001742
1743 rc = writeback(ctxt, ops);
1744 if (rc != X86EMUL_CONTINUE)
1745 return rc;
1746
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001747 ++reg;
1748 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001749
1750 /* Disable writeback. */
1751 c->dst.type = OP_NONE;
1752
1753 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001754}
1755
1756static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1757 struct x86_emulate_ops *ops)
1758{
1759 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001760 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001761 int reg = VCPU_REGS_RDI;
1762
1763 while (reg >= VCPU_REGS_RAX) {
1764 if (reg == VCPU_REGS_RSP) {
1765 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1766 c->op_bytes);
1767 --reg;
1768 }
1769
1770 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001771 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001772 break;
1773 --reg;
1774 }
1775 return rc;
1776}
1777
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001778static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1779 struct x86_emulate_ops *ops)
1780{
1781 struct decode_cache *c = &ctxt->decode;
1782 int rc = X86EMUL_CONTINUE;
1783 unsigned long temp_eip = 0;
1784 unsigned long temp_eflags = 0;
1785 unsigned long cs = 0;
1786 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1787 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1788 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1789 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1790
1791 /* TODO: Add stack limit check */
1792
1793 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1794
1795 if (rc != X86EMUL_CONTINUE)
1796 return rc;
1797
1798 if (temp_eip & ~0xffff) {
1799 emulate_gp(ctxt, 0);
1800 return X86EMUL_PROPAGATE_FAULT;
1801 }
1802
1803 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1804
1805 if (rc != X86EMUL_CONTINUE)
1806 return rc;
1807
1808 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1809
1810 if (rc != X86EMUL_CONTINUE)
1811 return rc;
1812
1813 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1814
1815 if (rc != X86EMUL_CONTINUE)
1816 return rc;
1817
1818 c->eip = temp_eip;
1819
1820
1821 if (c->op_bytes == 4)
1822 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1823 else if (c->op_bytes == 2) {
1824 ctxt->eflags &= ~0xffff;
1825 ctxt->eflags |= temp_eflags;
1826 }
1827
1828 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1829 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1830
1831 return rc;
1832}
1833
1834static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1835 struct x86_emulate_ops* ops)
1836{
1837 switch(ctxt->mode) {
1838 case X86EMUL_MODE_REAL:
1839 return emulate_iret_real(ctxt, ops);
1840 case X86EMUL_MODE_VM86:
1841 case X86EMUL_MODE_PROT16:
1842 case X86EMUL_MODE_PROT32:
1843 case X86EMUL_MODE_PROT64:
1844 default:
1845 /* iret from protected mode unimplemented yet */
1846 return X86EMUL_UNHANDLEABLE;
1847 }
1848}
1849
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001850static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1851 struct x86_emulate_ops *ops)
1852{
1853 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001854
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001855 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856}
1857
Laurent Vivier05f086f2007-09-24 11:10:55 +02001858static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001860 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861 switch (c->modrm_reg) {
1862 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001863 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001864 break;
1865 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001866 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001867 break;
1868 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001869 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001870 break;
1871 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001872 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 break;
1874 case 4: /* sal/shl */
1875 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001876 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877 break;
1878 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001879 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001880 break;
1881 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001882 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883 break;
1884 }
1885}
1886
1887static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001888 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001889{
1890 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891
1892 switch (c->modrm_reg) {
1893 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001894 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001895 break;
1896 case 2: /* not */
1897 c->dst.val = ~c->dst.val;
1898 break;
1899 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001900 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901 break;
1902 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001903 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001904 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001905 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906}
1907
1908static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001909 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001910{
1911 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001912
1913 switch (c->modrm_reg) {
1914 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001915 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001916 break;
1917 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001918 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001919 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001920 case 2: /* call near abs */ {
1921 long int old_eip;
1922 old_eip = c->eip;
1923 c->eip = c->src.val;
1924 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001925 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001926 break;
1927 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001928 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001929 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930 break;
1931 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001932 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001933 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001934 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001935 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001936}
1937
1938static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001939 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001940{
1941 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001942 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001943
1944 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1945 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001946 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1947 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001948 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001949 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001950 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1951 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001952
Laurent Vivier05f086f2007-09-24 11:10:55 +02001953 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001954 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001955 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001956}
1957
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001958static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1959 struct x86_emulate_ops *ops)
1960{
1961 struct decode_cache *c = &ctxt->decode;
1962 int rc;
1963 unsigned long cs;
1964
1965 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001966 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001967 return rc;
1968 if (c->op_bytes == 4)
1969 c->eip = (u32)c->eip;
1970 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001971 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001972 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001973 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001974 return rc;
1975}
1976
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001977static inline void
1978setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001979 struct x86_emulate_ops *ops, struct desc_struct *cs,
1980 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001981{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 memset(cs, 0, sizeof(struct desc_struct));
1983 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1984 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001985
1986 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001987 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001988 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001989 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001990 cs->type = 0x0b; /* Read, Execute, Accessed */
1991 cs->s = 1;
1992 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001993 cs->p = 1;
1994 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001995
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 set_desc_base(ss, 0); /* flat segment */
1997 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001998 ss->g = 1; /* 4kb granularity */
1999 ss->s = 1;
2000 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002001 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002002 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002003 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002004}
2005
2006static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002007emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002008{
2009 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002011 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002012 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002013
2014 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002015 if (ctxt->mode == X86EMUL_MODE_REAL ||
2016 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002017 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002018 return X86EMUL_PROPAGATE_FAULT;
2019 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002020
Gleb Natapov79168fd2010-04-28 19:15:30 +03002021 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002022
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002023 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002024 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002025 cs_sel = (u16)(msr_data & 0xfffc);
2026 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002027
2028 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002029 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002030 cs.l = 1;
2031 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002032 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2033 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2034 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2035 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002036
2037 c->regs[VCPU_REGS_RCX] = c->eip;
2038 if (is_long_mode(ctxt->vcpu)) {
2039#ifdef CONFIG_X86_64
2040 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
2041
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002042 ops->get_msr(ctxt->vcpu,
2043 ctxt->mode == X86EMUL_MODE_PROT64 ?
2044 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002045 c->eip = msr_data;
2046
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002047 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002048 ctxt->eflags &= ~(msr_data | EFLG_RF);
2049#endif
2050 } else {
2051 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002052 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002053 c->eip = (u32)msr_data;
2054
2055 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2056 }
2057
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002058 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002059}
2060
Andre Przywara8c604352009-06-18 12:56:01 +02002061static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002062emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002063{
2064 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002065 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002066 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002067 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002068
Gleb Natapova0044752010-02-10 14:21:31 +02002069 /* inject #GP if in real mode */
2070 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002071 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002072 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002073 }
2074
2075 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2076 * Therefore, we inject an #UD.
2077 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002078 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002079 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002080 return X86EMUL_PROPAGATE_FAULT;
2081 }
Andre Przywara8c604352009-06-18 12:56:01 +02002082
Gleb Natapov79168fd2010-04-28 19:15:30 +03002083 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002084
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002085 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002086 switch (ctxt->mode) {
2087 case X86EMUL_MODE_PROT32:
2088 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002089 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002090 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002091 }
2092 break;
2093 case X86EMUL_MODE_PROT64:
2094 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002095 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002096 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002097 }
2098 break;
2099 }
2100
2101 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 cs_sel = (u16)msr_data;
2103 cs_sel &= ~SELECTOR_RPL_MASK;
2104 ss_sel = cs_sel + 8;
2105 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002106 if (ctxt->mode == X86EMUL_MODE_PROT64
2107 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002109 cs.l = 1;
2110 }
2111
Gleb Natapov79168fd2010-04-28 19:15:30 +03002112 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2113 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2114 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2115 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002116
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002117 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002118 c->eip = msr_data;
2119
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002120 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002121 c->regs[VCPU_REGS_RSP] = msr_data;
2122
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002123 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002124}
2125
Andre Przywara4668f052009-06-18 12:56:02 +02002126static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002127emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002128{
2129 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002130 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002131 u64 msr_data;
2132 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002133 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002134
Gleb Natapova0044752010-02-10 14:21:31 +02002135 /* inject #GP if in real mode or Virtual 8086 mode */
2136 if (ctxt->mode == X86EMUL_MODE_REAL ||
2137 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002138 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002139 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002140 }
2141
Gleb Natapov79168fd2010-04-28 19:15:30 +03002142 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002143
2144 if ((c->rex_prefix & 0x8) != 0x0)
2145 usermode = X86EMUL_MODE_PROT64;
2146 else
2147 usermode = X86EMUL_MODE_PROT32;
2148
2149 cs.dpl = 3;
2150 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002151 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002152 switch (usermode) {
2153 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002154 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002155 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002156 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002157 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002158 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002159 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002160 break;
2161 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002162 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002163 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002164 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002165 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002166 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002167 ss_sel = cs_sel + 8;
2168 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002169 cs.l = 1;
2170 break;
2171 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002172 cs_sel |= SELECTOR_RPL_MASK;
2173 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002174
Gleb Natapov79168fd2010-04-28 19:15:30 +03002175 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2176 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2177 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2178 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002179
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002180 c->eip = c->regs[VCPU_REGS_RDX];
2181 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002182
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002183 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002184}
2185
Gleb Natapov9c537242010-03-18 15:20:05 +02002186static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2187 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002188{
2189 int iopl;
2190 if (ctxt->mode == X86EMUL_MODE_REAL)
2191 return false;
2192 if (ctxt->mode == X86EMUL_MODE_VM86)
2193 return true;
2194 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002195 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002196}
2197
2198static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2199 struct x86_emulate_ops *ops,
2200 u16 port, u16 len)
2201{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002203 int r;
2204 u16 io_bitmap_ptr;
2205 u8 perm, bit_idx = port & 0x7;
2206 unsigned mask = (1 << len) - 1;
2207
Gleb Natapov79168fd2010-04-28 19:15:30 +03002208 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2209 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002210 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002212 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2214 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002215 if (r != X86EMUL_CONTINUE)
2216 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002217 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002218 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2220 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002221 if (r != X86EMUL_CONTINUE)
2222 return false;
2223 if ((perm >> bit_idx) & mask)
2224 return false;
2225 return true;
2226}
2227
2228static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2229 struct x86_emulate_ops *ops,
2230 u16 port, u16 len)
2231{
Gleb Natapov9c537242010-03-18 15:20:05 +02002232 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002233 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2234 return false;
2235 return true;
2236}
2237
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002238static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2239 struct x86_emulate_ops *ops,
2240 struct tss_segment_16 *tss)
2241{
2242 struct decode_cache *c = &ctxt->decode;
2243
2244 tss->ip = c->eip;
2245 tss->flag = ctxt->eflags;
2246 tss->ax = c->regs[VCPU_REGS_RAX];
2247 tss->cx = c->regs[VCPU_REGS_RCX];
2248 tss->dx = c->regs[VCPU_REGS_RDX];
2249 tss->bx = c->regs[VCPU_REGS_RBX];
2250 tss->sp = c->regs[VCPU_REGS_RSP];
2251 tss->bp = c->regs[VCPU_REGS_RBP];
2252 tss->si = c->regs[VCPU_REGS_RSI];
2253 tss->di = c->regs[VCPU_REGS_RDI];
2254
2255 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2256 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2257 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2258 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2259 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2260}
2261
2262static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2263 struct x86_emulate_ops *ops,
2264 struct tss_segment_16 *tss)
2265{
2266 struct decode_cache *c = &ctxt->decode;
2267 int ret;
2268
2269 c->eip = tss->ip;
2270 ctxt->eflags = tss->flag | 2;
2271 c->regs[VCPU_REGS_RAX] = tss->ax;
2272 c->regs[VCPU_REGS_RCX] = tss->cx;
2273 c->regs[VCPU_REGS_RDX] = tss->dx;
2274 c->regs[VCPU_REGS_RBX] = tss->bx;
2275 c->regs[VCPU_REGS_RSP] = tss->sp;
2276 c->regs[VCPU_REGS_RBP] = tss->bp;
2277 c->regs[VCPU_REGS_RSI] = tss->si;
2278 c->regs[VCPU_REGS_RDI] = tss->di;
2279
2280 /*
2281 * SDM says that segment selectors are loaded before segment
2282 * descriptors
2283 */
2284 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2285 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2286 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2287 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2288 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2289
2290 /*
2291 * Now load segment descriptors. If fault happenes at this stage
2292 * it is handled in a context of new task
2293 */
2294 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2295 if (ret != X86EMUL_CONTINUE)
2296 return ret;
2297 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2298 if (ret != X86EMUL_CONTINUE)
2299 return ret;
2300 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2301 if (ret != X86EMUL_CONTINUE)
2302 return ret;
2303 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2304 if (ret != X86EMUL_CONTINUE)
2305 return ret;
2306 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2307 if (ret != X86EMUL_CONTINUE)
2308 return ret;
2309
2310 return X86EMUL_CONTINUE;
2311}
2312
2313static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2314 struct x86_emulate_ops *ops,
2315 u16 tss_selector, u16 old_tss_sel,
2316 ulong old_tss_base, struct desc_struct *new_desc)
2317{
2318 struct tss_segment_16 tss_seg;
2319 int ret;
2320 u32 err, new_tss_base = get_desc_base(new_desc);
2321
2322 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2323 &err);
2324 if (ret == X86EMUL_PROPAGATE_FAULT) {
2325 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002326 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327 return ret;
2328 }
2329
2330 save_state_to_tss16(ctxt, ops, &tss_seg);
2331
2332 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2333 &err);
2334 if (ret == X86EMUL_PROPAGATE_FAULT) {
2335 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002336 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 return ret;
2338 }
2339
2340 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2341 &err);
2342 if (ret == X86EMUL_PROPAGATE_FAULT) {
2343 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002344 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345 return ret;
2346 }
2347
2348 if (old_tss_sel != 0xffff) {
2349 tss_seg.prev_task_link = old_tss_sel;
2350
2351 ret = ops->write_std(new_tss_base,
2352 &tss_seg.prev_task_link,
2353 sizeof tss_seg.prev_task_link,
2354 ctxt->vcpu, &err);
2355 if (ret == X86EMUL_PROPAGATE_FAULT) {
2356 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002357 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002358 return ret;
2359 }
2360 }
2361
2362 return load_state_from_tss16(ctxt, ops, &tss_seg);
2363}
2364
2365static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2366 struct x86_emulate_ops *ops,
2367 struct tss_segment_32 *tss)
2368{
2369 struct decode_cache *c = &ctxt->decode;
2370
2371 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2372 tss->eip = c->eip;
2373 tss->eflags = ctxt->eflags;
2374 tss->eax = c->regs[VCPU_REGS_RAX];
2375 tss->ecx = c->regs[VCPU_REGS_RCX];
2376 tss->edx = c->regs[VCPU_REGS_RDX];
2377 tss->ebx = c->regs[VCPU_REGS_RBX];
2378 tss->esp = c->regs[VCPU_REGS_RSP];
2379 tss->ebp = c->regs[VCPU_REGS_RBP];
2380 tss->esi = c->regs[VCPU_REGS_RSI];
2381 tss->edi = c->regs[VCPU_REGS_RDI];
2382
2383 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2384 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2385 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2386 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2387 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2388 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2389 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2390}
2391
2392static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2393 struct x86_emulate_ops *ops,
2394 struct tss_segment_32 *tss)
2395{
2396 struct decode_cache *c = &ctxt->decode;
2397 int ret;
2398
Gleb Natapov0f122442010-04-28 19:15:31 +03002399 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002400 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002401 return X86EMUL_PROPAGATE_FAULT;
2402 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403 c->eip = tss->eip;
2404 ctxt->eflags = tss->eflags | 2;
2405 c->regs[VCPU_REGS_RAX] = tss->eax;
2406 c->regs[VCPU_REGS_RCX] = tss->ecx;
2407 c->regs[VCPU_REGS_RDX] = tss->edx;
2408 c->regs[VCPU_REGS_RBX] = tss->ebx;
2409 c->regs[VCPU_REGS_RSP] = tss->esp;
2410 c->regs[VCPU_REGS_RBP] = tss->ebp;
2411 c->regs[VCPU_REGS_RSI] = tss->esi;
2412 c->regs[VCPU_REGS_RDI] = tss->edi;
2413
2414 /*
2415 * SDM says that segment selectors are loaded before segment
2416 * descriptors
2417 */
2418 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2419 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2420 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2421 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2422 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2423 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2424 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2425
2426 /*
2427 * Now load segment descriptors. If fault happenes at this stage
2428 * it is handled in a context of new task
2429 */
2430 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2431 if (ret != X86EMUL_CONTINUE)
2432 return ret;
2433 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2434 if (ret != X86EMUL_CONTINUE)
2435 return ret;
2436 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2437 if (ret != X86EMUL_CONTINUE)
2438 return ret;
2439 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2440 if (ret != X86EMUL_CONTINUE)
2441 return ret;
2442 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2443 if (ret != X86EMUL_CONTINUE)
2444 return ret;
2445 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2446 if (ret != X86EMUL_CONTINUE)
2447 return ret;
2448 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2449 if (ret != X86EMUL_CONTINUE)
2450 return ret;
2451
2452 return X86EMUL_CONTINUE;
2453}
2454
2455static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2456 struct x86_emulate_ops *ops,
2457 u16 tss_selector, u16 old_tss_sel,
2458 ulong old_tss_base, struct desc_struct *new_desc)
2459{
2460 struct tss_segment_32 tss_seg;
2461 int ret;
2462 u32 err, new_tss_base = get_desc_base(new_desc);
2463
2464 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2465 &err);
2466 if (ret == X86EMUL_PROPAGATE_FAULT) {
2467 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002468 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 return ret;
2470 }
2471
2472 save_state_to_tss32(ctxt, ops, &tss_seg);
2473
2474 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2475 &err);
2476 if (ret == X86EMUL_PROPAGATE_FAULT) {
2477 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002478 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 return ret;
2480 }
2481
2482 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2483 &err);
2484 if (ret == X86EMUL_PROPAGATE_FAULT) {
2485 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002486 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487 return ret;
2488 }
2489
2490 if (old_tss_sel != 0xffff) {
2491 tss_seg.prev_task_link = old_tss_sel;
2492
2493 ret = ops->write_std(new_tss_base,
2494 &tss_seg.prev_task_link,
2495 sizeof tss_seg.prev_task_link,
2496 ctxt->vcpu, &err);
2497 if (ret == X86EMUL_PROPAGATE_FAULT) {
2498 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002499 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500 return ret;
2501 }
2502 }
2503
2504 return load_state_from_tss32(ctxt, ops, &tss_seg);
2505}
2506
2507static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002508 struct x86_emulate_ops *ops,
2509 u16 tss_selector, int reason,
2510 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511{
2512 struct desc_struct curr_tss_desc, next_tss_desc;
2513 int ret;
2514 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2515 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002516 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002517 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518
2519 /* FIXME: old_tss_base == ~0 ? */
2520
2521 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2522 if (ret != X86EMUL_CONTINUE)
2523 return ret;
2524 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2525 if (ret != X86EMUL_CONTINUE)
2526 return ret;
2527
2528 /* FIXME: check that next_tss_desc is tss */
2529
2530 if (reason != TASK_SWITCH_IRET) {
2531 if ((tss_selector & 3) > next_tss_desc.dpl ||
2532 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002533 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 return X86EMUL_PROPAGATE_FAULT;
2535 }
2536 }
2537
Gleb Natapovceffb452010-03-18 15:20:19 +02002538 desc_limit = desc_limit_scaled(&next_tss_desc);
2539 if (!next_tss_desc.p ||
2540 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2541 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002542 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 return X86EMUL_PROPAGATE_FAULT;
2544 }
2545
2546 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2547 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2548 write_segment_descriptor(ctxt, ops, old_tss_sel,
2549 &curr_tss_desc);
2550 }
2551
2552 if (reason == TASK_SWITCH_IRET)
2553 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2554
2555 /* set back link to prev task only if NT bit is set in eflags
2556 note that old_tss_sel is not used afetr this point */
2557 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2558 old_tss_sel = 0xffff;
2559
2560 if (next_tss_desc.type & 8)
2561 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2562 old_tss_base, &next_tss_desc);
2563 else
2564 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2565 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002566 if (ret != X86EMUL_CONTINUE)
2567 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568
2569 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2570 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2571
2572 if (reason != TASK_SWITCH_IRET) {
2573 next_tss_desc.type |= (1 << 1); /* set busy flag */
2574 write_segment_descriptor(ctxt, ops, tss_selector,
2575 &next_tss_desc);
2576 }
2577
2578 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2579 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2580 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2581
Jan Kiszkae269fb22010-04-14 15:51:09 +02002582 if (has_error_code) {
2583 struct decode_cache *c = &ctxt->decode;
2584
2585 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2586 c->lock_prefix = 0;
2587 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002588 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002589 }
2590
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 return ret;
2592}
2593
2594int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2595 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002596 u16 tss_selector, int reason,
2597 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598{
2599 struct decode_cache *c = &ctxt->decode;
2600 int rc;
2601
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002603 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
Jan Kiszkae269fb22010-04-14 15:51:09 +02002605 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2606 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607
2608 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002609 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002610 if (rc == X86EMUL_CONTINUE)
2611 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 }
2613
Gleb Natapov19d04432010-04-15 12:29:50 +03002614 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615}
2616
Gleb Natapova682e352010-03-18 15:20:21 +02002617static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002618 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002619{
2620 struct decode_cache *c = &ctxt->decode;
2621 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2622
Gleb Natapovd9271122010-03-18 15:20:22 +02002623 register_address_increment(c, &c->regs[reg], df * op->bytes);
2624 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002625}
2626
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002627int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002628x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002629{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002630 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002631 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002632 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002633 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002634
Gleb Natapov9de41572010-04-28 19:15:22 +03002635 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002636
Gleb Natapov11616242010-02-11 14:43:14 +02002637 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002638 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002639 goto done;
2640 }
2641
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002642 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002643 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002644 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002645 goto done;
2646 }
2647
Gleb Natapove92805a2010-02-10 14:21:35 +02002648 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002649 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002650 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002651 goto done;
2652 }
2653
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002654 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002655 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002656 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002657 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002658 string_done:
2659 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002660 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002661 goto done;
2662 }
2663 /* The second termination condition only applies for REPE
2664 * and REPNE. Test if the repeat string operation prefix is
2665 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2666 * corresponding termination condition according to:
2667 * - if REPE/REPZ and ZF = 0 then done
2668 * - if REPNE/REPNZ and ZF = 1 then done
2669 */
2670 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002671 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002672 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002673 ((ctxt->eflags & EFLG_ZF) == 0))
2674 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002675 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002676 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2677 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002678 }
Gleb Natapov063db062010-03-18 15:20:06 +02002679 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002680 }
2681
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002682 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002683 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002684 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002685 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002686 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002687 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002688 }
2689
Gleb Natapove35b7b92010-02-25 16:36:42 +02002690 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002691 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2692 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002693 if (rc != X86EMUL_CONTINUE)
2694 goto done;
2695 }
2696
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002697 if ((c->d & DstMask) == ImplicitOps)
2698 goto special_insn;
2699
2700
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002701 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2702 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002703 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2704 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002705 if (rc != X86EMUL_CONTINUE)
2706 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002707 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002708 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002709
Avi Kivity018a98d2007-11-27 19:30:56 +02002710special_insn:
2711
Laurent Viviere4e03de2007-09-18 11:52:50 +02002712 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 goto twobyte_insn;
2714
Laurent Viviere4e03de2007-09-18 11:52:50 +02002715 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 case 0x00 ... 0x05:
2717 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002718 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002720 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002721 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002722 break;
2723 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002724 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002725 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002726 goto done;
2727 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 case 0x08 ... 0x0d:
2729 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002730 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002732 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002733 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002734 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 case 0x10 ... 0x15:
2736 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002737 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002739 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002740 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 break;
2742 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002743 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002744 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002745 goto done;
2746 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 case 0x18 ... 0x1d:
2748 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002749 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002751 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002752 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002753 break;
2754 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002755 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002756 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002757 goto done;
2758 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002759 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002761 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 break;
2763 case 0x28 ... 0x2d:
2764 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002765 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
2767 case 0x30 ... 0x35:
2768 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002769 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
2771 case 0x38 ... 0x3d:
2772 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002773 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002775 case 0x40 ... 0x47: /* inc r16/r32 */
2776 emulate_1op("inc", c->dst, ctxt->eflags);
2777 break;
2778 case 0x48 ... 0x4f: /* dec r16/r32 */
2779 emulate_1op("dec", c->dst, ctxt->eflags);
2780 break;
2781 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002782 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002783 break;
2784 case 0x58 ... 0x5f: /* pop reg */
2785 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002786 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002787 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002788 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002789 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002790 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002791 rc = emulate_pusha(ctxt, ops);
2792 if (rc != X86EMUL_CONTINUE)
2793 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002794 break;
2795 case 0x61: /* popa */
2796 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002797 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002798 goto done;
2799 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002801 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002803 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002805 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002806 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002807 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002808 break;
2809 case 0x6c: /* insb */
2810 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002811 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002812 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002813 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002814 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002815 goto done;
2816 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002817 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2818 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002819 goto done; /* IO is needed, skip writeback */
2820 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002821 case 0x6e: /* outsb */
2822 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002823 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002824 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002825 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002826 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002827 goto done;
2828 }
Gleb Natapov79729952010-03-18 15:20:24 +02002829 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2830 &c->src.val, 1, ctxt->vcpu);
2831
2832 c->dst.type = OP_NONE; /* nothing to writeback */
2833 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002834 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002835 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002836 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002839 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 case 0:
2841 goto add;
2842 case 1:
2843 goto or;
2844 case 2:
2845 goto adc;
2846 case 3:
2847 goto sbb;
2848 case 4:
2849 goto and;
2850 case 5:
2851 goto sub;
2852 case 6:
2853 goto xor;
2854 case 7:
2855 goto cmp;
2856 }
2857 break;
2858 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002859 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002860 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 break;
2862 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002863 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002865 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002867 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 break;
2869 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002870 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 break;
2872 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002873 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break; /* 64b reg: zero-extend */
2875 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002876 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 break;
2878 }
2879 /*
2880 * Write back the memory destination with implicit LOCK
2881 * prefix.
2882 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002883 c->dst.val = c->src.val;
2884 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002887 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002888 case 0x8c: /* mov r/m, sreg */
2889 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002890 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002891 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002892 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002893 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002894 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002895 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002896 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002897 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002898 case 0x8e: { /* mov seg, r/m16 */
2899 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002900
2901 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002902
Gleb Natapovc6975182010-02-18 12:15:01 +02002903 if (c->modrm_reg == VCPU_SREG_CS ||
2904 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002905 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002906 goto done;
2907 }
2908
Glauber Costa310b5d32009-05-12 16:21:06 -04002909 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002910 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002911
Gleb Natapov2e873022010-03-18 15:20:18 +02002912 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002913
2914 c->dst.type = OP_NONE; /* Disable writeback. */
2915 break;
2916 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002918 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002919 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002922 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002923 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2924 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002925 break;
2926 }
2927 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002928 c->src.type = OP_REG;
2929 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002930 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2931 c->src.val = *(c->src.ptr);
2932 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002933 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002934 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002935 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002936 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002937 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002938 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002939 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002940 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002941 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2942 if (rc != X86EMUL_CONTINUE)
2943 goto done;
2944 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002945 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002947 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002949 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002950 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002951 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002952 case 0xa8 ... 0xa9: /* test ax, imm */
2953 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002955 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 break;
2957 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002958 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 case 0xae ... 0xaf: /* scas */
2960 DPRINTF("Urk! I don't handle SCAS.\n");
2961 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002962 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002963 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002964 case 0xc0 ... 0xc1:
2965 emulate_grp2(ctxt);
2966 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002967 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002968 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002969 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002970 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002971 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002972 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2973 mov:
2974 c->dst.val = c->src.val;
2975 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002976 case 0xcb: /* ret far */
2977 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002978 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002979 goto done;
2980 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002981 case 0xcf: /* iret */
2982 rc = emulate_iret(ctxt, ops);
2983
2984 if (rc != X86EMUL_CONTINUE)
2985 goto done;
2986 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002987 case 0xd0 ... 0xd1: /* Grp2 */
2988 c->src.val = 1;
2989 emulate_grp2(ctxt);
2990 break;
2991 case 0xd2 ... 0xd3: /* Grp2 */
2992 c->src.val = c->regs[VCPU_REGS_RCX];
2993 emulate_grp2(ctxt);
2994 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002995 case 0xe4: /* inb */
2996 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002997 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002998 case 0xe6: /* outb */
2999 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003000 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003001 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003002 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003003 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003004 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003005 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003006 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003007 }
3008 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003009 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003010 case 0xea: { /* jmp far */
3011 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003012 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003013 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3014
3015 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003016 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003017
Gleb Natapov414e6272010-04-28 19:15:26 +03003018 c->eip = 0;
3019 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003020 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003021 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003022 case 0xeb:
3023 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003024 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003025 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003026 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003027 case 0xec: /* in al,dx */
3028 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003029 c->src.val = c->regs[VCPU_REGS_RDX];
3030 do_io_in:
3031 c->dst.bytes = min(c->dst.bytes, 4u);
3032 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003033 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003034 goto done;
3035 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003036 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3037 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003038 goto done; /* IO is needed */
3039 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003040 case 0xee: /* out dx,al */
3041 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003042 c->src.val = c->regs[VCPU_REGS_RDX];
3043 do_io_out:
3044 c->dst.bytes = min(c->dst.bytes, 4u);
3045 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003046 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003047 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003048 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003049 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3050 ctxt->vcpu);
3051 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003052 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003053 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003054 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003055 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003056 case 0xf5: /* cmc */
3057 /* complement carry flag from eflags reg */
3058 ctxt->eflags ^= EFLG_CF;
3059 c->dst.type = OP_NONE; /* Disable writeback. */
3060 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003061 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003062 if (!emulate_grp3(ctxt, ops))
3063 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003064 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003065 case 0xf8: /* clc */
3066 ctxt->eflags &= ~EFLG_CF;
3067 c->dst.type = OP_NONE; /* Disable writeback. */
3068 break;
3069 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003070 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003071 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003072 goto done;
3073 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003074 ctxt->eflags &= ~X86_EFLAGS_IF;
3075 c->dst.type = OP_NONE; /* Disable writeback. */
3076 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003077 break;
3078 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003079 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003080 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003081 goto done;
3082 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003083 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003084 ctxt->eflags |= X86_EFLAGS_IF;
3085 c->dst.type = OP_NONE; /* Disable writeback. */
3086 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003087 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003088 case 0xfc: /* cld */
3089 ctxt->eflags &= ~EFLG_DF;
3090 c->dst.type = OP_NONE; /* Disable writeback. */
3091 break;
3092 case 0xfd: /* std */
3093 ctxt->eflags |= EFLG_DF;
3094 c->dst.type = OP_NONE; /* Disable writeback. */
3095 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003096 case 0xfe: /* Grp4 */
3097 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003098 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003099 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003100 goto done;
3101 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003102 case 0xff: /* Grp5 */
3103 if (c->modrm_reg == 5)
3104 goto jump_far;
3105 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003106 default:
3107 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003109
3110writeback:
3111 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003112 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003113 goto done;
3114
Gleb Natapov5cd21912010-03-18 15:20:26 +02003115 /*
3116 * restore dst type in case the decoding will be reused
3117 * (happens for string instruction )
3118 */
3119 c->dst.type = saved_dst_type;
3120
Gleb Natapova682e352010-03-18 15:20:21 +02003121 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003122 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3123 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003124
3125 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003126 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3127 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003128
Gleb Natapov5cd21912010-03-18 15:20:26 +02003129 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003130 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003131 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003132 /*
3133 * Re-enter guest when pio read ahead buffer is empty or,
3134 * if it is not used, after each 1024 iteration.
3135 */
3136 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3137 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003138 ctxt->restart = false;
3139 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003140 /*
3141 * reset read cache here in case string instruction is restared
3142 * without decoding
3143 */
3144 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003145 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003146
3147done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003148 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149
3150twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003151 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003153 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 u16 size;
3155 unsigned long address;
3156
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003157 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003158 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003159 goto cannot_emulate;
3160
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003161 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003162 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003163 goto done;
3164
Avi Kivity33e38852008-05-21 15:34:25 +03003165 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003166 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003167 /* Disable writeback. */
3168 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003169 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003171 rc = read_descriptor(ctxt, ops, c->src.ptr,
3172 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003173 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 goto done;
3175 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003176 /* Disable writeback. */
3177 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003179 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003180 if (c->modrm_mod == 3) {
3181 switch (c->modrm_rm) {
3182 case 1:
3183 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003184 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003185 goto done;
3186 break;
3187 default:
3188 goto cannot_emulate;
3189 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003190 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003191 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003192 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003193 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003194 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003195 goto done;
3196 realmode_lidt(ctxt->vcpu, size, address);
3197 }
Avi Kivity16286d02008-04-14 14:40:50 +03003198 /* Disable writeback. */
3199 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 break;
3201 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003202 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003203 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 break;
3205 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003206 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3207 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003208 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003210 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003211 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003212 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003214 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003215 /* Disable writeback. */
3216 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 break;
3218 default:
3219 goto cannot_emulate;
3220 }
3221 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003222 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003223 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003224 if (rc != X86EMUL_CONTINUE)
3225 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003226 else
3227 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003228 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003229 case 0x06:
3230 emulate_clts(ctxt->vcpu);
3231 c->dst.type = OP_NONE;
3232 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003233 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003234 kvm_emulate_wbinvd(ctxt->vcpu);
3235 c->dst.type = OP_NONE;
3236 break;
3237 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003238 case 0x0d: /* GrpP (prefetch) */
3239 case 0x18: /* Grp16 (prefetch/nop) */
3240 c->dst.type = OP_NONE;
3241 break;
3242 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003243 switch (c->modrm_reg) {
3244 case 1:
3245 case 5 ... 7:
3246 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003247 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003248 goto done;
3249 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003250 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003251 c->dst.type = OP_NONE; /* no writeback */
3252 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003254 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3255 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003256 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003257 goto done;
3258 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003259 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003260 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003262 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003263 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003264 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003265 goto done;
3266 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003267 c->dst.type = OP_NONE;
3268 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003270 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3271 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003272 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003273 goto done;
3274 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003275
Gleb Natapov338dbc92010-04-28 19:15:32 +03003276 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3277 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3278 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3279 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003280 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003281 goto done;
3282 }
3283
Laurent Viviera01af5e2007-09-24 11:10:56 +02003284 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003286 case 0x30:
3287 /* wrmsr */
3288 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3289 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003290 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003291 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003292 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003293 }
3294 rc = X86EMUL_CONTINUE;
3295 c->dst.type = OP_NONE;
3296 break;
3297 case 0x32:
3298 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003299 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003300 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003301 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003302 } else {
3303 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3304 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3305 }
3306 rc = X86EMUL_CONTINUE;
3307 c->dst.type = OP_NONE;
3308 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003309 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003310 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003311 if (rc != X86EMUL_CONTINUE)
3312 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003313 else
3314 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003315 break;
3316 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003317 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003318 if (rc != X86EMUL_CONTINUE)
3319 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003320 else
3321 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003322 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003324 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003325 if (!test_cc(c->b, ctxt->eflags))
3326 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003328 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003329 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003330 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003331 c->dst.type = OP_NONE;
3332 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003333 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003334 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003335 break;
3336 case 0xa1: /* pop fs */
3337 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003338 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003339 goto done;
3340 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003341 case 0xa3:
3342 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003343 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003344 /* only subword offset */
3345 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003346 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003347 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003348 case 0xa4: /* shld imm8, r, r/m */
3349 case 0xa5: /* shld cl, r, r/m */
3350 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3351 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003352 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003353 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003354 break;
3355 case 0xa9: /* pop gs */
3356 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003357 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003358 goto done;
3359 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003360 case 0xab:
3361 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003362 /* only subword offset */
3363 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003364 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003365 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003366 case 0xac: /* shrd imm8, r, r/m */
3367 case 0xad: /* shrd cl, r, r/m */
3368 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3369 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003370 case 0xae: /* clflush */
3371 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 case 0xb0 ... 0xb1: /* cmpxchg */
3373 /*
3374 * Save real source value, then compare EAX against
3375 * destination.
3376 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003377 c->src.orig_val = c->src.val;
3378 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003379 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3380 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003381 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003382 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 } else {
3384 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003385 c->dst.type = OP_REG;
3386 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 }
3388 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 case 0xb3:
3390 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003391 /* only subword offset */
3392 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003393 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003396 c->dst.bytes = c->op_bytes;
3397 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3398 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003401 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402 case 0:
3403 goto bt;
3404 case 1:
3405 goto bts;
3406 case 2:
3407 goto btr;
3408 case 3:
3409 goto btc;
3410 }
3411 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003412 case 0xbb:
3413 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003414 /* only subword offset */
3415 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003416 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003417 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003419 c->dst.bytes = c->op_bytes;
3420 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3421 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003423 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003424 c->dst.bytes = c->op_bytes;
3425 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3426 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003427 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003429 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003430 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003431 goto done;
3432 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003433 default:
3434 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 }
3436 goto writeback;
3437
3438cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003439 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440 return -1;
3441}