blob: 702bffffd27fbebedcb096639e6a4fc5b7ca32f1 [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
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020036#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038/*
39 * Opcode effective-address decode tables.
40 * Note that we only emulate instructions that have at least one memory
41 * operand (excluding implicit stack references). We assume that stack
42 * references and instruction fetches will never occur in special memory
43 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
44 * not be handled.
45 */
46
47/* Operand sizes: 8-bit operands or specified/overridden size. */
48#define ByteOp (1<<0) /* 8-bit operands. */
49/* Destination operand type. */
50#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
51#define DstReg (2<<1) /* Register operand. */
52#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define DstAcc (4<<1) /* Destination Accumulator */
54#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080055/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020056#define SrcNone (0<<4) /* No source operand. */
57#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
58#define SrcReg (1<<4) /* Register operand. */
59#define SrcMem (2<<4) /* Memory operand. */
60#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
61#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
62#define SrcImm (5<<4) /* Immediate operand. */
63#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010064#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030065#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030066#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
78#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030079/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020080#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020081#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030082#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010083/* Source 2 operand type */
84#define Src2None (0<<29)
85#define Src2CL (1<<29)
86#define Src2ImmByte (2<<29)
87#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030088#define Src2Imm16 (4<<29)
Gleb Natapove35b7b92010-02-25 16:36:42 +020089#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
90 in memory and second argument is located
91 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010092#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093
Avi Kivity43bb19c2008-01-18 12:46:50 +020094enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020095 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020096 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020097 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020098};
99
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100100static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200102 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300104 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300105 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200107 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200109 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
110 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200112 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300114 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300115 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200117 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300119 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300120 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200122 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200124 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200126 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
128 0, 0, 0, 0,
129 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200130 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
132 0, 0, 0, 0,
133 /* 0x38 - 0x3F */
134 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
135 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200136 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
137 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700138 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200139 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700140 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200141 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300142 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200143 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
144 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300145 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200146 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
147 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700148 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200149 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
150 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700151 0, 0, 0, 0,
152 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300153 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300154 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
155 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300156 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
158 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300159 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
161 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200163 Group | Group1_80, Group | Group1_81,
164 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200166 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 /* 0x88 - 0x8F */
168 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
169 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200170 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200171 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300172 /* 0x90 - 0x97 */
173 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
174 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300175 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300176 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200178 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
179 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200180 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
181 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200183 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
184 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
185 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300186 /* 0xB0 - 0xB7 */
187 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
188 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
191 /* 0xB8 - 0xBF */
192 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
193 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
195 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300197 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200198 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300199 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300201 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300202 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xD0 - 0xD7 */
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
206 0, 0, 0, 0,
207 /* 0xD8 - 0xDF */
208 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300209 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300210 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300211 ByteOp | SrcImmUByte, SrcImmUByte,
212 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300213 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300214 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300215 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300216 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
217 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0xF0 - 0xF7 */
219 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200220 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700222 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300223 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224};
225
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100226static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200228 0, Group | GroupDual | Group7, 0, 0,
229 0, ImplicitOps, ImplicitOps | Priv, 0,
230 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
231 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0x10 - 0x1F */
233 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
234 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 ModRM | ImplicitOps | Priv, ModRM | Priv,
237 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200240 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
241 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200242 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0x40 - 0x47 */
244 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 /* 0x48 - 0x4F */
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
253 /* 0x50 - 0x5F */
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 /* 0x60 - 0x6F */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 /* 0x70 - 0x7F */
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300260 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
261 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0x90 - 0x9F */
263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300265 ImplicitOps | Stack, ImplicitOps | Stack,
266 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100267 DstMem | SrcReg | Src2ImmByte | ModRM,
268 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300270 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200271 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100272 DstMem | SrcReg | Src2ImmByte | ModRM,
273 DstMem | SrcReg | Src2CL | ModRM,
274 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200276 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
277 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
279 DstReg | SrcMem16 | ModRM | Mov,
280 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200281 0, 0,
282 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
284 DstReg | SrcMem16 | ModRM | Mov,
285 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200286 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
287 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800288 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 /* 0xD0 - 0xDF */
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291 /* 0xE0 - 0xEF */
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293 /* 0xF0 - 0xFF */
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
295};
296
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100297static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200298 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200299 ByteOp | DstMem | SrcImm | ModRM | Lock,
300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM | Lock,
306 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200307 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM | Lock,
315 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200316 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200317 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
318 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
324 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200325 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM | Lock,
333 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200334 [Group1A*8] =
335 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200336 [Group3_Byte*8] =
337 ByteOp | SrcImm | DstMem | ModRM, 0,
338 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
339 0, 0, 0, 0,
340 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400341 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300342 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200343 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200344 [Group4*8] =
345 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
346 0, 0, 0, 0, 0, 0,
347 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300348 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
349 SrcMem | ModRM | Stack, 0,
Gleb Natapovea798492010-02-25 16:36:43 +0200350 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
351 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200352 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200353 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300354 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200355 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200356 [Group8*8] =
357 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200358 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
359 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200360 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200361 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200362};
363
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100364static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200365 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200366 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300367 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200368 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200369 [Group9*8] =
370 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200371};
372
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200374#define EFLG_ID (1<<21)
375#define EFLG_VIP (1<<20)
376#define EFLG_VIF (1<<19)
377#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200378#define EFLG_VM (1<<17)
379#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200380#define EFLG_IOPL (3<<12)
381#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382#define EFLG_OF (1<<11)
383#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200384#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200385#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386#define EFLG_SF (1<<7)
387#define EFLG_ZF (1<<6)
388#define EFLG_AF (1<<4)
389#define EFLG_PF (1<<2)
390#define EFLG_CF (1<<0)
391
392/*
393 * Instruction emulation:
394 * Most instructions are emulated directly via a fragment of inline assembly
395 * code. This allows us to save/restore EFLAGS and thus very easily pick up
396 * any modified flags.
397 */
398
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800399#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400#define _LO32 "k" /* force 32-bit operand */
401#define _STK "%%rsp" /* stack pointer */
402#elif defined(__i386__)
403#define _LO32 "" /* force 32-bit operand */
404#define _STK "%%esp" /* stack pointer */
405#endif
406
407/*
408 * These EFLAGS bits are restored from saved value during emulation, and
409 * any changes are written back to the saved value after emulation.
410 */
411#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
412
413/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200414#define _PRE_EFLAGS(_sav, _msk, _tmp) \
415 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
416 "movl %"_sav",%"_LO32 _tmp"; " \
417 "push %"_tmp"; " \
418 "push %"_tmp"; " \
419 "movl %"_msk",%"_LO32 _tmp"; " \
420 "andl %"_LO32 _tmp",("_STK"); " \
421 "pushf; " \
422 "notl %"_LO32 _tmp"; " \
423 "andl %"_LO32 _tmp",("_STK"); " \
424 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
425 "pop %"_tmp"; " \
426 "orl %"_LO32 _tmp",("_STK"); " \
427 "popf; " \
428 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
430/* After executing instruction: write-back necessary bits in EFLAGS. */
431#define _POST_EFLAGS(_sav, _msk, _tmp) \
432 /* _sav |= EFLAGS & _msk; */ \
433 "pushf; " \
434 "pop %"_tmp"; " \
435 "andl %"_msk",%"_LO32 _tmp"; " \
436 "orl %"_LO32 _tmp",%"_sav"; "
437
Avi Kivitydda96d82008-11-26 15:14:10 +0200438#ifdef CONFIG_X86_64
439#define ON64(x) x
440#else
441#define ON64(x)
442#endif
443
Avi Kivity6b7ad612008-11-26 15:30:45 +0200444#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
445 do { \
446 __asm__ __volatile__ ( \
447 _PRE_EFLAGS("0", "4", "2") \
448 _op _suffix " %"_x"3,%1; " \
449 _POST_EFLAGS("0", "4", "2") \
450 : "=m" (_eflags), "=m" ((_dst).val), \
451 "=&r" (_tmp) \
452 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200453 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200454
455
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456/* Raw emulation: instruction has two explicit operands. */
457#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200458 do { \
459 unsigned long _tmp; \
460 \
461 switch ((_dst).bytes) { \
462 case 2: \
463 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
464 break; \
465 case 4: \
466 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
467 break; \
468 case 8: \
469 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
470 break; \
471 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 } while (0)
473
474#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
475 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200476 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400477 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200479 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 break; \
481 default: \
482 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
483 _wx, _wy, _lx, _ly, _qx, _qy); \
484 break; \
485 } \
486 } while (0)
487
488/* Source operand is byte-sized and may be restricted to just %cl. */
489#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
490 __emulate_2op(_op, _src, _dst, _eflags, \
491 "b", "c", "b", "c", "b", "c", "b", "c")
492
493/* Source operand is byte, word, long or quad sized. */
494#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
495 __emulate_2op(_op, _src, _dst, _eflags, \
496 "b", "q", "w", "r", _LO32, "r", "", "r")
497
498/* Source operand is word, long or quad sized. */
499#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
500 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
501 "w", "r", _LO32, "r", "", "r")
502
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100503/* Instruction has three operands and one operand is stored in ECX register */
504#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
505 do { \
506 unsigned long _tmp; \
507 _type _clv = (_cl).val; \
508 _type _srcv = (_src).val; \
509 _type _dstv = (_dst).val; \
510 \
511 __asm__ __volatile__ ( \
512 _PRE_EFLAGS("0", "5", "2") \
513 _op _suffix " %4,%1 \n" \
514 _POST_EFLAGS("0", "5", "2") \
515 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
516 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
517 ); \
518 \
519 (_cl).val = (unsigned long) _clv; \
520 (_src).val = (unsigned long) _srcv; \
521 (_dst).val = (unsigned long) _dstv; \
522 } while (0)
523
524#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
525 do { \
526 switch ((_dst).bytes) { \
527 case 2: \
528 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
529 "w", unsigned short); \
530 break; \
531 case 4: \
532 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
533 "l", unsigned int); \
534 break; \
535 case 8: \
536 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
537 "q", unsigned long)); \
538 break; \
539 } \
540 } while (0)
541
Avi Kivitydda96d82008-11-26 15:14:10 +0200542#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 do { \
544 unsigned long _tmp; \
545 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200546 __asm__ __volatile__ ( \
547 _PRE_EFLAGS("0", "3", "2") \
548 _op _suffix " %1; " \
549 _POST_EFLAGS("0", "3", "2") \
550 : "=m" (_eflags), "+m" ((_dst).val), \
551 "=&r" (_tmp) \
552 : "i" (EFLAGS_MASK)); \
553 } while (0)
554
555/* Instruction has only one explicit operand (no source operand). */
556#define emulate_1op(_op, _dst, _eflags) \
557 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400558 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200559 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
560 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
561 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
562 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 } \
564 } while (0)
565
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566/* Fetch next part of the instruction being emulated. */
567#define insn_fetch(_type, _size, _eip) \
568({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200569 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200570 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 goto done; \
572 (_eip) += (_size); \
573 (_type)_x; \
574})
575
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800576static inline unsigned long ad_mask(struct decode_cache *c)
577{
578 return (1UL << (c->ad_bytes << 3)) - 1;
579}
580
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800582static inline unsigned long
583address_mask(struct decode_cache *c, unsigned long reg)
584{
585 if (c->ad_bytes == sizeof(unsigned long))
586 return reg;
587 else
588 return reg & ad_mask(c);
589}
590
591static inline unsigned long
592register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
593{
594 return base + address_mask(c, reg);
595}
596
Harvey Harrison7a9572752008-02-19 07:40:41 -0800597static inline void
598register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
599{
600 if (c->ad_bytes == sizeof(unsigned long))
601 *reg += inc;
602 else
603 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
604}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605
Harvey Harrison7a9572752008-02-19 07:40:41 -0800606static inline void jmp_rel(struct decode_cache *c, int rel)
607{
608 register_address_increment(c, &c->eip, rel);
609}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300610
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300611static void set_seg_override(struct decode_cache *c, int seg)
612{
613 c->has_seg_override = true;
614 c->seg_override = seg;
615}
616
617static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
618{
619 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
620 return 0;
621
622 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
623}
624
625static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
626 struct decode_cache *c)
627{
628 if (!c->has_seg_override)
629 return 0;
630
631 return seg_base(ctxt, c->seg_override);
632}
633
634static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
635{
636 return seg_base(ctxt, VCPU_SREG_ES);
637}
638
639static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
640{
641 return seg_base(ctxt, VCPU_SREG_SS);
642}
643
Avi Kivity62266862007-11-20 13:15:52 +0200644static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
645 struct x86_emulate_ops *ops,
646 unsigned long linear, u8 *dest)
647{
648 struct fetch_cache *fc = &ctxt->decode.fetch;
649 int rc;
650 int size;
651
652 if (linear < fc->start || linear >= fc->end) {
653 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200654 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900655 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200656 return rc;
657 fc->start = linear;
658 fc->end = linear + size;
659 }
660 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900661 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200662}
663
664static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
665 struct x86_emulate_ops *ops,
666 unsigned long eip, void *dest, unsigned size)
667{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900668 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200669
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200670 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200671 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200673 eip += ctxt->cs_base;
674 while (size--) {
675 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900676 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200677 return rc;
678 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900679 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200680}
681
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000682/*
683 * Given the 'reg' portion of a ModRM byte, and a register block, return a
684 * pointer into the block that addresses the relevant register.
685 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
686 */
687static void *decode_register(u8 modrm_reg, unsigned long *regs,
688 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689{
690 void *p;
691
692 p = &regs[modrm_reg];
693 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
694 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
695 return p;
696}
697
698static int read_descriptor(struct x86_emulate_ctxt *ctxt,
699 struct x86_emulate_ops *ops,
700 void *ptr,
701 u16 *size, unsigned long *address, int op_bytes)
702{
703 int rc;
704
705 if (op_bytes == 2)
706 op_bytes = 3;
707 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300708 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200709 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900710 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300712 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200713 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 return rc;
715}
716
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300717static int test_cc(unsigned int condition, unsigned int flags)
718{
719 int rc = 0;
720
721 switch ((condition & 15) >> 1) {
722 case 0: /* o */
723 rc |= (flags & EFLG_OF);
724 break;
725 case 1: /* b/c/nae */
726 rc |= (flags & EFLG_CF);
727 break;
728 case 2: /* z/e */
729 rc |= (flags & EFLG_ZF);
730 break;
731 case 3: /* be/na */
732 rc |= (flags & (EFLG_CF|EFLG_ZF));
733 break;
734 case 4: /* s */
735 rc |= (flags & EFLG_SF);
736 break;
737 case 5: /* p/pe */
738 rc |= (flags & EFLG_PF);
739 break;
740 case 7: /* le/ng */
741 rc |= (flags & EFLG_ZF);
742 /* fall through */
743 case 6: /* l/nge */
744 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
745 break;
746 }
747
748 /* Odd condition identifiers (lsb == 1) have inverted sense. */
749 return (!!rc ^ (condition & 1));
750}
751
Avi Kivity3c118e22007-10-31 10:27:04 +0200752static void decode_register_operand(struct operand *op,
753 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200754 int inhibit_bytereg)
755{
Avi Kivity33615aa2007-10-31 11:15:56 +0200756 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200757 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200758
759 if (!(c->d & ModRM))
760 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200761 op->type = OP_REG;
762 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200763 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200764 op->val = *(u8 *)op->ptr;
765 op->bytes = 1;
766 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200767 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200768 op->bytes = c->op_bytes;
769 switch (op->bytes) {
770 case 2:
771 op->val = *(u16 *)op->ptr;
772 break;
773 case 4:
774 op->val = *(u32 *)op->ptr;
775 break;
776 case 8:
777 op->val = *(u64 *) op->ptr;
778 break;
779 }
780 }
781 op->orig_val = op->val;
782}
783
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784static int decode_modrm(struct x86_emulate_ctxt *ctxt,
785 struct x86_emulate_ops *ops)
786{
787 struct decode_cache *c = &ctxt->decode;
788 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700789 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900790 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791
792 if (c->rex_prefix) {
793 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
794 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
795 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
796 }
797
798 c->modrm = insn_fetch(u8, 1, c->eip);
799 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
800 c->modrm_reg |= (c->modrm & 0x38) >> 3;
801 c->modrm_rm |= (c->modrm & 0x07);
802 c->modrm_ea = 0;
803 c->use_modrm_ea = 1;
804
805 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300806 c->modrm_ptr = decode_register(c->modrm_rm,
807 c->regs, c->d & ByteOp);
808 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 return rc;
810 }
811
812 if (c->ad_bytes == 2) {
813 unsigned bx = c->regs[VCPU_REGS_RBX];
814 unsigned bp = c->regs[VCPU_REGS_RBP];
815 unsigned si = c->regs[VCPU_REGS_RSI];
816 unsigned di = c->regs[VCPU_REGS_RDI];
817
818 /* 16-bit ModR/M decode. */
819 switch (c->modrm_mod) {
820 case 0:
821 if (c->modrm_rm == 6)
822 c->modrm_ea += insn_fetch(u16, 2, c->eip);
823 break;
824 case 1:
825 c->modrm_ea += insn_fetch(s8, 1, c->eip);
826 break;
827 case 2:
828 c->modrm_ea += insn_fetch(u16, 2, c->eip);
829 break;
830 }
831 switch (c->modrm_rm) {
832 case 0:
833 c->modrm_ea += bx + si;
834 break;
835 case 1:
836 c->modrm_ea += bx + di;
837 break;
838 case 2:
839 c->modrm_ea += bp + si;
840 break;
841 case 3:
842 c->modrm_ea += bp + di;
843 break;
844 case 4:
845 c->modrm_ea += si;
846 break;
847 case 5:
848 c->modrm_ea += di;
849 break;
850 case 6:
851 if (c->modrm_mod != 0)
852 c->modrm_ea += bp;
853 break;
854 case 7:
855 c->modrm_ea += bx;
856 break;
857 }
858 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
859 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300860 if (!c->has_seg_override)
861 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200862 c->modrm_ea = (u16)c->modrm_ea;
863 } else {
864 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700865 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200866 sib = insn_fetch(u8, 1, c->eip);
867 index_reg |= (sib >> 3) & 7;
868 base_reg |= sib & 7;
869 scale = sib >> 6;
870
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700871 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
872 c->modrm_ea += insn_fetch(s32, 4, c->eip);
873 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700875 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700877 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
878 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700879 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700880 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200882 switch (c->modrm_mod) {
883 case 0:
884 if (c->modrm_rm == 5)
885 c->modrm_ea += insn_fetch(s32, 4, c->eip);
886 break;
887 case 1:
888 c->modrm_ea += insn_fetch(s8, 1, c->eip);
889 break;
890 case 2:
891 c->modrm_ea += insn_fetch(s32, 4, c->eip);
892 break;
893 }
894 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200895done:
896 return rc;
897}
898
899static int decode_abs(struct x86_emulate_ctxt *ctxt,
900 struct x86_emulate_ops *ops)
901{
902 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900903 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200904
905 switch (c->ad_bytes) {
906 case 2:
907 c->modrm_ea = insn_fetch(u16, 2, c->eip);
908 break;
909 case 4:
910 c->modrm_ea = insn_fetch(u32, 4, c->eip);
911 break;
912 case 8:
913 c->modrm_ea = insn_fetch(u64, 8, c->eip);
914 break;
915 }
916done:
917 return rc;
918}
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200921x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200923 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900924 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200926 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200931 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300932 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800933 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 switch (mode) {
936 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200937 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200939 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 break;
941 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200942 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800944#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200946 def_op_bytes = 4;
947 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 break;
949#endif
950 default:
951 return -1;
952 }
953
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200954 c->op_bytes = def_op_bytes;
955 c->ad_bytes = def_ad_bytes;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200958 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200959 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200961 /* switch between 2/4 bytes */
962 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 break;
964 case 0x67: /* address-size override */
965 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200966 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200969 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200970 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300973 case 0x2e: /* CS override */
974 case 0x36: /* SS override */
975 case 0x3e: /* DS override */
976 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 break;
978 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300980 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200982 case 0x40 ... 0x4f: /* REX */
983 if (mode != X86EMUL_MODE_PROT64)
984 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200985 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200986 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200988 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200990 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100991 c->rep_prefix = REPNE_PREFIX;
992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100994 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 default:
997 goto done_prefixes;
998 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200999
1000 /* Any legacy prefix after a REX prefix nullifies its effect. */
1001
Avi Kivity33615aa2007-10-31 11:15:56 +02001002 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 }
1004
1005done_prefixes:
1006
1007 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001009 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->d = opcode_table[c->b];
1014 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 if (c->b == 0x0f) {
1017 c->twobyte = 1;
1018 c->b = insn_fetch(u8, 1, c->eip);
1019 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 if (c->d & Group) {
1024 group = c->d & GroupMask;
1025 c->modrm = insn_fetch(u8, 1, c->eip);
1026 --c->eip;
1027
1028 group = (group << 3) + ((c->modrm >> 3) & 7);
1029 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1030 c->d = group2_table[group];
1031 else
1032 c->d = group_table[group];
1033 }
1034
1035 /* Unrecognised? */
1036 if (c->d == 0) {
1037 DPRINTF("Cannot emulate %02x\n", c->b);
1038 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 }
1040
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001041 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1042 c->op_bytes = 8;
1043
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 if (c->d & ModRM)
1046 rc = decode_modrm(ctxt, ops);
1047 else if (c->d & MemAbs)
1048 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001049 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001050 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001052 if (!c->has_seg_override)
1053 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001054
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001055 if (!(!c->twobyte && c->b == 0x8d))
1056 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001057
1058 if (c->ad_bytes != 8)
1059 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 /*
1061 * Decode and fetch the source operand: register, memory
1062 * or immediate.
1063 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001064 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 case SrcNone:
1066 break;
1067 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001068 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 break;
1070 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001071 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 goto srcmem_common;
1073 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001074 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 goto srcmem_common;
1076 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.bytes = (c->d & ByteOp) ? 1 :
1078 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001079 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001080 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001081 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001082 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001083 /*
1084 * For instructions with a ModR/M byte, switch to register
1085 * access if Mod = 3.
1086 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001087 if ((c->d & ModRM) && c->modrm_mod == 3) {
1088 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001089 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001090 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001091 break;
1092 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 break;
1095 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001096 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001097 c->src.type = OP_IMM;
1098 c->src.ptr = (unsigned long *)c->eip;
1099 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1100 if (c->src.bytes == 8)
1101 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001105 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 break;
1107 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001108 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 break;
1110 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001114 if ((c->d & SrcMask) == SrcImmU) {
1115 switch (c->src.bytes) {
1116 case 1:
1117 c->src.val &= 0xff;
1118 break;
1119 case 2:
1120 c->src.val &= 0xffff;
1121 break;
1122 case 4:
1123 c->src.val &= 0xffffffff;
1124 break;
1125 }
1126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 break;
1128 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001129 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 c->src.type = OP_IMM;
1131 c->src.ptr = (unsigned long *)c->eip;
1132 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001133 if ((c->d & SrcMask) == SrcImmByte)
1134 c->src.val = insn_fetch(s8, 1, c->eip);
1135 else
1136 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001138 case SrcOne:
1139 c->src.bytes = 1;
1140 c->src.val = 1;
1141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 }
1143
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001144 /*
1145 * Decode and fetch the second source operand: register, memory
1146 * or immediate.
1147 */
1148 switch (c->d & Src2Mask) {
1149 case Src2None:
1150 break;
1151 case Src2CL:
1152 c->src2.bytes = 1;
1153 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1154 break;
1155 case Src2ImmByte:
1156 c->src2.type = OP_IMM;
1157 c->src2.ptr = (unsigned long *)c->eip;
1158 c->src2.bytes = 1;
1159 c->src2.val = insn_fetch(u8, 1, c->eip);
1160 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001161 case Src2Imm16:
1162 c->src2.type = OP_IMM;
1163 c->src2.ptr = (unsigned long *)c->eip;
1164 c->src2.bytes = 2;
1165 c->src2.val = insn_fetch(u16, 2, c->eip);
1166 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001167 case Src2One:
1168 c->src2.bytes = 1;
1169 c->src2.val = 1;
1170 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001171 case Src2Mem16:
1172 c->src2.bytes = 2;
1173 c->src2.type = OP_MEM;
1174 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001175 }
1176
Avi Kivity038e51d2007-01-22 20:40:40 -08001177 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001178 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001179 case ImplicitOps:
1180 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001181 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001182 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001183 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001184 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001185 break;
1186 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001187 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001188 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001189 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001190 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001191 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001192 break;
1193 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001194 c->dst.type = OP_MEM;
1195 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001196 case DstAcc:
1197 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001198 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001199 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001200 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001201 case 1:
1202 c->dst.val = *(u8 *)c->dst.ptr;
1203 break;
1204 case 2:
1205 c->dst.val = *(u16 *)c->dst.ptr;
1206 break;
1207 case 4:
1208 c->dst.val = *(u32 *)c->dst.ptr;
1209 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001210 case 8:
1211 c->dst.val = *(u64 *)c->dst.ptr;
1212 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001213 }
1214 c->dst.orig_val = c->dst.val;
1215 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001216 }
1217
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001218 if (c->rip_relative)
1219 c->modrm_ea += c->eip;
1220
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001221done:
1222 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1223}
1224
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001225static u32 desc_limit_scaled(struct desc_struct *desc)
1226{
1227 u32 limit = get_desc_limit(desc);
1228
1229 return desc->g ? (limit << 12) | 0xfff : limit;
1230}
1231
1232static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1233 struct x86_emulate_ops *ops,
1234 u16 selector, struct desc_ptr *dt)
1235{
1236 if (selector & 1 << 2) {
1237 struct desc_struct desc;
1238 memset (dt, 0, sizeof *dt);
1239 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1240 return;
1241
1242 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1243 dt->address = get_desc_base(&desc);
1244 } else
1245 ops->get_gdt(dt, ctxt->vcpu);
1246}
1247
1248/* allowed just for 8 bytes segments */
1249static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1250 struct x86_emulate_ops *ops,
1251 u16 selector, struct desc_struct *desc)
1252{
1253 struct desc_ptr dt;
1254 u16 index = selector >> 3;
1255 int ret;
1256 u32 err;
1257 ulong addr;
1258
1259 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1260
1261 if (dt.size < index * 8 + 7) {
1262 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1263 return X86EMUL_PROPAGATE_FAULT;
1264 }
1265 addr = dt.address + index * 8;
1266 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1267 if (ret == X86EMUL_PROPAGATE_FAULT)
1268 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1269
1270 return ret;
1271}
1272
1273/* allowed just for 8 bytes segments */
1274static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1275 struct x86_emulate_ops *ops,
1276 u16 selector, struct desc_struct *desc)
1277{
1278 struct desc_ptr dt;
1279 u16 index = selector >> 3;
1280 u32 err;
1281 ulong addr;
1282 int ret;
1283
1284 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1285
1286 if (dt.size < index * 8 + 7) {
1287 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1288 return X86EMUL_PROPAGATE_FAULT;
1289 }
1290
1291 addr = dt.address + index * 8;
1292 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1293 if (ret == X86EMUL_PROPAGATE_FAULT)
1294 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1295
1296 return ret;
1297}
1298
1299static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1300 struct x86_emulate_ops *ops,
1301 u16 selector, int seg)
1302{
1303 struct desc_struct seg_desc;
1304 u8 dpl, rpl, cpl;
1305 unsigned err_vec = GP_VECTOR;
1306 u32 err_code = 0;
1307 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1308 int ret;
1309
1310 memset(&seg_desc, 0, sizeof seg_desc);
1311
1312 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1313 || ctxt->mode == X86EMUL_MODE_REAL) {
1314 /* set real mode segment descriptor */
1315 set_desc_base(&seg_desc, selector << 4);
1316 set_desc_limit(&seg_desc, 0xffff);
1317 seg_desc.type = 3;
1318 seg_desc.p = 1;
1319 seg_desc.s = 1;
1320 goto load;
1321 }
1322
1323 /* NULL selector is not valid for TR, CS and SS */
1324 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1325 && null_selector)
1326 goto exception;
1327
1328 /* TR should be in GDT only */
1329 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1330 goto exception;
1331
1332 if (null_selector) /* for NULL selector skip all following checks */
1333 goto load;
1334
1335 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1336 if (ret != X86EMUL_CONTINUE)
1337 return ret;
1338
1339 err_code = selector & 0xfffc;
1340 err_vec = GP_VECTOR;
1341
1342 /* can't load system descriptor into segment selecor */
1343 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1344 goto exception;
1345
1346 if (!seg_desc.p) {
1347 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1348 goto exception;
1349 }
1350
1351 rpl = selector & 3;
1352 dpl = seg_desc.dpl;
1353 cpl = ops->cpl(ctxt->vcpu);
1354
1355 switch (seg) {
1356 case VCPU_SREG_SS:
1357 /*
1358 * segment is not a writable data segment or segment
1359 * selector's RPL != CPL or segment selector's RPL != CPL
1360 */
1361 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1362 goto exception;
1363 break;
1364 case VCPU_SREG_CS:
1365 if (!(seg_desc.type & 8))
1366 goto exception;
1367
1368 if (seg_desc.type & 4) {
1369 /* conforming */
1370 if (dpl > cpl)
1371 goto exception;
1372 } else {
1373 /* nonconforming */
1374 if (rpl > cpl || dpl != cpl)
1375 goto exception;
1376 }
1377 /* CS(RPL) <- CPL */
1378 selector = (selector & 0xfffc) | cpl;
1379 break;
1380 case VCPU_SREG_TR:
1381 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1382 goto exception;
1383 break;
1384 case VCPU_SREG_LDTR:
1385 if (seg_desc.s || seg_desc.type != 2)
1386 goto exception;
1387 break;
1388 default: /* DS, ES, FS, or GS */
1389 /*
1390 * segment is not a data or readable code segment or
1391 * ((segment is a data or nonconforming code segment)
1392 * and (both RPL and CPL > DPL))
1393 */
1394 if ((seg_desc.type & 0xa) == 0x8 ||
1395 (((seg_desc.type & 0xc) != 0xc) &&
1396 (rpl > dpl && cpl > dpl)))
1397 goto exception;
1398 break;
1399 }
1400
1401 if (seg_desc.s) {
1402 /* mark segment as accessed */
1403 seg_desc.type |= 1;
1404 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1405 if (ret != X86EMUL_CONTINUE)
1406 return ret;
1407 }
1408load:
1409 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1410 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1411 return X86EMUL_CONTINUE;
1412exception:
1413 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1414 return X86EMUL_PROPAGATE_FAULT;
1415}
1416
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001417static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1418{
1419 struct decode_cache *c = &ctxt->decode;
1420
1421 c->dst.type = OP_MEM;
1422 c->dst.bytes = c->op_bytes;
1423 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001424 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001425 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001426 c->regs[VCPU_REGS_RSP]);
1427}
1428
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001429static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001430 struct x86_emulate_ops *ops,
1431 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001432{
1433 struct decode_cache *c = &ctxt->decode;
1434 int rc;
1435
Avi Kivity781d0ed2008-11-27 18:00:28 +02001436 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1437 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001438 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001439 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 return rc;
1441
Avi Kivity350f69d2009-01-05 11:12:40 +02001442 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001443 return rc;
1444}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001446static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1447 struct x86_emulate_ops *ops,
1448 void *dest, int len)
1449{
1450 int rc;
1451 unsigned long val, change_mask;
1452 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001453 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001454
1455 rc = emulate_pop(ctxt, ops, &val, len);
1456 if (rc != X86EMUL_CONTINUE)
1457 return rc;
1458
1459 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1460 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1461
1462 switch(ctxt->mode) {
1463 case X86EMUL_MODE_PROT64:
1464 case X86EMUL_MODE_PROT32:
1465 case X86EMUL_MODE_PROT16:
1466 if (cpl == 0)
1467 change_mask |= EFLG_IOPL;
1468 if (cpl <= iopl)
1469 change_mask |= EFLG_IF;
1470 break;
1471 case X86EMUL_MODE_VM86:
1472 if (iopl < 3) {
1473 kvm_inject_gp(ctxt->vcpu, 0);
1474 return X86EMUL_PROPAGATE_FAULT;
1475 }
1476 change_mask |= EFLG_IF;
1477 break;
1478 default: /* real mode */
1479 change_mask |= (EFLG_IOPL | EFLG_IF);
1480 break;
1481 }
1482
1483 *(unsigned long *)dest =
1484 (ctxt->eflags & ~change_mask) | (val & change_mask);
1485
1486 return rc;
1487}
1488
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001489static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1490{
1491 struct decode_cache *c = &ctxt->decode;
1492 struct kvm_segment segment;
1493
1494 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1495
1496 c->src.val = segment.selector;
1497 emulate_push(ctxt);
1498}
1499
1500static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1501 struct x86_emulate_ops *ops, int seg)
1502{
1503 struct decode_cache *c = &ctxt->decode;
1504 unsigned long selector;
1505 int rc;
1506
1507 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001508 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001509 return rc;
1510
Gleb Natapov2e873022010-03-18 15:20:18 +02001511 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001512 return rc;
1513}
1514
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001515static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1516{
1517 struct decode_cache *c = &ctxt->decode;
1518 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1519 int reg = VCPU_REGS_RAX;
1520
1521 while (reg <= VCPU_REGS_RDI) {
1522 (reg == VCPU_REGS_RSP) ?
1523 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1524
1525 emulate_push(ctxt);
1526 ++reg;
1527 }
1528}
1529
1530static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1531 struct x86_emulate_ops *ops)
1532{
1533 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001534 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001535 int reg = VCPU_REGS_RDI;
1536
1537 while (reg >= VCPU_REGS_RAX) {
1538 if (reg == VCPU_REGS_RSP) {
1539 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1540 c->op_bytes);
1541 --reg;
1542 }
1543
1544 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001545 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001546 break;
1547 --reg;
1548 }
1549 return rc;
1550}
1551
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001552static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1553 struct x86_emulate_ops *ops)
1554{
1555 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001556
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001557 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001558}
1559
Laurent Vivier05f086f2007-09-24 11:10:55 +02001560static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001561{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001562 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001563 switch (c->modrm_reg) {
1564 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001565 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001566 break;
1567 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001568 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001569 break;
1570 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001571 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001572 break;
1573 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001574 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001575 break;
1576 case 4: /* sal/shl */
1577 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001578 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001579 break;
1580 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001581 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001582 break;
1583 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001584 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001585 break;
1586 }
1587}
1588
1589static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001590 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001591{
1592 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001593
1594 switch (c->modrm_reg) {
1595 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001596 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001597 break;
1598 case 2: /* not */
1599 c->dst.val = ~c->dst.val;
1600 break;
1601 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001602 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001603 break;
1604 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001605 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001606 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001607 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001608}
1609
1610static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001611 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001612{
1613 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614
1615 switch (c->modrm_reg) {
1616 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001617 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001618 break;
1619 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001620 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001621 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001622 case 2: /* call near abs */ {
1623 long int old_eip;
1624 old_eip = c->eip;
1625 c->eip = c->src.val;
1626 c->src.val = old_eip;
1627 emulate_push(ctxt);
1628 break;
1629 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001630 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001631 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001632 break;
1633 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001634 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001636 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001637 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638}
1639
1640static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1641 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001642 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001643{
1644 struct decode_cache *c = &ctxt->decode;
1645 u64 old, new;
1646 int rc;
1647
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001648 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001649 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 return rc;
1651
1652 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1653 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1654
1655 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1656 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001657 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001658
1659 } else {
1660 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1661 (u32) c->regs[VCPU_REGS_RBX];
1662
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001663 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001664 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001666 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001668 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001669}
1670
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001671static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1672 struct x86_emulate_ops *ops)
1673{
1674 struct decode_cache *c = &ctxt->decode;
1675 int rc;
1676 unsigned long cs;
1677
1678 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001679 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001680 return rc;
1681 if (c->op_bytes == 4)
1682 c->eip = (u32)c->eip;
1683 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001684 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001685 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001686 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001687 return rc;
1688}
1689
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690static inline int writeback(struct x86_emulate_ctxt *ctxt,
1691 struct x86_emulate_ops *ops)
1692{
1693 int rc;
1694 struct decode_cache *c = &ctxt->decode;
1695
1696 switch (c->dst.type) {
1697 case OP_REG:
1698 /* The 4-byte case *is* correct:
1699 * in 64-bit mode we zero-extend.
1700 */
1701 switch (c->dst.bytes) {
1702 case 1:
1703 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1704 break;
1705 case 2:
1706 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1707 break;
1708 case 4:
1709 *c->dst.ptr = (u32)c->dst.val;
1710 break; /* 64b: zero-ext */
1711 case 8:
1712 *c->dst.ptr = c->dst.val;
1713 break;
1714 }
1715 break;
1716 case OP_MEM:
1717 if (c->lock_prefix)
1718 rc = ops->cmpxchg_emulated(
1719 (unsigned long)c->dst.ptr,
1720 &c->dst.orig_val,
1721 &c->dst.val,
1722 c->dst.bytes,
1723 ctxt->vcpu);
1724 else
1725 rc = ops->write_emulated(
1726 (unsigned long)c->dst.ptr,
1727 &c->dst.val,
1728 c->dst.bytes,
1729 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001730 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001731 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001732 break;
1733 case OP_NONE:
1734 /* no writeback */
1735 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736 default:
1737 break;
1738 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001739 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001740}
1741
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301742static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001743{
1744 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1745 /*
1746 * an sti; sti; sequence only disable interrupts for the first
1747 * instruction. So, if the last instruction, be it emulated or
1748 * not, left the system with the INT_STI flag enabled, it
1749 * means that the last instruction is an sti. We should not
1750 * leave the flag on in this case. The same goes for mov ss
1751 */
1752 if (!(int_shadow & mask))
1753 ctxt->interruptibility = mask;
1754}
1755
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001756static inline void
1757setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1758 struct kvm_segment *cs, struct kvm_segment *ss)
1759{
1760 memset(cs, 0, sizeof(struct kvm_segment));
1761 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1762 memset(ss, 0, sizeof(struct kvm_segment));
1763
1764 cs->l = 0; /* will be adjusted later */
1765 cs->base = 0; /* flat segment */
1766 cs->g = 1; /* 4kb granularity */
1767 cs->limit = 0xffffffff; /* 4GB limit */
1768 cs->type = 0x0b; /* Read, Execute, Accessed */
1769 cs->s = 1;
1770 cs->dpl = 0; /* will be adjusted later */
1771 cs->present = 1;
1772 cs->db = 1;
1773
1774 ss->unusable = 0;
1775 ss->base = 0; /* flat segment */
1776 ss->limit = 0xffffffff; /* 4GB limit */
1777 ss->g = 1; /* 4kb granularity */
1778 ss->s = 1;
1779 ss->type = 0x03; /* Read/Write, Accessed */
1780 ss->db = 1; /* 32bit stack segment */
1781 ss->dpl = 0;
1782 ss->present = 1;
1783}
1784
1785static int
1786emulate_syscall(struct x86_emulate_ctxt *ctxt)
1787{
1788 struct decode_cache *c = &ctxt->decode;
1789 struct kvm_segment cs, ss;
1790 u64 msr_data;
1791
1792 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001793 if (ctxt->mode == X86EMUL_MODE_REAL ||
1794 ctxt->mode == X86EMUL_MODE_VM86) {
1795 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1796 return X86EMUL_PROPAGATE_FAULT;
1797 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001798
1799 setup_syscalls_segments(ctxt, &cs, &ss);
1800
1801 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1802 msr_data >>= 32;
1803 cs.selector = (u16)(msr_data & 0xfffc);
1804 ss.selector = (u16)(msr_data + 8);
1805
1806 if (is_long_mode(ctxt->vcpu)) {
1807 cs.db = 0;
1808 cs.l = 1;
1809 }
1810 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1811 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1812
1813 c->regs[VCPU_REGS_RCX] = c->eip;
1814 if (is_long_mode(ctxt->vcpu)) {
1815#ifdef CONFIG_X86_64
1816 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1817
1818 kvm_x86_ops->get_msr(ctxt->vcpu,
1819 ctxt->mode == X86EMUL_MODE_PROT64 ?
1820 MSR_LSTAR : MSR_CSTAR, &msr_data);
1821 c->eip = msr_data;
1822
1823 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1824 ctxt->eflags &= ~(msr_data | EFLG_RF);
1825#endif
1826 } else {
1827 /* legacy mode */
1828 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1829 c->eip = (u32)msr_data;
1830
1831 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1832 }
1833
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001834 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001835}
1836
Andre Przywara8c604352009-06-18 12:56:01 +02001837static int
1838emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1839{
1840 struct decode_cache *c = &ctxt->decode;
1841 struct kvm_segment cs, ss;
1842 u64 msr_data;
1843
Gleb Natapova0044752010-02-10 14:21:31 +02001844 /* inject #GP if in real mode */
1845 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001846 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001847 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001848 }
1849
1850 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1851 * Therefore, we inject an #UD.
1852 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001853 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1854 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1855 return X86EMUL_PROPAGATE_FAULT;
1856 }
Andre Przywara8c604352009-06-18 12:56:01 +02001857
1858 setup_syscalls_segments(ctxt, &cs, &ss);
1859
1860 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1861 switch (ctxt->mode) {
1862 case X86EMUL_MODE_PROT32:
1863 if ((msr_data & 0xfffc) == 0x0) {
1864 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001865 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001866 }
1867 break;
1868 case X86EMUL_MODE_PROT64:
1869 if (msr_data == 0x0) {
1870 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001871 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001872 }
1873 break;
1874 }
1875
1876 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1877 cs.selector = (u16)msr_data;
1878 cs.selector &= ~SELECTOR_RPL_MASK;
1879 ss.selector = cs.selector + 8;
1880 ss.selector &= ~SELECTOR_RPL_MASK;
1881 if (ctxt->mode == X86EMUL_MODE_PROT64
1882 || is_long_mode(ctxt->vcpu)) {
1883 cs.db = 0;
1884 cs.l = 1;
1885 }
1886
1887 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1888 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1889
1890 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1891 c->eip = msr_data;
1892
1893 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1894 c->regs[VCPU_REGS_RSP] = msr_data;
1895
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001896 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001897}
1898
Andre Przywara4668f052009-06-18 12:56:02 +02001899static int
1900emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1901{
1902 struct decode_cache *c = &ctxt->decode;
1903 struct kvm_segment cs, ss;
1904 u64 msr_data;
1905 int usermode;
1906
Gleb Natapova0044752010-02-10 14:21:31 +02001907 /* inject #GP if in real mode or Virtual 8086 mode */
1908 if (ctxt->mode == X86EMUL_MODE_REAL ||
1909 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001910 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001911 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001912 }
1913
Andre Przywara4668f052009-06-18 12:56:02 +02001914 setup_syscalls_segments(ctxt, &cs, &ss);
1915
1916 if ((c->rex_prefix & 0x8) != 0x0)
1917 usermode = X86EMUL_MODE_PROT64;
1918 else
1919 usermode = X86EMUL_MODE_PROT32;
1920
1921 cs.dpl = 3;
1922 ss.dpl = 3;
1923 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1924 switch (usermode) {
1925 case X86EMUL_MODE_PROT32:
1926 cs.selector = (u16)(msr_data + 16);
1927 if ((msr_data & 0xfffc) == 0x0) {
1928 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001929 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001930 }
1931 ss.selector = (u16)(msr_data + 24);
1932 break;
1933 case X86EMUL_MODE_PROT64:
1934 cs.selector = (u16)(msr_data + 32);
1935 if (msr_data == 0x0) {
1936 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001937 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001938 }
1939 ss.selector = cs.selector + 8;
1940 cs.db = 0;
1941 cs.l = 1;
1942 break;
1943 }
1944 cs.selector |= SELECTOR_RPL_MASK;
1945 ss.selector |= SELECTOR_RPL_MASK;
1946
1947 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1948 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1949
1950 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1951 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1952
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001953 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001954}
1955
Gleb Natapov9c537242010-03-18 15:20:05 +02001956static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1957 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001958{
1959 int iopl;
1960 if (ctxt->mode == X86EMUL_MODE_REAL)
1961 return false;
1962 if (ctxt->mode == X86EMUL_MODE_VM86)
1963 return true;
1964 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001965 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001966}
1967
1968static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1969 struct x86_emulate_ops *ops,
1970 u16 port, u16 len)
1971{
1972 struct kvm_segment tr_seg;
1973 int r;
1974 u16 io_bitmap_ptr;
1975 u8 perm, bit_idx = port & 0x7;
1976 unsigned mask = (1 << len) - 1;
1977
1978 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1979 if (tr_seg.unusable)
1980 return false;
1981 if (tr_seg.limit < 103)
1982 return false;
1983 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1984 NULL);
1985 if (r != X86EMUL_CONTINUE)
1986 return false;
1987 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1988 return false;
1989 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1990 ctxt->vcpu, NULL);
1991 if (r != X86EMUL_CONTINUE)
1992 return false;
1993 if ((perm >> bit_idx) & mask)
1994 return false;
1995 return true;
1996}
1997
1998static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1999 struct x86_emulate_ops *ops,
2000 u16 port, u16 len)
2001{
Gleb Natapov9c537242010-03-18 15:20:05 +02002002 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002003 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2004 return false;
2005 return true;
2006}
2007
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002008static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt,
2009 struct x86_emulate_ops *ops,
2010 int seg)
2011{
2012 struct desc_struct desc;
2013 if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu))
2014 return get_desc_base(&desc);
2015 else
2016 return ~0;
2017}
2018
2019static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2020 struct x86_emulate_ops *ops,
2021 struct tss_segment_16 *tss)
2022{
2023 struct decode_cache *c = &ctxt->decode;
2024
2025 tss->ip = c->eip;
2026 tss->flag = ctxt->eflags;
2027 tss->ax = c->regs[VCPU_REGS_RAX];
2028 tss->cx = c->regs[VCPU_REGS_RCX];
2029 tss->dx = c->regs[VCPU_REGS_RDX];
2030 tss->bx = c->regs[VCPU_REGS_RBX];
2031 tss->sp = c->regs[VCPU_REGS_RSP];
2032 tss->bp = c->regs[VCPU_REGS_RBP];
2033 tss->si = c->regs[VCPU_REGS_RSI];
2034 tss->di = c->regs[VCPU_REGS_RDI];
2035
2036 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2037 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2038 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2039 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2040 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2041}
2042
2043static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2044 struct x86_emulate_ops *ops,
2045 struct tss_segment_16 *tss)
2046{
2047 struct decode_cache *c = &ctxt->decode;
2048 int ret;
2049
2050 c->eip = tss->ip;
2051 ctxt->eflags = tss->flag | 2;
2052 c->regs[VCPU_REGS_RAX] = tss->ax;
2053 c->regs[VCPU_REGS_RCX] = tss->cx;
2054 c->regs[VCPU_REGS_RDX] = tss->dx;
2055 c->regs[VCPU_REGS_RBX] = tss->bx;
2056 c->regs[VCPU_REGS_RSP] = tss->sp;
2057 c->regs[VCPU_REGS_RBP] = tss->bp;
2058 c->regs[VCPU_REGS_RSI] = tss->si;
2059 c->regs[VCPU_REGS_RDI] = tss->di;
2060
2061 /*
2062 * SDM says that segment selectors are loaded before segment
2063 * descriptors
2064 */
2065 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2066 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2067 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2068 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2069 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2070
2071 /*
2072 * Now load segment descriptors. If fault happenes at this stage
2073 * it is handled in a context of new task
2074 */
2075 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2076 if (ret != X86EMUL_CONTINUE)
2077 return ret;
2078 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2079 if (ret != X86EMUL_CONTINUE)
2080 return ret;
2081 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2082 if (ret != X86EMUL_CONTINUE)
2083 return ret;
2084 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2085 if (ret != X86EMUL_CONTINUE)
2086 return ret;
2087 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2088 if (ret != X86EMUL_CONTINUE)
2089 return ret;
2090
2091 return X86EMUL_CONTINUE;
2092}
2093
2094static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2095 struct x86_emulate_ops *ops,
2096 u16 tss_selector, u16 old_tss_sel,
2097 ulong old_tss_base, struct desc_struct *new_desc)
2098{
2099 struct tss_segment_16 tss_seg;
2100 int ret;
2101 u32 err, new_tss_base = get_desc_base(new_desc);
2102
2103 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2104 &err);
2105 if (ret == X86EMUL_PROPAGATE_FAULT) {
2106 /* FIXME: need to provide precise fault address */
2107 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2108 return ret;
2109 }
2110
2111 save_state_to_tss16(ctxt, ops, &tss_seg);
2112
2113 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2114 &err);
2115 if (ret == X86EMUL_PROPAGATE_FAULT) {
2116 /* FIXME: need to provide precise fault address */
2117 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2118 return ret;
2119 }
2120
2121 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2122 &err);
2123 if (ret == X86EMUL_PROPAGATE_FAULT) {
2124 /* FIXME: need to provide precise fault address */
2125 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2126 return ret;
2127 }
2128
2129 if (old_tss_sel != 0xffff) {
2130 tss_seg.prev_task_link = old_tss_sel;
2131
2132 ret = ops->write_std(new_tss_base,
2133 &tss_seg.prev_task_link,
2134 sizeof tss_seg.prev_task_link,
2135 ctxt->vcpu, &err);
2136 if (ret == X86EMUL_PROPAGATE_FAULT) {
2137 /* FIXME: need to provide precise fault address */
2138 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2139 return ret;
2140 }
2141 }
2142
2143 return load_state_from_tss16(ctxt, ops, &tss_seg);
2144}
2145
2146static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2147 struct x86_emulate_ops *ops,
2148 struct tss_segment_32 *tss)
2149{
2150 struct decode_cache *c = &ctxt->decode;
2151
2152 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2153 tss->eip = c->eip;
2154 tss->eflags = ctxt->eflags;
2155 tss->eax = c->regs[VCPU_REGS_RAX];
2156 tss->ecx = c->regs[VCPU_REGS_RCX];
2157 tss->edx = c->regs[VCPU_REGS_RDX];
2158 tss->ebx = c->regs[VCPU_REGS_RBX];
2159 tss->esp = c->regs[VCPU_REGS_RSP];
2160 tss->ebp = c->regs[VCPU_REGS_RBP];
2161 tss->esi = c->regs[VCPU_REGS_RSI];
2162 tss->edi = c->regs[VCPU_REGS_RDI];
2163
2164 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2165 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2166 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2167 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2168 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2169 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2170 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2171}
2172
2173static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2174 struct x86_emulate_ops *ops,
2175 struct tss_segment_32 *tss)
2176{
2177 struct decode_cache *c = &ctxt->decode;
2178 int ret;
2179
2180 ops->set_cr(3, tss->cr3, ctxt->vcpu);
2181 c->eip = tss->eip;
2182 ctxt->eflags = tss->eflags | 2;
2183 c->regs[VCPU_REGS_RAX] = tss->eax;
2184 c->regs[VCPU_REGS_RCX] = tss->ecx;
2185 c->regs[VCPU_REGS_RDX] = tss->edx;
2186 c->regs[VCPU_REGS_RBX] = tss->ebx;
2187 c->regs[VCPU_REGS_RSP] = tss->esp;
2188 c->regs[VCPU_REGS_RBP] = tss->ebp;
2189 c->regs[VCPU_REGS_RSI] = tss->esi;
2190 c->regs[VCPU_REGS_RDI] = tss->edi;
2191
2192 /*
2193 * SDM says that segment selectors are loaded before segment
2194 * descriptors
2195 */
2196 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2197 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2198 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2199 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2200 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2201 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2202 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2203
2204 /*
2205 * Now load segment descriptors. If fault happenes at this stage
2206 * it is handled in a context of new task
2207 */
2208 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2209 if (ret != X86EMUL_CONTINUE)
2210 return ret;
2211 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2212 if (ret != X86EMUL_CONTINUE)
2213 return ret;
2214 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2215 if (ret != X86EMUL_CONTINUE)
2216 return ret;
2217 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2218 if (ret != X86EMUL_CONTINUE)
2219 return ret;
2220 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2221 if (ret != X86EMUL_CONTINUE)
2222 return ret;
2223 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2224 if (ret != X86EMUL_CONTINUE)
2225 return ret;
2226 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2227 if (ret != X86EMUL_CONTINUE)
2228 return ret;
2229
2230 return X86EMUL_CONTINUE;
2231}
2232
2233static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2234 struct x86_emulate_ops *ops,
2235 u16 tss_selector, u16 old_tss_sel,
2236 ulong old_tss_base, struct desc_struct *new_desc)
2237{
2238 struct tss_segment_32 tss_seg;
2239 int ret;
2240 u32 err, new_tss_base = get_desc_base(new_desc);
2241
2242 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2243 &err);
2244 if (ret == X86EMUL_PROPAGATE_FAULT) {
2245 /* FIXME: need to provide precise fault address */
2246 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2247 return ret;
2248 }
2249
2250 save_state_to_tss32(ctxt, ops, &tss_seg);
2251
2252 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2253 &err);
2254 if (ret == X86EMUL_PROPAGATE_FAULT) {
2255 /* FIXME: need to provide precise fault address */
2256 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2257 return ret;
2258 }
2259
2260 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2261 &err);
2262 if (ret == X86EMUL_PROPAGATE_FAULT) {
2263 /* FIXME: need to provide precise fault address */
2264 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2265 return ret;
2266 }
2267
2268 if (old_tss_sel != 0xffff) {
2269 tss_seg.prev_task_link = old_tss_sel;
2270
2271 ret = ops->write_std(new_tss_base,
2272 &tss_seg.prev_task_link,
2273 sizeof tss_seg.prev_task_link,
2274 ctxt->vcpu, &err);
2275 if (ret == X86EMUL_PROPAGATE_FAULT) {
2276 /* FIXME: need to provide precise fault address */
2277 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2278 return ret;
2279 }
2280 }
2281
2282 return load_state_from_tss32(ctxt, ops, &tss_seg);
2283}
2284
2285static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2286 struct x86_emulate_ops *ops,
2287 u16 tss_selector, int reason)
2288{
2289 struct desc_struct curr_tss_desc, next_tss_desc;
2290 int ret;
2291 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2292 ulong old_tss_base =
2293 get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR);
2294
2295 /* FIXME: old_tss_base == ~0 ? */
2296
2297 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2298 if (ret != X86EMUL_CONTINUE)
2299 return ret;
2300 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2301 if (ret != X86EMUL_CONTINUE)
2302 return ret;
2303
2304 /* FIXME: check that next_tss_desc is tss */
2305
2306 if (reason != TASK_SWITCH_IRET) {
2307 if ((tss_selector & 3) > next_tss_desc.dpl ||
2308 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2309 kvm_inject_gp(ctxt->vcpu, 0);
2310 return X86EMUL_PROPAGATE_FAULT;
2311 }
2312 }
2313
2314 if (!next_tss_desc.p || desc_limit_scaled(&next_tss_desc) < 0x67) {
2315 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2316 tss_selector & 0xfffc);
2317 return X86EMUL_PROPAGATE_FAULT;
2318 }
2319
2320 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2321 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2322 write_segment_descriptor(ctxt, ops, old_tss_sel,
2323 &curr_tss_desc);
2324 }
2325
2326 if (reason == TASK_SWITCH_IRET)
2327 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2328
2329 /* set back link to prev task only if NT bit is set in eflags
2330 note that old_tss_sel is not used afetr this point */
2331 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2332 old_tss_sel = 0xffff;
2333
2334 if (next_tss_desc.type & 8)
2335 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2336 old_tss_base, &next_tss_desc);
2337 else
2338 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2339 old_tss_base, &next_tss_desc);
2340
2341 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2342 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2343
2344 if (reason != TASK_SWITCH_IRET) {
2345 next_tss_desc.type |= (1 << 1); /* set busy flag */
2346 write_segment_descriptor(ctxt, ops, tss_selector,
2347 &next_tss_desc);
2348 }
2349
2350 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2351 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2352 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2353
2354 return ret;
2355}
2356
2357int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2358 struct x86_emulate_ops *ops,
2359 u16 tss_selector, int reason)
2360{
2361 struct decode_cache *c = &ctxt->decode;
2362 int rc;
2363
2364 memset(c, 0, sizeof(struct decode_cache));
2365 c->eip = ctxt->eip;
2366 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
2367
2368 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason);
2369
2370 if (rc == X86EMUL_CONTINUE) {
2371 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
2372 kvm_rip_write(ctxt->vcpu, c->eip);
2373 }
2374
2375 return rc;
2376}
2377
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002378int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002379x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002380{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002381 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002382 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02002383 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002384 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002385 unsigned int port;
2386 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002387 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002388
Glauber Costa310b5d32009-05-12 16:21:06 -04002389 ctxt->interruptibility = 0;
2390
Laurent Vivier34273182007-09-18 11:27:37 +02002391 /* Shadow copy of register state. Committed on successful emulation.
2392 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
2393 * modify them.
2394 */
2395
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002396 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02002397 saved_eip = c->eip;
2398
Gleb Natapov11616242010-02-11 14:43:14 +02002399 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2400 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2401 goto done;
2402 }
2403
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002404 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002405 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002406 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2407 goto done;
2408 }
2409
Gleb Natapove92805a2010-02-10 14:21:35 +02002410 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002411 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002412 kvm_inject_gp(ctxt->vcpu, 0);
2413 goto done;
2414 }
2415
Avi Kivityc7e75a32007-10-28 16:34:25 +02002416 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002417 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002418
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002419 if (c->rep_prefix && (c->d & String)) {
2420 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002421 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002422 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002423 goto done;
2424 }
2425 /* The second termination condition only applies for REPE
2426 * and REPNE. Test if the repeat string operation prefix is
2427 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2428 * corresponding termination condition according to:
2429 * - if REPE/REPZ and ZF = 0 then done
2430 * - if REPNE/REPNZ and ZF = 1 then done
2431 */
2432 if ((c->b == 0xa6) || (c->b == 0xa7) ||
2433 (c->b == 0xae) || (c->b == 0xaf)) {
2434 if ((c->rep_prefix == REPE_PREFIX) &&
2435 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002436 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002437 goto done;
2438 }
2439 if ((c->rep_prefix == REPNE_PREFIX) &&
2440 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002441 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002442 goto done;
2443 }
2444 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02002445 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02002446 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002447 }
2448
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002449 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002450 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002451 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04002452 rc = ops->read_emulated((unsigned long)c->src.ptr,
2453 &c->src.val,
2454 c->src.bytes,
2455 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002456 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002457 goto done;
2458 c->src.orig_val = c->src.val;
2459 }
2460
Gleb Natapove35b7b92010-02-25 16:36:42 +02002461 if (c->src2.type == OP_MEM) {
2462 c->src2.ptr = (unsigned long *)(memop + c->src.bytes);
2463 c->src2.val = 0;
2464 rc = ops->read_emulated((unsigned long)c->src2.ptr,
2465 &c->src2.val,
2466 c->src2.bytes,
2467 ctxt->vcpu);
2468 if (rc != X86EMUL_CONTINUE)
2469 goto done;
2470 }
2471
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002472 if ((c->d & DstMask) == ImplicitOps)
2473 goto special_insn;
2474
2475
2476 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002477 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002478 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2479 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002480 if (c->d & BitOp) {
2481 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02002482
Laurent Viviere4e03de2007-09-18 11:52:50 +02002483 c->dst.ptr = (void *)c->dst.ptr +
2484 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08002485 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002486 if (!(c->d & Mov)) {
2487 /* optimisation - avoid slow emulated read */
2488 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2489 &c->dst.val,
2490 c->dst.bytes,
2491 ctxt->vcpu);
2492 if (rc != X86EMUL_CONTINUE)
2493 goto done;
2494 }
Avi Kivity038e51d2007-01-22 20:40:40 -08002495 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002496 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002497
Avi Kivity018a98d2007-11-27 19:30:56 +02002498special_insn:
2499
Laurent Viviere4e03de2007-09-18 11:52:50 +02002500 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 goto twobyte_insn;
2502
Laurent Viviere4e03de2007-09-18 11:52:50 +02002503 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 case 0x00 ... 0x05:
2505 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002506 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002508 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002509 emulate_push_sreg(ctxt, VCPU_SREG_ES);
2510 break;
2511 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002512 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002513 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002514 goto done;
2515 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 case 0x08 ... 0x0d:
2517 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002518 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002520 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002521 emulate_push_sreg(ctxt, VCPU_SREG_CS);
2522 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 case 0x10 ... 0x15:
2524 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002525 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002527 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002528 emulate_push_sreg(ctxt, VCPU_SREG_SS);
2529 break;
2530 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002531 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002532 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002533 goto done;
2534 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 case 0x18 ... 0x1d:
2536 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002537 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002539 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002540 emulate_push_sreg(ctxt, VCPU_SREG_DS);
2541 break;
2542 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002543 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002544 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002545 goto done;
2546 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002547 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002549 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 break;
2551 case 0x28 ... 0x2d:
2552 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002553 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 break;
2555 case 0x30 ... 0x35:
2556 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002557 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 break;
2559 case 0x38 ... 0x3d:
2560 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002561 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002563 case 0x40 ... 0x47: /* inc r16/r32 */
2564 emulate_1op("inc", c->dst, ctxt->eflags);
2565 break;
2566 case 0x48 ... 0x4f: /* dec r16/r32 */
2567 emulate_1op("dec", c->dst, ctxt->eflags);
2568 break;
2569 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002570 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002571 break;
2572 case 0x58 ... 0x5f: /* pop reg */
2573 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002574 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002575 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002576 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002577 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002578 case 0x60: /* pusha */
2579 emulate_pusha(ctxt);
2580 break;
2581 case 0x61: /* popa */
2582 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002583 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002584 goto done;
2585 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002587 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002589 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002591 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002592 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002593 emulate_push(ctxt);
2594 break;
2595 case 0x6c: /* insb */
2596 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002597 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2598 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2599 kvm_inject_gp(ctxt->vcpu, 0);
2600 goto done;
2601 }
2602 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002603 1,
2604 (c->d & ByteOp) ? 1 : c->op_bytes,
2605 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002606 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002607 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002608 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002609 c->regs[VCPU_REGS_RDI]),
2610 c->rep_prefix,
2611 c->regs[VCPU_REGS_RDX]) == 0) {
2612 c->eip = saved_eip;
2613 return -1;
2614 }
2615 return 0;
2616 case 0x6e: /* outsb */
2617 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002618 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2619 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2620 kvm_inject_gp(ctxt->vcpu, 0);
2621 goto done;
2622 }
Avi Kivity851ba692009-08-24 11:10:17 +03002623 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002624 0,
2625 (c->d & ByteOp) ? 1 : c->op_bytes,
2626 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002627 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002628 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002629 register_address(c,
2630 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002631 c->regs[VCPU_REGS_RSI]),
2632 c->rep_prefix,
2633 c->regs[VCPU_REGS_RDX]) == 0) {
2634 c->eip = saved_eip;
2635 return -1;
2636 }
2637 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002638 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002639 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002640 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002641 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002643 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 case 0:
2645 goto add;
2646 case 1:
2647 goto or;
2648 case 2:
2649 goto adc;
2650 case 3:
2651 goto sbb;
2652 case 4:
2653 goto and;
2654 case 5:
2655 goto sub;
2656 case 6:
2657 goto xor;
2658 case 7:
2659 goto cmp;
2660 }
2661 break;
2662 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002663 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 break;
2665 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002666 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002668 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002670 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 break;
2672 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002673 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 break;
2675 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002676 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 break; /* 64b reg: zero-extend */
2678 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002679 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 break;
2681 }
2682 /*
2683 * Write back the memory destination with implicit LOCK
2684 * prefix.
2685 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002686 c->dst.val = c->src.val;
2687 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002690 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002691 case 0x8c: { /* mov r/m, sreg */
2692 struct kvm_segment segreg;
2693
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002694 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002695 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2696 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002697 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2698 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002699 }
2700 c->dst.val = segreg.selector;
2701 break;
2702 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002703 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002704 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002705 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002706 case 0x8e: { /* mov seg, r/m16 */
2707 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002708
2709 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002710
Gleb Natapovc6975182010-02-18 12:15:01 +02002711 if (c->modrm_reg == VCPU_SREG_CS ||
2712 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002713 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2714 goto done;
2715 }
2716
Glauber Costa310b5d32009-05-12 16:21:06 -04002717 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002718 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002719
Gleb Natapov2e873022010-03-18 15:20:18 +02002720 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002721
2722 c->dst.type = OP_NONE; /* Disable writeback. */
2723 break;
2724 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002726 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002727 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002730 case 0x90: /* nop / xchg r8,rax */
2731 if (!(c->rex_prefix & 1)) { /* nop */
2732 c->dst.type = OP_NONE;
2733 break;
2734 }
2735 case 0x91 ... 0x97: /* xchg reg,rax */
2736 c->src.type = c->dst.type = OP_REG;
2737 c->src.bytes = c->dst.bytes = c->op_bytes;
2738 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2739 c->src.val = *(c->src.ptr);
2740 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002741 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002742 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002743 emulate_push(ctxt);
2744 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002745 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002746 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002747 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002748 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002749 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2750 if (rc != X86EMUL_CONTINUE)
2751 goto done;
2752 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002753 case 0xa0 ... 0xa1: /* mov */
2754 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2755 c->dst.val = c->src.val;
2756 break;
2757 case 0xa2 ... 0xa3: /* mov */
2758 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2759 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002761 c->dst.type = OP_MEM;
2762 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002763 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002764 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002765 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002766 rc = ops->read_emulated(register_address(c,
2767 seg_override_base(ctxt, c),
2768 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002769 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002770 c->dst.bytes, ctxt->vcpu);
2771 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002773 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002774 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002775 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002776 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002777 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002778 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 break;
2780 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002781 c->src.type = OP_NONE; /* Disable writeback. */
2782 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002783 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002784 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002785 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002786 rc = ops->read_emulated((unsigned long)c->src.ptr,
2787 &c->src.val,
2788 c->src.bytes,
2789 ctxt->vcpu);
2790 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002791 goto done;
2792
2793 c->dst.type = OP_NONE; /* Disable writeback. */
2794 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002795 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002796 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002797 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002798 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2799 &c->dst.val,
2800 c->dst.bytes,
2801 ctxt->vcpu);
2802 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002803 goto done;
2804
2805 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2806
2807 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2808
Harvey Harrison7a9572752008-02-19 07:40:41 -08002809 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002810 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2811 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002812 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002813 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2814 : c->dst.bytes);
2815
2816 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002818 c->dst.type = OP_MEM;
2819 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002820 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002821 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002822 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002823 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002824 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002825 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002826 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 break;
2828 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002829 c->dst.type = OP_REG;
2830 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2831 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002832 rc = ops->read_emulated(register_address(c,
2833 seg_override_base(ctxt, c),
2834 c->regs[VCPU_REGS_RSI]),
2835 &c->dst.val,
2836 c->dst.bytes,
2837 ctxt->vcpu);
2838 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002840 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002841 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002842 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 break;
2844 case 0xae ... 0xaf: /* scas */
2845 DPRINTF("Urk! I don't handle SCAS.\n");
2846 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002847 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002848 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002849 case 0xc0 ... 0xc1:
2850 emulate_grp2(ctxt);
2851 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002852 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002853 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002854 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002855 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002856 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002857 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2858 mov:
2859 c->dst.val = c->src.val;
2860 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002861 case 0xcb: /* ret far */
2862 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002863 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002864 goto done;
2865 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002866 case 0xd0 ... 0xd1: /* Grp2 */
2867 c->src.val = 1;
2868 emulate_grp2(ctxt);
2869 break;
2870 case 0xd2 ... 0xd3: /* Grp2 */
2871 c->src.val = c->regs[VCPU_REGS_RCX];
2872 emulate_grp2(ctxt);
2873 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002874 case 0xe4: /* inb */
2875 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002876 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002877 io_dir_in = 1;
2878 goto do_io;
2879 case 0xe6: /* outb */
2880 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002881 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002882 io_dir_in = 0;
2883 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002884 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002885 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002886 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002887 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002888 emulate_push(ctxt);
2889 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002890 }
2891 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002892 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002893 case 0xea: /* jmp far */
Gleb Natapovea798492010-02-25 16:36:43 +02002894 jump_far:
Gleb Natapov2e873022010-03-18 15:20:18 +02002895 if (load_segment_descriptor(ctxt, ops, c->src2.val,
2896 VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002897 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002898
Gleb Natapov782b8772009-04-12 13:36:25 +03002899 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002900 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002901 case 0xeb:
2902 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002903 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002904 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002905 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002906 case 0xec: /* in al,dx */
2907 case 0xed: /* in (e/r)ax,dx */
2908 port = c->regs[VCPU_REGS_RDX];
2909 io_dir_in = 1;
2910 goto do_io;
2911 case 0xee: /* out al,dx */
2912 case 0xef: /* out (e/r)ax,dx */
2913 port = c->regs[VCPU_REGS_RDX];
2914 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002915 do_io:
2916 if (!emulator_io_permited(ctxt, ops, port,
2917 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2918 kvm_inject_gp(ctxt->vcpu, 0);
2919 goto done;
2920 }
2921 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002922 (c->d & ByteOp) ? 1 : c->op_bytes,
2923 port) != 0) {
2924 c->eip = saved_eip;
2925 goto cannot_emulate;
2926 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002927 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002928 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002929 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002930 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002931 case 0xf5: /* cmc */
2932 /* complement carry flag from eflags reg */
2933 ctxt->eflags ^= EFLG_CF;
2934 c->dst.type = OP_NONE; /* Disable writeback. */
2935 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002936 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002937 if (!emulate_grp3(ctxt, ops))
2938 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002939 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002940 case 0xf8: /* clc */
2941 ctxt->eflags &= ~EFLG_CF;
2942 c->dst.type = OP_NONE; /* Disable writeback. */
2943 break;
2944 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002945 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002946 kvm_inject_gp(ctxt->vcpu, 0);
2947 else {
2948 ctxt->eflags &= ~X86_EFLAGS_IF;
2949 c->dst.type = OP_NONE; /* Disable writeback. */
2950 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002951 break;
2952 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002953 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002954 kvm_inject_gp(ctxt->vcpu, 0);
2955 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002956 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002957 ctxt->eflags |= X86_EFLAGS_IF;
2958 c->dst.type = OP_NONE; /* Disable writeback. */
2959 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002960 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002961 case 0xfc: /* cld */
2962 ctxt->eflags &= ~EFLG_DF;
2963 c->dst.type = OP_NONE; /* Disable writeback. */
2964 break;
2965 case 0xfd: /* std */
2966 ctxt->eflags |= EFLG_DF;
2967 c->dst.type = OP_NONE; /* Disable writeback. */
2968 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002969 case 0xfe: /* Grp4 */
2970 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002971 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002972 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002973 goto done;
2974 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002975 case 0xff: /* Grp5 */
2976 if (c->modrm_reg == 5)
2977 goto jump_far;
2978 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002980
2981writeback:
2982 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002983 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002984 goto done;
2985
2986 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002987 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002988 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002989
2990done:
2991 if (rc == X86EMUL_UNHANDLEABLE) {
2992 c->eip = saved_eip;
2993 return -1;
2994 }
2995 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996
2997twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002998 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003000 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001 u16 size;
3002 unsigned long address;
3003
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003004 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003005 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003006 goto cannot_emulate;
3007
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003008 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003009 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003010 goto done;
3011
Avi Kivity33e38852008-05-21 15:34:25 +03003012 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003013 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003014 /* Disable writeback. */
3015 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003016 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003018 rc = read_descriptor(ctxt, ops, c->src.ptr,
3019 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003020 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 goto done;
3022 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003023 /* Disable writeback. */
3024 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003026 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003027 if (c->modrm_mod == 3) {
3028 switch (c->modrm_rm) {
3029 case 1:
3030 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003031 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003032 goto done;
3033 break;
3034 default:
3035 goto cannot_emulate;
3036 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003037 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003038 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003039 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003040 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003041 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003042 goto done;
3043 realmode_lidt(ctxt->vcpu, size, address);
3044 }
Avi Kivity16286d02008-04-14 14:40:50 +03003045 /* Disable writeback. */
3046 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 break;
3048 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003049 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003050 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 break;
3052 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003053 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3054 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003055 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003057 case 5: /* not defined */
3058 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3059 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08003061 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03003062 /* Disable writeback. */
3063 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 break;
3065 default:
3066 goto cannot_emulate;
3067 }
3068 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003069 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003070 rc = emulate_syscall(ctxt);
3071 if (rc != X86EMUL_CONTINUE)
3072 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003073 else
3074 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003075 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003076 case 0x06:
3077 emulate_clts(ctxt->vcpu);
3078 c->dst.type = OP_NONE;
3079 break;
3080 case 0x08: /* invd */
3081 case 0x09: /* wbinvd */
3082 case 0x0d: /* GrpP (prefetch) */
3083 case 0x18: /* Grp16 (prefetch/nop) */
3084 c->dst.type = OP_NONE;
3085 break;
3086 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003087 switch (c->modrm_reg) {
3088 case 1:
3089 case 5 ... 7:
3090 case 9 ... 15:
3091 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3092 goto done;
3093 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003094 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003095 c->dst.type = OP_NONE; /* no writeback */
3096 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003098 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3099 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3100 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3101 goto done;
3102 }
3103 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003104 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02003107 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 c->dst.type = OP_NONE;
3109 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003111 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3112 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3113 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3114 goto done;
3115 }
3116 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003117 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003119 case 0x30:
3120 /* wrmsr */
3121 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3122 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003123 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003124 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003125 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003126 }
3127 rc = X86EMUL_CONTINUE;
3128 c->dst.type = OP_NONE;
3129 break;
3130 case 0x32:
3131 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003132 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003133 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003134 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003135 } else {
3136 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3137 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3138 }
3139 rc = X86EMUL_CONTINUE;
3140 c->dst.type = OP_NONE;
3141 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003142 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003143 rc = emulate_sysenter(ctxt);
3144 if (rc != X86EMUL_CONTINUE)
3145 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003146 else
3147 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003148 break;
3149 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003150 rc = emulate_sysexit(ctxt);
3151 if (rc != X86EMUL_CONTINUE)
3152 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003153 else
3154 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003155 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003157 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003158 if (!test_cc(c->b, ctxt->eflags))
3159 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003161 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003162 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003163 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003164 c->dst.type = OP_NONE;
3165 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003166 case 0xa0: /* push fs */
3167 emulate_push_sreg(ctxt, VCPU_SREG_FS);
3168 break;
3169 case 0xa1: /* pop fs */
3170 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003171 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003172 goto done;
3173 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003174 case 0xa3:
3175 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003176 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003177 /* only subword offset */
3178 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003179 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003180 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003181 case 0xa4: /* shld imm8, r, r/m */
3182 case 0xa5: /* shld cl, r, r/m */
3183 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3184 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003185 case 0xa8: /* push gs */
3186 emulate_push_sreg(ctxt, VCPU_SREG_GS);
3187 break;
3188 case 0xa9: /* pop gs */
3189 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003190 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003191 goto done;
3192 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003193 case 0xab:
3194 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003195 /* only subword offset */
3196 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003197 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003198 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003199 case 0xac: /* shrd imm8, r, r/m */
3200 case 0xad: /* shrd cl, r, r/m */
3201 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3202 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003203 case 0xae: /* clflush */
3204 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 case 0xb0 ... 0xb1: /* cmpxchg */
3206 /*
3207 * Save real source value, then compare EAX against
3208 * destination.
3209 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003210 c->src.orig_val = c->src.val;
3211 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003212 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3213 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003215 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 } else {
3217 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003218 c->dst.type = OP_REG;
3219 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 }
3221 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 case 0xb3:
3223 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003224 /* only subword offset */
3225 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003226 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003229 c->dst.bytes = c->op_bytes;
3230 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3231 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003234 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 case 0:
3236 goto bt;
3237 case 1:
3238 goto bts;
3239 case 2:
3240 goto btr;
3241 case 3:
3242 goto btc;
3243 }
3244 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003245 case 0xbb:
3246 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003247 /* only subword offset */
3248 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003249 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003250 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003252 c->dst.bytes = c->op_bytes;
3253 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3254 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003256 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003257 c->dst.bytes = c->op_bytes;
3258 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3259 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003260 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08003262 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003263 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003264 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003265 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003266 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 }
3268 goto writeback;
3269
3270cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003271 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02003272 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 return -1;
3274}