blob: 3689f34a303a583ed2b3a02284df6aa133bbecb3 [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 Kivityab85b12b2010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
57#define DstMask (7<<1)
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 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Avi Kivity047a4812010-07-26 14:37:47 +030086#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020087#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020088#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030089#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010090/* Source 2 operand type */
91#define Src2None (0<<29)
92#define Src2CL (1<<29)
93#define Src2ImmByte (2<<29)
94#define Src2One (3<<29)
95#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivityea9ef042010-07-29 15:11:34 +030097#define X2(x) x, x
98#define X3(x) X2(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +030099#define X4(x) X2(x), X2(x)
Avi Kivityea9ef042010-07-29 15:11:34 +0300100#define X5(x) X4(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +0300101#define X6(x) X4(x), X2(x)
102#define X7(x) X4(x), X3(x)
103#define X8(x) X4(x), X4(x)
104#define X16(x) X8(x), X8(x)
105
Avi Kivityd65b1de2010-07-29 15:11:35 +0300106struct opcode {
107 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300108 union {
109 struct opcode *group;
110 struct group_dual *gdual;
111 } u;
112};
113
114struct group_dual {
115 struct opcode mod012[8];
116 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300117};
118
Avi Kivityfd853312010-07-29 15:11:36 +0300119#define D(_y) { .flags = (_y) }
120#define N D(0)
Avi Kivity120df892010-07-29 15:11:39 +0300121#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
122#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
Avi Kivityfd853312010-07-29 15:11:36 +0300123
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300124static struct opcode group1[] = {
125 X7(D(Lock)), N
126};
127
Avi Kivity99880c52010-07-29 15:11:41 +0300128static struct opcode group1A[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300129 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
Avi Kivity99880c52010-07-29 15:11:41 +0300130};
131
Avi Kivityee70ea32010-07-29 15:11:42 +0300132static struct opcode group3[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300133 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
134 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
135 X4(D(Undefined)),
Avi Kivityee70ea32010-07-29 15:11:42 +0300136};
137
Avi Kivity591c9d22010-07-29 15:11:43 +0300138static struct opcode group4[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300139 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
140 N, N, N, N, N, N,
Avi Kivity591c9d22010-07-29 15:11:43 +0300141};
142
Avi Kivityb67f9f02010-07-29 15:11:44 +0300143static struct opcode group5[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300144 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
145 D(SrcMem | ModRM | Stack), N,
146 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
147 D(SrcMem | ModRM | Stack), N,
Avi Kivityb67f9f02010-07-29 15:11:44 +0300148};
149
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300150static struct group_dual group7 = { {
Avi Kivity42a1c522010-07-29 15:11:37 +0300151 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
152 D(SrcNone | ModRM | DstMem | Mov), N,
153 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300154}, {
155 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
156 D(SrcNone | ModRM | DstMem | Mov), N,
157 D(SrcMem16 | ModRM | Mov | Priv), N,
158} };
159
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300160static struct opcode group8[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300161 N, N, N, N,
162 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
163 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300164};
165
Avi Kivity9f5d3222010-07-29 15:11:47 +0300166static struct group_dual group9 = { {
Avi Kivity42a1c522010-07-29 15:11:37 +0300167 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
Avi Kivity9f5d3222010-07-29 15:11:47 +0300168}, {
169 N, N, N, N, N, N, N, N,
170} };
171
Avi Kivityd65b1de2010-07-29 15:11:35 +0300172static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0x00 - 0x07 */
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), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0x08 - 0x0F */
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), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0x10 - 0x17 */
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 /* 0x18 - 0x1F */
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 | SrcImm), D(DstAcc | SrcImm),
192 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 /* 0x20 - 0x27 */
Avi Kivityfd853312010-07-29 15:11:36 +0300194 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
195 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
196 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 /* 0x28 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300198 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
199 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
200 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 /* 0x30 - 0x37 */
Avi Kivityfd853312010-07-29 15:11:36 +0300202 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
203 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
204 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0x38 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300206 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
207 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
208 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
209 N, N,
Avi Kivity749358a2010-07-26 14:37:40 +0300210 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300211 X16(D(DstReg)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300212 /* 0x50 - 0x57 */
Avi Kivityfd853312010-07-29 15:11:36 +0300213 X8(D(SrcReg | Stack)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300214 /* 0x58 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300215 X8(D(DstReg | Stack)),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700216 /* 0x60 - 0x67 */
Avi Kivityfd853312010-07-29 15:11:36 +0300217 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
218 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
219 N, N, N, N,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700220 /* 0x68 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300221 D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N,
222 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
223 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300224 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300225 X16(D(SrcImmByte)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 /* 0x80 - 0x87 */
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300227 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
228 G(DstMem | SrcImm | ModRM | Group, group1),
229 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
230 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivityfd853312010-07-29 15:11:36 +0300231 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
232 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 /* 0x88 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300234 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
235 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
236 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
Avi Kivity99880c52010-07-29 15:11:41 +0300237 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300238 /* 0x90 - 0x97 */
Avi Kivityfd853312010-07-29 15:11:36 +0300239 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300240 /* 0x98 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300241 N, N, D(SrcImmFAddr | No64), N,
242 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300244 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
245 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
246 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
247 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300249 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
250 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
251 D(ByteOp | DstDI | String), D(DstDI | String),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300252 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300253 X8(D(ByteOp | DstReg | SrcImm | Mov)),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300254 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300255 X8(D(DstReg | SrcImm | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 /* 0xC0 - 0xC7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300257 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
258 N, D(ImplicitOps | Stack), N, N,
259 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 /* 0xC8 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300261 N, N, N, D(ImplicitOps | Stack),
262 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 /* 0xD0 - 0xD7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300264 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
265 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
266 N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 /* 0xD8 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300268 N, N, N, N, N, N, N, N,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300269 /* 0xE0 - 0xE7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300270 N, N, N, N,
271 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
272 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Nitin A Kamble098c9372007-08-19 11:00:36 +0300273 /* 0xE8 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300274 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
275 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
276 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
277 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 /* 0xF0 - 0xF7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300279 N, N, N, N,
Avi Kivityee70ea32010-07-29 15:11:42 +0300280 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281 /* 0xF8 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300282 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
Avi Kivityb67f9f02010-07-29 15:11:44 +0300283 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284};
285
Avi Kivityd65b1de2010-07-29 15:11:35 +0300286static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 /* 0x00 - 0x0F */
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300288 N, GD(0, &group7), N, N,
Avi Kivityfd853312010-07-29 15:11:36 +0300289 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
290 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
291 N, D(ImplicitOps | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292 /* 0x10 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300293 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 -0800294 /* 0x20 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300295 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
296 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
297 N, N, N, N,
298 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299 /* 0x30 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300300 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
301 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
302 N, N, N, N, N, N, N, N,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300303 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300304 X16(D(DstReg | SrcMem | ModRM | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 /* 0x50 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300306 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 /* 0x60 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300308 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300310 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311 /* 0x80 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300312 X16(D(SrcImm)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 /* 0x90 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300314 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300316 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
317 N, D(DstMem | SrcReg | ModRM | BitOp),
318 D(DstMem | SrcReg | Src2ImmByte | ModRM),
319 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300321 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
322 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
323 D(DstMem | SrcReg | Src2ImmByte | ModRM),
324 D(DstMem | SrcReg | Src2CL | ModRM),
325 D(ModRM), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300327 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
328 N, 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 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300332 N, N,
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300333 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivityfd853312010-07-29 15:11:36 +0300334 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
335 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336 /* 0xC0 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300337 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity9f5d3222010-07-29 15:11:47 +0300338 N, N, N, GD(0, &group9),
Avi Kivityfd853312010-07-29 15:11:36 +0300339 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340 /* 0xD0 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300341 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342 /* 0xE0 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300343 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344 /* 0xF0 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300345 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346};
347
Avi Kivityfd853312010-07-29 15:11:36 +0300348#undef D
349#undef N
Avi Kivity120df892010-07-29 15:11:39 +0300350#undef G
351#undef GD
Avi Kivityfd853312010-07-29 15:11:36 +0300352
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200354#define EFLG_ID (1<<21)
355#define EFLG_VIP (1<<20)
356#define EFLG_VIF (1<<19)
357#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200358#define EFLG_VM (1<<17)
359#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200360#define EFLG_IOPL (3<<12)
361#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362#define EFLG_OF (1<<11)
363#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200364#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200365#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366#define EFLG_SF (1<<7)
367#define EFLG_ZF (1<<6)
368#define EFLG_AF (1<<4)
369#define EFLG_PF (1<<2)
370#define EFLG_CF (1<<0)
371
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300372#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
373#define EFLG_RESERVED_ONE_MASK 2
374
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375/*
376 * Instruction emulation:
377 * Most instructions are emulated directly via a fragment of inline assembly
378 * code. This allows us to save/restore EFLAGS and thus very easily pick up
379 * any modified flags.
380 */
381
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800382#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383#define _LO32 "k" /* force 32-bit operand */
384#define _STK "%%rsp" /* stack pointer */
385#elif defined(__i386__)
386#define _LO32 "" /* force 32-bit operand */
387#define _STK "%%esp" /* stack pointer */
388#endif
389
390/*
391 * These EFLAGS bits are restored from saved value during emulation, and
392 * any changes are written back to the saved value after emulation.
393 */
394#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
395
396/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200397#define _PRE_EFLAGS(_sav, _msk, _tmp) \
398 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
399 "movl %"_sav",%"_LO32 _tmp"; " \
400 "push %"_tmp"; " \
401 "push %"_tmp"; " \
402 "movl %"_msk",%"_LO32 _tmp"; " \
403 "andl %"_LO32 _tmp",("_STK"); " \
404 "pushf; " \
405 "notl %"_LO32 _tmp"; " \
406 "andl %"_LO32 _tmp",("_STK"); " \
407 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
408 "pop %"_tmp"; " \
409 "orl %"_LO32 _tmp",("_STK"); " \
410 "popf; " \
411 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412
413/* After executing instruction: write-back necessary bits in EFLAGS. */
414#define _POST_EFLAGS(_sav, _msk, _tmp) \
415 /* _sav |= EFLAGS & _msk; */ \
416 "pushf; " \
417 "pop %"_tmp"; " \
418 "andl %"_msk",%"_LO32 _tmp"; " \
419 "orl %"_LO32 _tmp",%"_sav"; "
420
Avi Kivitydda96d82008-11-26 15:14:10 +0200421#ifdef CONFIG_X86_64
422#define ON64(x) x
423#else
424#define ON64(x)
425#endif
426
Avi Kivity6b7ad612008-11-26 15:30:45 +0200427#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
428 do { \
429 __asm__ __volatile__ ( \
430 _PRE_EFLAGS("0", "4", "2") \
431 _op _suffix " %"_x"3,%1; " \
432 _POST_EFLAGS("0", "4", "2") \
433 : "=m" (_eflags), "=m" ((_dst).val), \
434 "=&r" (_tmp) \
435 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200436 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200437
438
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439/* Raw emulation: instruction has two explicit operands. */
440#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200441 do { \
442 unsigned long _tmp; \
443 \
444 switch ((_dst).bytes) { \
445 case 2: \
446 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
447 break; \
448 case 4: \
449 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
450 break; \
451 case 8: \
452 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
453 break; \
454 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 } while (0)
456
457#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
458 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200459 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400460 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200462 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 break; \
464 default: \
465 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
466 _wx, _wy, _lx, _ly, _qx, _qy); \
467 break; \
468 } \
469 } while (0)
470
471/* Source operand is byte-sized and may be restricted to just %cl. */
472#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
473 __emulate_2op(_op, _src, _dst, _eflags, \
474 "b", "c", "b", "c", "b", "c", "b", "c")
475
476/* Source operand is byte, word, long or quad sized. */
477#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
478 __emulate_2op(_op, _src, _dst, _eflags, \
479 "b", "q", "w", "r", _LO32, "r", "", "r")
480
481/* Source operand is word, long or quad sized. */
482#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
483 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
484 "w", "r", _LO32, "r", "", "r")
485
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100486/* Instruction has three operands and one operand is stored in ECX register */
487#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
488 do { \
489 unsigned long _tmp; \
490 _type _clv = (_cl).val; \
491 _type _srcv = (_src).val; \
492 _type _dstv = (_dst).val; \
493 \
494 __asm__ __volatile__ ( \
495 _PRE_EFLAGS("0", "5", "2") \
496 _op _suffix " %4,%1 \n" \
497 _POST_EFLAGS("0", "5", "2") \
498 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
499 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
500 ); \
501 \
502 (_cl).val = (unsigned long) _clv; \
503 (_src).val = (unsigned long) _srcv; \
504 (_dst).val = (unsigned long) _dstv; \
505 } while (0)
506
507#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
508 do { \
509 switch ((_dst).bytes) { \
510 case 2: \
511 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
512 "w", unsigned short); \
513 break; \
514 case 4: \
515 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
516 "l", unsigned int); \
517 break; \
518 case 8: \
519 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
520 "q", unsigned long)); \
521 break; \
522 } \
523 } while (0)
524
Avi Kivitydda96d82008-11-26 15:14:10 +0200525#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 do { \
527 unsigned long _tmp; \
528 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200529 __asm__ __volatile__ ( \
530 _PRE_EFLAGS("0", "3", "2") \
531 _op _suffix " %1; " \
532 _POST_EFLAGS("0", "3", "2") \
533 : "=m" (_eflags), "+m" ((_dst).val), \
534 "=&r" (_tmp) \
535 : "i" (EFLAGS_MASK)); \
536 } while (0)
537
538/* Instruction has only one explicit operand (no source operand). */
539#define emulate_1op(_op, _dst, _eflags) \
540 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400541 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200542 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
543 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
544 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
545 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 } \
547 } while (0)
548
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549/* Fetch next part of the instruction being emulated. */
550#define insn_fetch(_type, _size, _eip) \
551({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200552 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200553 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554 goto done; \
555 (_eip) += (_size); \
556 (_type)_x; \
557})
558
Gleb Natapov414e6272010-04-28 19:15:26 +0300559#define insn_fetch_arr(_arr, _size, _eip) \
560({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
561 if (rc != X86EMUL_CONTINUE) \
562 goto done; \
563 (_eip) += (_size); \
564})
565
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800566static inline unsigned long ad_mask(struct decode_cache *c)
567{
568 return (1UL << (c->ad_bytes << 3)) - 1;
569}
570
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800572static inline unsigned long
573address_mask(struct decode_cache *c, unsigned long reg)
574{
575 if (c->ad_bytes == sizeof(unsigned long))
576 return reg;
577 else
578 return reg & ad_mask(c);
579}
580
581static inline unsigned long
582register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
583{
584 return base + address_mask(c, reg);
585}
586
Harvey Harrison7a9572752008-02-19 07:40:41 -0800587static inline void
588register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
589{
590 if (c->ad_bytes == sizeof(unsigned long))
591 *reg += inc;
592 else
593 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
594}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
Harvey Harrison7a9572752008-02-19 07:40:41 -0800596static inline void jmp_rel(struct decode_cache *c, int rel)
597{
598 register_address_increment(c, &c->eip, rel);
599}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300600
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300601static void set_seg_override(struct decode_cache *c, int seg)
602{
603 c->has_seg_override = true;
604 c->seg_override = seg;
605}
606
Gleb Natapov79168fd2010-04-28 19:15:30 +0300607static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
608 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300609{
610 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
611 return 0;
612
Gleb Natapov79168fd2010-04-28 19:15:30 +0300613 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300614}
615
616static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300617 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300618 struct decode_cache *c)
619{
620 if (!c->has_seg_override)
621 return 0;
622
Gleb Natapov79168fd2010-04-28 19:15:30 +0300623 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300624}
625
Gleb Natapov79168fd2010-04-28 19:15:30 +0300626static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
627 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300628{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300629 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300630}
631
Gleb Natapov79168fd2010-04-28 19:15:30 +0300632static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
633 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300634{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300635 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300636}
637
Gleb Natapov54b84862010-04-28 19:15:44 +0300638static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
639 u32 error, bool valid)
640{
641 ctxt->exception = vec;
642 ctxt->error_code = error;
643 ctxt->error_code_valid = valid;
644 ctxt->restart = false;
645}
646
647static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
648{
649 emulate_exception(ctxt, GP_VECTOR, err, true);
650}
651
652static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
653 int err)
654{
655 ctxt->cr2 = addr;
656 emulate_exception(ctxt, PF_VECTOR, err, true);
657}
658
659static void emulate_ud(struct x86_emulate_ctxt *ctxt)
660{
661 emulate_exception(ctxt, UD_VECTOR, 0, false);
662}
663
664static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
665{
666 emulate_exception(ctxt, TS_VECTOR, err, true);
667}
668
Avi Kivity62266862007-11-20 13:15:52 +0200669static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
670 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300671 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200672{
673 struct fetch_cache *fc = &ctxt->decode.fetch;
674 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300675 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200676
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300677 if (eip == fc->end) {
678 cur_size = fc->end - fc->start;
679 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
680 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
681 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900682 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200683 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300684 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200685 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300686 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900687 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200688}
689
690static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
691 struct x86_emulate_ops *ops,
692 unsigned long eip, void *dest, unsigned size)
693{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900694 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200695
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200696 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200697 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200698 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200699 while (size--) {
700 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900701 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200702 return rc;
703 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900704 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200705}
706
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000707/*
708 * Given the 'reg' portion of a ModRM byte, and a register block, return a
709 * pointer into the block that addresses the relevant register.
710 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
711 */
712static void *decode_register(u8 modrm_reg, unsigned long *regs,
713 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714{
715 void *p;
716
717 p = &regs[modrm_reg];
718 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
719 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
720 return p;
721}
722
723static int read_descriptor(struct x86_emulate_ctxt *ctxt,
724 struct x86_emulate_ops *ops,
725 void *ptr,
726 u16 *size, unsigned long *address, int op_bytes)
727{
728 int rc;
729
730 if (op_bytes == 2)
731 op_bytes = 3;
732 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300733 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200734 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900735 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300737 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200738 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 return rc;
740}
741
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300742static int test_cc(unsigned int condition, unsigned int flags)
743{
744 int rc = 0;
745
746 switch ((condition & 15) >> 1) {
747 case 0: /* o */
748 rc |= (flags & EFLG_OF);
749 break;
750 case 1: /* b/c/nae */
751 rc |= (flags & EFLG_CF);
752 break;
753 case 2: /* z/e */
754 rc |= (flags & EFLG_ZF);
755 break;
756 case 3: /* be/na */
757 rc |= (flags & (EFLG_CF|EFLG_ZF));
758 break;
759 case 4: /* s */
760 rc |= (flags & EFLG_SF);
761 break;
762 case 5: /* p/pe */
763 rc |= (flags & EFLG_PF);
764 break;
765 case 7: /* le/ng */
766 rc |= (flags & EFLG_ZF);
767 /* fall through */
768 case 6: /* l/nge */
769 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
770 break;
771 }
772
773 /* Odd condition identifiers (lsb == 1) have inverted sense. */
774 return (!!rc ^ (condition & 1));
775}
776
Avi Kivity3c118e22007-10-31 10:27:04 +0200777static void decode_register_operand(struct operand *op,
778 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200779 int inhibit_bytereg)
780{
Avi Kivity33615aa2007-10-31 11:15:56 +0200781 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200782 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200783
784 if (!(c->d & ModRM))
785 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200786 op->type = OP_REG;
787 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200788 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200789 op->val = *(u8 *)op->ptr;
790 op->bytes = 1;
791 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200792 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200793 op->bytes = c->op_bytes;
794 switch (op->bytes) {
795 case 2:
796 op->val = *(u16 *)op->ptr;
797 break;
798 case 4:
799 op->val = *(u32 *)op->ptr;
800 break;
801 case 8:
802 op->val = *(u64 *) op->ptr;
803 break;
804 }
805 }
806 op->orig_val = op->val;
807}
808
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809static int decode_modrm(struct x86_emulate_ctxt *ctxt,
810 struct x86_emulate_ops *ops)
811{
812 struct decode_cache *c = &ctxt->decode;
813 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700814 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900815 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200816
817 if (c->rex_prefix) {
818 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
819 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
820 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
821 }
822
823 c->modrm = insn_fetch(u8, 1, c->eip);
824 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
825 c->modrm_reg |= (c->modrm & 0x38) >> 3;
826 c->modrm_rm |= (c->modrm & 0x07);
827 c->modrm_ea = 0;
828 c->use_modrm_ea = 1;
829
830 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300831 c->modrm_ptr = decode_register(c->modrm_rm,
832 c->regs, c->d & ByteOp);
833 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200834 return rc;
835 }
836
837 if (c->ad_bytes == 2) {
838 unsigned bx = c->regs[VCPU_REGS_RBX];
839 unsigned bp = c->regs[VCPU_REGS_RBP];
840 unsigned si = c->regs[VCPU_REGS_RSI];
841 unsigned di = c->regs[VCPU_REGS_RDI];
842
843 /* 16-bit ModR/M decode. */
844 switch (c->modrm_mod) {
845 case 0:
846 if (c->modrm_rm == 6)
847 c->modrm_ea += insn_fetch(u16, 2, c->eip);
848 break;
849 case 1:
850 c->modrm_ea += insn_fetch(s8, 1, c->eip);
851 break;
852 case 2:
853 c->modrm_ea += insn_fetch(u16, 2, c->eip);
854 break;
855 }
856 switch (c->modrm_rm) {
857 case 0:
858 c->modrm_ea += bx + si;
859 break;
860 case 1:
861 c->modrm_ea += bx + di;
862 break;
863 case 2:
864 c->modrm_ea += bp + si;
865 break;
866 case 3:
867 c->modrm_ea += bp + di;
868 break;
869 case 4:
870 c->modrm_ea += si;
871 break;
872 case 5:
873 c->modrm_ea += di;
874 break;
875 case 6:
876 if (c->modrm_mod != 0)
877 c->modrm_ea += bp;
878 break;
879 case 7:
880 c->modrm_ea += bx;
881 break;
882 }
883 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
884 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300885 if (!c->has_seg_override)
886 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200887 c->modrm_ea = (u16)c->modrm_ea;
888 } else {
889 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700890 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891 sib = insn_fetch(u8, 1, c->eip);
892 index_reg |= (sib >> 3) & 7;
893 base_reg |= sib & 7;
894 scale = sib >> 6;
895
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700896 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
897 c->modrm_ea += insn_fetch(s32, 4, c->eip);
898 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200899 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700900 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200901 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700902 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
903 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700904 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700905 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200907 switch (c->modrm_mod) {
908 case 0:
909 if (c->modrm_rm == 5)
910 c->modrm_ea += insn_fetch(s32, 4, c->eip);
911 break;
912 case 1:
913 c->modrm_ea += insn_fetch(s8, 1, c->eip);
914 break;
915 case 2:
916 c->modrm_ea += insn_fetch(s32, 4, c->eip);
917 break;
918 }
919 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200920done:
921 return rc;
922}
923
924static int decode_abs(struct x86_emulate_ctxt *ctxt,
925 struct x86_emulate_ops *ops)
926{
927 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900928 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929
930 switch (c->ad_bytes) {
931 case 2:
932 c->modrm_ea = insn_fetch(u16, 2, c->eip);
933 break;
934 case 4:
935 c->modrm_ea = insn_fetch(u32, 4, c->eip);
936 break;
937 case 8:
938 c->modrm_ea = insn_fetch(u64, 8, c->eip);
939 break;
940 }
941done:
942 return rc;
943}
944
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945int
Avi Kivity9aabc88f2010-07-29 15:11:50 +0300946x86_decode_insn(struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947{
Avi Kivity9aabc88f2010-07-29 15:11:50 +0300948 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200949 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900950 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 int mode = ctxt->mode;
Avi Kivity3885d532010-07-29 15:11:48 +0300952 int def_op_bytes, def_ad_bytes, dual, goffset;
Avi Kivity120df892010-07-29 15:11:39 +0300953 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954
Gleb Natapov5cd21912010-03-18 15:20:26 +0200955 /* we cannot decode insn before we complete previous rep insn */
956 WARN_ON(ctxt->restart);
957
Gleb Natapov063db062010-03-18 15:20:06 +0200958 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300959 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300960 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961
962 switch (mode) {
963 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200964 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200966 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 break;
968 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800971#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200973 def_op_bytes = 4;
974 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 break;
976#endif
977 default:
978 return -1;
979 }
980
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200981 c->op_bytes = def_op_bytes;
982 c->ad_bytes = def_ad_bytes;
983
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200985 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200986 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200988 /* switch between 2/4 bytes */
989 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 break;
991 case 0x67: /* address-size override */
992 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200993 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200994 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200997 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001000 case 0x2e: /* CS override */
1001 case 0x36: /* SS override */
1002 case 0x3e: /* DS override */
1003 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 break;
1005 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001007 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001009 case 0x40 ... 0x4f: /* REX */
1010 if (mode != X86EMUL_MODE_PROT64)
1011 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001012 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001013 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001017 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001018 c->rep_prefix = REPNE_PREFIX;
1019 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001021 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 default:
1024 goto done_prefixes;
1025 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001026
1027 /* Any legacy prefix after a REX prefix nullifies its effect. */
1028
Avi Kivity33615aa2007-10-31 11:15:56 +02001029 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 }
1031
1032done_prefixes:
1033
1034 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001035 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001036 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038
1039 /* Opcode byte(s). */
Avi Kivity120df892010-07-29 15:11:39 +03001040 opcode = opcode_table[c->b];
1041 if (opcode.flags == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 if (c->b == 0x0f) {
1044 c->twobyte = 1;
1045 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivity120df892010-07-29 15:11:39 +03001046 opcode = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001048 }
Avi Kivity120df892010-07-29 15:11:39 +03001049 c->d = opcode.flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050
Avi Kivitye09d0822008-01-18 12:38:59 +02001051 if (c->d & Group) {
Avi Kivity52811d72010-07-26 14:37:48 +03001052 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001053 c->modrm = insn_fetch(u8, 1, c->eip);
1054 --c->eip;
1055
Avi Kivity3885d532010-07-29 15:11:48 +03001056 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;
Avi Kivity120df892010-07-29 15:11:39 +03001061
Avi Kivity3885d532010-07-29 15:11:48 +03001062 c->d &= ~(Group | GroupDual);
Avi Kivity120df892010-07-29 15:11:39 +03001063
1064 goffset = (c->modrm >> 3) & 7;
1065
1066 if ((c->modrm >> 6) == 3)
1067 opcode = g_mod3[goffset];
Avi Kivitye09d0822008-01-18 12:38:59 +02001068 else
Avi Kivity120df892010-07-29 15:11:39 +03001069 opcode = g_mod012[goffset];
1070 c->d |= opcode.flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001071 }
1072
1073 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001074 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001075 DPRINTF("Cannot emulate %02x\n", c->b);
1076 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 }
1078
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001079 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1080 c->op_bytes = 8;
1081
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001083 if (c->d & ModRM)
1084 rc = decode_modrm(ctxt, ops);
1085 else if (c->d & MemAbs)
1086 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001087 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001088 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001090 if (!c->has_seg_override)
1091 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001092
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001093 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001094 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001095
1096 if (c->ad_bytes != 8)
1097 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001098
1099 if (c->rip_relative)
1100 c->modrm_ea += c->eip;
1101
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 /*
1103 * Decode and fetch the source operand: register, memory
1104 * or immediate.
1105 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001106 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 case SrcNone:
1108 break;
1109 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001110 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 break;
1112 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 goto srcmem_common;
1115 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 goto srcmem_common;
1118 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 c->src.bytes = (c->d & ByteOp) ? 1 :
1120 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001121 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001122 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001123 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001124 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001125 /*
1126 * For instructions with a ModR/M byte, switch to register
1127 * access if Mod = 3.
1128 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001129 if ((c->d & ModRM) && c->modrm_mod == 3) {
1130 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001131 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001132 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001133 break;
1134 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001135 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001136 c->src.ptr = (unsigned long *)c->modrm_ea;
1137 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 break;
1139 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001140 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001141 c->src.type = OP_IMM;
1142 c->src.ptr = (unsigned long *)c->eip;
1143 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1144 if (c->src.bytes == 8)
1145 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001147 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001149 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 break;
1151 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001152 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 break;
1154 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001155 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 break;
1157 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001158 if ((c->d & SrcMask) == SrcImmU) {
1159 switch (c->src.bytes) {
1160 case 1:
1161 c->src.val &= 0xff;
1162 break;
1163 case 2:
1164 c->src.val &= 0xffff;
1165 break;
1166 case 4:
1167 c->src.val &= 0xffffffff;
1168 break;
1169 }
1170 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 break;
1172 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001173 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001174 c->src.type = OP_IMM;
1175 c->src.ptr = (unsigned long *)c->eip;
1176 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001177 if ((c->d & SrcMask) == SrcImmByte)
1178 c->src.val = insn_fetch(s8, 1, c->eip);
1179 else
1180 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001182 case SrcAcc:
1183 c->src.type = OP_REG;
1184 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1185 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1186 switch (c->src.bytes) {
1187 case 1:
1188 c->src.val = *(u8 *)c->src.ptr;
1189 break;
1190 case 2:
1191 c->src.val = *(u16 *)c->src.ptr;
1192 break;
1193 case 4:
1194 c->src.val = *(u32 *)c->src.ptr;
1195 break;
1196 case 8:
1197 c->src.val = *(u64 *)c->src.ptr;
1198 break;
1199 }
1200 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001201 case SrcOne:
1202 c->src.bytes = 1;
1203 c->src.val = 1;
1204 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001205 case SrcSI:
1206 c->src.type = OP_MEM;
1207 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1208 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001209 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001210 c->regs[VCPU_REGS_RSI]);
1211 c->src.val = 0;
1212 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001213 case SrcImmFAddr:
1214 c->src.type = OP_IMM;
1215 c->src.ptr = (unsigned long *)c->eip;
1216 c->src.bytes = c->op_bytes + 2;
1217 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1218 break;
1219 case SrcMemFAddr:
1220 c->src.type = OP_MEM;
1221 c->src.ptr = (unsigned long *)c->modrm_ea;
1222 c->src.bytes = c->op_bytes + 2;
1223 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 }
1225
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001226 /*
1227 * Decode and fetch the second source operand: register, memory
1228 * or immediate.
1229 */
1230 switch (c->d & Src2Mask) {
1231 case Src2None:
1232 break;
1233 case Src2CL:
1234 c->src2.bytes = 1;
1235 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1236 break;
1237 case Src2ImmByte:
1238 c->src2.type = OP_IMM;
1239 c->src2.ptr = (unsigned long *)c->eip;
1240 c->src2.bytes = 1;
1241 c->src2.val = insn_fetch(u8, 1, c->eip);
1242 break;
1243 case Src2One:
1244 c->src2.bytes = 1;
1245 c->src2.val = 1;
1246 break;
1247 }
1248
Avi Kivity038e51d2007-01-22 20:40:40 -08001249 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001250 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001251 case ImplicitOps:
1252 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001253 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001254 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001255 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001256 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001257 break;
1258 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001259 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001260 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001261 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001262 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001263 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001264 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001265 break;
1266 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001267 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001268 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001269 if ((c->d & DstMask) == DstMem64)
1270 c->dst.bytes = 8;
1271 else
1272 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001273 c->dst.val = 0;
1274 if (c->d & BitOp) {
1275 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1276
1277 c->dst.ptr = (void *)c->dst.ptr +
1278 (c->src.val & mask) / 8;
1279 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001280 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001281 case DstAcc:
1282 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001283 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001284 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001285 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001286 case 1:
1287 c->dst.val = *(u8 *)c->dst.ptr;
1288 break;
1289 case 2:
1290 c->dst.val = *(u16 *)c->dst.ptr;
1291 break;
1292 case 4:
1293 c->dst.val = *(u32 *)c->dst.ptr;
1294 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001295 case 8:
1296 c->dst.val = *(u64 *)c->dst.ptr;
1297 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001298 }
1299 c->dst.orig_val = c->dst.val;
1300 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001301 case DstDI:
1302 c->dst.type = OP_MEM;
1303 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1304 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001305 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001306 c->regs[VCPU_REGS_RDI]);
1307 c->dst.val = 0;
1308 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001309 }
1310
1311done:
1312 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1313}
1314
Gleb Natapov9de41572010-04-28 19:15:22 +03001315static int read_emulated(struct x86_emulate_ctxt *ctxt,
1316 struct x86_emulate_ops *ops,
1317 unsigned long addr, void *dest, unsigned size)
1318{
1319 int rc;
1320 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001321 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001322
1323 while (size) {
1324 int n = min(size, 8u);
1325 size -= n;
1326 if (mc->pos < mc->end)
1327 goto read_cached;
1328
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001329 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1330 ctxt->vcpu);
1331 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001332 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001333 if (rc != X86EMUL_CONTINUE)
1334 return rc;
1335 mc->end += n;
1336
1337 read_cached:
1338 memcpy(dest, mc->data + mc->pos, n);
1339 mc->pos += n;
1340 dest += n;
1341 addr += n;
1342 }
1343 return X86EMUL_CONTINUE;
1344}
1345
Gleb Natapov7b262e92010-03-18 15:20:27 +02001346static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1347 struct x86_emulate_ops *ops,
1348 unsigned int size, unsigned short port,
1349 void *dest)
1350{
1351 struct read_cache *rc = &ctxt->decode.io_read;
1352
1353 if (rc->pos == rc->end) { /* refill pio read ahead */
1354 struct decode_cache *c = &ctxt->decode;
1355 unsigned int in_page, n;
1356 unsigned int count = c->rep_prefix ?
1357 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1358 in_page = (ctxt->eflags & EFLG_DF) ?
1359 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1360 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1361 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1362 count);
1363 if (n == 0)
1364 n = 1;
1365 rc->pos = rc->end = 0;
1366 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1367 return 0;
1368 rc->end = n * size;
1369 }
1370
1371 memcpy(dest, rc->data + rc->pos, size);
1372 rc->pos += size;
1373 return 1;
1374}
1375
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376static u32 desc_limit_scaled(struct desc_struct *desc)
1377{
1378 u32 limit = get_desc_limit(desc);
1379
1380 return desc->g ? (limit << 12) | 0xfff : limit;
1381}
1382
1383static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1384 struct x86_emulate_ops *ops,
1385 u16 selector, struct desc_ptr *dt)
1386{
1387 if (selector & 1 << 2) {
1388 struct desc_struct desc;
1389 memset (dt, 0, sizeof *dt);
1390 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1391 return;
1392
1393 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1394 dt->address = get_desc_base(&desc);
1395 } else
1396 ops->get_gdt(dt, ctxt->vcpu);
1397}
1398
1399/* allowed just for 8 bytes segments */
1400static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1401 struct x86_emulate_ops *ops,
1402 u16 selector, struct desc_struct *desc)
1403{
1404 struct desc_ptr dt;
1405 u16 index = selector >> 3;
1406 int ret;
1407 u32 err;
1408 ulong addr;
1409
1410 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1411
1412 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001413 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414 return X86EMUL_PROPAGATE_FAULT;
1415 }
1416 addr = dt.address + index * 8;
1417 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1418 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001419 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420
1421 return ret;
1422}
1423
1424/* allowed just for 8 bytes segments */
1425static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1426 struct x86_emulate_ops *ops,
1427 u16 selector, struct desc_struct *desc)
1428{
1429 struct desc_ptr dt;
1430 u16 index = selector >> 3;
1431 u32 err;
1432 ulong addr;
1433 int ret;
1434
1435 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1436
1437 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001438 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001439 return X86EMUL_PROPAGATE_FAULT;
1440 }
1441
1442 addr = dt.address + index * 8;
1443 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1444 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001445 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446
1447 return ret;
1448}
1449
1450static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1451 struct x86_emulate_ops *ops,
1452 u16 selector, int seg)
1453{
1454 struct desc_struct seg_desc;
1455 u8 dpl, rpl, cpl;
1456 unsigned err_vec = GP_VECTOR;
1457 u32 err_code = 0;
1458 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1459 int ret;
1460
1461 memset(&seg_desc, 0, sizeof seg_desc);
1462
1463 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1464 || ctxt->mode == X86EMUL_MODE_REAL) {
1465 /* set real mode segment descriptor */
1466 set_desc_base(&seg_desc, selector << 4);
1467 set_desc_limit(&seg_desc, 0xffff);
1468 seg_desc.type = 3;
1469 seg_desc.p = 1;
1470 seg_desc.s = 1;
1471 goto load;
1472 }
1473
1474 /* NULL selector is not valid for TR, CS and SS */
1475 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1476 && null_selector)
1477 goto exception;
1478
1479 /* TR should be in GDT only */
1480 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1481 goto exception;
1482
1483 if (null_selector) /* for NULL selector skip all following checks */
1484 goto load;
1485
1486 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1487 if (ret != X86EMUL_CONTINUE)
1488 return ret;
1489
1490 err_code = selector & 0xfffc;
1491 err_vec = GP_VECTOR;
1492
1493 /* can't load system descriptor into segment selecor */
1494 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1495 goto exception;
1496
1497 if (!seg_desc.p) {
1498 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1499 goto exception;
1500 }
1501
1502 rpl = selector & 3;
1503 dpl = seg_desc.dpl;
1504 cpl = ops->cpl(ctxt->vcpu);
1505
1506 switch (seg) {
1507 case VCPU_SREG_SS:
1508 /*
1509 * segment is not a writable data segment or segment
1510 * selector's RPL != CPL or segment selector's RPL != CPL
1511 */
1512 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1513 goto exception;
1514 break;
1515 case VCPU_SREG_CS:
1516 if (!(seg_desc.type & 8))
1517 goto exception;
1518
1519 if (seg_desc.type & 4) {
1520 /* conforming */
1521 if (dpl > cpl)
1522 goto exception;
1523 } else {
1524 /* nonconforming */
1525 if (rpl > cpl || dpl != cpl)
1526 goto exception;
1527 }
1528 /* CS(RPL) <- CPL */
1529 selector = (selector & 0xfffc) | cpl;
1530 break;
1531 case VCPU_SREG_TR:
1532 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1533 goto exception;
1534 break;
1535 case VCPU_SREG_LDTR:
1536 if (seg_desc.s || seg_desc.type != 2)
1537 goto exception;
1538 break;
1539 default: /* DS, ES, FS, or GS */
1540 /*
1541 * segment is not a data or readable code segment or
1542 * ((segment is a data or nonconforming code segment)
1543 * and (both RPL and CPL > DPL))
1544 */
1545 if ((seg_desc.type & 0xa) == 0x8 ||
1546 (((seg_desc.type & 0xc) != 0xc) &&
1547 (rpl > dpl && cpl > dpl)))
1548 goto exception;
1549 break;
1550 }
1551
1552 if (seg_desc.s) {
1553 /* mark segment as accessed */
1554 seg_desc.type |= 1;
1555 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1556 if (ret != X86EMUL_CONTINUE)
1557 return ret;
1558 }
1559load:
1560 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1561 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1562 return X86EMUL_CONTINUE;
1563exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001564 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001565 return X86EMUL_PROPAGATE_FAULT;
1566}
1567
Wei Yongjunc37eda12010-06-15 09:03:33 +08001568static inline int writeback(struct x86_emulate_ctxt *ctxt,
1569 struct x86_emulate_ops *ops)
1570{
1571 int rc;
1572 struct decode_cache *c = &ctxt->decode;
1573 u32 err;
1574
1575 switch (c->dst.type) {
1576 case OP_REG:
1577 /* The 4-byte case *is* correct:
1578 * in 64-bit mode we zero-extend.
1579 */
1580 switch (c->dst.bytes) {
1581 case 1:
1582 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1583 break;
1584 case 2:
1585 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1586 break;
1587 case 4:
1588 *c->dst.ptr = (u32)c->dst.val;
1589 break; /* 64b: zero-ext */
1590 case 8:
1591 *c->dst.ptr = c->dst.val;
1592 break;
1593 }
1594 break;
1595 case OP_MEM:
1596 if (c->lock_prefix)
1597 rc = ops->cmpxchg_emulated(
1598 (unsigned long)c->dst.ptr,
1599 &c->dst.orig_val,
1600 &c->dst.val,
1601 c->dst.bytes,
1602 &err,
1603 ctxt->vcpu);
1604 else
1605 rc = ops->write_emulated(
1606 (unsigned long)c->dst.ptr,
1607 &c->dst.val,
1608 c->dst.bytes,
1609 &err,
1610 ctxt->vcpu);
1611 if (rc == X86EMUL_PROPAGATE_FAULT)
1612 emulate_pf(ctxt,
1613 (unsigned long)c->dst.ptr, err);
1614 if (rc != X86EMUL_CONTINUE)
1615 return rc;
1616 break;
1617 case OP_NONE:
1618 /* no writeback */
1619 break;
1620 default:
1621 break;
1622 }
1623 return X86EMUL_CONTINUE;
1624}
1625
Gleb Natapov79168fd2010-04-28 19:15:30 +03001626static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1627 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628{
1629 struct decode_cache *c = &ctxt->decode;
1630
1631 c->dst.type = OP_MEM;
1632 c->dst.bytes = c->op_bytes;
1633 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001634 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001635 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001636 c->regs[VCPU_REGS_RSP]);
1637}
1638
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001639static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001640 struct x86_emulate_ops *ops,
1641 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001642{
1643 struct decode_cache *c = &ctxt->decode;
1644 int rc;
1645
Gleb Natapov79168fd2010-04-28 19:15:30 +03001646 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001647 c->regs[VCPU_REGS_RSP]),
1648 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001649 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 return rc;
1651
Avi Kivity350f69d2009-01-05 11:12:40 +02001652 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001653 return rc;
1654}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001655
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001656static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1657 struct x86_emulate_ops *ops,
1658 void *dest, int len)
1659{
1660 int rc;
1661 unsigned long val, change_mask;
1662 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001663 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001664
1665 rc = emulate_pop(ctxt, ops, &val, len);
1666 if (rc != X86EMUL_CONTINUE)
1667 return rc;
1668
1669 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1670 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1671
1672 switch(ctxt->mode) {
1673 case X86EMUL_MODE_PROT64:
1674 case X86EMUL_MODE_PROT32:
1675 case X86EMUL_MODE_PROT16:
1676 if (cpl == 0)
1677 change_mask |= EFLG_IOPL;
1678 if (cpl <= iopl)
1679 change_mask |= EFLG_IF;
1680 break;
1681 case X86EMUL_MODE_VM86:
1682 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001683 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001684 return X86EMUL_PROPAGATE_FAULT;
1685 }
1686 change_mask |= EFLG_IF;
1687 break;
1688 default: /* real mode */
1689 change_mask |= (EFLG_IOPL | EFLG_IF);
1690 break;
1691 }
1692
1693 *(unsigned long *)dest =
1694 (ctxt->eflags & ~change_mask) | (val & change_mask);
1695
1696 return rc;
1697}
1698
Gleb Natapov79168fd2010-04-28 19:15:30 +03001699static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1700 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001701{
1702 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001703
Gleb Natapov79168fd2010-04-28 19:15:30 +03001704 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001705
Gleb Natapov79168fd2010-04-28 19:15:30 +03001706 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001707}
1708
1709static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1710 struct x86_emulate_ops *ops, int seg)
1711{
1712 struct decode_cache *c = &ctxt->decode;
1713 unsigned long selector;
1714 int rc;
1715
1716 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001717 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001718 return rc;
1719
Gleb Natapov2e873022010-03-18 15:20:18 +02001720 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001721 return rc;
1722}
1723
Wei Yongjunc37eda12010-06-15 09:03:33 +08001724static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001725 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001726{
1727 struct decode_cache *c = &ctxt->decode;
1728 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001729 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001730 int reg = VCPU_REGS_RAX;
1731
1732 while (reg <= VCPU_REGS_RDI) {
1733 (reg == VCPU_REGS_RSP) ?
1734 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1735
Gleb Natapov79168fd2010-04-28 19:15:30 +03001736 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001737
1738 rc = writeback(ctxt, ops);
1739 if (rc != X86EMUL_CONTINUE)
1740 return rc;
1741
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001742 ++reg;
1743 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001744
1745 /* Disable writeback. */
1746 c->dst.type = OP_NONE;
1747
1748 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001749}
1750
1751static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1752 struct x86_emulate_ops *ops)
1753{
1754 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001755 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001756 int reg = VCPU_REGS_RDI;
1757
1758 while (reg >= VCPU_REGS_RAX) {
1759 if (reg == VCPU_REGS_RSP) {
1760 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1761 c->op_bytes);
1762 --reg;
1763 }
1764
1765 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001766 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001767 break;
1768 --reg;
1769 }
1770 return rc;
1771}
1772
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001773static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1774 struct x86_emulate_ops *ops)
1775{
1776 struct decode_cache *c = &ctxt->decode;
1777 int rc = X86EMUL_CONTINUE;
1778 unsigned long temp_eip = 0;
1779 unsigned long temp_eflags = 0;
1780 unsigned long cs = 0;
1781 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1782 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1783 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1784 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1785
1786 /* TODO: Add stack limit check */
1787
1788 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1789
1790 if (rc != X86EMUL_CONTINUE)
1791 return rc;
1792
1793 if (temp_eip & ~0xffff) {
1794 emulate_gp(ctxt, 0);
1795 return X86EMUL_PROPAGATE_FAULT;
1796 }
1797
1798 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1799
1800 if (rc != X86EMUL_CONTINUE)
1801 return rc;
1802
1803 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1804
1805 if (rc != X86EMUL_CONTINUE)
1806 return rc;
1807
1808 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1809
1810 if (rc != X86EMUL_CONTINUE)
1811 return rc;
1812
1813 c->eip = temp_eip;
1814
1815
1816 if (c->op_bytes == 4)
1817 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1818 else if (c->op_bytes == 2) {
1819 ctxt->eflags &= ~0xffff;
1820 ctxt->eflags |= temp_eflags;
1821 }
1822
1823 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1824 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1825
1826 return rc;
1827}
1828
1829static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1830 struct x86_emulate_ops* ops)
1831{
1832 switch(ctxt->mode) {
1833 case X86EMUL_MODE_REAL:
1834 return emulate_iret_real(ctxt, ops);
1835 case X86EMUL_MODE_VM86:
1836 case X86EMUL_MODE_PROT16:
1837 case X86EMUL_MODE_PROT32:
1838 case X86EMUL_MODE_PROT64:
1839 default:
1840 /* iret from protected mode unimplemented yet */
1841 return X86EMUL_UNHANDLEABLE;
1842 }
1843}
1844
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001845static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1846 struct x86_emulate_ops *ops)
1847{
1848 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001849
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001850 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001851}
1852
Laurent Vivier05f086f2007-09-24 11:10:55 +02001853static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001854{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001855 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856 switch (c->modrm_reg) {
1857 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001858 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859 break;
1860 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001861 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001862 break;
1863 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001864 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001865 break;
1866 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001867 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001868 break;
1869 case 4: /* sal/shl */
1870 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001871 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001872 break;
1873 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001874 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001875 break;
1876 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001877 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001878 break;
1879 }
1880}
1881
1882static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001883 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001884{
1885 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886
1887 switch (c->modrm_reg) {
1888 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001889 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001890 break;
1891 case 2: /* not */
1892 c->dst.val = ~c->dst.val;
1893 break;
1894 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001895 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001896 break;
1897 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001898 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001899 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001900 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901}
1902
1903static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001904 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001905{
1906 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907
1908 switch (c->modrm_reg) {
1909 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001910 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001911 break;
1912 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001913 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001914 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001915 case 2: /* call near abs */ {
1916 long int old_eip;
1917 old_eip = c->eip;
1918 c->eip = c->src.val;
1919 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001920 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001921 break;
1922 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001924 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001925 break;
1926 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001927 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001928 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001929 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001930 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001931}
1932
1933static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001934 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001935{
1936 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001937 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001938
1939 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1940 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001941 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1942 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001943 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001944 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001945 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1946 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001947
Laurent Vivier05f086f2007-09-24 11:10:55 +02001948 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001949 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001950 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001951}
1952
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001953static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1954 struct x86_emulate_ops *ops)
1955{
1956 struct decode_cache *c = &ctxt->decode;
1957 int rc;
1958 unsigned long cs;
1959
1960 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001961 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001962 return rc;
1963 if (c->op_bytes == 4)
1964 c->eip = (u32)c->eip;
1965 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001966 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001967 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001968 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001969 return rc;
1970}
1971
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001972static inline void
1973setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001974 struct x86_emulate_ops *ops, struct desc_struct *cs,
1975 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001976{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001977 memset(cs, 0, sizeof(struct desc_struct));
1978 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1979 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001980
1981 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001983 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001984 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001985 cs->type = 0x0b; /* Read, Execute, Accessed */
1986 cs->s = 1;
1987 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001988 cs->p = 1;
1989 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001990
Gleb Natapov79168fd2010-04-28 19:15:30 +03001991 set_desc_base(ss, 0); /* flat segment */
1992 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001993 ss->g = 1; /* 4kb granularity */
1994 ss->s = 1;
1995 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001997 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001998 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001999}
2000
2001static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002002emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002003{
2004 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002006 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002007 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002008
2009 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002010 if (ctxt->mode == X86EMUL_MODE_REAL ||
2011 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002012 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002013 return X86EMUL_PROPAGATE_FAULT;
2014 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002015
Gleb Natapov79168fd2010-04-28 19:15:30 +03002016 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002017
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002018 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002019 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002020 cs_sel = (u16)(msr_data & 0xfffc);
2021 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002022
2023 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002025 cs.l = 1;
2026 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002027 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2028 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2029 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2030 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002031
2032 c->regs[VCPU_REGS_RCX] = c->eip;
2033 if (is_long_mode(ctxt->vcpu)) {
2034#ifdef CONFIG_X86_64
2035 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
2036
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002037 ops->get_msr(ctxt->vcpu,
2038 ctxt->mode == X86EMUL_MODE_PROT64 ?
2039 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002040 c->eip = msr_data;
2041
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002042 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002043 ctxt->eflags &= ~(msr_data | EFLG_RF);
2044#endif
2045 } else {
2046 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002047 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002048 c->eip = (u32)msr_data;
2049
2050 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2051 }
2052
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002053 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002054}
2055
Andre Przywara8c604352009-06-18 12:56:01 +02002056static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002057emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002058{
2059 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002060 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002061 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002062 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002063
Gleb Natapova0044752010-02-10 14:21:31 +02002064 /* inject #GP if in real mode */
2065 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002066 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002067 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002068 }
2069
2070 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2071 * Therefore, we inject an #UD.
2072 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002073 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002074 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002075 return X86EMUL_PROPAGATE_FAULT;
2076 }
Andre Przywara8c604352009-06-18 12:56:01 +02002077
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002079
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002080 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002081 switch (ctxt->mode) {
2082 case X86EMUL_MODE_PROT32:
2083 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002084 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002085 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002086 }
2087 break;
2088 case X86EMUL_MODE_PROT64:
2089 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002090 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002091 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002092 }
2093 break;
2094 }
2095
2096 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002097 cs_sel = (u16)msr_data;
2098 cs_sel &= ~SELECTOR_RPL_MASK;
2099 ss_sel = cs_sel + 8;
2100 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002101 if (ctxt->mode == X86EMUL_MODE_PROT64
2102 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002103 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002104 cs.l = 1;
2105 }
2106
Gleb Natapov79168fd2010-04-28 19:15:30 +03002107 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2108 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2109 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2110 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002111
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002112 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002113 c->eip = msr_data;
2114
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002115 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002116 c->regs[VCPU_REGS_RSP] = msr_data;
2117
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002118 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002119}
2120
Andre Przywara4668f052009-06-18 12:56:02 +02002121static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002122emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002123{
2124 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002125 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002126 u64 msr_data;
2127 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002128 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002129
Gleb Natapova0044752010-02-10 14:21:31 +02002130 /* inject #GP if in real mode or Virtual 8086 mode */
2131 if (ctxt->mode == X86EMUL_MODE_REAL ||
2132 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002133 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002134 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002135 }
2136
Gleb Natapov79168fd2010-04-28 19:15:30 +03002137 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002138
2139 if ((c->rex_prefix & 0x8) != 0x0)
2140 usermode = X86EMUL_MODE_PROT64;
2141 else
2142 usermode = X86EMUL_MODE_PROT32;
2143
2144 cs.dpl = 3;
2145 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002146 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002147 switch (usermode) {
2148 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002149 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002150 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002151 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002152 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002153 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002154 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002155 break;
2156 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002157 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002158 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002159 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002160 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002161 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002162 ss_sel = cs_sel + 8;
2163 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002164 cs.l = 1;
2165 break;
2166 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002167 cs_sel |= SELECTOR_RPL_MASK;
2168 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002169
Gleb Natapov79168fd2010-04-28 19:15:30 +03002170 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2171 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2172 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2173 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002174
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002175 c->eip = c->regs[VCPU_REGS_RDX];
2176 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002177
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002178 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002179}
2180
Gleb Natapov9c537242010-03-18 15:20:05 +02002181static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2182 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002183{
2184 int iopl;
2185 if (ctxt->mode == X86EMUL_MODE_REAL)
2186 return false;
2187 if (ctxt->mode == X86EMUL_MODE_VM86)
2188 return true;
2189 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002190 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002191}
2192
2193static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2194 struct x86_emulate_ops *ops,
2195 u16 port, u16 len)
2196{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002198 int r;
2199 u16 io_bitmap_ptr;
2200 u8 perm, bit_idx = port & 0x7;
2201 unsigned mask = (1 << len) - 1;
2202
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2204 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002205 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002206 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002207 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002208 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2209 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002210 if (r != X86EMUL_CONTINUE)
2211 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002212 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002213 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002214 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2215 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002216 if (r != X86EMUL_CONTINUE)
2217 return false;
2218 if ((perm >> bit_idx) & mask)
2219 return false;
2220 return true;
2221}
2222
2223static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2224 struct x86_emulate_ops *ops,
2225 u16 port, u16 len)
2226{
Gleb Natapov9c537242010-03-18 15:20:05 +02002227 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002228 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2229 return false;
2230 return true;
2231}
2232
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002233static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2234 struct x86_emulate_ops *ops,
2235 struct tss_segment_16 *tss)
2236{
2237 struct decode_cache *c = &ctxt->decode;
2238
2239 tss->ip = c->eip;
2240 tss->flag = ctxt->eflags;
2241 tss->ax = c->regs[VCPU_REGS_RAX];
2242 tss->cx = c->regs[VCPU_REGS_RCX];
2243 tss->dx = c->regs[VCPU_REGS_RDX];
2244 tss->bx = c->regs[VCPU_REGS_RBX];
2245 tss->sp = c->regs[VCPU_REGS_RSP];
2246 tss->bp = c->regs[VCPU_REGS_RBP];
2247 tss->si = c->regs[VCPU_REGS_RSI];
2248 tss->di = c->regs[VCPU_REGS_RDI];
2249
2250 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2251 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2252 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2253 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2254 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2255}
2256
2257static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2258 struct x86_emulate_ops *ops,
2259 struct tss_segment_16 *tss)
2260{
2261 struct decode_cache *c = &ctxt->decode;
2262 int ret;
2263
2264 c->eip = tss->ip;
2265 ctxt->eflags = tss->flag | 2;
2266 c->regs[VCPU_REGS_RAX] = tss->ax;
2267 c->regs[VCPU_REGS_RCX] = tss->cx;
2268 c->regs[VCPU_REGS_RDX] = tss->dx;
2269 c->regs[VCPU_REGS_RBX] = tss->bx;
2270 c->regs[VCPU_REGS_RSP] = tss->sp;
2271 c->regs[VCPU_REGS_RBP] = tss->bp;
2272 c->regs[VCPU_REGS_RSI] = tss->si;
2273 c->regs[VCPU_REGS_RDI] = tss->di;
2274
2275 /*
2276 * SDM says that segment selectors are loaded before segment
2277 * descriptors
2278 */
2279 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2280 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2281 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2282 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2283 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2284
2285 /*
2286 * Now load segment descriptors. If fault happenes at this stage
2287 * it is handled in a context of new task
2288 */
2289 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2290 if (ret != X86EMUL_CONTINUE)
2291 return ret;
2292 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2293 if (ret != X86EMUL_CONTINUE)
2294 return ret;
2295 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2296 if (ret != X86EMUL_CONTINUE)
2297 return ret;
2298 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2299 if (ret != X86EMUL_CONTINUE)
2300 return ret;
2301 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2302 if (ret != X86EMUL_CONTINUE)
2303 return ret;
2304
2305 return X86EMUL_CONTINUE;
2306}
2307
2308static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2309 struct x86_emulate_ops *ops,
2310 u16 tss_selector, u16 old_tss_sel,
2311 ulong old_tss_base, struct desc_struct *new_desc)
2312{
2313 struct tss_segment_16 tss_seg;
2314 int ret;
2315 u32 err, new_tss_base = get_desc_base(new_desc);
2316
2317 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2318 &err);
2319 if (ret == X86EMUL_PROPAGATE_FAULT) {
2320 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002321 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002322 return ret;
2323 }
2324
2325 save_state_to_tss16(ctxt, ops, &tss_seg);
2326
2327 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2328 &err);
2329 if (ret == X86EMUL_PROPAGATE_FAULT) {
2330 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002331 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332 return ret;
2333 }
2334
2335 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2336 &err);
2337 if (ret == X86EMUL_PROPAGATE_FAULT) {
2338 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002339 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340 return ret;
2341 }
2342
2343 if (old_tss_sel != 0xffff) {
2344 tss_seg.prev_task_link = old_tss_sel;
2345
2346 ret = ops->write_std(new_tss_base,
2347 &tss_seg.prev_task_link,
2348 sizeof tss_seg.prev_task_link,
2349 ctxt->vcpu, &err);
2350 if (ret == X86EMUL_PROPAGATE_FAULT) {
2351 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002352 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002353 return ret;
2354 }
2355 }
2356
2357 return load_state_from_tss16(ctxt, ops, &tss_seg);
2358}
2359
2360static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2361 struct x86_emulate_ops *ops,
2362 struct tss_segment_32 *tss)
2363{
2364 struct decode_cache *c = &ctxt->decode;
2365
2366 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2367 tss->eip = c->eip;
2368 tss->eflags = ctxt->eflags;
2369 tss->eax = c->regs[VCPU_REGS_RAX];
2370 tss->ecx = c->regs[VCPU_REGS_RCX];
2371 tss->edx = c->regs[VCPU_REGS_RDX];
2372 tss->ebx = c->regs[VCPU_REGS_RBX];
2373 tss->esp = c->regs[VCPU_REGS_RSP];
2374 tss->ebp = c->regs[VCPU_REGS_RBP];
2375 tss->esi = c->regs[VCPU_REGS_RSI];
2376 tss->edi = c->regs[VCPU_REGS_RDI];
2377
2378 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2379 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2380 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2381 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2382 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2383 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2384 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2385}
2386
2387static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2388 struct x86_emulate_ops *ops,
2389 struct tss_segment_32 *tss)
2390{
2391 struct decode_cache *c = &ctxt->decode;
2392 int ret;
2393
Gleb Natapov0f122442010-04-28 19:15:31 +03002394 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002395 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002396 return X86EMUL_PROPAGATE_FAULT;
2397 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398 c->eip = tss->eip;
2399 ctxt->eflags = tss->eflags | 2;
2400 c->regs[VCPU_REGS_RAX] = tss->eax;
2401 c->regs[VCPU_REGS_RCX] = tss->ecx;
2402 c->regs[VCPU_REGS_RDX] = tss->edx;
2403 c->regs[VCPU_REGS_RBX] = tss->ebx;
2404 c->regs[VCPU_REGS_RSP] = tss->esp;
2405 c->regs[VCPU_REGS_RBP] = tss->ebp;
2406 c->regs[VCPU_REGS_RSI] = tss->esi;
2407 c->regs[VCPU_REGS_RDI] = tss->edi;
2408
2409 /*
2410 * SDM says that segment selectors are loaded before segment
2411 * descriptors
2412 */
2413 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2414 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2415 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2416 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2417 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2418 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2419 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2420
2421 /*
2422 * Now load segment descriptors. If fault happenes at this stage
2423 * it is handled in a context of new task
2424 */
2425 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2426 if (ret != X86EMUL_CONTINUE)
2427 return ret;
2428 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2429 if (ret != X86EMUL_CONTINUE)
2430 return ret;
2431 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2432 if (ret != X86EMUL_CONTINUE)
2433 return ret;
2434 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2435 if (ret != X86EMUL_CONTINUE)
2436 return ret;
2437 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2438 if (ret != X86EMUL_CONTINUE)
2439 return ret;
2440 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2441 if (ret != X86EMUL_CONTINUE)
2442 return ret;
2443 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2444 if (ret != X86EMUL_CONTINUE)
2445 return ret;
2446
2447 return X86EMUL_CONTINUE;
2448}
2449
2450static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2451 struct x86_emulate_ops *ops,
2452 u16 tss_selector, u16 old_tss_sel,
2453 ulong old_tss_base, struct desc_struct *new_desc)
2454{
2455 struct tss_segment_32 tss_seg;
2456 int ret;
2457 u32 err, new_tss_base = get_desc_base(new_desc);
2458
2459 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2460 &err);
2461 if (ret == X86EMUL_PROPAGATE_FAULT) {
2462 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002463 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002464 return ret;
2465 }
2466
2467 save_state_to_tss32(ctxt, ops, &tss_seg);
2468
2469 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2470 &err);
2471 if (ret == X86EMUL_PROPAGATE_FAULT) {
2472 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002473 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474 return ret;
2475 }
2476
2477 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2478 &err);
2479 if (ret == X86EMUL_PROPAGATE_FAULT) {
2480 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002481 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002482 return ret;
2483 }
2484
2485 if (old_tss_sel != 0xffff) {
2486 tss_seg.prev_task_link = old_tss_sel;
2487
2488 ret = ops->write_std(new_tss_base,
2489 &tss_seg.prev_task_link,
2490 sizeof tss_seg.prev_task_link,
2491 ctxt->vcpu, &err);
2492 if (ret == X86EMUL_PROPAGATE_FAULT) {
2493 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002494 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 return ret;
2496 }
2497 }
2498
2499 return load_state_from_tss32(ctxt, ops, &tss_seg);
2500}
2501
2502static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002503 struct x86_emulate_ops *ops,
2504 u16 tss_selector, int reason,
2505 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506{
2507 struct desc_struct curr_tss_desc, next_tss_desc;
2508 int ret;
2509 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2510 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002511 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002512 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513
2514 /* FIXME: old_tss_base == ~0 ? */
2515
2516 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2517 if (ret != X86EMUL_CONTINUE)
2518 return ret;
2519 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2520 if (ret != X86EMUL_CONTINUE)
2521 return ret;
2522
2523 /* FIXME: check that next_tss_desc is tss */
2524
2525 if (reason != TASK_SWITCH_IRET) {
2526 if ((tss_selector & 3) > next_tss_desc.dpl ||
2527 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002528 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529 return X86EMUL_PROPAGATE_FAULT;
2530 }
2531 }
2532
Gleb Natapovceffb452010-03-18 15:20:19 +02002533 desc_limit = desc_limit_scaled(&next_tss_desc);
2534 if (!next_tss_desc.p ||
2535 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2536 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002537 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538 return X86EMUL_PROPAGATE_FAULT;
2539 }
2540
2541 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2542 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2543 write_segment_descriptor(ctxt, ops, old_tss_sel,
2544 &curr_tss_desc);
2545 }
2546
2547 if (reason == TASK_SWITCH_IRET)
2548 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2549
2550 /* set back link to prev task only if NT bit is set in eflags
2551 note that old_tss_sel is not used afetr this point */
2552 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2553 old_tss_sel = 0xffff;
2554
2555 if (next_tss_desc.type & 8)
2556 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2557 old_tss_base, &next_tss_desc);
2558 else
2559 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2560 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002561 if (ret != X86EMUL_CONTINUE)
2562 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563
2564 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2565 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2566
2567 if (reason != TASK_SWITCH_IRET) {
2568 next_tss_desc.type |= (1 << 1); /* set busy flag */
2569 write_segment_descriptor(ctxt, ops, tss_selector,
2570 &next_tss_desc);
2571 }
2572
2573 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2574 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2575 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2576
Jan Kiszkae269fb22010-04-14 15:51:09 +02002577 if (has_error_code) {
2578 struct decode_cache *c = &ctxt->decode;
2579
2580 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2581 c->lock_prefix = 0;
2582 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002583 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002584 }
2585
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 return ret;
2587}
2588
2589int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002590 u16 tss_selector, int reason,
2591 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002593 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 struct decode_cache *c = &ctxt->decode;
2595 int rc;
2596
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002598 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599
Jan Kiszkae269fb22010-04-14 15:51:09 +02002600 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2601 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602
2603 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002604 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002605 if (rc == X86EMUL_CONTINUE)
2606 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 }
2608
Gleb Natapov19d04432010-04-15 12:29:50 +03002609 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610}
2611
Gleb Natapova682e352010-03-18 15:20:21 +02002612static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002613 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002614{
2615 struct decode_cache *c = &ctxt->decode;
2616 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2617
Gleb Natapovd9271122010-03-18 15:20:22 +02002618 register_address_increment(c, &c->regs[reg], df * op->bytes);
2619 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002620}
2621
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002622int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002623x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002624{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002625 struct x86_emulate_ops *ops = ctxt->ops;
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}