blob: c6f435917538c8297c4d328ca9dfe79ef9ba2b34 [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 {
Avi Kivityef65c882010-07-29 15:11:51 +0300109 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300110 struct opcode *group;
111 struct group_dual *gdual;
112 } u;
113};
114
115struct group_dual {
116 struct opcode mod012[8];
117 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300118};
119
Avi Kivityfd853312010-07-29 15:11:36 +0300120#define D(_y) { .flags = (_y) }
121#define N D(0)
Avi Kivity120df892010-07-29 15:11:39 +0300122#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
123#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
Avi Kivityef65c882010-07-29 15:11:51 +0300124#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityfd853312010-07-29 15:11:36 +0300125
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300126static struct opcode group1[] = {
127 X7(D(Lock)), N
128};
129
Avi Kivity99880c52010-07-29 15:11:41 +0300130static struct opcode group1A[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300131 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
Avi Kivity99880c52010-07-29 15:11:41 +0300132};
133
Avi Kivityee70ea32010-07-29 15:11:42 +0300134static struct opcode group3[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300135 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
136 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
137 X4(D(Undefined)),
Avi Kivityee70ea32010-07-29 15:11:42 +0300138};
139
Avi Kivity591c9d22010-07-29 15:11:43 +0300140static struct opcode group4[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300141 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
142 N, N, N, N, N, N,
Avi Kivity591c9d22010-07-29 15:11:43 +0300143};
144
Avi Kivityb67f9f02010-07-29 15:11:44 +0300145static struct opcode group5[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300146 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
147 D(SrcMem | ModRM | Stack), N,
148 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
149 D(SrcMem | ModRM | Stack), N,
Avi Kivityb67f9f02010-07-29 15:11:44 +0300150};
151
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300152static struct group_dual group7 = { {
Avi Kivity42a1c522010-07-29 15:11:37 +0300153 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
154 D(SrcNone | ModRM | DstMem | Mov), N,
155 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300156}, {
157 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
158 D(SrcNone | ModRM | DstMem | Mov), N,
159 D(SrcMem16 | ModRM | Mov | Priv), N,
160} };
161
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300162static struct opcode group8[] = {
Avi Kivity42a1c522010-07-29 15:11:37 +0300163 N, N, N, N,
164 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
165 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300166};
167
Avi Kivity9f5d3222010-07-29 15:11:47 +0300168static struct group_dual group9 = { {
Avi Kivity42a1c522010-07-29 15:11:37 +0300169 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
Avi Kivity9f5d3222010-07-29 15:11:47 +0300170}, {
171 N, N, N, N, N, N, N, N,
172} };
173
Avi Kivityd65b1de2010-07-29 15:11:35 +0300174static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175 /* 0x00 - 0x07 */
Avi Kivityfd853312010-07-29 15:11:36 +0300176 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
177 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
178 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
179 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0x08 - 0x0F */
Avi Kivityfd853312010-07-29 15:11:36 +0300181 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
182 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
183 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
184 D(ImplicitOps | Stack | No64), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0x10 - 0x17 */
Avi Kivityfd853312010-07-29 15:11:36 +0300186 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
187 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
188 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
189 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190 /* 0x18 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300191 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
192 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
193 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
194 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195 /* 0x20 - 0x27 */
Avi Kivityfd853312010-07-29 15:11:36 +0300196 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
197 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
198 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0x28 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300200 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
201 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
202 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0x30 - 0x37 */
Avi Kivityfd853312010-07-29 15:11:36 +0300204 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
205 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
206 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 /* 0x38 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300208 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
209 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
210 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
211 N, N,
Avi Kivity749358a2010-07-26 14:37:40 +0300212 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300213 X16(D(DstReg)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300214 /* 0x50 - 0x57 */
Avi Kivityfd853312010-07-29 15:11:36 +0300215 X8(D(SrcReg | Stack)),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300216 /* 0x58 - 0x5F */
Avi Kivityfd853312010-07-29 15:11:36 +0300217 X8(D(DstReg | Stack)),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700218 /* 0x60 - 0x67 */
Avi Kivityfd853312010-07-29 15:11:36 +0300219 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
220 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
221 N, N, N, N,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700222 /* 0x68 - 0x6F */
Avi Kivityfd853312010-07-29 15:11:36 +0300223 D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N,
224 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
225 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300226 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300227 X16(D(SrcImmByte)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x80 - 0x87 */
Avi Kivity5b92b5f2010-07-29 15:11:40 +0300229 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
230 G(DstMem | SrcImm | ModRM | Group, group1),
231 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
232 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivityfd853312010-07-29 15:11:36 +0300233 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
234 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 /* 0x88 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300236 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
237 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
238 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
Avi Kivity99880c52010-07-29 15:11:41 +0300239 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300240 /* 0x90 - 0x97 */
Avi Kivityfd853312010-07-29 15:11:36 +0300241 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300242 /* 0x98 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300243 N, N, D(SrcImmFAddr | No64), N,
244 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300246 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
247 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
248 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
249 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300251 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
252 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
253 D(ByteOp | DstDI | String), D(DstDI | String),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300254 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300255 X8(D(ByteOp | DstReg | SrcImm | Mov)),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300256 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300257 X8(D(DstReg | SrcImm | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 /* 0xC0 - 0xC7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300259 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
260 N, D(ImplicitOps | Stack), N, N,
261 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xC8 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300263 N, N, N, D(ImplicitOps | Stack),
264 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xD0 - 0xD7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300266 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
267 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
268 N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 /* 0xD8 - 0xDF */
Avi Kivityfd853312010-07-29 15:11:36 +0300270 N, N, N, N, N, N, N, N,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300271 /* 0xE0 - 0xE7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300272 N, N, N, N,
273 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
274 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Nitin A Kamble098c9372007-08-19 11:00:36 +0300275 /* 0xE8 - 0xEF */
Avi Kivityfd853312010-07-29 15:11:36 +0300276 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
277 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
278 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
279 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 /* 0xF0 - 0xF7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300281 N, N, N, N,
Avi Kivityee70ea32010-07-29 15:11:42 +0300282 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 /* 0xF8 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300284 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
Avi Kivityb67f9f02010-07-29 15:11:44 +0300285 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286};
287
Avi Kivityd65b1de2010-07-29 15:11:35 +0300288static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 /* 0x00 - 0x0F */
Avi Kivity2f3a9bc2010-07-29 15:11:45 +0300290 N, GD(0, &group7), N, N,
Avi Kivityfd853312010-07-29 15:11:36 +0300291 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
292 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
293 N, D(ImplicitOps | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 /* 0x10 - 0x1F */
Avi Kivityfd853312010-07-29 15:11:36 +0300295 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 -0800296 /* 0x20 - 0x2F */
Avi Kivityfd853312010-07-29 15:11:36 +0300297 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
298 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
299 N, N, N, N,
300 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 /* 0x30 - 0x3F */
Avi Kivityfd853312010-07-29 15:11:36 +0300302 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
303 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
304 N, N, N, N, N, N, N, N,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300305 /* 0x40 - 0x4F */
Avi Kivityfd853312010-07-29 15:11:36 +0300306 X16(D(DstReg | SrcMem | ModRM | Mov)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 /* 0x50 - 0x5F */
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 /* 0x60 - 0x6F */
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 /* 0x70 - 0x7F */
Avi Kivityfd853312010-07-29 15:11:36 +0300312 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 /* 0x80 - 0x8F */
Avi Kivityfd853312010-07-29 15:11:36 +0300314 X16(D(SrcImm)),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 /* 0x90 - 0x9F */
Avi Kivityfd853312010-07-29 15:11:36 +0300316 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 /* 0xA0 - 0xA7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300318 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
319 N, D(DstMem | SrcReg | ModRM | BitOp),
320 D(DstMem | SrcReg | Src2ImmByte | ModRM),
321 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 /* 0xA8 - 0xAF */
Avi Kivityfd853312010-07-29 15:11:36 +0300323 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
324 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
325 D(DstMem | SrcReg | Src2ImmByte | ModRM),
326 D(DstMem | SrcReg | Src2CL | ModRM),
327 D(ModRM), N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 /* 0xB0 - 0xB7 */
Avi Kivityfd853312010-07-29 15:11:36 +0300329 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
330 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
331 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
332 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 /* 0xB8 - 0xBF */
Avi Kivityfd853312010-07-29 15:11:36 +0300334 N, N,
Avi Kivity2cb20bc2010-07-29 15:11:46 +0300335 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivityfd853312010-07-29 15:11:36 +0300336 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
337 D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 /* 0xC0 - 0xCF */
Avi Kivityfd853312010-07-29 15:11:36 +0300339 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity9f5d3222010-07-29 15:11:47 +0300340 N, N, N, GD(0, &group9),
Avi Kivityfd853312010-07-29 15:11:36 +0300341 N, N, N, N, N, N, N, N,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342 /* 0xD0 - 0xDF */
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 /* 0xE0 - 0xEF */
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 /* 0xF0 - 0xFF */
Avi Kivityfd853312010-07-29 15:11:36 +0300347 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348};
349
Avi Kivityfd853312010-07-29 15:11:36 +0300350#undef D
351#undef N
Avi Kivity120df892010-07-29 15:11:39 +0300352#undef G
353#undef GD
Avi Kivityef65c882010-07-29 15:11:51 +0300354#undef I
Avi Kivityfd853312010-07-29 15:11:36 +0300355
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200357#define EFLG_ID (1<<21)
358#define EFLG_VIP (1<<20)
359#define EFLG_VIF (1<<19)
360#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200361#define EFLG_VM (1<<17)
362#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200363#define EFLG_IOPL (3<<12)
364#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365#define EFLG_OF (1<<11)
366#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200367#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200368#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369#define EFLG_SF (1<<7)
370#define EFLG_ZF (1<<6)
371#define EFLG_AF (1<<4)
372#define EFLG_PF (1<<2)
373#define EFLG_CF (1<<0)
374
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300375#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
376#define EFLG_RESERVED_ONE_MASK 2
377
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378/*
379 * Instruction emulation:
380 * Most instructions are emulated directly via a fragment of inline assembly
381 * code. This allows us to save/restore EFLAGS and thus very easily pick up
382 * any modified flags.
383 */
384
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800385#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386#define _LO32 "k" /* force 32-bit operand */
387#define _STK "%%rsp" /* stack pointer */
388#elif defined(__i386__)
389#define _LO32 "" /* force 32-bit operand */
390#define _STK "%%esp" /* stack pointer */
391#endif
392
393/*
394 * These EFLAGS bits are restored from saved value during emulation, and
395 * any changes are written back to the saved value after emulation.
396 */
397#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
398
399/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200400#define _PRE_EFLAGS(_sav, _msk, _tmp) \
401 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
402 "movl %"_sav",%"_LO32 _tmp"; " \
403 "push %"_tmp"; " \
404 "push %"_tmp"; " \
405 "movl %"_msk",%"_LO32 _tmp"; " \
406 "andl %"_LO32 _tmp",("_STK"); " \
407 "pushf; " \
408 "notl %"_LO32 _tmp"; " \
409 "andl %"_LO32 _tmp",("_STK"); " \
410 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
411 "pop %"_tmp"; " \
412 "orl %"_LO32 _tmp",("_STK"); " \
413 "popf; " \
414 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415
416/* After executing instruction: write-back necessary bits in EFLAGS. */
417#define _POST_EFLAGS(_sav, _msk, _tmp) \
418 /* _sav |= EFLAGS & _msk; */ \
419 "pushf; " \
420 "pop %"_tmp"; " \
421 "andl %"_msk",%"_LO32 _tmp"; " \
422 "orl %"_LO32 _tmp",%"_sav"; "
423
Avi Kivitydda96d82008-11-26 15:14:10 +0200424#ifdef CONFIG_X86_64
425#define ON64(x) x
426#else
427#define ON64(x)
428#endif
429
Avi Kivity6b7ad612008-11-26 15:30:45 +0200430#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
431 do { \
432 __asm__ __volatile__ ( \
433 _PRE_EFLAGS("0", "4", "2") \
434 _op _suffix " %"_x"3,%1; " \
435 _POST_EFLAGS("0", "4", "2") \
436 : "=m" (_eflags), "=m" ((_dst).val), \
437 "=&r" (_tmp) \
438 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200439 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200440
441
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442/* Raw emulation: instruction has two explicit operands. */
443#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200444 do { \
445 unsigned long _tmp; \
446 \
447 switch ((_dst).bytes) { \
448 case 2: \
449 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
450 break; \
451 case 4: \
452 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
453 break; \
454 case 8: \
455 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
456 break; \
457 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 } while (0)
459
460#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
461 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200462 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400463 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200465 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 break; \
467 default: \
468 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
469 _wx, _wy, _lx, _ly, _qx, _qy); \
470 break; \
471 } \
472 } while (0)
473
474/* Source operand is byte-sized and may be restricted to just %cl. */
475#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
476 __emulate_2op(_op, _src, _dst, _eflags, \
477 "b", "c", "b", "c", "b", "c", "b", "c")
478
479/* Source operand is byte, word, long or quad sized. */
480#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
481 __emulate_2op(_op, _src, _dst, _eflags, \
482 "b", "q", "w", "r", _LO32, "r", "", "r")
483
484/* Source operand is word, long or quad sized. */
485#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
486 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
487 "w", "r", _LO32, "r", "", "r")
488
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100489/* Instruction has three operands and one operand is stored in ECX register */
490#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
491 do { \
492 unsigned long _tmp; \
493 _type _clv = (_cl).val; \
494 _type _srcv = (_src).val; \
495 _type _dstv = (_dst).val; \
496 \
497 __asm__ __volatile__ ( \
498 _PRE_EFLAGS("0", "5", "2") \
499 _op _suffix " %4,%1 \n" \
500 _POST_EFLAGS("0", "5", "2") \
501 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
502 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
503 ); \
504 \
505 (_cl).val = (unsigned long) _clv; \
506 (_src).val = (unsigned long) _srcv; \
507 (_dst).val = (unsigned long) _dstv; \
508 } while (0)
509
510#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
511 do { \
512 switch ((_dst).bytes) { \
513 case 2: \
514 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
515 "w", unsigned short); \
516 break; \
517 case 4: \
518 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
519 "l", unsigned int); \
520 break; \
521 case 8: \
522 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
523 "q", unsigned long)); \
524 break; \
525 } \
526 } while (0)
527
Avi Kivitydda96d82008-11-26 15:14:10 +0200528#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 do { \
530 unsigned long _tmp; \
531 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200532 __asm__ __volatile__ ( \
533 _PRE_EFLAGS("0", "3", "2") \
534 _op _suffix " %1; " \
535 _POST_EFLAGS("0", "3", "2") \
536 : "=m" (_eflags), "+m" ((_dst).val), \
537 "=&r" (_tmp) \
538 : "i" (EFLAGS_MASK)); \
539 } while (0)
540
541/* Instruction has only one explicit operand (no source operand). */
542#define emulate_1op(_op, _dst, _eflags) \
543 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400544 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200545 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
546 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
547 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
548 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 } \
550 } while (0)
551
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552/* Fetch next part of the instruction being emulated. */
553#define insn_fetch(_type, _size, _eip) \
554({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200555 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200556 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 goto done; \
558 (_eip) += (_size); \
559 (_type)_x; \
560})
561
Gleb Natapov414e6272010-04-28 19:15:26 +0300562#define insn_fetch_arr(_arr, _size, _eip) \
563({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
564 if (rc != X86EMUL_CONTINUE) \
565 goto done; \
566 (_eip) += (_size); \
567})
568
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800569static inline unsigned long ad_mask(struct decode_cache *c)
570{
571 return (1UL << (c->ad_bytes << 3)) - 1;
572}
573
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800575static inline unsigned long
576address_mask(struct decode_cache *c, unsigned long reg)
577{
578 if (c->ad_bytes == sizeof(unsigned long))
579 return reg;
580 else
581 return reg & ad_mask(c);
582}
583
584static inline unsigned long
585register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
586{
587 return base + address_mask(c, reg);
588}
589
Harvey Harrison7a9572752008-02-19 07:40:41 -0800590static inline void
591register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
592{
593 if (c->ad_bytes == sizeof(unsigned long))
594 *reg += inc;
595 else
596 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
597}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598
Harvey Harrison7a9572752008-02-19 07:40:41 -0800599static inline void jmp_rel(struct decode_cache *c, int rel)
600{
601 register_address_increment(c, &c->eip, rel);
602}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300603
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300604static void set_seg_override(struct decode_cache *c, int seg)
605{
606 c->has_seg_override = true;
607 c->seg_override = seg;
608}
609
Gleb Natapov79168fd2010-04-28 19:15:30 +0300610static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
611 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300612{
613 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
614 return 0;
615
Gleb Natapov79168fd2010-04-28 19:15:30 +0300616 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300617}
618
619static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300620 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300621 struct decode_cache *c)
622{
623 if (!c->has_seg_override)
624 return 0;
625
Gleb Natapov79168fd2010-04-28 19:15:30 +0300626 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300627}
628
Gleb Natapov79168fd2010-04-28 19:15:30 +0300629static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
630 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300631{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300632 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300633}
634
Gleb Natapov79168fd2010-04-28 19:15:30 +0300635static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
636 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300637{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300638 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300639}
640
Gleb Natapov54b84862010-04-28 19:15:44 +0300641static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
642 u32 error, bool valid)
643{
644 ctxt->exception = vec;
645 ctxt->error_code = error;
646 ctxt->error_code_valid = valid;
647 ctxt->restart = false;
648}
649
650static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
651{
652 emulate_exception(ctxt, GP_VECTOR, err, true);
653}
654
655static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
656 int err)
657{
658 ctxt->cr2 = addr;
659 emulate_exception(ctxt, PF_VECTOR, err, true);
660}
661
662static void emulate_ud(struct x86_emulate_ctxt *ctxt)
663{
664 emulate_exception(ctxt, UD_VECTOR, 0, false);
665}
666
667static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
668{
669 emulate_exception(ctxt, TS_VECTOR, err, true);
670}
671
Avi Kivity62266862007-11-20 13:15:52 +0200672static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
673 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300674 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200675{
676 struct fetch_cache *fc = &ctxt->decode.fetch;
677 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300678 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200679
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300680 if (eip == fc->end) {
681 cur_size = fc->end - fc->start;
682 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
683 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
684 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900685 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200686 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300687 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200688 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300689 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900690 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200691}
692
693static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
694 struct x86_emulate_ops *ops,
695 unsigned long eip, void *dest, unsigned size)
696{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900697 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200698
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200699 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200700 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200701 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200702 while (size--) {
703 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900704 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200705 return rc;
706 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900707 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200708}
709
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000710/*
711 * Given the 'reg' portion of a ModRM byte, and a register block, return a
712 * pointer into the block that addresses the relevant register.
713 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
714 */
715static void *decode_register(u8 modrm_reg, unsigned long *regs,
716 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717{
718 void *p;
719
720 p = &regs[modrm_reg];
721 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
722 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
723 return p;
724}
725
726static int read_descriptor(struct x86_emulate_ctxt *ctxt,
727 struct x86_emulate_ops *ops,
728 void *ptr,
729 u16 *size, unsigned long *address, int op_bytes)
730{
731 int rc;
732
733 if (op_bytes == 2)
734 op_bytes = 3;
735 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300736 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200737 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900738 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300740 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200741 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 return rc;
743}
744
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300745static int test_cc(unsigned int condition, unsigned int flags)
746{
747 int rc = 0;
748
749 switch ((condition & 15) >> 1) {
750 case 0: /* o */
751 rc |= (flags & EFLG_OF);
752 break;
753 case 1: /* b/c/nae */
754 rc |= (flags & EFLG_CF);
755 break;
756 case 2: /* z/e */
757 rc |= (flags & EFLG_ZF);
758 break;
759 case 3: /* be/na */
760 rc |= (flags & (EFLG_CF|EFLG_ZF));
761 break;
762 case 4: /* s */
763 rc |= (flags & EFLG_SF);
764 break;
765 case 5: /* p/pe */
766 rc |= (flags & EFLG_PF);
767 break;
768 case 7: /* le/ng */
769 rc |= (flags & EFLG_ZF);
770 /* fall through */
771 case 6: /* l/nge */
772 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
773 break;
774 }
775
776 /* Odd condition identifiers (lsb == 1) have inverted sense. */
777 return (!!rc ^ (condition & 1));
778}
779
Avi Kivity3c118e22007-10-31 10:27:04 +0200780static void decode_register_operand(struct operand *op,
781 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200782 int inhibit_bytereg)
783{
Avi Kivity33615aa2007-10-31 11:15:56 +0200784 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200785 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200786
787 if (!(c->d & ModRM))
788 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200789 op->type = OP_REG;
790 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200791 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200792 op->val = *(u8 *)op->ptr;
793 op->bytes = 1;
794 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200795 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200796 op->bytes = c->op_bytes;
797 switch (op->bytes) {
798 case 2:
799 op->val = *(u16 *)op->ptr;
800 break;
801 case 4:
802 op->val = *(u32 *)op->ptr;
803 break;
804 case 8:
805 op->val = *(u64 *) op->ptr;
806 break;
807 }
808 }
809 op->orig_val = op->val;
810}
811
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200812static int decode_modrm(struct x86_emulate_ctxt *ctxt,
813 struct x86_emulate_ops *ops)
814{
815 struct decode_cache *c = &ctxt->decode;
816 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700817 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900818 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200819
820 if (c->rex_prefix) {
821 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
822 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
823 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
824 }
825
826 c->modrm = insn_fetch(u8, 1, c->eip);
827 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
828 c->modrm_reg |= (c->modrm & 0x38) >> 3;
829 c->modrm_rm |= (c->modrm & 0x07);
830 c->modrm_ea = 0;
831 c->use_modrm_ea = 1;
832
833 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300834 c->modrm_ptr = decode_register(c->modrm_rm,
835 c->regs, c->d & ByteOp);
836 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200837 return rc;
838 }
839
840 if (c->ad_bytes == 2) {
841 unsigned bx = c->regs[VCPU_REGS_RBX];
842 unsigned bp = c->regs[VCPU_REGS_RBP];
843 unsigned si = c->regs[VCPU_REGS_RSI];
844 unsigned di = c->regs[VCPU_REGS_RDI];
845
846 /* 16-bit ModR/M decode. */
847 switch (c->modrm_mod) {
848 case 0:
849 if (c->modrm_rm == 6)
850 c->modrm_ea += insn_fetch(u16, 2, c->eip);
851 break;
852 case 1:
853 c->modrm_ea += insn_fetch(s8, 1, c->eip);
854 break;
855 case 2:
856 c->modrm_ea += insn_fetch(u16, 2, c->eip);
857 break;
858 }
859 switch (c->modrm_rm) {
860 case 0:
861 c->modrm_ea += bx + si;
862 break;
863 case 1:
864 c->modrm_ea += bx + di;
865 break;
866 case 2:
867 c->modrm_ea += bp + si;
868 break;
869 case 3:
870 c->modrm_ea += bp + di;
871 break;
872 case 4:
873 c->modrm_ea += si;
874 break;
875 case 5:
876 c->modrm_ea += di;
877 break;
878 case 6:
879 if (c->modrm_mod != 0)
880 c->modrm_ea += bp;
881 break;
882 case 7:
883 c->modrm_ea += bx;
884 break;
885 }
886 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
887 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300888 if (!c->has_seg_override)
889 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890 c->modrm_ea = (u16)c->modrm_ea;
891 } else {
892 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700893 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894 sib = insn_fetch(u8, 1, c->eip);
895 index_reg |= (sib >> 3) & 7;
896 base_reg |= sib & 7;
897 scale = sib >> 6;
898
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700899 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
900 c->modrm_ea += insn_fetch(s32, 4, c->eip);
901 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700903 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200904 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700905 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
906 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700907 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700908 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200909 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200910 switch (c->modrm_mod) {
911 case 0:
912 if (c->modrm_rm == 5)
913 c->modrm_ea += insn_fetch(s32, 4, c->eip);
914 break;
915 case 1:
916 c->modrm_ea += insn_fetch(s8, 1, c->eip);
917 break;
918 case 2:
919 c->modrm_ea += insn_fetch(s32, 4, c->eip);
920 break;
921 }
922 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923done:
924 return rc;
925}
926
927static int decode_abs(struct x86_emulate_ctxt *ctxt,
928 struct x86_emulate_ops *ops)
929{
930 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900931 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932
933 switch (c->ad_bytes) {
934 case 2:
935 c->modrm_ea = insn_fetch(u16, 2, c->eip);
936 break;
937 case 4:
938 c->modrm_ea = insn_fetch(u32, 4, c->eip);
939 break;
940 case 8:
941 c->modrm_ea = insn_fetch(u64, 8, c->eip);
942 break;
943 }
944done:
945 return rc;
946}
947
Gleb Natapov9de41572010-04-28 19:15:22 +0300948static int read_emulated(struct x86_emulate_ctxt *ctxt,
949 struct x86_emulate_ops *ops,
950 unsigned long addr, void *dest, unsigned size)
951{
952 int rc;
953 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300954 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300955
956 while (size) {
957 int n = min(size, 8u);
958 size -= n;
959 if (mc->pos < mc->end)
960 goto read_cached;
961
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300962 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
963 ctxt->vcpu);
964 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300965 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300966 if (rc != X86EMUL_CONTINUE)
967 return rc;
968 mc->end += n;
969
970 read_cached:
971 memcpy(dest, mc->data + mc->pos, n);
972 mc->pos += n;
973 dest += n;
974 addr += n;
975 }
976 return X86EMUL_CONTINUE;
977}
978
Gleb Natapov7b262e92010-03-18 15:20:27 +0200979static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
980 struct x86_emulate_ops *ops,
981 unsigned int size, unsigned short port,
982 void *dest)
983{
984 struct read_cache *rc = &ctxt->decode.io_read;
985
986 if (rc->pos == rc->end) { /* refill pio read ahead */
987 struct decode_cache *c = &ctxt->decode;
988 unsigned int in_page, n;
989 unsigned int count = c->rep_prefix ?
990 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
991 in_page = (ctxt->eflags & EFLG_DF) ?
992 offset_in_page(c->regs[VCPU_REGS_RDI]) :
993 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
994 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
995 count);
996 if (n == 0)
997 n = 1;
998 rc->pos = rc->end = 0;
999 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1000 return 0;
1001 rc->end = n * size;
1002 }
1003
1004 memcpy(dest, rc->data + rc->pos, size);
1005 rc->pos += size;
1006 return 1;
1007}
1008
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001009static u32 desc_limit_scaled(struct desc_struct *desc)
1010{
1011 u32 limit = get_desc_limit(desc);
1012
1013 return desc->g ? (limit << 12) | 0xfff : limit;
1014}
1015
1016static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1017 struct x86_emulate_ops *ops,
1018 u16 selector, struct desc_ptr *dt)
1019{
1020 if (selector & 1 << 2) {
1021 struct desc_struct desc;
1022 memset (dt, 0, sizeof *dt);
1023 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1024 return;
1025
1026 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1027 dt->address = get_desc_base(&desc);
1028 } else
1029 ops->get_gdt(dt, ctxt->vcpu);
1030}
1031
1032/* allowed just for 8 bytes segments */
1033static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1034 struct x86_emulate_ops *ops,
1035 u16 selector, struct desc_struct *desc)
1036{
1037 struct desc_ptr dt;
1038 u16 index = selector >> 3;
1039 int ret;
1040 u32 err;
1041 ulong addr;
1042
1043 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1044
1045 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001046 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001047 return X86EMUL_PROPAGATE_FAULT;
1048 }
1049 addr = dt.address + index * 8;
1050 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1051 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001052 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001053
1054 return ret;
1055}
1056
1057/* allowed just for 8 bytes segments */
1058static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1059 struct x86_emulate_ops *ops,
1060 u16 selector, struct desc_struct *desc)
1061{
1062 struct desc_ptr dt;
1063 u16 index = selector >> 3;
1064 u32 err;
1065 ulong addr;
1066 int ret;
1067
1068 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1069
1070 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001071 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001072 return X86EMUL_PROPAGATE_FAULT;
1073 }
1074
1075 addr = dt.address + index * 8;
1076 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1077 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001078 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001079
1080 return ret;
1081}
1082
1083static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1084 struct x86_emulate_ops *ops,
1085 u16 selector, int seg)
1086{
1087 struct desc_struct seg_desc;
1088 u8 dpl, rpl, cpl;
1089 unsigned err_vec = GP_VECTOR;
1090 u32 err_code = 0;
1091 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1092 int ret;
1093
1094 memset(&seg_desc, 0, sizeof seg_desc);
1095
1096 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1097 || ctxt->mode == X86EMUL_MODE_REAL) {
1098 /* set real mode segment descriptor */
1099 set_desc_base(&seg_desc, selector << 4);
1100 set_desc_limit(&seg_desc, 0xffff);
1101 seg_desc.type = 3;
1102 seg_desc.p = 1;
1103 seg_desc.s = 1;
1104 goto load;
1105 }
1106
1107 /* NULL selector is not valid for TR, CS and SS */
1108 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1109 && null_selector)
1110 goto exception;
1111
1112 /* TR should be in GDT only */
1113 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1114 goto exception;
1115
1116 if (null_selector) /* for NULL selector skip all following checks */
1117 goto load;
1118
1119 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1120 if (ret != X86EMUL_CONTINUE)
1121 return ret;
1122
1123 err_code = selector & 0xfffc;
1124 err_vec = GP_VECTOR;
1125
1126 /* can't load system descriptor into segment selecor */
1127 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1128 goto exception;
1129
1130 if (!seg_desc.p) {
1131 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1132 goto exception;
1133 }
1134
1135 rpl = selector & 3;
1136 dpl = seg_desc.dpl;
1137 cpl = ops->cpl(ctxt->vcpu);
1138
1139 switch (seg) {
1140 case VCPU_SREG_SS:
1141 /*
1142 * segment is not a writable data segment or segment
1143 * selector's RPL != CPL or segment selector's RPL != CPL
1144 */
1145 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1146 goto exception;
1147 break;
1148 case VCPU_SREG_CS:
1149 if (!(seg_desc.type & 8))
1150 goto exception;
1151
1152 if (seg_desc.type & 4) {
1153 /* conforming */
1154 if (dpl > cpl)
1155 goto exception;
1156 } else {
1157 /* nonconforming */
1158 if (rpl > cpl || dpl != cpl)
1159 goto exception;
1160 }
1161 /* CS(RPL) <- CPL */
1162 selector = (selector & 0xfffc) | cpl;
1163 break;
1164 case VCPU_SREG_TR:
1165 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1166 goto exception;
1167 break;
1168 case VCPU_SREG_LDTR:
1169 if (seg_desc.s || seg_desc.type != 2)
1170 goto exception;
1171 break;
1172 default: /* DS, ES, FS, or GS */
1173 /*
1174 * segment is not a data or readable code segment or
1175 * ((segment is a data or nonconforming code segment)
1176 * and (both RPL and CPL > DPL))
1177 */
1178 if ((seg_desc.type & 0xa) == 0x8 ||
1179 (((seg_desc.type & 0xc) != 0xc) &&
1180 (rpl > dpl && cpl > dpl)))
1181 goto exception;
1182 break;
1183 }
1184
1185 if (seg_desc.s) {
1186 /* mark segment as accessed */
1187 seg_desc.type |= 1;
1188 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1189 if (ret != X86EMUL_CONTINUE)
1190 return ret;
1191 }
1192load:
1193 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1194 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1195 return X86EMUL_CONTINUE;
1196exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001197 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198 return X86EMUL_PROPAGATE_FAULT;
1199}
1200
Wei Yongjunc37eda12010-06-15 09:03:33 +08001201static inline int writeback(struct x86_emulate_ctxt *ctxt,
1202 struct x86_emulate_ops *ops)
1203{
1204 int rc;
1205 struct decode_cache *c = &ctxt->decode;
1206 u32 err;
1207
1208 switch (c->dst.type) {
1209 case OP_REG:
1210 /* The 4-byte case *is* correct:
1211 * in 64-bit mode we zero-extend.
1212 */
1213 switch (c->dst.bytes) {
1214 case 1:
1215 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1216 break;
1217 case 2:
1218 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1219 break;
1220 case 4:
1221 *c->dst.ptr = (u32)c->dst.val;
1222 break; /* 64b: zero-ext */
1223 case 8:
1224 *c->dst.ptr = c->dst.val;
1225 break;
1226 }
1227 break;
1228 case OP_MEM:
1229 if (c->lock_prefix)
1230 rc = ops->cmpxchg_emulated(
1231 (unsigned long)c->dst.ptr,
1232 &c->dst.orig_val,
1233 &c->dst.val,
1234 c->dst.bytes,
1235 &err,
1236 ctxt->vcpu);
1237 else
1238 rc = ops->write_emulated(
1239 (unsigned long)c->dst.ptr,
1240 &c->dst.val,
1241 c->dst.bytes,
1242 &err,
1243 ctxt->vcpu);
1244 if (rc == X86EMUL_PROPAGATE_FAULT)
1245 emulate_pf(ctxt,
1246 (unsigned long)c->dst.ptr, err);
1247 if (rc != X86EMUL_CONTINUE)
1248 return rc;
1249 break;
1250 case OP_NONE:
1251 /* no writeback */
1252 break;
1253 default:
1254 break;
1255 }
1256 return X86EMUL_CONTINUE;
1257}
1258
Gleb Natapov79168fd2010-04-28 19:15:30 +03001259static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1260 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261{
1262 struct decode_cache *c = &ctxt->decode;
1263
1264 c->dst.type = OP_MEM;
1265 c->dst.bytes = c->op_bytes;
1266 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001267 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001268 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 c->regs[VCPU_REGS_RSP]);
1270}
1271
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001272static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001273 struct x86_emulate_ops *ops,
1274 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001275{
1276 struct decode_cache *c = &ctxt->decode;
1277 int rc;
1278
Gleb Natapov79168fd2010-04-28 19:15:30 +03001279 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001280 c->regs[VCPU_REGS_RSP]),
1281 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001282 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001283 return rc;
1284
Avi Kivity350f69d2009-01-05 11:12:40 +02001285 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001286 return rc;
1287}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001288
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001289static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1290 struct x86_emulate_ops *ops,
1291 void *dest, int len)
1292{
1293 int rc;
1294 unsigned long val, change_mask;
1295 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001296 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001297
1298 rc = emulate_pop(ctxt, ops, &val, len);
1299 if (rc != X86EMUL_CONTINUE)
1300 return rc;
1301
1302 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1303 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1304
1305 switch(ctxt->mode) {
1306 case X86EMUL_MODE_PROT64:
1307 case X86EMUL_MODE_PROT32:
1308 case X86EMUL_MODE_PROT16:
1309 if (cpl == 0)
1310 change_mask |= EFLG_IOPL;
1311 if (cpl <= iopl)
1312 change_mask |= EFLG_IF;
1313 break;
1314 case X86EMUL_MODE_VM86:
1315 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001316 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001317 return X86EMUL_PROPAGATE_FAULT;
1318 }
1319 change_mask |= EFLG_IF;
1320 break;
1321 default: /* real mode */
1322 change_mask |= (EFLG_IOPL | EFLG_IF);
1323 break;
1324 }
1325
1326 *(unsigned long *)dest =
1327 (ctxt->eflags & ~change_mask) | (val & change_mask);
1328
1329 return rc;
1330}
1331
Gleb Natapov79168fd2010-04-28 19:15:30 +03001332static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1333 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001334{
1335 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001336
Gleb Natapov79168fd2010-04-28 19:15:30 +03001337 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001338
Gleb Natapov79168fd2010-04-28 19:15:30 +03001339 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001340}
1341
1342static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1343 struct x86_emulate_ops *ops, int seg)
1344{
1345 struct decode_cache *c = &ctxt->decode;
1346 unsigned long selector;
1347 int rc;
1348
1349 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001350 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001351 return rc;
1352
Gleb Natapov2e873022010-03-18 15:20:18 +02001353 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001354 return rc;
1355}
1356
Wei Yongjunc37eda12010-06-15 09:03:33 +08001357static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001358 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001359{
1360 struct decode_cache *c = &ctxt->decode;
1361 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001362 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001363 int reg = VCPU_REGS_RAX;
1364
1365 while (reg <= VCPU_REGS_RDI) {
1366 (reg == VCPU_REGS_RSP) ?
1367 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1368
Gleb Natapov79168fd2010-04-28 19:15:30 +03001369 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001370
1371 rc = writeback(ctxt, ops);
1372 if (rc != X86EMUL_CONTINUE)
1373 return rc;
1374
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001375 ++reg;
1376 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001377
1378 /* Disable writeback. */
1379 c->dst.type = OP_NONE;
1380
1381 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001382}
1383
1384static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1385 struct x86_emulate_ops *ops)
1386{
1387 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001388 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001389 int reg = VCPU_REGS_RDI;
1390
1391 while (reg >= VCPU_REGS_RAX) {
1392 if (reg == VCPU_REGS_RSP) {
1393 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1394 c->op_bytes);
1395 --reg;
1396 }
1397
1398 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001399 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001400 break;
1401 --reg;
1402 }
1403 return rc;
1404}
1405
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001406static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1407 struct x86_emulate_ops *ops)
1408{
1409 struct decode_cache *c = &ctxt->decode;
1410 int rc = X86EMUL_CONTINUE;
1411 unsigned long temp_eip = 0;
1412 unsigned long temp_eflags = 0;
1413 unsigned long cs = 0;
1414 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1415 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1416 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1417 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1418
1419 /* TODO: Add stack limit check */
1420
1421 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1422
1423 if (rc != X86EMUL_CONTINUE)
1424 return rc;
1425
1426 if (temp_eip & ~0xffff) {
1427 emulate_gp(ctxt, 0);
1428 return X86EMUL_PROPAGATE_FAULT;
1429 }
1430
1431 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1432
1433 if (rc != X86EMUL_CONTINUE)
1434 return rc;
1435
1436 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1437
1438 if (rc != X86EMUL_CONTINUE)
1439 return rc;
1440
1441 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1442
1443 if (rc != X86EMUL_CONTINUE)
1444 return rc;
1445
1446 c->eip = temp_eip;
1447
1448
1449 if (c->op_bytes == 4)
1450 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1451 else if (c->op_bytes == 2) {
1452 ctxt->eflags &= ~0xffff;
1453 ctxt->eflags |= temp_eflags;
1454 }
1455
1456 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1457 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1458
1459 return rc;
1460}
1461
1462static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1463 struct x86_emulate_ops* ops)
1464{
1465 switch(ctxt->mode) {
1466 case X86EMUL_MODE_REAL:
1467 return emulate_iret_real(ctxt, ops);
1468 case X86EMUL_MODE_VM86:
1469 case X86EMUL_MODE_PROT16:
1470 case X86EMUL_MODE_PROT32:
1471 case X86EMUL_MODE_PROT64:
1472 default:
1473 /* iret from protected mode unimplemented yet */
1474 return X86EMUL_UNHANDLEABLE;
1475 }
1476}
1477
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001478static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1479 struct x86_emulate_ops *ops)
1480{
1481 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001482
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001483 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001484}
1485
Laurent Vivier05f086f2007-09-24 11:10:55 +02001486static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001487{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001488 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001489 switch (c->modrm_reg) {
1490 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001491 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001492 break;
1493 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001494 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001495 break;
1496 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001497 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001498 break;
1499 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001500 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001501 break;
1502 case 4: /* sal/shl */
1503 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001504 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505 break;
1506 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001507 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001508 break;
1509 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001510 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001511 break;
1512 }
1513}
1514
1515static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001516 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001517{
1518 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519
1520 switch (c->modrm_reg) {
1521 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001522 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523 break;
1524 case 2: /* not */
1525 c->dst.val = ~c->dst.val;
1526 break;
1527 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001528 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529 break;
1530 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001531 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001532 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001533 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001534}
1535
1536static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001537 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538{
1539 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001540
1541 switch (c->modrm_reg) {
1542 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001543 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001544 break;
1545 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001546 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001547 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001548 case 2: /* call near abs */ {
1549 long int old_eip;
1550 old_eip = c->eip;
1551 c->eip = c->src.val;
1552 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001553 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001554 break;
1555 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001556 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001557 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001558 break;
1559 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001560 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001561 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001562 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001563 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001564}
1565
1566static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001567 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001568{
1569 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001570 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001571
1572 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1573 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001574 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1575 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001576 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001577 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001578 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1579 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001580
Laurent Vivier05f086f2007-09-24 11:10:55 +02001581 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001582 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001583 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001584}
1585
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001586static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1587 struct x86_emulate_ops *ops)
1588{
1589 struct decode_cache *c = &ctxt->decode;
1590 int rc;
1591 unsigned long cs;
1592
1593 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001594 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001595 return rc;
1596 if (c->op_bytes == 4)
1597 c->eip = (u32)c->eip;
1598 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001599 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001600 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001601 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001602 return rc;
1603}
1604
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001605static inline void
1606setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 struct x86_emulate_ops *ops, struct desc_struct *cs,
1608 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001609{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001610 memset(cs, 0, sizeof(struct desc_struct));
1611 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1612 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001613
1614 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001615 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001616 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001617 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001618 cs->type = 0x0b; /* Read, Execute, Accessed */
1619 cs->s = 1;
1620 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001621 cs->p = 1;
1622 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001623
Gleb Natapov79168fd2010-04-28 19:15:30 +03001624 set_desc_base(ss, 0); /* flat segment */
1625 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001626 ss->g = 1; /* 4kb granularity */
1627 ss->s = 1;
1628 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001629 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001630 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001631 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001632}
1633
1634static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001635emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001636{
1637 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001638 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001639 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001640 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001641
1642 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001643 if (ctxt->mode == X86EMUL_MODE_REAL ||
1644 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001645 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001646 return X86EMUL_PROPAGATE_FAULT;
1647 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001648
Gleb Natapov79168fd2010-04-28 19:15:30 +03001649 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001650
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001651 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001652 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001653 cs_sel = (u16)(msr_data & 0xfffc);
1654 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001655
1656 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001658 cs.l = 1;
1659 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001660 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1661 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1662 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1663 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001664
1665 c->regs[VCPU_REGS_RCX] = c->eip;
1666 if (is_long_mode(ctxt->vcpu)) {
1667#ifdef CONFIG_X86_64
1668 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1669
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001670 ops->get_msr(ctxt->vcpu,
1671 ctxt->mode == X86EMUL_MODE_PROT64 ?
1672 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001673 c->eip = msr_data;
1674
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001675 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001676 ctxt->eflags &= ~(msr_data | EFLG_RF);
1677#endif
1678 } else {
1679 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001680 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001681 c->eip = (u32)msr_data;
1682
1683 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1684 }
1685
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001686 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001687}
1688
Andre Przywara8c604352009-06-18 12:56:01 +02001689static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001690emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001691{
1692 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001693 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001694 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001695 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001696
Gleb Natapova0044752010-02-10 14:21:31 +02001697 /* inject #GP if in real mode */
1698 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001699 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001700 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001701 }
1702
1703 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1704 * Therefore, we inject an #UD.
1705 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001706 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001707 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001708 return X86EMUL_PROPAGATE_FAULT;
1709 }
Andre Przywara8c604352009-06-18 12:56:01 +02001710
Gleb Natapov79168fd2010-04-28 19:15:30 +03001711 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001712
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001713 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001714 switch (ctxt->mode) {
1715 case X86EMUL_MODE_PROT32:
1716 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001717 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001718 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001719 }
1720 break;
1721 case X86EMUL_MODE_PROT64:
1722 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001723 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001724 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001725 }
1726 break;
1727 }
1728
1729 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001730 cs_sel = (u16)msr_data;
1731 cs_sel &= ~SELECTOR_RPL_MASK;
1732 ss_sel = cs_sel + 8;
1733 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001734 if (ctxt->mode == X86EMUL_MODE_PROT64
1735 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001736 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001737 cs.l = 1;
1738 }
1739
Gleb Natapov79168fd2010-04-28 19:15:30 +03001740 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1741 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1742 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1743 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001744
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001745 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001746 c->eip = msr_data;
1747
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001748 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001749 c->regs[VCPU_REGS_RSP] = msr_data;
1750
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001751 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001752}
1753
Andre Przywara4668f052009-06-18 12:56:02 +02001754static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001755emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001756{
1757 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001758 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001759 u64 msr_data;
1760 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001761 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001762
Gleb Natapova0044752010-02-10 14:21:31 +02001763 /* inject #GP if in real mode or Virtual 8086 mode */
1764 if (ctxt->mode == X86EMUL_MODE_REAL ||
1765 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001766 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001767 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001768 }
1769
Gleb Natapov79168fd2010-04-28 19:15:30 +03001770 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001771
1772 if ((c->rex_prefix & 0x8) != 0x0)
1773 usermode = X86EMUL_MODE_PROT64;
1774 else
1775 usermode = X86EMUL_MODE_PROT32;
1776
1777 cs.dpl = 3;
1778 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001779 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001780 switch (usermode) {
1781 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001782 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001783 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001784 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001785 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001786 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001787 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001788 break;
1789 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001790 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001791 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001792 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001793 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001794 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001795 ss_sel = cs_sel + 8;
1796 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001797 cs.l = 1;
1798 break;
1799 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001800 cs_sel |= SELECTOR_RPL_MASK;
1801 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001802
Gleb Natapov79168fd2010-04-28 19:15:30 +03001803 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1804 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1805 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1806 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001807
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001808 c->eip = c->regs[VCPU_REGS_RDX];
1809 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001810
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001811 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001812}
1813
Gleb Natapov9c537242010-03-18 15:20:05 +02001814static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1815 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001816{
1817 int iopl;
1818 if (ctxt->mode == X86EMUL_MODE_REAL)
1819 return false;
1820 if (ctxt->mode == X86EMUL_MODE_VM86)
1821 return true;
1822 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001823 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001824}
1825
1826static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1827 struct x86_emulate_ops *ops,
1828 u16 port, u16 len)
1829{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001830 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001831 int r;
1832 u16 io_bitmap_ptr;
1833 u8 perm, bit_idx = port & 0x7;
1834 unsigned mask = (1 << len) - 1;
1835
Gleb Natapov79168fd2010-04-28 19:15:30 +03001836 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1837 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001838 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001839 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001840 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001841 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1842 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001843 if (r != X86EMUL_CONTINUE)
1844 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001845 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001846 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001847 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1848 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001849 if (r != X86EMUL_CONTINUE)
1850 return false;
1851 if ((perm >> bit_idx) & mask)
1852 return false;
1853 return true;
1854}
1855
1856static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1857 struct x86_emulate_ops *ops,
1858 u16 port, u16 len)
1859{
Gleb Natapov9c537242010-03-18 15:20:05 +02001860 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001861 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1862 return false;
1863 return true;
1864}
1865
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001866static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1867 struct x86_emulate_ops *ops,
1868 struct tss_segment_16 *tss)
1869{
1870 struct decode_cache *c = &ctxt->decode;
1871
1872 tss->ip = c->eip;
1873 tss->flag = ctxt->eflags;
1874 tss->ax = c->regs[VCPU_REGS_RAX];
1875 tss->cx = c->regs[VCPU_REGS_RCX];
1876 tss->dx = c->regs[VCPU_REGS_RDX];
1877 tss->bx = c->regs[VCPU_REGS_RBX];
1878 tss->sp = c->regs[VCPU_REGS_RSP];
1879 tss->bp = c->regs[VCPU_REGS_RBP];
1880 tss->si = c->regs[VCPU_REGS_RSI];
1881 tss->di = c->regs[VCPU_REGS_RDI];
1882
1883 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1884 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1885 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1886 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1887 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1888}
1889
1890static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1891 struct x86_emulate_ops *ops,
1892 struct tss_segment_16 *tss)
1893{
1894 struct decode_cache *c = &ctxt->decode;
1895 int ret;
1896
1897 c->eip = tss->ip;
1898 ctxt->eflags = tss->flag | 2;
1899 c->regs[VCPU_REGS_RAX] = tss->ax;
1900 c->regs[VCPU_REGS_RCX] = tss->cx;
1901 c->regs[VCPU_REGS_RDX] = tss->dx;
1902 c->regs[VCPU_REGS_RBX] = tss->bx;
1903 c->regs[VCPU_REGS_RSP] = tss->sp;
1904 c->regs[VCPU_REGS_RBP] = tss->bp;
1905 c->regs[VCPU_REGS_RSI] = tss->si;
1906 c->regs[VCPU_REGS_RDI] = tss->di;
1907
1908 /*
1909 * SDM says that segment selectors are loaded before segment
1910 * descriptors
1911 */
1912 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1913 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1914 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1915 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1916 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1917
1918 /*
1919 * Now load segment descriptors. If fault happenes at this stage
1920 * it is handled in a context of new task
1921 */
1922 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1923 if (ret != X86EMUL_CONTINUE)
1924 return ret;
1925 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1926 if (ret != X86EMUL_CONTINUE)
1927 return ret;
1928 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1929 if (ret != X86EMUL_CONTINUE)
1930 return ret;
1931 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1932 if (ret != X86EMUL_CONTINUE)
1933 return ret;
1934 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1935 if (ret != X86EMUL_CONTINUE)
1936 return ret;
1937
1938 return X86EMUL_CONTINUE;
1939}
1940
1941static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1942 struct x86_emulate_ops *ops,
1943 u16 tss_selector, u16 old_tss_sel,
1944 ulong old_tss_base, struct desc_struct *new_desc)
1945{
1946 struct tss_segment_16 tss_seg;
1947 int ret;
1948 u32 err, new_tss_base = get_desc_base(new_desc);
1949
1950 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1951 &err);
1952 if (ret == X86EMUL_PROPAGATE_FAULT) {
1953 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001954 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001955 return ret;
1956 }
1957
1958 save_state_to_tss16(ctxt, ops, &tss_seg);
1959
1960 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1961 &err);
1962 if (ret == X86EMUL_PROPAGATE_FAULT) {
1963 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001964 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001965 return ret;
1966 }
1967
1968 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1969 &err);
1970 if (ret == X86EMUL_PROPAGATE_FAULT) {
1971 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001972 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001973 return ret;
1974 }
1975
1976 if (old_tss_sel != 0xffff) {
1977 tss_seg.prev_task_link = old_tss_sel;
1978
1979 ret = ops->write_std(new_tss_base,
1980 &tss_seg.prev_task_link,
1981 sizeof tss_seg.prev_task_link,
1982 ctxt->vcpu, &err);
1983 if (ret == X86EMUL_PROPAGATE_FAULT) {
1984 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001985 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001986 return ret;
1987 }
1988 }
1989
1990 return load_state_from_tss16(ctxt, ops, &tss_seg);
1991}
1992
1993static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1994 struct x86_emulate_ops *ops,
1995 struct tss_segment_32 *tss)
1996{
1997 struct decode_cache *c = &ctxt->decode;
1998
1999 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2000 tss->eip = c->eip;
2001 tss->eflags = ctxt->eflags;
2002 tss->eax = c->regs[VCPU_REGS_RAX];
2003 tss->ecx = c->regs[VCPU_REGS_RCX];
2004 tss->edx = c->regs[VCPU_REGS_RDX];
2005 tss->ebx = c->regs[VCPU_REGS_RBX];
2006 tss->esp = c->regs[VCPU_REGS_RSP];
2007 tss->ebp = c->regs[VCPU_REGS_RBP];
2008 tss->esi = c->regs[VCPU_REGS_RSI];
2009 tss->edi = c->regs[VCPU_REGS_RDI];
2010
2011 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2012 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2013 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2014 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2015 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2016 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2017 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2018}
2019
2020static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2021 struct x86_emulate_ops *ops,
2022 struct tss_segment_32 *tss)
2023{
2024 struct decode_cache *c = &ctxt->decode;
2025 int ret;
2026
Gleb Natapov0f122442010-04-28 19:15:31 +03002027 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002028 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002029 return X86EMUL_PROPAGATE_FAULT;
2030 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002031 c->eip = tss->eip;
2032 ctxt->eflags = tss->eflags | 2;
2033 c->regs[VCPU_REGS_RAX] = tss->eax;
2034 c->regs[VCPU_REGS_RCX] = tss->ecx;
2035 c->regs[VCPU_REGS_RDX] = tss->edx;
2036 c->regs[VCPU_REGS_RBX] = tss->ebx;
2037 c->regs[VCPU_REGS_RSP] = tss->esp;
2038 c->regs[VCPU_REGS_RBP] = tss->ebp;
2039 c->regs[VCPU_REGS_RSI] = tss->esi;
2040 c->regs[VCPU_REGS_RDI] = tss->edi;
2041
2042 /*
2043 * SDM says that segment selectors are loaded before segment
2044 * descriptors
2045 */
2046 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2047 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2048 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2049 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2050 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2051 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2052 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2053
2054 /*
2055 * Now load segment descriptors. If fault happenes at this stage
2056 * it is handled in a context of new task
2057 */
2058 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2059 if (ret != X86EMUL_CONTINUE)
2060 return ret;
2061 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2062 if (ret != X86EMUL_CONTINUE)
2063 return ret;
2064 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2065 if (ret != X86EMUL_CONTINUE)
2066 return ret;
2067 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2068 if (ret != X86EMUL_CONTINUE)
2069 return ret;
2070 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2071 if (ret != X86EMUL_CONTINUE)
2072 return ret;
2073 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2074 if (ret != X86EMUL_CONTINUE)
2075 return ret;
2076 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2077 if (ret != X86EMUL_CONTINUE)
2078 return ret;
2079
2080 return X86EMUL_CONTINUE;
2081}
2082
2083static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2084 struct x86_emulate_ops *ops,
2085 u16 tss_selector, u16 old_tss_sel,
2086 ulong old_tss_base, struct desc_struct *new_desc)
2087{
2088 struct tss_segment_32 tss_seg;
2089 int ret;
2090 u32 err, new_tss_base = get_desc_base(new_desc);
2091
2092 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2093 &err);
2094 if (ret == X86EMUL_PROPAGATE_FAULT) {
2095 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002096 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002097 return ret;
2098 }
2099
2100 save_state_to_tss32(ctxt, ops, &tss_seg);
2101
2102 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2103 &err);
2104 if (ret == X86EMUL_PROPAGATE_FAULT) {
2105 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002106 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002107 return ret;
2108 }
2109
2110 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2111 &err);
2112 if (ret == X86EMUL_PROPAGATE_FAULT) {
2113 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002114 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002115 return ret;
2116 }
2117
2118 if (old_tss_sel != 0xffff) {
2119 tss_seg.prev_task_link = old_tss_sel;
2120
2121 ret = ops->write_std(new_tss_base,
2122 &tss_seg.prev_task_link,
2123 sizeof tss_seg.prev_task_link,
2124 ctxt->vcpu, &err);
2125 if (ret == X86EMUL_PROPAGATE_FAULT) {
2126 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002127 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002128 return ret;
2129 }
2130 }
2131
2132 return load_state_from_tss32(ctxt, ops, &tss_seg);
2133}
2134
2135static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002136 struct x86_emulate_ops *ops,
2137 u16 tss_selector, int reason,
2138 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002139{
2140 struct desc_struct curr_tss_desc, next_tss_desc;
2141 int ret;
2142 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2143 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002144 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002145 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146
2147 /* FIXME: old_tss_base == ~0 ? */
2148
2149 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2150 if (ret != X86EMUL_CONTINUE)
2151 return ret;
2152 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2153 if (ret != X86EMUL_CONTINUE)
2154 return ret;
2155
2156 /* FIXME: check that next_tss_desc is tss */
2157
2158 if (reason != TASK_SWITCH_IRET) {
2159 if ((tss_selector & 3) > next_tss_desc.dpl ||
2160 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002161 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162 return X86EMUL_PROPAGATE_FAULT;
2163 }
2164 }
2165
Gleb Natapovceffb452010-03-18 15:20:19 +02002166 desc_limit = desc_limit_scaled(&next_tss_desc);
2167 if (!next_tss_desc.p ||
2168 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2169 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002170 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002171 return X86EMUL_PROPAGATE_FAULT;
2172 }
2173
2174 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2175 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2176 write_segment_descriptor(ctxt, ops, old_tss_sel,
2177 &curr_tss_desc);
2178 }
2179
2180 if (reason == TASK_SWITCH_IRET)
2181 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2182
2183 /* set back link to prev task only if NT bit is set in eflags
2184 note that old_tss_sel is not used afetr this point */
2185 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2186 old_tss_sel = 0xffff;
2187
2188 if (next_tss_desc.type & 8)
2189 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2190 old_tss_base, &next_tss_desc);
2191 else
2192 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2193 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002194 if (ret != X86EMUL_CONTINUE)
2195 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196
2197 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2198 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2199
2200 if (reason != TASK_SWITCH_IRET) {
2201 next_tss_desc.type |= (1 << 1); /* set busy flag */
2202 write_segment_descriptor(ctxt, ops, tss_selector,
2203 &next_tss_desc);
2204 }
2205
2206 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2207 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2208 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2209
Jan Kiszkae269fb22010-04-14 15:51:09 +02002210 if (has_error_code) {
2211 struct decode_cache *c = &ctxt->decode;
2212
2213 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2214 c->lock_prefix = 0;
2215 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002216 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002217 }
2218
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219 return ret;
2220}
2221
2222int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002223 u16 tss_selector, int reason,
2224 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002225{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002226 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002227 struct decode_cache *c = &ctxt->decode;
2228 int rc;
2229
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002230 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002231 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002232
Jan Kiszkae269fb22010-04-14 15:51:09 +02002233 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2234 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002235
2236 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002237 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002238 if (rc == X86EMUL_CONTINUE)
2239 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002240 }
2241
Gleb Natapov19d04432010-04-15 12:29:50 +03002242 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002243}
2244
Gleb Natapova682e352010-03-18 15:20:21 +02002245static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002246 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002247{
2248 struct decode_cache *c = &ctxt->decode;
2249 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2250
Gleb Natapovd9271122010-03-18 15:20:22 +02002251 register_address_increment(c, &c->regs[reg], df * op->bytes);
2252 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002253}
2254
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002255int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002256x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2257{
2258 struct x86_emulate_ops *ops = ctxt->ops;
2259 struct decode_cache *c = &ctxt->decode;
2260 int rc = X86EMUL_CONTINUE;
2261 int mode = ctxt->mode;
2262 int def_op_bytes, def_ad_bytes, dual, goffset;
2263 struct opcode opcode, *g_mod012, *g_mod3;
2264
2265 /* we cannot decode insn before we complete previous rep insn */
2266 WARN_ON(ctxt->restart);
2267
2268 c->eip = ctxt->eip;
2269 c->fetch.start = c->fetch.end = c->eip;
2270 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2271
2272 switch (mode) {
2273 case X86EMUL_MODE_REAL:
2274 case X86EMUL_MODE_VM86:
2275 case X86EMUL_MODE_PROT16:
2276 def_op_bytes = def_ad_bytes = 2;
2277 break;
2278 case X86EMUL_MODE_PROT32:
2279 def_op_bytes = def_ad_bytes = 4;
2280 break;
2281#ifdef CONFIG_X86_64
2282 case X86EMUL_MODE_PROT64:
2283 def_op_bytes = 4;
2284 def_ad_bytes = 8;
2285 break;
2286#endif
2287 default:
2288 return -1;
2289 }
2290
2291 c->op_bytes = def_op_bytes;
2292 c->ad_bytes = def_ad_bytes;
2293
2294 /* Legacy prefixes. */
2295 for (;;) {
2296 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2297 case 0x66: /* operand-size override */
2298 /* switch between 2/4 bytes */
2299 c->op_bytes = def_op_bytes ^ 6;
2300 break;
2301 case 0x67: /* address-size override */
2302 if (mode == X86EMUL_MODE_PROT64)
2303 /* switch between 4/8 bytes */
2304 c->ad_bytes = def_ad_bytes ^ 12;
2305 else
2306 /* switch between 2/4 bytes */
2307 c->ad_bytes = def_ad_bytes ^ 6;
2308 break;
2309 case 0x26: /* ES override */
2310 case 0x2e: /* CS override */
2311 case 0x36: /* SS override */
2312 case 0x3e: /* DS override */
2313 set_seg_override(c, (c->b >> 3) & 3);
2314 break;
2315 case 0x64: /* FS override */
2316 case 0x65: /* GS override */
2317 set_seg_override(c, c->b & 7);
2318 break;
2319 case 0x40 ... 0x4f: /* REX */
2320 if (mode != X86EMUL_MODE_PROT64)
2321 goto done_prefixes;
2322 c->rex_prefix = c->b;
2323 continue;
2324 case 0xf0: /* LOCK */
2325 c->lock_prefix = 1;
2326 break;
2327 case 0xf2: /* REPNE/REPNZ */
2328 c->rep_prefix = REPNE_PREFIX;
2329 break;
2330 case 0xf3: /* REP/REPE/REPZ */
2331 c->rep_prefix = REPE_PREFIX;
2332 break;
2333 default:
2334 goto done_prefixes;
2335 }
2336
2337 /* Any legacy prefix after a REX prefix nullifies its effect. */
2338
2339 c->rex_prefix = 0;
2340 }
2341
2342done_prefixes:
2343
2344 /* REX prefix. */
2345 if (c->rex_prefix)
2346 if (c->rex_prefix & 8)
2347 c->op_bytes = 8; /* REX.W */
2348
2349 /* Opcode byte(s). */
2350 opcode = opcode_table[c->b];
2351 if (opcode.flags == 0) {
2352 /* Two-byte opcode? */
2353 if (c->b == 0x0f) {
2354 c->twobyte = 1;
2355 c->b = insn_fetch(u8, 1, c->eip);
2356 opcode = twobyte_table[c->b];
2357 }
2358 }
2359 c->d = opcode.flags;
2360
2361 if (c->d & Group) {
2362 dual = c->d & GroupDual;
2363 c->modrm = insn_fetch(u8, 1, c->eip);
2364 --c->eip;
2365
2366 if (c->d & GroupDual) {
2367 g_mod012 = opcode.u.gdual->mod012;
2368 g_mod3 = opcode.u.gdual->mod3;
2369 } else
2370 g_mod012 = g_mod3 = opcode.u.group;
2371
2372 c->d &= ~(Group | GroupDual);
2373
2374 goffset = (c->modrm >> 3) & 7;
2375
2376 if ((c->modrm >> 6) == 3)
2377 opcode = g_mod3[goffset];
2378 else
2379 opcode = g_mod012[goffset];
2380 c->d |= opcode.flags;
2381 }
2382
2383 c->execute = opcode.u.execute;
2384
2385 /* Unrecognised? */
2386 if (c->d == 0 || (c->d & Undefined)) {
2387 DPRINTF("Cannot emulate %02x\n", c->b);
2388 return -1;
2389 }
2390
2391 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2392 c->op_bytes = 8;
2393
2394 /* ModRM and SIB bytes. */
2395 if (c->d & ModRM)
2396 rc = decode_modrm(ctxt, ops);
2397 else if (c->d & MemAbs)
2398 rc = decode_abs(ctxt, ops);
2399 if (rc != X86EMUL_CONTINUE)
2400 goto done;
2401
2402 if (!c->has_seg_override)
2403 set_seg_override(c, VCPU_SREG_DS);
2404
2405 if (!(!c->twobyte && c->b == 0x8d))
2406 c->modrm_ea += seg_override_base(ctxt, ops, c);
2407
2408 if (c->ad_bytes != 8)
2409 c->modrm_ea = (u32)c->modrm_ea;
2410
2411 if (c->rip_relative)
2412 c->modrm_ea += c->eip;
2413
2414 /*
2415 * Decode and fetch the source operand: register, memory
2416 * or immediate.
2417 */
2418 switch (c->d & SrcMask) {
2419 case SrcNone:
2420 break;
2421 case SrcReg:
2422 decode_register_operand(&c->src, c, 0);
2423 break;
2424 case SrcMem16:
2425 c->src.bytes = 2;
2426 goto srcmem_common;
2427 case SrcMem32:
2428 c->src.bytes = 4;
2429 goto srcmem_common;
2430 case SrcMem:
2431 c->src.bytes = (c->d & ByteOp) ? 1 :
2432 c->op_bytes;
2433 /* Don't fetch the address for invlpg: it could be unmapped. */
2434 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
2435 break;
2436 srcmem_common:
2437 /*
2438 * For instructions with a ModR/M byte, switch to register
2439 * access if Mod = 3.
2440 */
2441 if ((c->d & ModRM) && c->modrm_mod == 3) {
2442 c->src.type = OP_REG;
2443 c->src.val = c->modrm_val;
2444 c->src.ptr = c->modrm_ptr;
2445 break;
2446 }
2447 c->src.type = OP_MEM;
2448 c->src.ptr = (unsigned long *)c->modrm_ea;
2449 c->src.val = 0;
2450 break;
2451 case SrcImm:
2452 case SrcImmU:
2453 c->src.type = OP_IMM;
2454 c->src.ptr = (unsigned long *)c->eip;
2455 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2456 if (c->src.bytes == 8)
2457 c->src.bytes = 4;
2458 /* NB. Immediates are sign-extended as necessary. */
2459 switch (c->src.bytes) {
2460 case 1:
2461 c->src.val = insn_fetch(s8, 1, c->eip);
2462 break;
2463 case 2:
2464 c->src.val = insn_fetch(s16, 2, c->eip);
2465 break;
2466 case 4:
2467 c->src.val = insn_fetch(s32, 4, c->eip);
2468 break;
2469 }
2470 if ((c->d & SrcMask) == SrcImmU) {
2471 switch (c->src.bytes) {
2472 case 1:
2473 c->src.val &= 0xff;
2474 break;
2475 case 2:
2476 c->src.val &= 0xffff;
2477 break;
2478 case 4:
2479 c->src.val &= 0xffffffff;
2480 break;
2481 }
2482 }
2483 break;
2484 case SrcImmByte:
2485 case SrcImmUByte:
2486 c->src.type = OP_IMM;
2487 c->src.ptr = (unsigned long *)c->eip;
2488 c->src.bytes = 1;
2489 if ((c->d & SrcMask) == SrcImmByte)
2490 c->src.val = insn_fetch(s8, 1, c->eip);
2491 else
2492 c->src.val = insn_fetch(u8, 1, c->eip);
2493 break;
2494 case SrcAcc:
2495 c->src.type = OP_REG;
2496 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2497 c->src.ptr = &c->regs[VCPU_REGS_RAX];
2498 switch (c->src.bytes) {
2499 case 1:
2500 c->src.val = *(u8 *)c->src.ptr;
2501 break;
2502 case 2:
2503 c->src.val = *(u16 *)c->src.ptr;
2504 break;
2505 case 4:
2506 c->src.val = *(u32 *)c->src.ptr;
2507 break;
2508 case 8:
2509 c->src.val = *(u64 *)c->src.ptr;
2510 break;
2511 }
2512 break;
2513 case SrcOne:
2514 c->src.bytes = 1;
2515 c->src.val = 1;
2516 break;
2517 case SrcSI:
2518 c->src.type = OP_MEM;
2519 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2520 c->src.ptr = (unsigned long *)
2521 register_address(c, seg_override_base(ctxt, ops, c),
2522 c->regs[VCPU_REGS_RSI]);
2523 c->src.val = 0;
2524 break;
2525 case SrcImmFAddr:
2526 c->src.type = OP_IMM;
2527 c->src.ptr = (unsigned long *)c->eip;
2528 c->src.bytes = c->op_bytes + 2;
2529 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2530 break;
2531 case SrcMemFAddr:
2532 c->src.type = OP_MEM;
2533 c->src.ptr = (unsigned long *)c->modrm_ea;
2534 c->src.bytes = c->op_bytes + 2;
2535 break;
2536 }
2537
2538 /*
2539 * Decode and fetch the second source operand: register, memory
2540 * or immediate.
2541 */
2542 switch (c->d & Src2Mask) {
2543 case Src2None:
2544 break;
2545 case Src2CL:
2546 c->src2.bytes = 1;
2547 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2548 break;
2549 case Src2ImmByte:
2550 c->src2.type = OP_IMM;
2551 c->src2.ptr = (unsigned long *)c->eip;
2552 c->src2.bytes = 1;
2553 c->src2.val = insn_fetch(u8, 1, c->eip);
2554 break;
2555 case Src2One:
2556 c->src2.bytes = 1;
2557 c->src2.val = 1;
2558 break;
2559 }
2560
2561 /* Decode and fetch the destination operand: register or memory. */
2562 switch (c->d & DstMask) {
2563 case ImplicitOps:
2564 /* Special instructions do their own operand decoding. */
2565 return 0;
2566 case DstReg:
2567 decode_register_operand(&c->dst, c,
2568 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2569 break;
2570 case DstMem:
2571 case DstMem64:
2572 if ((c->d & ModRM) && c->modrm_mod == 3) {
2573 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2574 c->dst.type = OP_REG;
2575 c->dst.val = c->dst.orig_val = c->modrm_val;
2576 c->dst.ptr = c->modrm_ptr;
2577 break;
2578 }
2579 c->dst.type = OP_MEM;
2580 c->dst.ptr = (unsigned long *)c->modrm_ea;
2581 if ((c->d & DstMask) == DstMem64)
2582 c->dst.bytes = 8;
2583 else
2584 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2585 c->dst.val = 0;
2586 if (c->d & BitOp) {
2587 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2588
2589 c->dst.ptr = (void *)c->dst.ptr +
2590 (c->src.val & mask) / 8;
2591 }
2592 break;
2593 case DstAcc:
2594 c->dst.type = OP_REG;
2595 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2596 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
2597 switch (c->dst.bytes) {
2598 case 1:
2599 c->dst.val = *(u8 *)c->dst.ptr;
2600 break;
2601 case 2:
2602 c->dst.val = *(u16 *)c->dst.ptr;
2603 break;
2604 case 4:
2605 c->dst.val = *(u32 *)c->dst.ptr;
2606 break;
2607 case 8:
2608 c->dst.val = *(u64 *)c->dst.ptr;
2609 break;
2610 }
2611 c->dst.orig_val = c->dst.val;
2612 break;
2613 case DstDI:
2614 c->dst.type = OP_MEM;
2615 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2616 c->dst.ptr = (unsigned long *)
2617 register_address(c, es_base(ctxt, ops),
2618 c->regs[VCPU_REGS_RDI]);
2619 c->dst.val = 0;
2620 break;
2621 }
2622
2623done:
2624 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2625}
2626
2627int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002628x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002629{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002630 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002631 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002632 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002633 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002634 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002635
Gleb Natapov9de41572010-04-28 19:15:22 +03002636 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002637
Gleb Natapov11616242010-02-11 14:43:14 +02002638 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002639 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002640 goto done;
2641 }
2642
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002643 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002644 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002645 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002646 goto done;
2647 }
2648
Gleb Natapove92805a2010-02-10 14:21:35 +02002649 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002650 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002651 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002652 goto done;
2653 }
2654
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002655 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002656 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002657 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002658 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002659 string_done:
2660 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002661 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002662 goto done;
2663 }
2664 /* The second termination condition only applies for REPE
2665 * and REPNE. Test if the repeat string operation prefix is
2666 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2667 * corresponding termination condition according to:
2668 * - if REPE/REPZ and ZF = 0 then done
2669 * - if REPNE/REPNZ and ZF = 1 then done
2670 */
2671 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002672 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002673 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002674 ((ctxt->eflags & EFLG_ZF) == 0))
2675 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002676 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002677 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2678 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002679 }
Gleb Natapov063db062010-03-18 15:20:06 +02002680 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002681 }
2682
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002683 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002684 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002685 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002686 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002687 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002688 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002689 }
2690
Gleb Natapove35b7b92010-02-25 16:36:42 +02002691 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002692 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2693 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002694 if (rc != X86EMUL_CONTINUE)
2695 goto done;
2696 }
2697
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002698 if ((c->d & DstMask) == ImplicitOps)
2699 goto special_insn;
2700
2701
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002702 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2703 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002704 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2705 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002706 if (rc != X86EMUL_CONTINUE)
2707 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002708 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002709 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002710
Avi Kivity018a98d2007-11-27 19:30:56 +02002711special_insn:
2712
Avi Kivityef65c882010-07-29 15:11:51 +03002713 if (c->execute) {
2714 rc = c->execute(ctxt);
2715 if (rc != X86EMUL_CONTINUE)
2716 goto done;
2717 goto writeback;
2718 }
2719
Laurent Viviere4e03de2007-09-18 11:52:50 +02002720 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 goto twobyte_insn;
2722
Laurent Viviere4e03de2007-09-18 11:52:50 +02002723 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 case 0x00 ... 0x05:
2725 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002726 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002728 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002729 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002730 break;
2731 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002732 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002733 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002734 goto done;
2735 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 case 0x08 ... 0x0d:
2737 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002738 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002740 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002741 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002742 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 case 0x10 ... 0x15:
2744 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002745 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002747 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002748 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002749 break;
2750 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002751 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002752 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002753 goto done;
2754 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 case 0x18 ... 0x1d:
2756 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002757 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002759 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002760 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002761 break;
2762 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002763 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002764 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002765 goto done;
2766 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002767 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002769 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
2771 case 0x28 ... 0x2d:
2772 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002773 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 break;
2775 case 0x30 ... 0x35:
2776 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002777 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 break;
2779 case 0x38 ... 0x3d:
2780 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002781 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002783 case 0x40 ... 0x47: /* inc r16/r32 */
2784 emulate_1op("inc", c->dst, ctxt->eflags);
2785 break;
2786 case 0x48 ... 0x4f: /* dec r16/r32 */
2787 emulate_1op("dec", c->dst, ctxt->eflags);
2788 break;
2789 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002790 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002791 break;
2792 case 0x58 ... 0x5f: /* pop reg */
2793 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002794 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002795 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002796 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002797 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002798 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002799 rc = emulate_pusha(ctxt, ops);
2800 if (rc != X86EMUL_CONTINUE)
2801 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002802 break;
2803 case 0x61: /* popa */
2804 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002805 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002806 goto done;
2807 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002809 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002811 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002813 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002814 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002815 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002816 break;
2817 case 0x6c: /* insb */
2818 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002819 c->dst.bytes = min(c->dst.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->dst.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 Natapov7b262e92010-03-18 15:20:27 +02002825 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2826 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002827 goto done; /* IO is needed, skip writeback */
2828 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002829 case 0x6e: /* outsb */
2830 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002831 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002832 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002833 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002834 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002835 goto done;
2836 }
Gleb Natapov79729952010-03-18 15:20:24 +02002837 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2838 &c->src.val, 1, ctxt->vcpu);
2839
2840 c->dst.type = OP_NONE; /* nothing to writeback */
2841 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002842 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002843 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002844 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002845 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002847 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 case 0:
2849 goto add;
2850 case 1:
2851 goto or;
2852 case 2:
2853 goto adc;
2854 case 3:
2855 goto sbb;
2856 case 4:
2857 goto and;
2858 case 5:
2859 goto sub;
2860 case 6:
2861 goto xor;
2862 case 7:
2863 goto cmp;
2864 }
2865 break;
2866 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002867 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002868 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 break;
2870 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002871 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002873 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002875 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 break;
2877 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002878 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 break;
2880 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002881 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 break; /* 64b reg: zero-extend */
2883 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002884 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 break;
2886 }
2887 /*
2888 * Write back the memory destination with implicit LOCK
2889 * prefix.
2890 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002891 c->dst.val = c->src.val;
2892 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002895 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002896 case 0x8c: /* mov r/m, sreg */
2897 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002898 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002899 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002900 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002901 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002902 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002903 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002904 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002905 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002906 case 0x8e: { /* mov seg, r/m16 */
2907 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002908
2909 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002910
Gleb Natapovc6975182010-02-18 12:15:01 +02002911 if (c->modrm_reg == VCPU_SREG_CS ||
2912 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002913 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002914 goto done;
2915 }
2916
Glauber Costa310b5d32009-05-12 16:21:06 -04002917 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002918 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002919
Gleb Natapov2e873022010-03-18 15:20:18 +02002920 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002921
2922 c->dst.type = OP_NONE; /* Disable writeback. */
2923 break;
2924 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002926 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002927 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002930 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002931 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2932 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002933 break;
2934 }
2935 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002936 c->src.type = OP_REG;
2937 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002938 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2939 c->src.val = *(c->src.ptr);
2940 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002941 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002942 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002943 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002944 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002945 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002946 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002947 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002948 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002949 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2950 if (rc != X86EMUL_CONTINUE)
2951 goto done;
2952 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002953 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002955 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002957 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002958 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002959 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002960 case 0xa8 ... 0xa9: /* test ax, imm */
2961 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002963 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 break;
2965 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002966 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 case 0xae ... 0xaf: /* scas */
2968 DPRINTF("Urk! I don't handle SCAS.\n");
2969 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002970 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002971 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002972 case 0xc0 ... 0xc1:
2973 emulate_grp2(ctxt);
2974 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002975 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002976 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002977 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002978 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002979 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002980 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2981 mov:
2982 c->dst.val = c->src.val;
2983 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002984 case 0xcb: /* ret far */
2985 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002986 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002987 goto done;
2988 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002989 case 0xcf: /* iret */
2990 rc = emulate_iret(ctxt, ops);
2991
2992 if (rc != X86EMUL_CONTINUE)
2993 goto done;
2994 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002995 case 0xd0 ... 0xd1: /* Grp2 */
2996 c->src.val = 1;
2997 emulate_grp2(ctxt);
2998 break;
2999 case 0xd2 ... 0xd3: /* Grp2 */
3000 c->src.val = c->regs[VCPU_REGS_RCX];
3001 emulate_grp2(ctxt);
3002 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003003 case 0xe4: /* inb */
3004 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003005 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003006 case 0xe6: /* outb */
3007 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003008 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003009 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003010 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003011 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003012 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003013 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003014 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003015 }
3016 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003017 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003018 case 0xea: { /* jmp far */
3019 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003020 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003021 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3022
3023 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003024 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003025
Gleb Natapov414e6272010-04-28 19:15:26 +03003026 c->eip = 0;
3027 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003028 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003029 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003030 case 0xeb:
3031 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003032 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003033 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003034 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003035 case 0xec: /* in al,dx */
3036 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003037 c->src.val = c->regs[VCPU_REGS_RDX];
3038 do_io_in:
3039 c->dst.bytes = min(c->dst.bytes, 4u);
3040 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003041 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003042 goto done;
3043 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003044 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3045 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003046 goto done; /* IO is needed */
3047 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003048 case 0xee: /* out dx,al */
3049 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003050 c->src.val = c->regs[VCPU_REGS_RDX];
3051 do_io_out:
3052 c->dst.bytes = min(c->dst.bytes, 4u);
3053 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003054 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003055 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003056 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003057 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3058 ctxt->vcpu);
3059 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003060 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003061 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003062 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003063 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003064 case 0xf5: /* cmc */
3065 /* complement carry flag from eflags reg */
3066 ctxt->eflags ^= EFLG_CF;
3067 c->dst.type = OP_NONE; /* Disable writeback. */
3068 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003069 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003070 if (!emulate_grp3(ctxt, ops))
3071 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003072 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003073 case 0xf8: /* clc */
3074 ctxt->eflags &= ~EFLG_CF;
3075 c->dst.type = OP_NONE; /* Disable writeback. */
3076 break;
3077 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003078 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003079 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003080 goto done;
3081 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003082 ctxt->eflags &= ~X86_EFLAGS_IF;
3083 c->dst.type = OP_NONE; /* Disable writeback. */
3084 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003085 break;
3086 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003087 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003088 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003089 goto done;
3090 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003091 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003092 ctxt->eflags |= X86_EFLAGS_IF;
3093 c->dst.type = OP_NONE; /* Disable writeback. */
3094 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003095 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003096 case 0xfc: /* cld */
3097 ctxt->eflags &= ~EFLG_DF;
3098 c->dst.type = OP_NONE; /* Disable writeback. */
3099 break;
3100 case 0xfd: /* std */
3101 ctxt->eflags |= EFLG_DF;
3102 c->dst.type = OP_NONE; /* Disable writeback. */
3103 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003104 case 0xfe: /* Grp4 */
3105 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003107 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 goto done;
3109 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003110 case 0xff: /* Grp5 */
3111 if (c->modrm_reg == 5)
3112 goto jump_far;
3113 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003114 default:
3115 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003117
3118writeback:
3119 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003120 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003121 goto done;
3122
Gleb Natapov5cd21912010-03-18 15:20:26 +02003123 /*
3124 * restore dst type in case the decoding will be reused
3125 * (happens for string instruction )
3126 */
3127 c->dst.type = saved_dst_type;
3128
Gleb Natapova682e352010-03-18 15:20:21 +02003129 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003130 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3131 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003132
3133 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003134 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3135 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003136
Gleb Natapov5cd21912010-03-18 15:20:26 +02003137 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003138 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003139 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003140 /*
3141 * Re-enter guest when pio read ahead buffer is empty or,
3142 * if it is not used, after each 1024 iteration.
3143 */
3144 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3145 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003146 ctxt->restart = false;
3147 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003148 /*
3149 * reset read cache here in case string instruction is restared
3150 * without decoding
3151 */
3152 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003153 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003154
3155done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003156 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157
3158twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003159 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003161 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 u16 size;
3163 unsigned long address;
3164
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003165 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003166 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003167 goto cannot_emulate;
3168
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003169 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003170 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003171 goto done;
3172
Avi Kivity33e38852008-05-21 15:34:25 +03003173 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003174 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003175 /* Disable writeback. */
3176 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003177 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003179 rc = read_descriptor(ctxt, ops, c->src.ptr,
3180 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003181 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 goto done;
3183 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003184 /* Disable writeback. */
3185 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003187 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003188 if (c->modrm_mod == 3) {
3189 switch (c->modrm_rm) {
3190 case 1:
3191 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003192 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003193 goto done;
3194 break;
3195 default:
3196 goto cannot_emulate;
3197 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003198 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003199 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003200 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003201 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003202 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003203 goto done;
3204 realmode_lidt(ctxt->vcpu, size, address);
3205 }
Avi Kivity16286d02008-04-14 14:40:50 +03003206 /* Disable writeback. */
3207 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
3209 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003210 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003211 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
3213 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003214 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3215 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003216 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003218 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003219 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003220 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003222 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003223 /* Disable writeback. */
3224 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 break;
3226 default:
3227 goto cannot_emulate;
3228 }
3229 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003230 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003231 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003232 if (rc != X86EMUL_CONTINUE)
3233 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003234 else
3235 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003236 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003237 case 0x06:
3238 emulate_clts(ctxt->vcpu);
3239 c->dst.type = OP_NONE;
3240 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003241 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003242 kvm_emulate_wbinvd(ctxt->vcpu);
3243 c->dst.type = OP_NONE;
3244 break;
3245 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003246 case 0x0d: /* GrpP (prefetch) */
3247 case 0x18: /* Grp16 (prefetch/nop) */
3248 c->dst.type = OP_NONE;
3249 break;
3250 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003251 switch (c->modrm_reg) {
3252 case 1:
3253 case 5 ... 7:
3254 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003255 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003256 goto done;
3257 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003258 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003259 c->dst.type = OP_NONE; /* no writeback */
3260 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003262 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3263 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003264 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003265 goto done;
3266 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003267 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003268 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003270 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003271 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003272 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003273 goto done;
3274 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003275 c->dst.type = OP_NONE;
3276 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003278 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3279 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003280 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003281 goto done;
3282 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003283
Gleb Natapov338dbc92010-04-28 19:15:32 +03003284 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3285 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3286 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3287 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003288 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003289 goto done;
3290 }
3291
Laurent Viviera01af5e2007-09-24 11:10:56 +02003292 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003294 case 0x30:
3295 /* wrmsr */
3296 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3297 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003298 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003299 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003300 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003301 }
3302 rc = X86EMUL_CONTINUE;
3303 c->dst.type = OP_NONE;
3304 break;
3305 case 0x32:
3306 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003307 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003308 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003309 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003310 } else {
3311 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3312 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3313 }
3314 rc = X86EMUL_CONTINUE;
3315 c->dst.type = OP_NONE;
3316 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003317 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003318 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003319 if (rc != X86EMUL_CONTINUE)
3320 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003321 else
3322 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003323 break;
3324 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003325 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003326 if (rc != X86EMUL_CONTINUE)
3327 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003328 else
3329 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003330 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003332 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003333 if (!test_cc(c->b, ctxt->eflags))
3334 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003336 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003337 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003338 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003339 c->dst.type = OP_NONE;
3340 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003341 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003342 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003343 break;
3344 case 0xa1: /* pop fs */
3345 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003346 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003347 goto done;
3348 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003349 case 0xa3:
3350 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003351 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003352 /* only subword offset */
3353 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003354 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003355 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003356 case 0xa4: /* shld imm8, r, r/m */
3357 case 0xa5: /* shld cl, r, r/m */
3358 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3359 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003360 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003361 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003362 break;
3363 case 0xa9: /* pop gs */
3364 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003365 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003366 goto done;
3367 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003368 case 0xab:
3369 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003370 /* only subword offset */
3371 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003372 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003373 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003374 case 0xac: /* shrd imm8, r, r/m */
3375 case 0xad: /* shrd cl, r, r/m */
3376 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3377 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003378 case 0xae: /* clflush */
3379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 case 0xb0 ... 0xb1: /* cmpxchg */
3381 /*
3382 * Save real source value, then compare EAX against
3383 * destination.
3384 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003385 c->src.orig_val = c->src.val;
3386 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003387 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3388 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003390 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 } else {
3392 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003393 c->dst.type = OP_REG;
3394 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 }
3396 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 case 0xb3:
3398 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003399 /* only subword offset */
3400 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003401 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003404 c->dst.bytes = c->op_bytes;
3405 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3406 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003409 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410 case 0:
3411 goto bt;
3412 case 1:
3413 goto bts;
3414 case 2:
3415 goto btr;
3416 case 3:
3417 goto btc;
3418 }
3419 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003420 case 0xbb:
3421 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003422 /* only subword offset */
3423 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003424 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003425 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003427 c->dst.bytes = c->op_bytes;
3428 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3429 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003431 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003432 c->dst.bytes = c->op_bytes;
3433 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3434 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003435 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003437 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003438 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003439 goto done;
3440 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003441 default:
3442 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 }
3444 goto writeback;
3445
3446cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003447 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448 return -1;
3449}