blob: f3b984427d10ed6bf6d3fc67f0dc37865d762275 [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 Kivity793d5a82010-07-29 15:11:38 +0300108 NoGrp, Group1, Group1A, Group3, Group4, Group5, Group7, Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200109};
110
Avi Kivityd65b1de2010-07-29 15:11:35 +0300111struct opcode {
112 u32 flags;
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 Kivity42a1c522010-07-29 15:11:37 +0300129static struct opcode group_table[] = {
130 [Group1*8] =
131 X7(D(Lock)), N,
132 [Group1A*8] =
133 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
134 [Group3*8] =
135 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
136 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
137 X4(D(Undefined)),
138 [Group4*8] =
139 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
140 N, N, N, N, N, N,
141 [Group5*8] =
142 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
143 D(SrcMem | ModRM | Stack), N,
144 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
145 D(SrcMem | ModRM | Stack), N,
146 [Group7*8] =
147 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
148 D(SrcNone | ModRM | DstMem | Mov), N,
149 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
150 [Group8*8] =
151 N, N, N, N,
152 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
153 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
154 [Group9*8] =
155 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
156};
157
158static struct opcode group2_table[] = {
159 [Group7*8] =
160 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
161 D(SrcNone | ModRM | DstMem | Mov), N,
162 D(SrcMem16 | ModRM | Mov | Priv), N,
163 [Group9*8] =
164 N, N, N, N, N, N, N, N,
165};
166
Avi Kivityd65b1de2010-07-29 15:11:35 +0300167static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0x00 - 0x07 */
Avi Kivityfd853312010-07-29 15:11:36 +0300169 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
170 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
171 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
172 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0x08 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300174 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
175 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
176 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
177 D(ImplicitOps | Stack | No64), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0x10 - 0x17 */
Avi Kivityfd853312010-07-29 15:11:36 +0300179 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
180 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
181 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
182 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0x18 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300184 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
185 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
186 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
187 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188 /* 0x20 - 0x27 */
Avi Kivityfd853312010-07-29 15:11:36 +0300189 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
190 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
191 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0x28 - 0x2F */
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 /* 0x30 - 0x37 */
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 /* 0x38 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300201 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
202 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
203 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
204 N, N,
Avi Kivity749358a2010-07-26 14:37:40 +0300205 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300206 X16(D(DstReg)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300207 /* 0x50 - 0x57 */
Avi Kivityfd853312010-07-29 15:11:36 +0300208 X8(D(SrcReg | Stack)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300209 /* 0x58 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300210 X8(D(DstReg | Stack)),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700211 /* 0x60 - 0x67 */
Avi Kivityfd853312010-07-29 15:11:36 +0300212 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
213 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
214 N, N, N, N,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700215 /* 0x68 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300216 D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N,
217 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
218 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300219 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300220 X16(D(SrcImmByte)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0x80 - 0x87 */
Avi Kivityfd853312010-07-29 15:11:36 +0300222 D(ByteOp | DstMem | SrcImm | ModRM | Group | Group1),
223 D(DstMem | SrcImm | ModRM | Group | Group1),
224 D(ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1),
225 D(DstMem | SrcImmByte | ModRM | Group | Group1),
226 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
227 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x88 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300229 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
230 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
231 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
232 D(ImplicitOps | SrcMem16 | ModRM), D(Group | Group1A),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300233 /* 0x90 - 0x97 */
Avi Kivityfd853312010-07-29 15:11:36 +0300234 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300235 /* 0x98 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300236 N, N, D(SrcImmFAddr | No64), N,
237 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300239 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
240 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
241 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
242 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300244 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
245 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
246 D(ByteOp | DstDI | String), D(DstDI | String),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300247 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300248 X8(D(ByteOp | DstReg | SrcImm | Mov)),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300249 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300250 X8(D(DstReg | SrcImm | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 /* 0xC0 - 0xC7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300252 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
253 N, D(ImplicitOps | Stack), N, N,
254 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 /* 0xC8 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300256 N, N, N, D(ImplicitOps | Stack),
257 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 /* 0xD0 - 0xD7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300259 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
260 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
261 N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xD8 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300263 N, N, N, N, N, N, N, N,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300264 /* 0xE0 - 0xE7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300265 N, N, N, N,
266 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
267 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Nitin A Kamble098c9372007-08-19 11:00:36 +0300268 /* 0xE8 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300269 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
270 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
271 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
272 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273 /* 0xF0 - 0xF7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300274 N, N, N, N,
275 D(ImplicitOps | Priv), D(ImplicitOps), D(ByteOp | Group | Group3), D(Group | Group3),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 /* 0xF8 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300277 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
278 D(ImplicitOps), D(ImplicitOps), D(Group | Group4), D(Group | Group5),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279};
280
Avi Kivityd65b1de2010-07-29 15:11:35 +0300281static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 /* 0x00 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300283 N, D(Group | GroupDual | Group7), N, N,
284 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
285 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
286 N, D(ImplicitOps | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 /* 0x10 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300288 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 -0800289 /* 0x20 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300290 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
291 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
292 N, N, N, N,
293 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 /* 0x30 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300295 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
296 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
297 N, N, N, N, N, N, N, N,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300298 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300299 X16(D(DstReg | SrcMem | ModRM | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300 /* 0x50 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300301 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 /* 0x60 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300303 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 /* 0x70 - 0x7F */
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 /* 0x80 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300307 X16(D(SrcImm)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 /* 0x90 - 0x9F */
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 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300311 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
312 N, D(DstMem | SrcReg | ModRM | BitOp),
313 D(DstMem | SrcReg | Src2ImmByte | ModRM),
314 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300316 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
317 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
318 D(DstMem | SrcReg | Src2ImmByte | ModRM),
319 D(DstMem | SrcReg | Src2CL | ModRM),
320 D(ModRM), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300322 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
323 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
324 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
325 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300327 N, N,
328 D(Group | Group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
329 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
330 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331 /* 0xC0 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300332 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
333 N, N, N, D(Group | GroupDual | Group9),
334 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335 /* 0xD0 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300336 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 /* 0xE0 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300338 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339 /* 0xF0 - 0xFF */
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};
342
Avi Kivityfd853312010-07-29 15:11:36 +0300343#undef D
344#undef N
Avi Kivity120df892010-07-29 15:11:39 +0300345#undef G
346#undef GD
Avi Kivityfd853312010-07-29 15:11:36 +0300347
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200349#define EFLG_ID (1<<21)
350#define EFLG_VIP (1<<20)
351#define EFLG_VIF (1<<19)
352#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200353#define EFLG_VM (1<<17)
354#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200355#define EFLG_IOPL (3<<12)
356#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357#define EFLG_OF (1<<11)
358#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200359#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200360#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361#define EFLG_SF (1<<7)
362#define EFLG_ZF (1<<6)
363#define EFLG_AF (1<<4)
364#define EFLG_PF (1<<2)
365#define EFLG_CF (1<<0)
366
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300367#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
368#define EFLG_RESERVED_ONE_MASK 2
369
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370/*
371 * Instruction emulation:
372 * Most instructions are emulated directly via a fragment of inline assembly
373 * code. This allows us to save/restore EFLAGS and thus very easily pick up
374 * any modified flags.
375 */
376
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800377#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378#define _LO32 "k" /* force 32-bit operand */
379#define _STK "%%rsp" /* stack pointer */
380#elif defined(__i386__)
381#define _LO32 "" /* force 32-bit operand */
382#define _STK "%%esp" /* stack pointer */
383#endif
384
385/*
386 * These EFLAGS bits are restored from saved value during emulation, and
387 * any changes are written back to the saved value after emulation.
388 */
389#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
390
391/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200392#define _PRE_EFLAGS(_sav, _msk, _tmp) \
393 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
394 "movl %"_sav",%"_LO32 _tmp"; " \
395 "push %"_tmp"; " \
396 "push %"_tmp"; " \
397 "movl %"_msk",%"_LO32 _tmp"; " \
398 "andl %"_LO32 _tmp",("_STK"); " \
399 "pushf; " \
400 "notl %"_LO32 _tmp"; " \
401 "andl %"_LO32 _tmp",("_STK"); " \
402 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
403 "pop %"_tmp"; " \
404 "orl %"_LO32 _tmp",("_STK"); " \
405 "popf; " \
406 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407
408/* After executing instruction: write-back necessary bits in EFLAGS. */
409#define _POST_EFLAGS(_sav, _msk, _tmp) \
410 /* _sav |= EFLAGS & _msk; */ \
411 "pushf; " \
412 "pop %"_tmp"; " \
413 "andl %"_msk",%"_LO32 _tmp"; " \
414 "orl %"_LO32 _tmp",%"_sav"; "
415
Avi Kivitydda96d82008-11-26 15:14:10 +0200416#ifdef CONFIG_X86_64
417#define ON64(x) x
418#else
419#define ON64(x)
420#endif
421
Avi Kivity6b7ad612008-11-26 15:30:45 +0200422#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
423 do { \
424 __asm__ __volatile__ ( \
425 _PRE_EFLAGS("0", "4", "2") \
426 _op _suffix " %"_x"3,%1; " \
427 _POST_EFLAGS("0", "4", "2") \
428 : "=m" (_eflags), "=m" ((_dst).val), \
429 "=&r" (_tmp) \
430 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200431 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200432
433
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434/* Raw emulation: instruction has two explicit operands. */
435#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200436 do { \
437 unsigned long _tmp; \
438 \
439 switch ((_dst).bytes) { \
440 case 2: \
441 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
442 break; \
443 case 4: \
444 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
445 break; \
446 case 8: \
447 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
448 break; \
449 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 } while (0)
451
452#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
453 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200454 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400455 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200457 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 break; \
459 default: \
460 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
461 _wx, _wy, _lx, _ly, _qx, _qy); \
462 break; \
463 } \
464 } while (0)
465
466/* Source operand is byte-sized and may be restricted to just %cl. */
467#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
468 __emulate_2op(_op, _src, _dst, _eflags, \
469 "b", "c", "b", "c", "b", "c", "b", "c")
470
471/* Source operand is byte, word, long or quad sized. */
472#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
473 __emulate_2op(_op, _src, _dst, _eflags, \
474 "b", "q", "w", "r", _LO32, "r", "", "r")
475
476/* Source operand is word, long or quad sized. */
477#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
478 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
479 "w", "r", _LO32, "r", "", "r")
480
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100481/* Instruction has three operands and one operand is stored in ECX register */
482#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
483 do { \
484 unsigned long _tmp; \
485 _type _clv = (_cl).val; \
486 _type _srcv = (_src).val; \
487 _type _dstv = (_dst).val; \
488 \
489 __asm__ __volatile__ ( \
490 _PRE_EFLAGS("0", "5", "2") \
491 _op _suffix " %4,%1 \n" \
492 _POST_EFLAGS("0", "5", "2") \
493 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
494 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
495 ); \
496 \
497 (_cl).val = (unsigned long) _clv; \
498 (_src).val = (unsigned long) _srcv; \
499 (_dst).val = (unsigned long) _dstv; \
500 } while (0)
501
502#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
503 do { \
504 switch ((_dst).bytes) { \
505 case 2: \
506 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
507 "w", unsigned short); \
508 break; \
509 case 4: \
510 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
511 "l", unsigned int); \
512 break; \
513 case 8: \
514 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
515 "q", unsigned long)); \
516 break; \
517 } \
518 } while (0)
519
Avi Kivitydda96d82008-11-26 15:14:10 +0200520#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 do { \
522 unsigned long _tmp; \
523 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200524 __asm__ __volatile__ ( \
525 _PRE_EFLAGS("0", "3", "2") \
526 _op _suffix " %1; " \
527 _POST_EFLAGS("0", "3", "2") \
528 : "=m" (_eflags), "+m" ((_dst).val), \
529 "=&r" (_tmp) \
530 : "i" (EFLAGS_MASK)); \
531 } while (0)
532
533/* Instruction has only one explicit operand (no source operand). */
534#define emulate_1op(_op, _dst, _eflags) \
535 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400536 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200537 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
538 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
539 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
540 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 } \
542 } while (0)
543
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544/* Fetch next part of the instruction being emulated. */
545#define insn_fetch(_type, _size, _eip) \
546({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200547 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200548 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 goto done; \
550 (_eip) += (_size); \
551 (_type)_x; \
552})
553
Gleb Natapov414e6272010-04-28 19:15:26 +0300554#define insn_fetch_arr(_arr, _size, _eip) \
555({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
556 if (rc != X86EMUL_CONTINUE) \
557 goto done; \
558 (_eip) += (_size); \
559})
560
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800561static inline unsigned long ad_mask(struct decode_cache *c)
562{
563 return (1UL << (c->ad_bytes << 3)) - 1;
564}
565
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800567static inline unsigned long
568address_mask(struct decode_cache *c, unsigned long reg)
569{
570 if (c->ad_bytes == sizeof(unsigned long))
571 return reg;
572 else
573 return reg & ad_mask(c);
574}
575
576static inline unsigned long
577register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
578{
579 return base + address_mask(c, reg);
580}
581
Harvey Harrison7a9572752008-02-19 07:40:41 -0800582static inline void
583register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
584{
585 if (c->ad_bytes == sizeof(unsigned long))
586 *reg += inc;
587 else
588 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
589}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
Harvey Harrison7a9572752008-02-19 07:40:41 -0800591static inline void jmp_rel(struct decode_cache *c, int rel)
592{
593 register_address_increment(c, &c->eip, rel);
594}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300595
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300596static void set_seg_override(struct decode_cache *c, int seg)
597{
598 c->has_seg_override = true;
599 c->seg_override = seg;
600}
601
Gleb Natapov79168fd2010-04-28 19:15:30 +0300602static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
603 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300604{
605 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
606 return 0;
607
Gleb Natapov79168fd2010-04-28 19:15:30 +0300608 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300609}
610
611static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300612 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300613 struct decode_cache *c)
614{
615 if (!c->has_seg_override)
616 return 0;
617
Gleb Natapov79168fd2010-04-28 19:15:30 +0300618 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300619}
620
Gleb Natapov79168fd2010-04-28 19:15:30 +0300621static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
622 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300623{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300624 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300625}
626
Gleb Natapov79168fd2010-04-28 19:15:30 +0300627static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
628 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300629{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300630 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300631}
632
Gleb Natapov54b84862010-04-28 19:15:44 +0300633static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
634 u32 error, bool valid)
635{
636 ctxt->exception = vec;
637 ctxt->error_code = error;
638 ctxt->error_code_valid = valid;
639 ctxt->restart = false;
640}
641
642static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
643{
644 emulate_exception(ctxt, GP_VECTOR, err, true);
645}
646
647static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
648 int err)
649{
650 ctxt->cr2 = addr;
651 emulate_exception(ctxt, PF_VECTOR, err, true);
652}
653
654static void emulate_ud(struct x86_emulate_ctxt *ctxt)
655{
656 emulate_exception(ctxt, UD_VECTOR, 0, false);
657}
658
659static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
660{
661 emulate_exception(ctxt, TS_VECTOR, err, true);
662}
663
Avi Kivity62266862007-11-20 13:15:52 +0200664static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
665 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300666 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200667{
668 struct fetch_cache *fc = &ctxt->decode.fetch;
669 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300670 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200671
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300672 if (eip == fc->end) {
673 cur_size = fc->end - fc->start;
674 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
675 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
676 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900677 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200678 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300679 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200680 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300681 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900682 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200683}
684
685static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
686 struct x86_emulate_ops *ops,
687 unsigned long eip, void *dest, unsigned size)
688{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900689 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200690
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200691 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200692 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200693 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200694 while (size--) {
695 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900696 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200697 return rc;
698 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900699 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200700}
701
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000702/*
703 * Given the 'reg' portion of a ModRM byte, and a register block, return a
704 * pointer into the block that addresses the relevant register.
705 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
706 */
707static void *decode_register(u8 modrm_reg, unsigned long *regs,
708 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709{
710 void *p;
711
712 p = &regs[modrm_reg];
713 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
714 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
715 return p;
716}
717
718static int read_descriptor(struct x86_emulate_ctxt *ctxt,
719 struct x86_emulate_ops *ops,
720 void *ptr,
721 u16 *size, unsigned long *address, int op_bytes)
722{
723 int rc;
724
725 if (op_bytes == 2)
726 op_bytes = 3;
727 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300728 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200729 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900730 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300732 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200733 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 return rc;
735}
736
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300737static int test_cc(unsigned int condition, unsigned int flags)
738{
739 int rc = 0;
740
741 switch ((condition & 15) >> 1) {
742 case 0: /* o */
743 rc |= (flags & EFLG_OF);
744 break;
745 case 1: /* b/c/nae */
746 rc |= (flags & EFLG_CF);
747 break;
748 case 2: /* z/e */
749 rc |= (flags & EFLG_ZF);
750 break;
751 case 3: /* be/na */
752 rc |= (flags & (EFLG_CF|EFLG_ZF));
753 break;
754 case 4: /* s */
755 rc |= (flags & EFLG_SF);
756 break;
757 case 5: /* p/pe */
758 rc |= (flags & EFLG_PF);
759 break;
760 case 7: /* le/ng */
761 rc |= (flags & EFLG_ZF);
762 /* fall through */
763 case 6: /* l/nge */
764 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
765 break;
766 }
767
768 /* Odd condition identifiers (lsb == 1) have inverted sense. */
769 return (!!rc ^ (condition & 1));
770}
771
Avi Kivity3c118e22007-10-31 10:27:04 +0200772static void decode_register_operand(struct operand *op,
773 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200774 int inhibit_bytereg)
775{
Avi Kivity33615aa2007-10-31 11:15:56 +0200776 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200777 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200778
779 if (!(c->d & ModRM))
780 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200781 op->type = OP_REG;
782 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200783 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200784 op->val = *(u8 *)op->ptr;
785 op->bytes = 1;
786 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200787 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200788 op->bytes = c->op_bytes;
789 switch (op->bytes) {
790 case 2:
791 op->val = *(u16 *)op->ptr;
792 break;
793 case 4:
794 op->val = *(u32 *)op->ptr;
795 break;
796 case 8:
797 op->val = *(u64 *) op->ptr;
798 break;
799 }
800 }
801 op->orig_val = op->val;
802}
803
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804static int decode_modrm(struct x86_emulate_ctxt *ctxt,
805 struct x86_emulate_ops *ops)
806{
807 struct decode_cache *c = &ctxt->decode;
808 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700809 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900810 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200811
812 if (c->rex_prefix) {
813 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
814 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
815 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
816 }
817
818 c->modrm = insn_fetch(u8, 1, c->eip);
819 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
820 c->modrm_reg |= (c->modrm & 0x38) >> 3;
821 c->modrm_rm |= (c->modrm & 0x07);
822 c->modrm_ea = 0;
823 c->use_modrm_ea = 1;
824
825 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300826 c->modrm_ptr = decode_register(c->modrm_rm,
827 c->regs, c->d & ByteOp);
828 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200829 return rc;
830 }
831
832 if (c->ad_bytes == 2) {
833 unsigned bx = c->regs[VCPU_REGS_RBX];
834 unsigned bp = c->regs[VCPU_REGS_RBP];
835 unsigned si = c->regs[VCPU_REGS_RSI];
836 unsigned di = c->regs[VCPU_REGS_RDI];
837
838 /* 16-bit ModR/M decode. */
839 switch (c->modrm_mod) {
840 case 0:
841 if (c->modrm_rm == 6)
842 c->modrm_ea += insn_fetch(u16, 2, c->eip);
843 break;
844 case 1:
845 c->modrm_ea += insn_fetch(s8, 1, c->eip);
846 break;
847 case 2:
848 c->modrm_ea += insn_fetch(u16, 2, c->eip);
849 break;
850 }
851 switch (c->modrm_rm) {
852 case 0:
853 c->modrm_ea += bx + si;
854 break;
855 case 1:
856 c->modrm_ea += bx + di;
857 break;
858 case 2:
859 c->modrm_ea += bp + si;
860 break;
861 case 3:
862 c->modrm_ea += bp + di;
863 break;
864 case 4:
865 c->modrm_ea += si;
866 break;
867 case 5:
868 c->modrm_ea += di;
869 break;
870 case 6:
871 if (c->modrm_mod != 0)
872 c->modrm_ea += bp;
873 break;
874 case 7:
875 c->modrm_ea += bx;
876 break;
877 }
878 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
879 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300880 if (!c->has_seg_override)
881 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200882 c->modrm_ea = (u16)c->modrm_ea;
883 } else {
884 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700885 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886 sib = insn_fetch(u8, 1, c->eip);
887 index_reg |= (sib >> 3) & 7;
888 base_reg |= sib & 7;
889 scale = sib >> 6;
890
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700891 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
892 c->modrm_ea += insn_fetch(s32, 4, c->eip);
893 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700895 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700897 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
898 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700899 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700900 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200901 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 switch (c->modrm_mod) {
903 case 0:
904 if (c->modrm_rm == 5)
905 c->modrm_ea += insn_fetch(s32, 4, c->eip);
906 break;
907 case 1:
908 c->modrm_ea += insn_fetch(s8, 1, c->eip);
909 break;
910 case 2:
911 c->modrm_ea += insn_fetch(s32, 4, c->eip);
912 break;
913 }
914 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200915done:
916 return rc;
917}
918
919static int decode_abs(struct x86_emulate_ctxt *ctxt,
920 struct x86_emulate_ops *ops)
921{
922 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924
925 switch (c->ad_bytes) {
926 case 2:
927 c->modrm_ea = insn_fetch(u16, 2, c->eip);
928 break;
929 case 4:
930 c->modrm_ea = insn_fetch(u32, 4, c->eip);
931 break;
932 case 8:
933 c->modrm_ea = insn_fetch(u64, 8, c->eip);
934 break;
935 }
936done:
937 return rc;
938}
939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200941x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200943 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900944 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 int mode = ctxt->mode;
Avi Kivity120df892010-07-29 15:11:39 +0300946 int def_op_bytes, def_ad_bytes, group, dual, goffset;
947 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948
Gleb Natapov5cd21912010-03-18 15:20:26 +0200949 /* we cannot decode insn before we complete previous rep insn */
950 WARN_ON(ctxt->restart);
951
Gleb Natapov063db062010-03-18 15:20:06 +0200952 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300953 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300954 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
956 switch (mode) {
957 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200958 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200960 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 break;
962 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200963 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800965#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 def_op_bytes = 4;
968 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 break;
970#endif
971 default:
972 return -1;
973 }
974
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200975 c->op_bytes = def_op_bytes;
976 c->ad_bytes = def_ad_bytes;
977
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200979 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200980 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200982 /* switch between 2/4 bytes */
983 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 break;
985 case 0x67: /* address-size override */
986 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200987 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200988 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200990 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200991 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300994 case 0x2e: /* CS override */
995 case 0x36: /* SS override */
996 case 0x3e: /* DS override */
997 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 break;
999 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001001 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001003 case 0x40 ... 0x4f: /* REX */
1004 if (mode != X86EMUL_MODE_PROT64)
1005 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001006 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001007 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001011 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001012 c->rep_prefix = REPNE_PREFIX;
1013 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001015 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 default:
1018 goto done_prefixes;
1019 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001020
1021 /* Any legacy prefix after a REX prefix nullifies its effect. */
1022
Avi Kivity33615aa2007-10-31 11:15:56 +02001023 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 }
1025
1026done_prefixes:
1027
1028 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001029 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001030 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032
1033 /* Opcode byte(s). */
Avi Kivity120df892010-07-29 15:11:39 +03001034 opcode = opcode_table[c->b];
1035 if (opcode.flags == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 if (c->b == 0x0f) {
1038 c->twobyte = 1;
1039 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivity120df892010-07-29 15:11:39 +03001040 opcode = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001042 }
Avi Kivity120df892010-07-29 15:11:39 +03001043 c->d = opcode.flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044
Avi Kivitye09d0822008-01-18 12:38:59 +02001045 if (c->d & Group) {
1046 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001047 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001048 c->modrm = insn_fetch(u8, 1, c->eip);
1049 --c->eip;
1050
Avi Kivity120df892010-07-29 15:11:39 +03001051 if (group) {
1052 g_mod012 = g_mod3 = &group_table[group * 8];
1053 if (c->d & GroupDual)
1054 g_mod3 = &group2_table[group * 8];
1055 } else {
1056 if (c->d & GroupDual) {
1057 g_mod012 = opcode.u.gdual->mod012;
1058 g_mod3 = opcode.u.gdual->mod3;
1059 } else
1060 g_mod012 = g_mod3 = opcode.u.group;
1061 }
1062
Avi Kivity52811d72010-07-26 14:37:48 +03001063 c->d &= ~(Group | GroupDual | GroupMask);
Avi Kivity120df892010-07-29 15:11:39 +03001064
1065 goffset = (c->modrm >> 3) & 7;
1066
1067 if ((c->modrm >> 6) == 3)
1068 opcode = g_mod3[goffset];
Avi Kivitye09d0822008-01-18 12:38:59 +02001069 else
Avi Kivity120df892010-07-29 15:11:39 +03001070 opcode = g_mod012[goffset];
1071 c->d |= opcode.flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001072 }
1073
1074 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001075 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001076 DPRINTF("Cannot emulate %02x\n", c->b);
1077 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 }
1079
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001080 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1081 c->op_bytes = 8;
1082
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001084 if (c->d & ModRM)
1085 rc = decode_modrm(ctxt, ops);
1086 else if (c->d & MemAbs)
1087 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001088 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001089 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001091 if (!c->has_seg_override)
1092 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001093
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001094 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001095 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001096
1097 if (c->ad_bytes != 8)
1098 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001099
1100 if (c->rip_relative)
1101 c->modrm_ea += c->eip;
1102
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 /*
1104 * Decode and fetch the source operand: register, memory
1105 * or immediate.
1106 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001107 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 case SrcNone:
1109 break;
1110 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001111 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001114 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 goto srcmem_common;
1116 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001117 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 goto srcmem_common;
1119 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001120 c->src.bytes = (c->d & ByteOp) ? 1 :
1121 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001122 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001123 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001124 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001125 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001126 /*
1127 * For instructions with a ModR/M byte, switch to register
1128 * access if Mod = 3.
1129 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 if ((c->d & ModRM) && c->modrm_mod == 3) {
1131 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001132 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001133 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001134 break;
1135 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001137 c->src.ptr = (unsigned long *)c->modrm_ea;
1138 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 break;
1140 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001141 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001142 c->src.type = OP_IMM;
1143 c->src.ptr = (unsigned long *)c->eip;
1144 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1145 if (c->src.bytes == 8)
1146 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001148 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001150 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 break;
1152 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001153 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 break;
1155 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001156 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 break;
1158 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001159 if ((c->d & SrcMask) == SrcImmU) {
1160 switch (c->src.bytes) {
1161 case 1:
1162 c->src.val &= 0xff;
1163 break;
1164 case 2:
1165 c->src.val &= 0xffff;
1166 break;
1167 case 4:
1168 c->src.val &= 0xffffffff;
1169 break;
1170 }
1171 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 break;
1173 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001174 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001175 c->src.type = OP_IMM;
1176 c->src.ptr = (unsigned long *)c->eip;
1177 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001178 if ((c->d & SrcMask) == SrcImmByte)
1179 c->src.val = insn_fetch(s8, 1, c->eip);
1180 else
1181 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001183 case SrcAcc:
1184 c->src.type = OP_REG;
1185 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1186 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1187 switch (c->src.bytes) {
1188 case 1:
1189 c->src.val = *(u8 *)c->src.ptr;
1190 break;
1191 case 2:
1192 c->src.val = *(u16 *)c->src.ptr;
1193 break;
1194 case 4:
1195 c->src.val = *(u32 *)c->src.ptr;
1196 break;
1197 case 8:
1198 c->src.val = *(u64 *)c->src.ptr;
1199 break;
1200 }
1201 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001202 case SrcOne:
1203 c->src.bytes = 1;
1204 c->src.val = 1;
1205 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001206 case SrcSI:
1207 c->src.type = OP_MEM;
1208 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1209 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001210 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001211 c->regs[VCPU_REGS_RSI]);
1212 c->src.val = 0;
1213 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001214 case SrcImmFAddr:
1215 c->src.type = OP_IMM;
1216 c->src.ptr = (unsigned long *)c->eip;
1217 c->src.bytes = c->op_bytes + 2;
1218 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1219 break;
1220 case SrcMemFAddr:
1221 c->src.type = OP_MEM;
1222 c->src.ptr = (unsigned long *)c->modrm_ea;
1223 c->src.bytes = c->op_bytes + 2;
1224 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 }
1226
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001227 /*
1228 * Decode and fetch the second source operand: register, memory
1229 * or immediate.
1230 */
1231 switch (c->d & Src2Mask) {
1232 case Src2None:
1233 break;
1234 case Src2CL:
1235 c->src2.bytes = 1;
1236 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1237 break;
1238 case Src2ImmByte:
1239 c->src2.type = OP_IMM;
1240 c->src2.ptr = (unsigned long *)c->eip;
1241 c->src2.bytes = 1;
1242 c->src2.val = insn_fetch(u8, 1, c->eip);
1243 break;
1244 case Src2One:
1245 c->src2.bytes = 1;
1246 c->src2.val = 1;
1247 break;
1248 }
1249
Avi Kivity038e51d2007-01-22 20:40:40 -08001250 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001251 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001252 case ImplicitOps:
1253 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001254 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001255 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001256 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001257 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001258 break;
1259 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001260 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001261 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001262 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001263 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001264 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001265 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001266 break;
1267 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001268 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001269 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001270 if ((c->d & DstMask) == DstMem64)
1271 c->dst.bytes = 8;
1272 else
1273 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001274 c->dst.val = 0;
1275 if (c->d & BitOp) {
1276 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1277
1278 c->dst.ptr = (void *)c->dst.ptr +
1279 (c->src.val & mask) / 8;
1280 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001281 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001282 case DstAcc:
1283 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001284 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001285 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001286 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001287 case 1:
1288 c->dst.val = *(u8 *)c->dst.ptr;
1289 break;
1290 case 2:
1291 c->dst.val = *(u16 *)c->dst.ptr;
1292 break;
1293 case 4:
1294 c->dst.val = *(u32 *)c->dst.ptr;
1295 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001296 case 8:
1297 c->dst.val = *(u64 *)c->dst.ptr;
1298 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001299 }
1300 c->dst.orig_val = c->dst.val;
1301 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001302 case DstDI:
1303 c->dst.type = OP_MEM;
1304 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1305 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001306 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001307 c->regs[VCPU_REGS_RDI]);
1308 c->dst.val = 0;
1309 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001310 }
1311
1312done:
1313 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1314}
1315
Gleb Natapov9de41572010-04-28 19:15:22 +03001316static int read_emulated(struct x86_emulate_ctxt *ctxt,
1317 struct x86_emulate_ops *ops,
1318 unsigned long addr, void *dest, unsigned size)
1319{
1320 int rc;
1321 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001322 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001323
1324 while (size) {
1325 int n = min(size, 8u);
1326 size -= n;
1327 if (mc->pos < mc->end)
1328 goto read_cached;
1329
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001330 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1331 ctxt->vcpu);
1332 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001333 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001334 if (rc != X86EMUL_CONTINUE)
1335 return rc;
1336 mc->end += n;
1337
1338 read_cached:
1339 memcpy(dest, mc->data + mc->pos, n);
1340 mc->pos += n;
1341 dest += n;
1342 addr += n;
1343 }
1344 return X86EMUL_CONTINUE;
1345}
1346
Gleb Natapov7b262e92010-03-18 15:20:27 +02001347static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1348 struct x86_emulate_ops *ops,
1349 unsigned int size, unsigned short port,
1350 void *dest)
1351{
1352 struct read_cache *rc = &ctxt->decode.io_read;
1353
1354 if (rc->pos == rc->end) { /* refill pio read ahead */
1355 struct decode_cache *c = &ctxt->decode;
1356 unsigned int in_page, n;
1357 unsigned int count = c->rep_prefix ?
1358 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1359 in_page = (ctxt->eflags & EFLG_DF) ?
1360 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1361 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1362 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1363 count);
1364 if (n == 0)
1365 n = 1;
1366 rc->pos = rc->end = 0;
1367 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1368 return 0;
1369 rc->end = n * size;
1370 }
1371
1372 memcpy(dest, rc->data + rc->pos, size);
1373 rc->pos += size;
1374 return 1;
1375}
1376
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377static u32 desc_limit_scaled(struct desc_struct *desc)
1378{
1379 u32 limit = get_desc_limit(desc);
1380
1381 return desc->g ? (limit << 12) | 0xfff : limit;
1382}
1383
1384static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1385 struct x86_emulate_ops *ops,
1386 u16 selector, struct desc_ptr *dt)
1387{
1388 if (selector & 1 << 2) {
1389 struct desc_struct desc;
1390 memset (dt, 0, sizeof *dt);
1391 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1392 return;
1393
1394 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1395 dt->address = get_desc_base(&desc);
1396 } else
1397 ops->get_gdt(dt, ctxt->vcpu);
1398}
1399
1400/* allowed just for 8 bytes segments */
1401static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1402 struct x86_emulate_ops *ops,
1403 u16 selector, struct desc_struct *desc)
1404{
1405 struct desc_ptr dt;
1406 u16 index = selector >> 3;
1407 int ret;
1408 u32 err;
1409 ulong addr;
1410
1411 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1412
1413 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001414 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415 return X86EMUL_PROPAGATE_FAULT;
1416 }
1417 addr = dt.address + index * 8;
1418 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1419 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001420 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001421
1422 return ret;
1423}
1424
1425/* allowed just for 8 bytes segments */
1426static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1427 struct x86_emulate_ops *ops,
1428 u16 selector, struct desc_struct *desc)
1429{
1430 struct desc_ptr dt;
1431 u16 index = selector >> 3;
1432 u32 err;
1433 ulong addr;
1434 int ret;
1435
1436 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1437
1438 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001439 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 return X86EMUL_PROPAGATE_FAULT;
1441 }
1442
1443 addr = dt.address + index * 8;
1444 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1445 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001446 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001447
1448 return ret;
1449}
1450
1451static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1452 struct x86_emulate_ops *ops,
1453 u16 selector, int seg)
1454{
1455 struct desc_struct seg_desc;
1456 u8 dpl, rpl, cpl;
1457 unsigned err_vec = GP_VECTOR;
1458 u32 err_code = 0;
1459 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1460 int ret;
1461
1462 memset(&seg_desc, 0, sizeof seg_desc);
1463
1464 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1465 || ctxt->mode == X86EMUL_MODE_REAL) {
1466 /* set real mode segment descriptor */
1467 set_desc_base(&seg_desc, selector << 4);
1468 set_desc_limit(&seg_desc, 0xffff);
1469 seg_desc.type = 3;
1470 seg_desc.p = 1;
1471 seg_desc.s = 1;
1472 goto load;
1473 }
1474
1475 /* NULL selector is not valid for TR, CS and SS */
1476 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1477 && null_selector)
1478 goto exception;
1479
1480 /* TR should be in GDT only */
1481 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1482 goto exception;
1483
1484 if (null_selector) /* for NULL selector skip all following checks */
1485 goto load;
1486
1487 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1488 if (ret != X86EMUL_CONTINUE)
1489 return ret;
1490
1491 err_code = selector & 0xfffc;
1492 err_vec = GP_VECTOR;
1493
1494 /* can't load system descriptor into segment selecor */
1495 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1496 goto exception;
1497
1498 if (!seg_desc.p) {
1499 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1500 goto exception;
1501 }
1502
1503 rpl = selector & 3;
1504 dpl = seg_desc.dpl;
1505 cpl = ops->cpl(ctxt->vcpu);
1506
1507 switch (seg) {
1508 case VCPU_SREG_SS:
1509 /*
1510 * segment is not a writable data segment or segment
1511 * selector's RPL != CPL or segment selector's RPL != CPL
1512 */
1513 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1514 goto exception;
1515 break;
1516 case VCPU_SREG_CS:
1517 if (!(seg_desc.type & 8))
1518 goto exception;
1519
1520 if (seg_desc.type & 4) {
1521 /* conforming */
1522 if (dpl > cpl)
1523 goto exception;
1524 } else {
1525 /* nonconforming */
1526 if (rpl > cpl || dpl != cpl)
1527 goto exception;
1528 }
1529 /* CS(RPL) <- CPL */
1530 selector = (selector & 0xfffc) | cpl;
1531 break;
1532 case VCPU_SREG_TR:
1533 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1534 goto exception;
1535 break;
1536 case VCPU_SREG_LDTR:
1537 if (seg_desc.s || seg_desc.type != 2)
1538 goto exception;
1539 break;
1540 default: /* DS, ES, FS, or GS */
1541 /*
1542 * segment is not a data or readable code segment or
1543 * ((segment is a data or nonconforming code segment)
1544 * and (both RPL and CPL > DPL))
1545 */
1546 if ((seg_desc.type & 0xa) == 0x8 ||
1547 (((seg_desc.type & 0xc) != 0xc) &&
1548 (rpl > dpl && cpl > dpl)))
1549 goto exception;
1550 break;
1551 }
1552
1553 if (seg_desc.s) {
1554 /* mark segment as accessed */
1555 seg_desc.type |= 1;
1556 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1557 if (ret != X86EMUL_CONTINUE)
1558 return ret;
1559 }
1560load:
1561 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1562 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1563 return X86EMUL_CONTINUE;
1564exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001565 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001566 return X86EMUL_PROPAGATE_FAULT;
1567}
1568
Wei Yongjunc37eda12010-06-15 09:03:33 +08001569static inline int writeback(struct x86_emulate_ctxt *ctxt,
1570 struct x86_emulate_ops *ops)
1571{
1572 int rc;
1573 struct decode_cache *c = &ctxt->decode;
1574 u32 err;
1575
1576 switch (c->dst.type) {
1577 case OP_REG:
1578 /* The 4-byte case *is* correct:
1579 * in 64-bit mode we zero-extend.
1580 */
1581 switch (c->dst.bytes) {
1582 case 1:
1583 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1584 break;
1585 case 2:
1586 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1587 break;
1588 case 4:
1589 *c->dst.ptr = (u32)c->dst.val;
1590 break; /* 64b: zero-ext */
1591 case 8:
1592 *c->dst.ptr = c->dst.val;
1593 break;
1594 }
1595 break;
1596 case OP_MEM:
1597 if (c->lock_prefix)
1598 rc = ops->cmpxchg_emulated(
1599 (unsigned long)c->dst.ptr,
1600 &c->dst.orig_val,
1601 &c->dst.val,
1602 c->dst.bytes,
1603 &err,
1604 ctxt->vcpu);
1605 else
1606 rc = ops->write_emulated(
1607 (unsigned long)c->dst.ptr,
1608 &c->dst.val,
1609 c->dst.bytes,
1610 &err,
1611 ctxt->vcpu);
1612 if (rc == X86EMUL_PROPAGATE_FAULT)
1613 emulate_pf(ctxt,
1614 (unsigned long)c->dst.ptr, err);
1615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617 break;
1618 case OP_NONE:
1619 /* no writeback */
1620 break;
1621 default:
1622 break;
1623 }
1624 return X86EMUL_CONTINUE;
1625}
1626
Gleb Natapov79168fd2010-04-28 19:15:30 +03001627static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1628 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001629{
1630 struct decode_cache *c = &ctxt->decode;
1631
1632 c->dst.type = OP_MEM;
1633 c->dst.bytes = c->op_bytes;
1634 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001635 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001636 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637 c->regs[VCPU_REGS_RSP]);
1638}
1639
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001640static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001641 struct x86_emulate_ops *ops,
1642 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001643{
1644 struct decode_cache *c = &ctxt->decode;
1645 int rc;
1646
Gleb Natapov79168fd2010-04-28 19:15:30 +03001647 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001648 c->regs[VCPU_REGS_RSP]),
1649 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001650 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001651 return rc;
1652
Avi Kivity350f69d2009-01-05 11:12:40 +02001653 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001654 return rc;
1655}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001657static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1658 struct x86_emulate_ops *ops,
1659 void *dest, int len)
1660{
1661 int rc;
1662 unsigned long val, change_mask;
1663 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001664 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001665
1666 rc = emulate_pop(ctxt, ops, &val, len);
1667 if (rc != X86EMUL_CONTINUE)
1668 return rc;
1669
1670 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1671 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1672
1673 switch(ctxt->mode) {
1674 case X86EMUL_MODE_PROT64:
1675 case X86EMUL_MODE_PROT32:
1676 case X86EMUL_MODE_PROT16:
1677 if (cpl == 0)
1678 change_mask |= EFLG_IOPL;
1679 if (cpl <= iopl)
1680 change_mask |= EFLG_IF;
1681 break;
1682 case X86EMUL_MODE_VM86:
1683 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001684 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001685 return X86EMUL_PROPAGATE_FAULT;
1686 }
1687 change_mask |= EFLG_IF;
1688 break;
1689 default: /* real mode */
1690 change_mask |= (EFLG_IOPL | EFLG_IF);
1691 break;
1692 }
1693
1694 *(unsigned long *)dest =
1695 (ctxt->eflags & ~change_mask) | (val & change_mask);
1696
1697 return rc;
1698}
1699
Gleb Natapov79168fd2010-04-28 19:15:30 +03001700static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1701 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001702{
1703 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001704
Gleb Natapov79168fd2010-04-28 19:15:30 +03001705 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001706
Gleb Natapov79168fd2010-04-28 19:15:30 +03001707 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001708}
1709
1710static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1711 struct x86_emulate_ops *ops, int seg)
1712{
1713 struct decode_cache *c = &ctxt->decode;
1714 unsigned long selector;
1715 int rc;
1716
1717 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001718 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001719 return rc;
1720
Gleb Natapov2e873022010-03-18 15:20:18 +02001721 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001722 return rc;
1723}
1724
Wei Yongjunc37eda12010-06-15 09:03:33 +08001725static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001726 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001727{
1728 struct decode_cache *c = &ctxt->decode;
1729 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001730 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001731 int reg = VCPU_REGS_RAX;
1732
1733 while (reg <= VCPU_REGS_RDI) {
1734 (reg == VCPU_REGS_RSP) ?
1735 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1736
Gleb Natapov79168fd2010-04-28 19:15:30 +03001737 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001738
1739 rc = writeback(ctxt, ops);
1740 if (rc != X86EMUL_CONTINUE)
1741 return rc;
1742
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001743 ++reg;
1744 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001745
1746 /* Disable writeback. */
1747 c->dst.type = OP_NONE;
1748
1749 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001750}
1751
1752static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1753 struct x86_emulate_ops *ops)
1754{
1755 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001756 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001757 int reg = VCPU_REGS_RDI;
1758
1759 while (reg >= VCPU_REGS_RAX) {
1760 if (reg == VCPU_REGS_RSP) {
1761 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1762 c->op_bytes);
1763 --reg;
1764 }
1765
1766 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001767 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001768 break;
1769 --reg;
1770 }
1771 return rc;
1772}
1773
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001774static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1775 struct x86_emulate_ops *ops)
1776{
1777 struct decode_cache *c = &ctxt->decode;
1778 int rc = X86EMUL_CONTINUE;
1779 unsigned long temp_eip = 0;
1780 unsigned long temp_eflags = 0;
1781 unsigned long cs = 0;
1782 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1783 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1784 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1785 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1786
1787 /* TODO: Add stack limit check */
1788
1789 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1790
1791 if (rc != X86EMUL_CONTINUE)
1792 return rc;
1793
1794 if (temp_eip & ~0xffff) {
1795 emulate_gp(ctxt, 0);
1796 return X86EMUL_PROPAGATE_FAULT;
1797 }
1798
1799 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1800
1801 if (rc != X86EMUL_CONTINUE)
1802 return rc;
1803
1804 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1805
1806 if (rc != X86EMUL_CONTINUE)
1807 return rc;
1808
1809 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1810
1811 if (rc != X86EMUL_CONTINUE)
1812 return rc;
1813
1814 c->eip = temp_eip;
1815
1816
1817 if (c->op_bytes == 4)
1818 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1819 else if (c->op_bytes == 2) {
1820 ctxt->eflags &= ~0xffff;
1821 ctxt->eflags |= temp_eflags;
1822 }
1823
1824 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1825 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1826
1827 return rc;
1828}
1829
1830static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1831 struct x86_emulate_ops* ops)
1832{
1833 switch(ctxt->mode) {
1834 case X86EMUL_MODE_REAL:
1835 return emulate_iret_real(ctxt, ops);
1836 case X86EMUL_MODE_VM86:
1837 case X86EMUL_MODE_PROT16:
1838 case X86EMUL_MODE_PROT32:
1839 case X86EMUL_MODE_PROT64:
1840 default:
1841 /* iret from protected mode unimplemented yet */
1842 return X86EMUL_UNHANDLEABLE;
1843 }
1844}
1845
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001846static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1847 struct x86_emulate_ops *ops)
1848{
1849 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001850
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001851 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001852}
1853
Laurent Vivier05f086f2007-09-24 11:10:55 +02001854static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001855{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001856 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001857 switch (c->modrm_reg) {
1858 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001859 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001860 break;
1861 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001862 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001863 break;
1864 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001865 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 break;
1867 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001868 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001869 break;
1870 case 4: /* sal/shl */
1871 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001872 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 break;
1874 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001875 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001876 break;
1877 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001878 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001879 break;
1880 }
1881}
1882
1883static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001884 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001885{
1886 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001887
1888 switch (c->modrm_reg) {
1889 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001890 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891 break;
1892 case 2: /* not */
1893 c->dst.val = ~c->dst.val;
1894 break;
1895 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001896 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897 break;
1898 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001899 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001901 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001902}
1903
1904static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001905 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906{
1907 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001908
1909 switch (c->modrm_reg) {
1910 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001911 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001912 break;
1913 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001914 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001915 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001916 case 2: /* call near abs */ {
1917 long int old_eip;
1918 old_eip = c->eip;
1919 c->eip = c->src.val;
1920 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001921 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001922 break;
1923 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001924 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001925 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001926 break;
1927 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001928 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001929 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001931 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001932}
1933
1934static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001935 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001936{
1937 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001938 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001939
1940 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1941 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001942 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1943 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001944 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001945 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001946 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1947 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001948
Laurent Vivier05f086f2007-09-24 11:10:55 +02001949 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001950 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001951 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001952}
1953
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001954static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1955 struct x86_emulate_ops *ops)
1956{
1957 struct decode_cache *c = &ctxt->decode;
1958 int rc;
1959 unsigned long cs;
1960
1961 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001962 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001963 return rc;
1964 if (c->op_bytes == 4)
1965 c->eip = (u32)c->eip;
1966 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001967 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001968 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001969 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001970 return rc;
1971}
1972
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001973static inline void
1974setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001975 struct x86_emulate_ops *ops, struct desc_struct *cs,
1976 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001977{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001978 memset(cs, 0, sizeof(struct desc_struct));
1979 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1980 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001981
1982 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001983 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001984 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001986 cs->type = 0x0b; /* Read, Execute, Accessed */
1987 cs->s = 1;
1988 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001989 cs->p = 1;
1990 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991
Gleb Natapov79168fd2010-04-28 19:15:30 +03001992 set_desc_base(ss, 0); /* flat segment */
1993 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001994 ss->g = 1; /* 4kb granularity */
1995 ss->s = 1;
1996 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001997 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001998 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002000}
2001
2002static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002003emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002004{
2005 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002006 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002007 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002009
2010 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002011 if (ctxt->mode == X86EMUL_MODE_REAL ||
2012 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002013 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002014 return X86EMUL_PROPAGATE_FAULT;
2015 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002016
Gleb Natapov79168fd2010-04-28 19:15:30 +03002017 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002018
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002019 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002020 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002021 cs_sel = (u16)(msr_data & 0xfffc);
2022 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002023
2024 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002025 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002026 cs.l = 1;
2027 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002028 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2029 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2030 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2031 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002032
2033 c->regs[VCPU_REGS_RCX] = c->eip;
2034 if (is_long_mode(ctxt->vcpu)) {
2035#ifdef CONFIG_X86_64
2036 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
2037
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002038 ops->get_msr(ctxt->vcpu,
2039 ctxt->mode == X86EMUL_MODE_PROT64 ?
2040 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002041 c->eip = msr_data;
2042
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002043 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002044 ctxt->eflags &= ~(msr_data | EFLG_RF);
2045#endif
2046 } else {
2047 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002048 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002049 c->eip = (u32)msr_data;
2050
2051 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2052 }
2053
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002054 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002055}
2056
Andre Przywara8c604352009-06-18 12:56:01 +02002057static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002058emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002059{
2060 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002061 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002062 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002063 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002064
Gleb Natapova0044752010-02-10 14:21:31 +02002065 /* inject #GP if in real mode */
2066 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002067 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002068 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002069 }
2070
2071 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2072 * Therefore, we inject an #UD.
2073 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002074 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002075 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002076 return X86EMUL_PROPAGATE_FAULT;
2077 }
Andre Przywara8c604352009-06-18 12:56:01 +02002078
Gleb Natapov79168fd2010-04-28 19:15:30 +03002079 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002080
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002081 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002082 switch (ctxt->mode) {
2083 case X86EMUL_MODE_PROT32:
2084 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002085 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002086 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002087 }
2088 break;
2089 case X86EMUL_MODE_PROT64:
2090 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002091 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002092 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002093 }
2094 break;
2095 }
2096
2097 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002098 cs_sel = (u16)msr_data;
2099 cs_sel &= ~SELECTOR_RPL_MASK;
2100 ss_sel = cs_sel + 8;
2101 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002102 if (ctxt->mode == X86EMUL_MODE_PROT64
2103 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002104 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002105 cs.l = 1;
2106 }
2107
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2109 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2110 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2111 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002112
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002113 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002114 c->eip = msr_data;
2115
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002116 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002117 c->regs[VCPU_REGS_RSP] = msr_data;
2118
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002119 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002120}
2121
Andre Przywara4668f052009-06-18 12:56:02 +02002122static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002123emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002124{
2125 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002126 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002127 u64 msr_data;
2128 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002129 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002130
Gleb Natapova0044752010-02-10 14:21:31 +02002131 /* inject #GP if in real mode or Virtual 8086 mode */
2132 if (ctxt->mode == X86EMUL_MODE_REAL ||
2133 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002134 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002135 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002136 }
2137
Gleb Natapov79168fd2010-04-28 19:15:30 +03002138 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002139
2140 if ((c->rex_prefix & 0x8) != 0x0)
2141 usermode = X86EMUL_MODE_PROT64;
2142 else
2143 usermode = X86EMUL_MODE_PROT32;
2144
2145 cs.dpl = 3;
2146 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002147 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002148 switch (usermode) {
2149 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002150 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002151 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002152 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002153 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002154 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002155 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002156 break;
2157 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002158 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002159 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002160 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002161 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002162 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002163 ss_sel = cs_sel + 8;
2164 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002165 cs.l = 1;
2166 break;
2167 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002168 cs_sel |= SELECTOR_RPL_MASK;
2169 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002170
Gleb Natapov79168fd2010-04-28 19:15:30 +03002171 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2172 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2173 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2174 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002175
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002176 c->eip = c->regs[VCPU_REGS_RDX];
2177 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002178
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002179 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002180}
2181
Gleb Natapov9c537242010-03-18 15:20:05 +02002182static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2183 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002184{
2185 int iopl;
2186 if (ctxt->mode == X86EMUL_MODE_REAL)
2187 return false;
2188 if (ctxt->mode == X86EMUL_MODE_VM86)
2189 return true;
2190 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002191 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002192}
2193
2194static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2195 struct x86_emulate_ops *ops,
2196 u16 port, u16 len)
2197{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002199 int r;
2200 u16 io_bitmap_ptr;
2201 u8 perm, bit_idx = port & 0x7;
2202 unsigned mask = (1 << len) - 1;
2203
Gleb Natapov79168fd2010-04-28 19:15:30 +03002204 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2205 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002206 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002207 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002208 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002209 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2210 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002211 if (r != X86EMUL_CONTINUE)
2212 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002214 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002215 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2216 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002217 if (r != X86EMUL_CONTINUE)
2218 return false;
2219 if ((perm >> bit_idx) & mask)
2220 return false;
2221 return true;
2222}
2223
2224static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2225 struct x86_emulate_ops *ops,
2226 u16 port, u16 len)
2227{
Gleb Natapov9c537242010-03-18 15:20:05 +02002228 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002229 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2230 return false;
2231 return true;
2232}
2233
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002234static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2235 struct x86_emulate_ops *ops,
2236 struct tss_segment_16 *tss)
2237{
2238 struct decode_cache *c = &ctxt->decode;
2239
2240 tss->ip = c->eip;
2241 tss->flag = ctxt->eflags;
2242 tss->ax = c->regs[VCPU_REGS_RAX];
2243 tss->cx = c->regs[VCPU_REGS_RCX];
2244 tss->dx = c->regs[VCPU_REGS_RDX];
2245 tss->bx = c->regs[VCPU_REGS_RBX];
2246 tss->sp = c->regs[VCPU_REGS_RSP];
2247 tss->bp = c->regs[VCPU_REGS_RBP];
2248 tss->si = c->regs[VCPU_REGS_RSI];
2249 tss->di = c->regs[VCPU_REGS_RDI];
2250
2251 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2252 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2253 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2254 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2255 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2256}
2257
2258static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2259 struct x86_emulate_ops *ops,
2260 struct tss_segment_16 *tss)
2261{
2262 struct decode_cache *c = &ctxt->decode;
2263 int ret;
2264
2265 c->eip = tss->ip;
2266 ctxt->eflags = tss->flag | 2;
2267 c->regs[VCPU_REGS_RAX] = tss->ax;
2268 c->regs[VCPU_REGS_RCX] = tss->cx;
2269 c->regs[VCPU_REGS_RDX] = tss->dx;
2270 c->regs[VCPU_REGS_RBX] = tss->bx;
2271 c->regs[VCPU_REGS_RSP] = tss->sp;
2272 c->regs[VCPU_REGS_RBP] = tss->bp;
2273 c->regs[VCPU_REGS_RSI] = tss->si;
2274 c->regs[VCPU_REGS_RDI] = tss->di;
2275
2276 /*
2277 * SDM says that segment selectors are loaded before segment
2278 * descriptors
2279 */
2280 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2281 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2282 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2283 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2284 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2285
2286 /*
2287 * Now load segment descriptors. If fault happenes at this stage
2288 * it is handled in a context of new task
2289 */
2290 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2291 if (ret != X86EMUL_CONTINUE)
2292 return ret;
2293 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2294 if (ret != X86EMUL_CONTINUE)
2295 return ret;
2296 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2297 if (ret != X86EMUL_CONTINUE)
2298 return ret;
2299 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2300 if (ret != X86EMUL_CONTINUE)
2301 return ret;
2302 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2303 if (ret != X86EMUL_CONTINUE)
2304 return ret;
2305
2306 return X86EMUL_CONTINUE;
2307}
2308
2309static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2310 struct x86_emulate_ops *ops,
2311 u16 tss_selector, u16 old_tss_sel,
2312 ulong old_tss_base, struct desc_struct *new_desc)
2313{
2314 struct tss_segment_16 tss_seg;
2315 int ret;
2316 u32 err, new_tss_base = get_desc_base(new_desc);
2317
2318 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2319 &err);
2320 if (ret == X86EMUL_PROPAGATE_FAULT) {
2321 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002322 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323 return ret;
2324 }
2325
2326 save_state_to_tss16(ctxt, ops, &tss_seg);
2327
2328 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2329 &err);
2330 if (ret == X86EMUL_PROPAGATE_FAULT) {
2331 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002332 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 return ret;
2334 }
2335
2336 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2337 &err);
2338 if (ret == X86EMUL_PROPAGATE_FAULT) {
2339 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002340 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002341 return ret;
2342 }
2343
2344 if (old_tss_sel != 0xffff) {
2345 tss_seg.prev_task_link = old_tss_sel;
2346
2347 ret = ops->write_std(new_tss_base,
2348 &tss_seg.prev_task_link,
2349 sizeof tss_seg.prev_task_link,
2350 ctxt->vcpu, &err);
2351 if (ret == X86EMUL_PROPAGATE_FAULT) {
2352 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002353 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 return ret;
2355 }
2356 }
2357
2358 return load_state_from_tss16(ctxt, ops, &tss_seg);
2359}
2360
2361static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2362 struct x86_emulate_ops *ops,
2363 struct tss_segment_32 *tss)
2364{
2365 struct decode_cache *c = &ctxt->decode;
2366
2367 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2368 tss->eip = c->eip;
2369 tss->eflags = ctxt->eflags;
2370 tss->eax = c->regs[VCPU_REGS_RAX];
2371 tss->ecx = c->regs[VCPU_REGS_RCX];
2372 tss->edx = c->regs[VCPU_REGS_RDX];
2373 tss->ebx = c->regs[VCPU_REGS_RBX];
2374 tss->esp = c->regs[VCPU_REGS_RSP];
2375 tss->ebp = c->regs[VCPU_REGS_RBP];
2376 tss->esi = c->regs[VCPU_REGS_RSI];
2377 tss->edi = c->regs[VCPU_REGS_RDI];
2378
2379 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2380 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2381 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2382 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2383 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2384 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2385 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2386}
2387
2388static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2389 struct x86_emulate_ops *ops,
2390 struct tss_segment_32 *tss)
2391{
2392 struct decode_cache *c = &ctxt->decode;
2393 int ret;
2394
Gleb Natapov0f122442010-04-28 19:15:31 +03002395 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002396 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002397 return X86EMUL_PROPAGATE_FAULT;
2398 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 c->eip = tss->eip;
2400 ctxt->eflags = tss->eflags | 2;
2401 c->regs[VCPU_REGS_RAX] = tss->eax;
2402 c->regs[VCPU_REGS_RCX] = tss->ecx;
2403 c->regs[VCPU_REGS_RDX] = tss->edx;
2404 c->regs[VCPU_REGS_RBX] = tss->ebx;
2405 c->regs[VCPU_REGS_RSP] = tss->esp;
2406 c->regs[VCPU_REGS_RBP] = tss->ebp;
2407 c->regs[VCPU_REGS_RSI] = tss->esi;
2408 c->regs[VCPU_REGS_RDI] = tss->edi;
2409
2410 /*
2411 * SDM says that segment selectors are loaded before segment
2412 * descriptors
2413 */
2414 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2415 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2416 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2417 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2418 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2419 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2420 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2421
2422 /*
2423 * Now load segment descriptors. If fault happenes at this stage
2424 * it is handled in a context of new task
2425 */
2426 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2427 if (ret != X86EMUL_CONTINUE)
2428 return ret;
2429 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2430 if (ret != X86EMUL_CONTINUE)
2431 return ret;
2432 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2433 if (ret != X86EMUL_CONTINUE)
2434 return ret;
2435 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2436 if (ret != X86EMUL_CONTINUE)
2437 return ret;
2438 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2439 if (ret != X86EMUL_CONTINUE)
2440 return ret;
2441 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2442 if (ret != X86EMUL_CONTINUE)
2443 return ret;
2444 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2445 if (ret != X86EMUL_CONTINUE)
2446 return ret;
2447
2448 return X86EMUL_CONTINUE;
2449}
2450
2451static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2452 struct x86_emulate_ops *ops,
2453 u16 tss_selector, u16 old_tss_sel,
2454 ulong old_tss_base, struct desc_struct *new_desc)
2455{
2456 struct tss_segment_32 tss_seg;
2457 int ret;
2458 u32 err, new_tss_base = get_desc_base(new_desc);
2459
2460 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2461 &err);
2462 if (ret == X86EMUL_PROPAGATE_FAULT) {
2463 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002464 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002465 return ret;
2466 }
2467
2468 save_state_to_tss32(ctxt, ops, &tss_seg);
2469
2470 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2471 &err);
2472 if (ret == X86EMUL_PROPAGATE_FAULT) {
2473 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002474 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475 return ret;
2476 }
2477
2478 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2479 &err);
2480 if (ret == X86EMUL_PROPAGATE_FAULT) {
2481 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002482 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 return ret;
2484 }
2485
2486 if (old_tss_sel != 0xffff) {
2487 tss_seg.prev_task_link = old_tss_sel;
2488
2489 ret = ops->write_std(new_tss_base,
2490 &tss_seg.prev_task_link,
2491 sizeof tss_seg.prev_task_link,
2492 ctxt->vcpu, &err);
2493 if (ret == X86EMUL_PROPAGATE_FAULT) {
2494 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002495 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 return ret;
2497 }
2498 }
2499
2500 return load_state_from_tss32(ctxt, ops, &tss_seg);
2501}
2502
2503static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002504 struct x86_emulate_ops *ops,
2505 u16 tss_selector, int reason,
2506 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507{
2508 struct desc_struct curr_tss_desc, next_tss_desc;
2509 int ret;
2510 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2511 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002512 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002513 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514
2515 /* FIXME: old_tss_base == ~0 ? */
2516
2517 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2518 if (ret != X86EMUL_CONTINUE)
2519 return ret;
2520 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2521 if (ret != X86EMUL_CONTINUE)
2522 return ret;
2523
2524 /* FIXME: check that next_tss_desc is tss */
2525
2526 if (reason != TASK_SWITCH_IRET) {
2527 if ((tss_selector & 3) > next_tss_desc.dpl ||
2528 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002529 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 return X86EMUL_PROPAGATE_FAULT;
2531 }
2532 }
2533
Gleb Natapovceffb452010-03-18 15:20:19 +02002534 desc_limit = desc_limit_scaled(&next_tss_desc);
2535 if (!next_tss_desc.p ||
2536 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2537 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002538 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539 return X86EMUL_PROPAGATE_FAULT;
2540 }
2541
2542 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2543 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2544 write_segment_descriptor(ctxt, ops, old_tss_sel,
2545 &curr_tss_desc);
2546 }
2547
2548 if (reason == TASK_SWITCH_IRET)
2549 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2550
2551 /* set back link to prev task only if NT bit is set in eflags
2552 note that old_tss_sel is not used afetr this point */
2553 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2554 old_tss_sel = 0xffff;
2555
2556 if (next_tss_desc.type & 8)
2557 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2558 old_tss_base, &next_tss_desc);
2559 else
2560 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2561 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002562 if (ret != X86EMUL_CONTINUE)
2563 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564
2565 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2566 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2567
2568 if (reason != TASK_SWITCH_IRET) {
2569 next_tss_desc.type |= (1 << 1); /* set busy flag */
2570 write_segment_descriptor(ctxt, ops, tss_selector,
2571 &next_tss_desc);
2572 }
2573
2574 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2575 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2576 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2577
Jan Kiszkae269fb22010-04-14 15:51:09 +02002578 if (has_error_code) {
2579 struct decode_cache *c = &ctxt->decode;
2580
2581 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2582 c->lock_prefix = 0;
2583 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002584 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002585 }
2586
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 return ret;
2588}
2589
2590int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2591 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002592 u16 tss_selector, int reason,
2593 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594{
2595 struct decode_cache *c = &ctxt->decode;
2596 int rc;
2597
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002599 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600
Jan Kiszkae269fb22010-04-14 15:51:09 +02002601 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2602 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
2604 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002605 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002606 if (rc == X86EMUL_CONTINUE)
2607 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 }
2609
Gleb Natapov19d04432010-04-15 12:29:50 +03002610 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611}
2612
Gleb Natapova682e352010-03-18 15:20:21 +02002613static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002614 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002615{
2616 struct decode_cache *c = &ctxt->decode;
2617 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2618
Gleb Natapovd9271122010-03-18 15:20:22 +02002619 register_address_increment(c, &c->regs[reg], df * op->bytes);
2620 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002621}
2622
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002623int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002624x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002625{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002626 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002627 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002628 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002629 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002630
Gleb Natapov9de41572010-04-28 19:15:22 +03002631 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002632
Gleb Natapov11616242010-02-11 14:43:14 +02002633 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002634 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002635 goto done;
2636 }
2637
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002638 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002639 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002640 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002641 goto done;
2642 }
2643
Gleb Natapove92805a2010-02-10 14:21:35 +02002644 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002645 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002646 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002647 goto done;
2648 }
2649
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002650 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002651 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002652 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002653 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002654 string_done:
2655 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002656 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002657 goto done;
2658 }
2659 /* The second termination condition only applies for REPE
2660 * and REPNE. Test if the repeat string operation prefix is
2661 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2662 * corresponding termination condition according to:
2663 * - if REPE/REPZ and ZF = 0 then done
2664 * - if REPNE/REPNZ and ZF = 1 then done
2665 */
2666 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002667 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002668 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002669 ((ctxt->eflags & EFLG_ZF) == 0))
2670 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002671 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002672 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2673 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002674 }
Gleb Natapov063db062010-03-18 15:20:06 +02002675 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002676 }
2677
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002678 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002679 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002680 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002681 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002682 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002683 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002684 }
2685
Gleb Natapove35b7b92010-02-25 16:36:42 +02002686 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002687 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2688 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002689 if (rc != X86EMUL_CONTINUE)
2690 goto done;
2691 }
2692
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002693 if ((c->d & DstMask) == ImplicitOps)
2694 goto special_insn;
2695
2696
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002697 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2698 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002699 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2700 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002701 if (rc != X86EMUL_CONTINUE)
2702 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002703 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002704 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002705
Avi Kivity018a98d2007-11-27 19:30:56 +02002706special_insn:
2707
Laurent Viviere4e03de2007-09-18 11:52:50 +02002708 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 goto twobyte_insn;
2710
Laurent Viviere4e03de2007-09-18 11:52:50 +02002711 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 case 0x00 ... 0x05:
2713 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002714 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002716 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002717 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002718 break;
2719 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002720 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002721 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002722 goto done;
2723 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 case 0x08 ... 0x0d:
2725 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002726 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002728 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002729 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002730 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 case 0x10 ... 0x15:
2732 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002733 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002735 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002736 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002737 break;
2738 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002739 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002740 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 goto done;
2742 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 case 0x18 ... 0x1d:
2744 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002745 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002747 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002748 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002749 break;
2750 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002751 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002752 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002753 goto done;
2754 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002755 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002757 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
2759 case 0x28 ... 0x2d:
2760 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002761 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 break;
2763 case 0x30 ... 0x35:
2764 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002765 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
2767 case 0x38 ... 0x3d:
2768 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002769 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002771 case 0x40 ... 0x47: /* inc r16/r32 */
2772 emulate_1op("inc", c->dst, ctxt->eflags);
2773 break;
2774 case 0x48 ... 0x4f: /* dec r16/r32 */
2775 emulate_1op("dec", c->dst, ctxt->eflags);
2776 break;
2777 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002778 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002779 break;
2780 case 0x58 ... 0x5f: /* pop reg */
2781 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002782 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002783 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002784 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002785 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002786 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002787 rc = emulate_pusha(ctxt, ops);
2788 if (rc != X86EMUL_CONTINUE)
2789 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002790 break;
2791 case 0x61: /* popa */
2792 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002793 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002794 goto done;
2795 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002797 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002799 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002801 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002802 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002803 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002804 break;
2805 case 0x6c: /* insb */
2806 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002807 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002808 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002809 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002810 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002811 goto done;
2812 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002813 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2814 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002815 goto done; /* IO is needed, skip writeback */
2816 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002817 case 0x6e: /* outsb */
2818 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002819 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002820 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002821 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002822 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002823 goto done;
2824 }
Gleb Natapov79729952010-03-18 15:20:24 +02002825 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2826 &c->src.val, 1, ctxt->vcpu);
2827
2828 c->dst.type = OP_NONE; /* nothing to writeback */
2829 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002830 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002831 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002832 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002833 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002835 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 case 0:
2837 goto add;
2838 case 1:
2839 goto or;
2840 case 2:
2841 goto adc;
2842 case 3:
2843 goto sbb;
2844 case 4:
2845 goto and;
2846 case 5:
2847 goto sub;
2848 case 6:
2849 goto xor;
2850 case 7:
2851 goto cmp;
2852 }
2853 break;
2854 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002855 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002856 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 break;
2858 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002859 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002861 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002863 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 break;
2865 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002866 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 break;
2868 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002869 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 break; /* 64b reg: zero-extend */
2871 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002872 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 break;
2874 }
2875 /*
2876 * Write back the memory destination with implicit LOCK
2877 * prefix.
2878 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002879 c->dst.val = c->src.val;
2880 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002883 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002884 case 0x8c: /* mov r/m, sreg */
2885 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002886 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002887 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002888 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002890 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002891 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002892 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002893 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002894 case 0x8e: { /* mov seg, r/m16 */
2895 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002896
2897 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002898
Gleb Natapovc6975182010-02-18 12:15:01 +02002899 if (c->modrm_reg == VCPU_SREG_CS ||
2900 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002901 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002902 goto done;
2903 }
2904
Glauber Costa310b5d32009-05-12 16:21:06 -04002905 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002906 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002907
Gleb Natapov2e873022010-03-18 15:20:18 +02002908 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002909
2910 c->dst.type = OP_NONE; /* Disable writeback. */
2911 break;
2912 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002914 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002915 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002918 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002919 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2920 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002921 break;
2922 }
2923 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002924 c->src.type = OP_REG;
2925 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002926 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2927 c->src.val = *(c->src.ptr);
2928 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002929 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002930 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002931 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002932 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002933 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002934 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002935 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002936 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002937 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2938 if (rc != X86EMUL_CONTINUE)
2939 goto done;
2940 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002941 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002943 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002945 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002946 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002947 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002948 case 0xa8 ... 0xa9: /* test ax, imm */
2949 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002951 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 break;
2953 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002954 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955 case 0xae ... 0xaf: /* scas */
2956 DPRINTF("Urk! I don't handle SCAS.\n");
2957 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002958 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002959 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002960 case 0xc0 ... 0xc1:
2961 emulate_grp2(ctxt);
2962 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002963 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002964 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002965 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002966 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002967 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002968 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2969 mov:
2970 c->dst.val = c->src.val;
2971 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002972 case 0xcb: /* ret far */
2973 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002974 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002975 goto done;
2976 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002977 case 0xcf: /* iret */
2978 rc = emulate_iret(ctxt, ops);
2979
2980 if (rc != X86EMUL_CONTINUE)
2981 goto done;
2982 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002983 case 0xd0 ... 0xd1: /* Grp2 */
2984 c->src.val = 1;
2985 emulate_grp2(ctxt);
2986 break;
2987 case 0xd2 ... 0xd3: /* Grp2 */
2988 c->src.val = c->regs[VCPU_REGS_RCX];
2989 emulate_grp2(ctxt);
2990 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002991 case 0xe4: /* inb */
2992 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002993 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002994 case 0xe6: /* outb */
2995 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002996 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002997 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002998 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002999 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003000 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003001 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003002 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003003 }
3004 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003005 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003006 case 0xea: { /* jmp far */
3007 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003008 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003009 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3010
3011 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003012 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003013
Gleb Natapov414e6272010-04-28 19:15:26 +03003014 c->eip = 0;
3015 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003016 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003017 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003018 case 0xeb:
3019 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003020 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003021 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003022 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003023 case 0xec: /* in al,dx */
3024 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003025 c->src.val = c->regs[VCPU_REGS_RDX];
3026 do_io_in:
3027 c->dst.bytes = min(c->dst.bytes, 4u);
3028 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003029 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003030 goto done;
3031 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003032 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3033 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003034 goto done; /* IO is needed */
3035 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003036 case 0xee: /* out dx,al */
3037 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003038 c->src.val = c->regs[VCPU_REGS_RDX];
3039 do_io_out:
3040 c->dst.bytes = min(c->dst.bytes, 4u);
3041 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003042 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003043 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003044 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003045 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3046 ctxt->vcpu);
3047 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003048 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003049 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003050 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003051 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003052 case 0xf5: /* cmc */
3053 /* complement carry flag from eflags reg */
3054 ctxt->eflags ^= EFLG_CF;
3055 c->dst.type = OP_NONE; /* Disable writeback. */
3056 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003057 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003058 if (!emulate_grp3(ctxt, ops))
3059 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003060 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003061 case 0xf8: /* clc */
3062 ctxt->eflags &= ~EFLG_CF;
3063 c->dst.type = OP_NONE; /* Disable writeback. */
3064 break;
3065 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003066 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003067 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003068 goto done;
3069 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003070 ctxt->eflags &= ~X86_EFLAGS_IF;
3071 c->dst.type = OP_NONE; /* Disable writeback. */
3072 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003073 break;
3074 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003075 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003076 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003077 goto done;
3078 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003079 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003080 ctxt->eflags |= X86_EFLAGS_IF;
3081 c->dst.type = OP_NONE; /* Disable writeback. */
3082 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003083 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003084 case 0xfc: /* cld */
3085 ctxt->eflags &= ~EFLG_DF;
3086 c->dst.type = OP_NONE; /* Disable writeback. */
3087 break;
3088 case 0xfd: /* std */
3089 ctxt->eflags |= EFLG_DF;
3090 c->dst.type = OP_NONE; /* Disable writeback. */
3091 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003092 case 0xfe: /* Grp4 */
3093 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003094 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003095 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003096 goto done;
3097 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003098 case 0xff: /* Grp5 */
3099 if (c->modrm_reg == 5)
3100 goto jump_far;
3101 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003102 default:
3103 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003105
3106writeback:
3107 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003108 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003109 goto done;
3110
Gleb Natapov5cd21912010-03-18 15:20:26 +02003111 /*
3112 * restore dst type in case the decoding will be reused
3113 * (happens for string instruction )
3114 */
3115 c->dst.type = saved_dst_type;
3116
Gleb Natapova682e352010-03-18 15:20:21 +02003117 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003118 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3119 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003120
3121 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003122 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3123 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003124
Gleb Natapov5cd21912010-03-18 15:20:26 +02003125 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003126 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003127 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003128 /*
3129 * Re-enter guest when pio read ahead buffer is empty or,
3130 * if it is not used, after each 1024 iteration.
3131 */
3132 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3133 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003134 ctxt->restart = false;
3135 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003136 /*
3137 * reset read cache here in case string instruction is restared
3138 * without decoding
3139 */
3140 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003141 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003142
3143done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003144 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145
3146twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003147 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003149 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 u16 size;
3151 unsigned long address;
3152
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003153 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003154 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003155 goto cannot_emulate;
3156
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003157 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003158 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003159 goto done;
3160
Avi Kivity33e38852008-05-21 15:34:25 +03003161 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003162 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003163 /* Disable writeback. */
3164 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003165 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003167 rc = read_descriptor(ctxt, ops, c->src.ptr,
3168 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003169 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 goto done;
3171 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003172 /* Disable writeback. */
3173 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003175 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003176 if (c->modrm_mod == 3) {
3177 switch (c->modrm_rm) {
3178 case 1:
3179 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003180 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003181 goto done;
3182 break;
3183 default:
3184 goto cannot_emulate;
3185 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003186 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003187 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003188 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003189 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003190 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003191 goto done;
3192 realmode_lidt(ctxt->vcpu, size, address);
3193 }
Avi Kivity16286d02008-04-14 14:40:50 +03003194 /* Disable writeback. */
3195 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 break;
3197 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003198 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003199 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 break;
3201 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003202 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3203 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003204 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003206 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003207 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003208 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003210 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003211 /* Disable writeback. */
3212 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 break;
3214 default:
3215 goto cannot_emulate;
3216 }
3217 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003218 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003219 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003220 if (rc != X86EMUL_CONTINUE)
3221 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003222 else
3223 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003224 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003225 case 0x06:
3226 emulate_clts(ctxt->vcpu);
3227 c->dst.type = OP_NONE;
3228 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003229 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003230 kvm_emulate_wbinvd(ctxt->vcpu);
3231 c->dst.type = OP_NONE;
3232 break;
3233 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003234 case 0x0d: /* GrpP (prefetch) */
3235 case 0x18: /* Grp16 (prefetch/nop) */
3236 c->dst.type = OP_NONE;
3237 break;
3238 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003239 switch (c->modrm_reg) {
3240 case 1:
3241 case 5 ... 7:
3242 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003243 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003244 goto done;
3245 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003246 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003247 c->dst.type = OP_NONE; /* no writeback */
3248 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003250 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3251 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003252 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003253 goto done;
3254 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003255 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003256 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003258 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003259 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003260 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003261 goto done;
3262 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003263 c->dst.type = OP_NONE;
3264 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003266 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3267 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003268 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003269 goto done;
3270 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003271
Gleb Natapov338dbc92010-04-28 19:15:32 +03003272 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3273 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3274 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3275 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003276 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003277 goto done;
3278 }
3279
Laurent Viviera01af5e2007-09-24 11:10:56 +02003280 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003282 case 0x30:
3283 /* wrmsr */
3284 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3285 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003286 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003287 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003288 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003289 }
3290 rc = X86EMUL_CONTINUE;
3291 c->dst.type = OP_NONE;
3292 break;
3293 case 0x32:
3294 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003295 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003296 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003297 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003298 } else {
3299 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3300 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3301 }
3302 rc = X86EMUL_CONTINUE;
3303 c->dst.type = OP_NONE;
3304 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003305 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003306 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003307 if (rc != X86EMUL_CONTINUE)
3308 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003309 else
3310 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003311 break;
3312 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003313 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003314 if (rc != X86EMUL_CONTINUE)
3315 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003316 else
3317 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003318 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003320 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003321 if (!test_cc(c->b, ctxt->eflags))
3322 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003324 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003325 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003326 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003327 c->dst.type = OP_NONE;
3328 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003329 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003330 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003331 break;
3332 case 0xa1: /* pop fs */
3333 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003334 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003335 goto done;
3336 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003337 case 0xa3:
3338 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003339 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003340 /* only subword offset */
3341 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003342 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003343 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003344 case 0xa4: /* shld imm8, r, r/m */
3345 case 0xa5: /* shld cl, r, r/m */
3346 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3347 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003348 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003349 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003350 break;
3351 case 0xa9: /* pop gs */
3352 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003353 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003354 goto done;
3355 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003356 case 0xab:
3357 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003358 /* only subword offset */
3359 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003360 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003361 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003362 case 0xac: /* shrd imm8, r, r/m */
3363 case 0xad: /* shrd cl, r, r/m */
3364 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3365 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003366 case 0xae: /* clflush */
3367 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 case 0xb0 ... 0xb1: /* cmpxchg */
3369 /*
3370 * Save real source value, then compare EAX against
3371 * destination.
3372 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003373 c->src.orig_val = c->src.val;
3374 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003375 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3376 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003378 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 } else {
3380 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003381 c->dst.type = OP_REG;
3382 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 }
3384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 case 0xb3:
3386 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003387 /* only subword offset */
3388 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003389 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003392 c->dst.bytes = c->op_bytes;
3393 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3394 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003397 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398 case 0:
3399 goto bt;
3400 case 1:
3401 goto bts;
3402 case 2:
3403 goto btr;
3404 case 3:
3405 goto btc;
3406 }
3407 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003408 case 0xbb:
3409 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003410 /* only subword offset */
3411 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003412 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003413 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003415 c->dst.bytes = c->op_bytes;
3416 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3417 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003419 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003420 c->dst.bytes = c->op_bytes;
3421 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3422 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003423 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003425 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003426 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003427 goto done;
3428 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003429 default:
3430 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 }
3432 goto writeback;
3433
3434cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003435 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 return -1;
3437}