blob: 35f7acd4a91f7921d222bfbcd49f715266d40f79 [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"
Andre Przywarae99f0502009-06-17 15:50:33 +020036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020079#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020080#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082/* Source 2 operand type */
83#define Src2None (0<<29)
84#define Src2CL (1<<29)
85#define Src2ImmByte (2<<29)
86#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030087#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010088#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089
Avi Kivity43bb19c2008-01-18 12:46:50 +020090enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020091 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020092 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020093 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020094};
95
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010096static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080097 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020098 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300100 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300101 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200103 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200105 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
106 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200108 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300110 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300111 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200113 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300115 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300116 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200118 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200120 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 /* 0x28 - 0x2F */
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,
124 0, 0, 0, 0,
125 /* 0x30 - 0x37 */
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 /* 0x38 - 0x3F */
130 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
131 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200132 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
133 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700134 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200135 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700136 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200137 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300138 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200139 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
140 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300141 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200142 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
143 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700144 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200145 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
146 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700147 0, 0, 0, 0,
148 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300149 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300150 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
151 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300152 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300153 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
154 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300155 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300156 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200159 Group | Group1_80, Group | Group1_81,
160 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200162 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 /* 0x88 - 0x8F */
164 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
165 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200166 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200167 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300168 /* 0x90 - 0x97 */
169 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
170 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300171 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300172 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200174 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
175 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200176 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
177 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200179 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
180 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
181 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300182 /* 0xB0 - 0xB7 */
183 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
184 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
185 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
186 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
187 /* 0xB8 - 0xBF */
188 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
189 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
190 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
191 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300193 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200194 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300195 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300197 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300198 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xD0 - 0xD7 */
200 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
201 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
202 0, 0, 0, 0,
203 /* 0xD8 - 0xDF */
204 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300205 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300206 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300207 ByteOp | SrcImmUByte, SrcImmUByte,
208 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300209 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300210 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300211 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300212 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
213 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0xF0 - 0xF7 */
215 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200216 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700218 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300219 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220};
221
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100222static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200224 0, Group | GroupDual | Group7, 0, 0,
225 0, ImplicitOps, ImplicitOps | Priv, 0,
226 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
227 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x10 - 0x1F */
229 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
230 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200231 ModRM | ImplicitOps | Priv, ModRM | Priv,
232 ModRM | ImplicitOps | Priv, ModRM | Priv,
233 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 0, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200236 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
237 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200238 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 /* 0x40 - 0x47 */
240 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
241 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
242 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
243 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
244 /* 0x48 - 0x4F */
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 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 /* 0x50 - 0x5F */
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0x60 - 0x6F */
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
253 /* 0x70 - 0x7F */
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300256 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
257 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 /* 0x90 - 0x9F */
259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300261 ImplicitOps | Stack, ImplicitOps | Stack,
262 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100263 DstMem | SrcReg | Src2ImmByte | ModRM,
264 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300266 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200267 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100268 DstMem | SrcReg | Src2ImmByte | ModRM,
269 DstMem | SrcReg | Src2CL | ModRM,
270 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200272 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
273 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
275 DstReg | SrcMem16 | ModRM | Mov,
276 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200277 0, 0,
278 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
280 DstReg | SrcMem16 | ModRM | Mov,
281 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200282 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
283 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800284 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 /* 0xD0 - 0xDF */
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 /* 0xE0 - 0xEF */
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 /* 0xF0 - 0xFF */
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
291};
292
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100293static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200294 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200295 ByteOp | DstMem | SrcImm | ModRM | Lock,
296 ByteOp | DstMem | SrcImm | ModRM | Lock,
297 ByteOp | DstMem | SrcImm | ModRM | Lock,
298 ByteOp | DstMem | SrcImm | ModRM | Lock,
299 ByteOp | DstMem | SrcImm | ModRM | Lock,
300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200303 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200304 DstMem | SrcImm | ModRM | Lock,
305 DstMem | SrcImm | ModRM | Lock,
306 DstMem | SrcImm | ModRM | Lock,
307 DstMem | SrcImm | ModRM | Lock,
308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200312 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200313 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
314 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
315 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
316 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
317 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,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200321 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200322 DstMem | SrcImmByte | ModRM | Lock,
323 DstMem | SrcImmByte | ModRM | Lock,
324 DstMem | SrcImmByte | ModRM | Lock,
325 DstMem | SrcImmByte | ModRM | Lock,
326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200330 [Group1A*8] =
331 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200332 [Group3_Byte*8] =
333 ByteOp | SrcImm | DstMem | ModRM, 0,
334 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
335 0, 0, 0, 0,
336 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400337 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300338 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200339 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200340 [Group4*8] =
341 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
342 0, 0, 0, 0, 0, 0,
343 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300344 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
345 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300346 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200347 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200348 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300349 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200350 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200351 [Group8*8] =
352 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200353 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
354 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200355 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200356 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200357};
358
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100359static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200360 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200361 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300362 SrcNone | ModRM | DstMem | Mov, 0,
363 SrcMem16 | ModRM | Mov, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200364 [Group9*8] =
365 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200366};
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200369#define EFLG_ID (1<<21)
370#define EFLG_VIP (1<<20)
371#define EFLG_VIF (1<<19)
372#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200373#define EFLG_VM (1<<17)
374#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200375#define EFLG_IOPL (3<<12)
376#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377#define EFLG_OF (1<<11)
378#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200379#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200380#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381#define EFLG_SF (1<<7)
382#define EFLG_ZF (1<<6)
383#define EFLG_AF (1<<4)
384#define EFLG_PF (1<<2)
385#define EFLG_CF (1<<0)
386
387/*
388 * Instruction emulation:
389 * Most instructions are emulated directly via a fragment of inline assembly
390 * code. This allows us to save/restore EFLAGS and thus very easily pick up
391 * any modified flags.
392 */
393
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800394#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395#define _LO32 "k" /* force 32-bit operand */
396#define _STK "%%rsp" /* stack pointer */
397#elif defined(__i386__)
398#define _LO32 "" /* force 32-bit operand */
399#define _STK "%%esp" /* stack pointer */
400#endif
401
402/*
403 * These EFLAGS bits are restored from saved value during emulation, and
404 * any changes are written back to the saved value after emulation.
405 */
406#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
407
408/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200409#define _PRE_EFLAGS(_sav, _msk, _tmp) \
410 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
411 "movl %"_sav",%"_LO32 _tmp"; " \
412 "push %"_tmp"; " \
413 "push %"_tmp"; " \
414 "movl %"_msk",%"_LO32 _tmp"; " \
415 "andl %"_LO32 _tmp",("_STK"); " \
416 "pushf; " \
417 "notl %"_LO32 _tmp"; " \
418 "andl %"_LO32 _tmp",("_STK"); " \
419 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
420 "pop %"_tmp"; " \
421 "orl %"_LO32 _tmp",("_STK"); " \
422 "popf; " \
423 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
425/* After executing instruction: write-back necessary bits in EFLAGS. */
426#define _POST_EFLAGS(_sav, _msk, _tmp) \
427 /* _sav |= EFLAGS & _msk; */ \
428 "pushf; " \
429 "pop %"_tmp"; " \
430 "andl %"_msk",%"_LO32 _tmp"; " \
431 "orl %"_LO32 _tmp",%"_sav"; "
432
Avi Kivitydda96d82008-11-26 15:14:10 +0200433#ifdef CONFIG_X86_64
434#define ON64(x) x
435#else
436#define ON64(x)
437#endif
438
Avi Kivity6b7ad612008-11-26 15:30:45 +0200439#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
440 do { \
441 __asm__ __volatile__ ( \
442 _PRE_EFLAGS("0", "4", "2") \
443 _op _suffix " %"_x"3,%1; " \
444 _POST_EFLAGS("0", "4", "2") \
445 : "=m" (_eflags), "=m" ((_dst).val), \
446 "=&r" (_tmp) \
447 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200448 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200449
450
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451/* Raw emulation: instruction has two explicit operands. */
452#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200453 do { \
454 unsigned long _tmp; \
455 \
456 switch ((_dst).bytes) { \
457 case 2: \
458 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
459 break; \
460 case 4: \
461 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
462 break; \
463 case 8: \
464 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
465 break; \
466 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 } while (0)
468
469#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
470 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200471 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400472 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200474 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 break; \
476 default: \
477 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
478 _wx, _wy, _lx, _ly, _qx, _qy); \
479 break; \
480 } \
481 } while (0)
482
483/* Source operand is byte-sized and may be restricted to just %cl. */
484#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
485 __emulate_2op(_op, _src, _dst, _eflags, \
486 "b", "c", "b", "c", "b", "c", "b", "c")
487
488/* Source operand is byte, word, long or quad sized. */
489#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
490 __emulate_2op(_op, _src, _dst, _eflags, \
491 "b", "q", "w", "r", _LO32, "r", "", "r")
492
493/* Source operand is word, long or quad sized. */
494#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
495 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
496 "w", "r", _LO32, "r", "", "r")
497
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100498/* Instruction has three operands and one operand is stored in ECX register */
499#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
500 do { \
501 unsigned long _tmp; \
502 _type _clv = (_cl).val; \
503 _type _srcv = (_src).val; \
504 _type _dstv = (_dst).val; \
505 \
506 __asm__ __volatile__ ( \
507 _PRE_EFLAGS("0", "5", "2") \
508 _op _suffix " %4,%1 \n" \
509 _POST_EFLAGS("0", "5", "2") \
510 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
511 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
512 ); \
513 \
514 (_cl).val = (unsigned long) _clv; \
515 (_src).val = (unsigned long) _srcv; \
516 (_dst).val = (unsigned long) _dstv; \
517 } while (0)
518
519#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
520 do { \
521 switch ((_dst).bytes) { \
522 case 2: \
523 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
524 "w", unsigned short); \
525 break; \
526 case 4: \
527 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
528 "l", unsigned int); \
529 break; \
530 case 8: \
531 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
532 "q", unsigned long)); \
533 break; \
534 } \
535 } while (0)
536
Avi Kivitydda96d82008-11-26 15:14:10 +0200537#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 do { \
539 unsigned long _tmp; \
540 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200541 __asm__ __volatile__ ( \
542 _PRE_EFLAGS("0", "3", "2") \
543 _op _suffix " %1; " \
544 _POST_EFLAGS("0", "3", "2") \
545 : "=m" (_eflags), "+m" ((_dst).val), \
546 "=&r" (_tmp) \
547 : "i" (EFLAGS_MASK)); \
548 } while (0)
549
550/* Instruction has only one explicit operand (no source operand). */
551#define emulate_1op(_op, _dst, _eflags) \
552 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400553 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200554 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
555 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
556 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
557 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 } \
559 } while (0)
560
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561/* Fetch next part of the instruction being emulated. */
562#define insn_fetch(_type, _size, _eip) \
563({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200564 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400565 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 goto done; \
567 (_eip) += (_size); \
568 (_type)_x; \
569})
570
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800571static inline unsigned long ad_mask(struct decode_cache *c)
572{
573 return (1UL << (c->ad_bytes << 3)) - 1;
574}
575
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800577static inline unsigned long
578address_mask(struct decode_cache *c, unsigned long reg)
579{
580 if (c->ad_bytes == sizeof(unsigned long))
581 return reg;
582 else
583 return reg & ad_mask(c);
584}
585
586static inline unsigned long
587register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
588{
589 return base + address_mask(c, reg);
590}
591
Harvey Harrison7a9572752008-02-19 07:40:41 -0800592static inline void
593register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
594{
595 if (c->ad_bytes == sizeof(unsigned long))
596 *reg += inc;
597 else
598 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
599}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600
Harvey Harrison7a9572752008-02-19 07:40:41 -0800601static inline void jmp_rel(struct decode_cache *c, int rel)
602{
603 register_address_increment(c, &c->eip, rel);
604}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300605
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606static void set_seg_override(struct decode_cache *c, int seg)
607{
608 c->has_seg_override = true;
609 c->seg_override = seg;
610}
611
612static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
613{
614 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
615 return 0;
616
617 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
618}
619
620static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
621 struct decode_cache *c)
622{
623 if (!c->has_seg_override)
624 return 0;
625
626 return seg_base(ctxt, c->seg_override);
627}
628
629static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
630{
631 return seg_base(ctxt, VCPU_SREG_ES);
632}
633
634static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
635{
636 return seg_base(ctxt, VCPU_SREG_SS);
637}
638
Avi Kivity62266862007-11-20 13:15:52 +0200639static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
640 struct x86_emulate_ops *ops,
641 unsigned long linear, u8 *dest)
642{
643 struct fetch_cache *fc = &ctxt->decode.fetch;
644 int rc;
645 int size;
646
647 if (linear < fc->start || linear >= fc->end) {
648 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200649 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900650 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200651 return rc;
652 fc->start = linear;
653 fc->end = linear + size;
654 }
655 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900656 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200657}
658
659static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
660 struct x86_emulate_ops *ops,
661 unsigned long eip, void *dest, unsigned size)
662{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200664
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200665 /* x86 instructions are limited to 15 bytes. */
666 if (eip + size - ctxt->decode.eip_orig > 15)
667 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200668 eip += ctxt->cs_base;
669 while (size--) {
670 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900671 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200672 return rc;
673 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900674 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200675}
676
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000677/*
678 * Given the 'reg' portion of a ModRM byte, and a register block, return a
679 * pointer into the block that addresses the relevant register.
680 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
681 */
682static void *decode_register(u8 modrm_reg, unsigned long *regs,
683 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684{
685 void *p;
686
687 p = &regs[modrm_reg];
688 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
689 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
690 return p;
691}
692
693static int read_descriptor(struct x86_emulate_ctxt *ctxt,
694 struct x86_emulate_ops *ops,
695 void *ptr,
696 u16 *size, unsigned long *address, int op_bytes)
697{
698 int rc;
699
700 if (op_bytes == 2)
701 op_bytes = 3;
702 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300703 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200704 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900705 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300707 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200708 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 return rc;
710}
711
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300712static int test_cc(unsigned int condition, unsigned int flags)
713{
714 int rc = 0;
715
716 switch ((condition & 15) >> 1) {
717 case 0: /* o */
718 rc |= (flags & EFLG_OF);
719 break;
720 case 1: /* b/c/nae */
721 rc |= (flags & EFLG_CF);
722 break;
723 case 2: /* z/e */
724 rc |= (flags & EFLG_ZF);
725 break;
726 case 3: /* be/na */
727 rc |= (flags & (EFLG_CF|EFLG_ZF));
728 break;
729 case 4: /* s */
730 rc |= (flags & EFLG_SF);
731 break;
732 case 5: /* p/pe */
733 rc |= (flags & EFLG_PF);
734 break;
735 case 7: /* le/ng */
736 rc |= (flags & EFLG_ZF);
737 /* fall through */
738 case 6: /* l/nge */
739 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
740 break;
741 }
742
743 /* Odd condition identifiers (lsb == 1) have inverted sense. */
744 return (!!rc ^ (condition & 1));
745}
746
Avi Kivity3c118e22007-10-31 10:27:04 +0200747static void decode_register_operand(struct operand *op,
748 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200749 int inhibit_bytereg)
750{
Avi Kivity33615aa2007-10-31 11:15:56 +0200751 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200752 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200753
754 if (!(c->d & ModRM))
755 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200756 op->type = OP_REG;
757 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200758 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200759 op->val = *(u8 *)op->ptr;
760 op->bytes = 1;
761 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200762 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->bytes = c->op_bytes;
764 switch (op->bytes) {
765 case 2:
766 op->val = *(u16 *)op->ptr;
767 break;
768 case 4:
769 op->val = *(u32 *)op->ptr;
770 break;
771 case 8:
772 op->val = *(u64 *) op->ptr;
773 break;
774 }
775 }
776 op->orig_val = op->val;
777}
778
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200779static int decode_modrm(struct x86_emulate_ctxt *ctxt,
780 struct x86_emulate_ops *ops)
781{
782 struct decode_cache *c = &ctxt->decode;
783 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700784 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900785 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786
787 if (c->rex_prefix) {
788 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
789 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
790 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
791 }
792
793 c->modrm = insn_fetch(u8, 1, c->eip);
794 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
795 c->modrm_reg |= (c->modrm & 0x38) >> 3;
796 c->modrm_rm |= (c->modrm & 0x07);
797 c->modrm_ea = 0;
798 c->use_modrm_ea = 1;
799
800 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300801 c->modrm_ptr = decode_register(c->modrm_rm,
802 c->regs, c->d & ByteOp);
803 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804 return rc;
805 }
806
807 if (c->ad_bytes == 2) {
808 unsigned bx = c->regs[VCPU_REGS_RBX];
809 unsigned bp = c->regs[VCPU_REGS_RBP];
810 unsigned si = c->regs[VCPU_REGS_RSI];
811 unsigned di = c->regs[VCPU_REGS_RDI];
812
813 /* 16-bit ModR/M decode. */
814 switch (c->modrm_mod) {
815 case 0:
816 if (c->modrm_rm == 6)
817 c->modrm_ea += insn_fetch(u16, 2, c->eip);
818 break;
819 case 1:
820 c->modrm_ea += insn_fetch(s8, 1, c->eip);
821 break;
822 case 2:
823 c->modrm_ea += insn_fetch(u16, 2, c->eip);
824 break;
825 }
826 switch (c->modrm_rm) {
827 case 0:
828 c->modrm_ea += bx + si;
829 break;
830 case 1:
831 c->modrm_ea += bx + di;
832 break;
833 case 2:
834 c->modrm_ea += bp + si;
835 break;
836 case 3:
837 c->modrm_ea += bp + di;
838 break;
839 case 4:
840 c->modrm_ea += si;
841 break;
842 case 5:
843 c->modrm_ea += di;
844 break;
845 case 6:
846 if (c->modrm_mod != 0)
847 c->modrm_ea += bp;
848 break;
849 case 7:
850 c->modrm_ea += bx;
851 break;
852 }
853 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
854 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300855 if (!c->has_seg_override)
856 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200857 c->modrm_ea = (u16)c->modrm_ea;
858 } else {
859 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700860 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200861 sib = insn_fetch(u8, 1, c->eip);
862 index_reg |= (sib >> 3) & 7;
863 base_reg |= sib & 7;
864 scale = sib >> 6;
865
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700866 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
867 c->modrm_ea += insn_fetch(s32, 4, c->eip);
868 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200869 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700870 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700872 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
873 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700874 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700875 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877 switch (c->modrm_mod) {
878 case 0:
879 if (c->modrm_rm == 5)
880 c->modrm_ea += insn_fetch(s32, 4, c->eip);
881 break;
882 case 1:
883 c->modrm_ea += insn_fetch(s8, 1, c->eip);
884 break;
885 case 2:
886 c->modrm_ea += insn_fetch(s32, 4, c->eip);
887 break;
888 }
889 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890done:
891 return rc;
892}
893
894static int decode_abs(struct x86_emulate_ctxt *ctxt,
895 struct x86_emulate_ops *ops)
896{
897 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900898 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200899
900 switch (c->ad_bytes) {
901 case 2:
902 c->modrm_ea = insn_fetch(u16, 2, c->eip);
903 break;
904 case 4:
905 c->modrm_ea = insn_fetch(u32, 4, c->eip);
906 break;
907 case 8:
908 c->modrm_ea = insn_fetch(u64, 8, c->eip);
909 break;
910 }
911done:
912 return rc;
913}
914
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200916x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200918 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900919 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200921 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922
923 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
Laurent Viviere4e03de2007-09-18 11:52:50 +0200925 memset(c, 0, sizeof(struct decode_cache));
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200926 c->eip = c->eip_orig = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300927 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800928 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 switch (mode) {
931 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200932 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200934 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 break;
936 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200937 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800939#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = 4;
942 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
944#endif
945 default:
946 return -1;
947 }
948
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200949 c->op_bytes = def_op_bytes;
950 c->ad_bytes = def_ad_bytes;
951
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200953 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200954 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200956 /* switch between 2/4 bytes */
957 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 break;
959 case 0x67: /* address-size override */
960 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200961 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200962 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200965 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300968 case 0x2e: /* CS override */
969 case 0x36: /* SS override */
970 case 0x3e: /* DS override */
971 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 break;
973 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300975 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200977 case 0x40 ... 0x4f: /* REX */
978 if (mode != X86EMUL_MODE_PROT64)
979 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200980 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200981 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200983 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200985 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100986 c->rep_prefix = REPNE_PREFIX;
987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100989 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 default:
992 goto done_prefixes;
993 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200994
995 /* Any legacy prefix after a REX prefix nullifies its effect. */
996
Avi Kivity33615aa2007-10-31 11:15:56 +0200997 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 }
999
1000done_prefixes:
1001
1002 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001004 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001005 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006
1007 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->d = opcode_table[c->b];
1009 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 if (c->b == 0x0f) {
1012 c->twobyte = 1;
1013 c->b = insn_fetch(u8, 1, c->eip);
1014 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001016 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
Avi Kivitye09d0822008-01-18 12:38:59 +02001018 if (c->d & Group) {
1019 group = c->d & GroupMask;
1020 c->modrm = insn_fetch(u8, 1, c->eip);
1021 --c->eip;
1022
1023 group = (group << 3) + ((c->modrm >> 3) & 7);
1024 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1025 c->d = group2_table[group];
1026 else
1027 c->d = group_table[group];
1028 }
1029
1030 /* Unrecognised? */
1031 if (c->d == 0) {
1032 DPRINTF("Cannot emulate %02x\n", c->b);
1033 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 }
1035
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001036 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1037 c->op_bytes = 8;
1038
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040 if (c->d & ModRM)
1041 rc = decode_modrm(ctxt, ops);
1042 else if (c->d & MemAbs)
1043 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001044 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001047 if (!c->has_seg_override)
1048 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001049
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001050 if (!(!c->twobyte && c->b == 0x8d))
1051 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001052
1053 if (c->ad_bytes != 8)
1054 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055 /*
1056 * Decode and fetch the source operand: register, memory
1057 * or immediate.
1058 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001059 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 case SrcNone:
1061 break;
1062 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001063 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 break;
1065 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001066 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 goto srcmem_common;
1068 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001069 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 goto srcmem_common;
1071 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001072 c->src.bytes = (c->d & ByteOp) ? 1 :
1073 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001074 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001075 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001076 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001077 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001078 /*
1079 * For instructions with a ModR/M byte, switch to register
1080 * access if Mod = 3.
1081 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001082 if ((c->d & ModRM) && c->modrm_mod == 3) {
1083 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001084 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001085 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001086 break;
1087 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001088 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 break;
1090 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001091 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001092 c->src.type = OP_IMM;
1093 c->src.ptr = (unsigned long *)c->eip;
1094 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1095 if (c->src.bytes == 8)
1096 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001098 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001100 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 break;
1102 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 break;
1105 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001106 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 break;
1108 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001109 if ((c->d & SrcMask) == SrcImmU) {
1110 switch (c->src.bytes) {
1111 case 1:
1112 c->src.val &= 0xff;
1113 break;
1114 case 2:
1115 c->src.val &= 0xffff;
1116 break;
1117 case 4:
1118 c->src.val &= 0xffffffff;
1119 break;
1120 }
1121 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 break;
1123 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001124 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001125 c->src.type = OP_IMM;
1126 c->src.ptr = (unsigned long *)c->eip;
1127 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001128 if ((c->d & SrcMask) == SrcImmByte)
1129 c->src.val = insn_fetch(s8, 1, c->eip);
1130 else
1131 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001133 case SrcOne:
1134 c->src.bytes = 1;
1135 c->src.val = 1;
1136 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 }
1138
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001139 /*
1140 * Decode and fetch the second source operand: register, memory
1141 * or immediate.
1142 */
1143 switch (c->d & Src2Mask) {
1144 case Src2None:
1145 break;
1146 case Src2CL:
1147 c->src2.bytes = 1;
1148 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1149 break;
1150 case Src2ImmByte:
1151 c->src2.type = OP_IMM;
1152 c->src2.ptr = (unsigned long *)c->eip;
1153 c->src2.bytes = 1;
1154 c->src2.val = insn_fetch(u8, 1, c->eip);
1155 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001156 case Src2Imm16:
1157 c->src2.type = OP_IMM;
1158 c->src2.ptr = (unsigned long *)c->eip;
1159 c->src2.bytes = 2;
1160 c->src2.val = insn_fetch(u16, 2, c->eip);
1161 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001162 case Src2One:
1163 c->src2.bytes = 1;
1164 c->src2.val = 1;
1165 break;
1166 }
1167
Avi Kivity038e51d2007-01-22 20:40:40 -08001168 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001169 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001170 case ImplicitOps:
1171 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001172 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001173 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001174 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001175 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001176 break;
1177 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001178 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001179 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001180 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001181 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001182 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001183 break;
1184 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001185 c->dst.type = OP_MEM;
1186 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001187 case DstAcc:
1188 c->dst.type = OP_REG;
1189 c->dst.bytes = c->op_bytes;
1190 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1191 switch (c->op_bytes) {
1192 case 1:
1193 c->dst.val = *(u8 *)c->dst.ptr;
1194 break;
1195 case 2:
1196 c->dst.val = *(u16 *)c->dst.ptr;
1197 break;
1198 case 4:
1199 c->dst.val = *(u32 *)c->dst.ptr;
1200 break;
1201 }
1202 c->dst.orig_val = c->dst.val;
1203 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001204 }
1205
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001206 if (c->rip_relative)
1207 c->modrm_ea += c->eip;
1208
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001209done:
1210 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1211}
1212
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1214{
1215 struct decode_cache *c = &ctxt->decode;
1216
1217 c->dst.type = OP_MEM;
1218 c->dst.bytes = c->op_bytes;
1219 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001220 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001221 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001222 c->regs[VCPU_REGS_RSP]);
1223}
1224
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001225static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001226 struct x86_emulate_ops *ops,
1227 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001228{
1229 struct decode_cache *c = &ctxt->decode;
1230 int rc;
1231
Avi Kivity781d0ed2008-11-27 18:00:28 +02001232 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1233 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001234 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001235 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001236 return rc;
1237
Avi Kivity350f69d2009-01-05 11:12:40 +02001238 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001239 return rc;
1240}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001241
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001242static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1243 struct x86_emulate_ops *ops,
1244 void *dest, int len)
1245{
1246 int rc;
1247 unsigned long val, change_mask;
1248 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1249 int cpl = kvm_x86_ops->get_cpl(ctxt->vcpu);
1250
1251 rc = emulate_pop(ctxt, ops, &val, len);
1252 if (rc != X86EMUL_CONTINUE)
1253 return rc;
1254
1255 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1256 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1257
1258 switch(ctxt->mode) {
1259 case X86EMUL_MODE_PROT64:
1260 case X86EMUL_MODE_PROT32:
1261 case X86EMUL_MODE_PROT16:
1262 if (cpl == 0)
1263 change_mask |= EFLG_IOPL;
1264 if (cpl <= iopl)
1265 change_mask |= EFLG_IF;
1266 break;
1267 case X86EMUL_MODE_VM86:
1268 if (iopl < 3) {
1269 kvm_inject_gp(ctxt->vcpu, 0);
1270 return X86EMUL_PROPAGATE_FAULT;
1271 }
1272 change_mask |= EFLG_IF;
1273 break;
1274 default: /* real mode */
1275 change_mask |= (EFLG_IOPL | EFLG_IF);
1276 break;
1277 }
1278
1279 *(unsigned long *)dest =
1280 (ctxt->eflags & ~change_mask) | (val & change_mask);
1281
1282 return rc;
1283}
1284
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001285static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1286{
1287 struct decode_cache *c = &ctxt->decode;
1288 struct kvm_segment segment;
1289
1290 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1291
1292 c->src.val = segment.selector;
1293 emulate_push(ctxt);
1294}
1295
1296static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1297 struct x86_emulate_ops *ops, int seg)
1298{
1299 struct decode_cache *c = &ctxt->decode;
1300 unsigned long selector;
1301 int rc;
1302
1303 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001304 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001305 return rc;
1306
Gleb Natapovc6975182010-02-18 12:15:01 +02001307 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001308 return rc;
1309}
1310
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001311static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1312{
1313 struct decode_cache *c = &ctxt->decode;
1314 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1315 int reg = VCPU_REGS_RAX;
1316
1317 while (reg <= VCPU_REGS_RDI) {
1318 (reg == VCPU_REGS_RSP) ?
1319 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1320
1321 emulate_push(ctxt);
1322 ++reg;
1323 }
1324}
1325
1326static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1327 struct x86_emulate_ops *ops)
1328{
1329 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001330 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001331 int reg = VCPU_REGS_RDI;
1332
1333 while (reg >= VCPU_REGS_RAX) {
1334 if (reg == VCPU_REGS_RSP) {
1335 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1336 c->op_bytes);
1337 --reg;
1338 }
1339
1340 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001341 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001342 break;
1343 --reg;
1344 }
1345 return rc;
1346}
1347
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001348static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1349 struct x86_emulate_ops *ops)
1350{
1351 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001352
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001353 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001354}
1355
Laurent Vivier05f086f2007-09-24 11:10:55 +02001356static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001357{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001358 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001359 switch (c->modrm_reg) {
1360 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001361 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001362 break;
1363 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001364 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365 break;
1366 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001367 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368 break;
1369 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001370 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001371 break;
1372 case 4: /* sal/shl */
1373 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001374 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001375 break;
1376 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001377 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378 break;
1379 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001380 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381 break;
1382 }
1383}
1384
1385static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001386 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001387{
1388 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001389 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390
1391 switch (c->modrm_reg) {
1392 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001393 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394 break;
1395 case 2: /* not */
1396 c->dst.val = ~c->dst.val;
1397 break;
1398 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001399 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001400 break;
1401 default:
1402 DPRINTF("Cannot emulate %02x\n", c->b);
1403 rc = X86EMUL_UNHANDLEABLE;
1404 break;
1405 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001406 return rc;
1407}
1408
1409static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001410 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001411{
1412 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413
1414 switch (c->modrm_reg) {
1415 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001416 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001417 break;
1418 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001419 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001420 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001421 case 2: /* call near abs */ {
1422 long int old_eip;
1423 old_eip = c->eip;
1424 c->eip = c->src.val;
1425 c->src.val = old_eip;
1426 emulate_push(ctxt);
1427 break;
1428 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001430 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001431 break;
1432 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001433 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001434 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001435 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001436 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001437}
1438
1439static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1440 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001441 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442{
1443 struct decode_cache *c = &ctxt->decode;
1444 u64 old, new;
1445 int rc;
1446
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001447 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001448 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001449 return rc;
1450
1451 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1452 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1453
1454 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1455 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001456 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001457
1458 } else {
1459 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1460 (u32) c->regs[VCPU_REGS_RBX];
1461
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001462 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001463 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001464 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001465 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001466 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001467 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468}
1469
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001470static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1471 struct x86_emulate_ops *ops)
1472{
1473 struct decode_cache *c = &ctxt->decode;
1474 int rc;
1475 unsigned long cs;
1476
1477 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001478 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001479 return rc;
1480 if (c->op_bytes == 4)
1481 c->eip = (u32)c->eip;
1482 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001483 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001484 return rc;
Gleb Natapovc6975182010-02-18 12:15:01 +02001485 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001486 return rc;
1487}
1488
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001489static inline int writeback(struct x86_emulate_ctxt *ctxt,
1490 struct x86_emulate_ops *ops)
1491{
1492 int rc;
1493 struct decode_cache *c = &ctxt->decode;
1494
1495 switch (c->dst.type) {
1496 case OP_REG:
1497 /* The 4-byte case *is* correct:
1498 * in 64-bit mode we zero-extend.
1499 */
1500 switch (c->dst.bytes) {
1501 case 1:
1502 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1503 break;
1504 case 2:
1505 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1506 break;
1507 case 4:
1508 *c->dst.ptr = (u32)c->dst.val;
1509 break; /* 64b: zero-ext */
1510 case 8:
1511 *c->dst.ptr = c->dst.val;
1512 break;
1513 }
1514 break;
1515 case OP_MEM:
1516 if (c->lock_prefix)
1517 rc = ops->cmpxchg_emulated(
1518 (unsigned long)c->dst.ptr,
1519 &c->dst.orig_val,
1520 &c->dst.val,
1521 c->dst.bytes,
1522 ctxt->vcpu);
1523 else
1524 rc = ops->write_emulated(
1525 (unsigned long)c->dst.ptr,
1526 &c->dst.val,
1527 c->dst.bytes,
1528 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001529 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001530 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001531 break;
1532 case OP_NONE:
1533 /* no writeback */
1534 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001535 default:
1536 break;
1537 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001538 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539}
1540
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301541static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001542{
1543 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1544 /*
1545 * an sti; sti; sequence only disable interrupts for the first
1546 * instruction. So, if the last instruction, be it emulated or
1547 * not, left the system with the INT_STI flag enabled, it
1548 * means that the last instruction is an sti. We should not
1549 * leave the flag on in this case. The same goes for mov ss
1550 */
1551 if (!(int_shadow & mask))
1552 ctxt->interruptibility = mask;
1553}
1554
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001555static inline void
1556setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1557 struct kvm_segment *cs, struct kvm_segment *ss)
1558{
1559 memset(cs, 0, sizeof(struct kvm_segment));
1560 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1561 memset(ss, 0, sizeof(struct kvm_segment));
1562
1563 cs->l = 0; /* will be adjusted later */
1564 cs->base = 0; /* flat segment */
1565 cs->g = 1; /* 4kb granularity */
1566 cs->limit = 0xffffffff; /* 4GB limit */
1567 cs->type = 0x0b; /* Read, Execute, Accessed */
1568 cs->s = 1;
1569 cs->dpl = 0; /* will be adjusted later */
1570 cs->present = 1;
1571 cs->db = 1;
1572
1573 ss->unusable = 0;
1574 ss->base = 0; /* flat segment */
1575 ss->limit = 0xffffffff; /* 4GB limit */
1576 ss->g = 1; /* 4kb granularity */
1577 ss->s = 1;
1578 ss->type = 0x03; /* Read/Write, Accessed */
1579 ss->db = 1; /* 32bit stack segment */
1580 ss->dpl = 0;
1581 ss->present = 1;
1582}
1583
1584static int
1585emulate_syscall(struct x86_emulate_ctxt *ctxt)
1586{
1587 struct decode_cache *c = &ctxt->decode;
1588 struct kvm_segment cs, ss;
1589 u64 msr_data;
1590
1591 /* syscall is not available in real mode */
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001592 if (ctxt->mode == X86EMUL_MODE_REAL || ctxt->mode == X86EMUL_MODE_VM86)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001593 return X86EMUL_UNHANDLEABLE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001594
1595 setup_syscalls_segments(ctxt, &cs, &ss);
1596
1597 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1598 msr_data >>= 32;
1599 cs.selector = (u16)(msr_data & 0xfffc);
1600 ss.selector = (u16)(msr_data + 8);
1601
1602 if (is_long_mode(ctxt->vcpu)) {
1603 cs.db = 0;
1604 cs.l = 1;
1605 }
1606 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1607 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1608
1609 c->regs[VCPU_REGS_RCX] = c->eip;
1610 if (is_long_mode(ctxt->vcpu)) {
1611#ifdef CONFIG_X86_64
1612 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1613
1614 kvm_x86_ops->get_msr(ctxt->vcpu,
1615 ctxt->mode == X86EMUL_MODE_PROT64 ?
1616 MSR_LSTAR : MSR_CSTAR, &msr_data);
1617 c->eip = msr_data;
1618
1619 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1620 ctxt->eflags &= ~(msr_data | EFLG_RF);
1621#endif
1622 } else {
1623 /* legacy mode */
1624 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1625 c->eip = (u32)msr_data;
1626
1627 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1628 }
1629
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001630 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001631}
1632
Andre Przywara8c604352009-06-18 12:56:01 +02001633static int
1634emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1635{
1636 struct decode_cache *c = &ctxt->decode;
1637 struct kvm_segment cs, ss;
1638 u64 msr_data;
1639
Gleb Natapova0044752010-02-10 14:21:31 +02001640 /* inject #GP if in real mode */
1641 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001642 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001643 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001644 }
1645
1646 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1647 * Therefore, we inject an #UD.
1648 */
1649 if (ctxt->mode == X86EMUL_MODE_PROT64)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001650 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001651
1652 setup_syscalls_segments(ctxt, &cs, &ss);
1653
1654 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1655 switch (ctxt->mode) {
1656 case X86EMUL_MODE_PROT32:
1657 if ((msr_data & 0xfffc) == 0x0) {
1658 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001659 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001660 }
1661 break;
1662 case X86EMUL_MODE_PROT64:
1663 if (msr_data == 0x0) {
1664 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001665 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001666 }
1667 break;
1668 }
1669
1670 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1671 cs.selector = (u16)msr_data;
1672 cs.selector &= ~SELECTOR_RPL_MASK;
1673 ss.selector = cs.selector + 8;
1674 ss.selector &= ~SELECTOR_RPL_MASK;
1675 if (ctxt->mode == X86EMUL_MODE_PROT64
1676 || is_long_mode(ctxt->vcpu)) {
1677 cs.db = 0;
1678 cs.l = 1;
1679 }
1680
1681 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1682 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1683
1684 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1685 c->eip = msr_data;
1686
1687 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1688 c->regs[VCPU_REGS_RSP] = msr_data;
1689
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001690 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001691}
1692
Andre Przywara4668f052009-06-18 12:56:02 +02001693static int
1694emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1695{
1696 struct decode_cache *c = &ctxt->decode;
1697 struct kvm_segment cs, ss;
1698 u64 msr_data;
1699 int usermode;
1700
Gleb Natapova0044752010-02-10 14:21:31 +02001701 /* inject #GP if in real mode or Virtual 8086 mode */
1702 if (ctxt->mode == X86EMUL_MODE_REAL ||
1703 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001704 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001705 return X86EMUL_UNHANDLEABLE;
Andre Przywara4668f052009-06-18 12:56:02 +02001706 }
1707
Andre Przywara4668f052009-06-18 12:56:02 +02001708 setup_syscalls_segments(ctxt, &cs, &ss);
1709
1710 if ((c->rex_prefix & 0x8) != 0x0)
1711 usermode = X86EMUL_MODE_PROT64;
1712 else
1713 usermode = X86EMUL_MODE_PROT32;
1714
1715 cs.dpl = 3;
1716 ss.dpl = 3;
1717 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1718 switch (usermode) {
1719 case X86EMUL_MODE_PROT32:
1720 cs.selector = (u16)(msr_data + 16);
1721 if ((msr_data & 0xfffc) == 0x0) {
1722 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001723 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001724 }
1725 ss.selector = (u16)(msr_data + 24);
1726 break;
1727 case X86EMUL_MODE_PROT64:
1728 cs.selector = (u16)(msr_data + 32);
1729 if (msr_data == 0x0) {
1730 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001731 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001732 }
1733 ss.selector = cs.selector + 8;
1734 cs.db = 0;
1735 cs.l = 1;
1736 break;
1737 }
1738 cs.selector |= SELECTOR_RPL_MASK;
1739 ss.selector |= SELECTOR_RPL_MASK;
1740
1741 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1742 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1743
1744 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1745 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1746
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001747 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001748}
1749
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001750static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
1751{
1752 int iopl;
1753 if (ctxt->mode == X86EMUL_MODE_REAL)
1754 return false;
1755 if (ctxt->mode == X86EMUL_MODE_VM86)
1756 return true;
1757 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1758 return kvm_x86_ops->get_cpl(ctxt->vcpu) > iopl;
1759}
1760
1761static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1762 struct x86_emulate_ops *ops,
1763 u16 port, u16 len)
1764{
1765 struct kvm_segment tr_seg;
1766 int r;
1767 u16 io_bitmap_ptr;
1768 u8 perm, bit_idx = port & 0x7;
1769 unsigned mask = (1 << len) - 1;
1770
1771 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1772 if (tr_seg.unusable)
1773 return false;
1774 if (tr_seg.limit < 103)
1775 return false;
1776 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1777 NULL);
1778 if (r != X86EMUL_CONTINUE)
1779 return false;
1780 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1781 return false;
1782 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1783 ctxt->vcpu, NULL);
1784 if (r != X86EMUL_CONTINUE)
1785 return false;
1786 if ((perm >> bit_idx) & mask)
1787 return false;
1788 return true;
1789}
1790
1791static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1792 struct x86_emulate_ops *ops,
1793 u16 port, u16 len)
1794{
1795 if (emulator_bad_iopl(ctxt))
1796 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1797 return false;
1798 return true;
1799}
1800
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001801int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001802x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001803{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001804 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001805 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001806 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001807 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001808 unsigned int port;
1809 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001810 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001811
Glauber Costa310b5d32009-05-12 16:21:06 -04001812 ctxt->interruptibility = 0;
1813
Laurent Vivier34273182007-09-18 11:27:37 +02001814 /* Shadow copy of register state. Committed on successful emulation.
1815 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1816 * modify them.
1817 */
1818
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001819 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001820 saved_eip = c->eip;
1821
Gleb Natapov11616242010-02-11 14:43:14 +02001822 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
1823 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1824 goto done;
1825 }
1826
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001827 /* LOCK prefix is allowed only with some instructions */
1828 if (c->lock_prefix && !(c->d & Lock)) {
1829 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1830 goto done;
1831 }
1832
Gleb Natapove92805a2010-02-10 14:21:35 +02001833 /* Privileged instruction can be executed only in CPL=0 */
1834 if ((c->d & Priv) && kvm_x86_ops->get_cpl(ctxt->vcpu)) {
1835 kvm_inject_gp(ctxt->vcpu, 0);
1836 goto done;
1837 }
1838
Avi Kivityc7e75a32007-10-28 16:34:25 +02001839 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001840 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001841
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001842 if (c->rep_prefix && (c->d & String)) {
1843 /* All REP prefixes have the same first termination condition */
1844 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001845 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001846 goto done;
1847 }
1848 /* The second termination condition only applies for REPE
1849 * and REPNE. Test if the repeat string operation prefix is
1850 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1851 * corresponding termination condition according to:
1852 * - if REPE/REPZ and ZF = 0 then done
1853 * - if REPNE/REPNZ and ZF = 1 then done
1854 */
1855 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1856 (c->b == 0xae) || (c->b == 0xaf)) {
1857 if ((c->rep_prefix == REPE_PREFIX) &&
1858 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001859 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001860 goto done;
1861 }
1862 if ((c->rep_prefix == REPNE_PREFIX) &&
1863 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001864 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001865 goto done;
1866 }
1867 }
1868 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001869 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001870 }
1871
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001872 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001873 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001874 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001875 rc = ops->read_emulated((unsigned long)c->src.ptr,
1876 &c->src.val,
1877 c->src.bytes,
1878 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001879 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001880 goto done;
1881 c->src.orig_val = c->src.val;
1882 }
1883
1884 if ((c->d & DstMask) == ImplicitOps)
1885 goto special_insn;
1886
1887
1888 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001889 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001890 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1891 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001892 if (c->d & BitOp) {
1893 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001894
Laurent Viviere4e03de2007-09-18 11:52:50 +02001895 c->dst.ptr = (void *)c->dst.ptr +
1896 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001897 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001898 if (!(c->d & Mov)) {
1899 /* optimisation - avoid slow emulated read */
1900 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1901 &c->dst.val,
1902 c->dst.bytes,
1903 ctxt->vcpu);
1904 if (rc != X86EMUL_CONTINUE)
1905 goto done;
1906 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001907 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001908 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001909
Avi Kivity018a98d2007-11-27 19:30:56 +02001910special_insn:
1911
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 goto twobyte_insn;
1914
Laurent Viviere4e03de2007-09-18 11:52:50 +02001915 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 case 0x00 ... 0x05:
1917 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001918 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001920 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001921 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1922 break;
1923 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001924 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001925 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001926 goto done;
1927 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 case 0x08 ... 0x0d:
1929 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001930 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001932 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001933 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1934 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 case 0x10 ... 0x15:
1936 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001937 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001939 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001940 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1941 break;
1942 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001943 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001944 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001945 goto done;
1946 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 case 0x18 ... 0x1d:
1948 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001949 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001951 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001952 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1953 break;
1954 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001955 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001956 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001957 goto done;
1958 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001959 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001961 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 break;
1963 case 0x28 ... 0x2d:
1964 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001965 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
1967 case 0x30 ... 0x35:
1968 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001969 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 break;
1971 case 0x38 ... 0x3d:
1972 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001973 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001975 case 0x40 ... 0x47: /* inc r16/r32 */
1976 emulate_1op("inc", c->dst, ctxt->eflags);
1977 break;
1978 case 0x48 ... 0x4f: /* dec r16/r32 */
1979 emulate_1op("dec", c->dst, ctxt->eflags);
1980 break;
1981 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001982 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001983 break;
1984 case 0x58 ... 0x5f: /* pop reg */
1985 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001986 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001987 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02001988 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001989 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001990 case 0x60: /* pusha */
1991 emulate_pusha(ctxt);
1992 break;
1993 case 0x61: /* popa */
1994 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001995 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001996 goto done;
1997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001999 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002001 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002003 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002004 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002005 emulate_push(ctxt);
2006 break;
2007 case 0x6c: /* insb */
2008 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002009 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2010 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2011 kvm_inject_gp(ctxt->vcpu, 0);
2012 goto done;
2013 }
2014 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002015 1,
2016 (c->d & ByteOp) ? 1 : c->op_bytes,
2017 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002018 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002019 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002020 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002021 c->regs[VCPU_REGS_RDI]),
2022 c->rep_prefix,
2023 c->regs[VCPU_REGS_RDX]) == 0) {
2024 c->eip = saved_eip;
2025 return -1;
2026 }
2027 return 0;
2028 case 0x6e: /* outsb */
2029 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002030 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2031 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2032 kvm_inject_gp(ctxt->vcpu, 0);
2033 goto done;
2034 }
Avi Kivity851ba692009-08-24 11:10:17 +03002035 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002036 0,
2037 (c->d & ByteOp) ? 1 : c->op_bytes,
2038 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002039 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002040 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002041 register_address(c,
2042 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002043 c->regs[VCPU_REGS_RSI]),
2044 c->rep_prefix,
2045 c->regs[VCPU_REGS_RDX]) == 0) {
2046 c->eip = saved_eip;
2047 return -1;
2048 }
2049 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002050 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002051 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002052 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002053 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002055 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 case 0:
2057 goto add;
2058 case 1:
2059 goto or;
2060 case 2:
2061 goto adc;
2062 case 3:
2063 goto sbb;
2064 case 4:
2065 goto and;
2066 case 5:
2067 goto sub;
2068 case 6:
2069 goto xor;
2070 case 7:
2071 goto cmp;
2072 }
2073 break;
2074 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002075 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 break;
2077 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002078 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002080 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002082 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 break;
2084 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002085 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086 break;
2087 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002088 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 break; /* 64b reg: zero-extend */
2090 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002091 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 break;
2093 }
2094 /*
2095 * Write back the memory destination with implicit LOCK
2096 * prefix.
2097 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002098 c->dst.val = c->src.val;
2099 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002102 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002103 case 0x8c: { /* mov r/m, sreg */
2104 struct kvm_segment segreg;
2105
2106 if (c->modrm_reg <= 5)
2107 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2108 else {
2109 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
2110 c->modrm);
2111 goto cannot_emulate;
2112 }
2113 c->dst.val = segreg.selector;
2114 break;
2115 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002116 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002117 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002118 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002119 case 0x8e: { /* mov seg, r/m16 */
2120 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002121
2122 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002123
Gleb Natapovc6975182010-02-18 12:15:01 +02002124 if (c->modrm_reg == VCPU_SREG_CS ||
2125 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002126 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2127 goto done;
2128 }
2129
Glauber Costa310b5d32009-05-12 16:21:06 -04002130 if (c->modrm_reg == VCPU_SREG_SS)
2131 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
2132
Gleb Natapovc6975182010-02-18 12:15:01 +02002133 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002134
2135 c->dst.type = OP_NONE; /* Disable writeback. */
2136 break;
2137 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002139 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002140 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002143 case 0x90: /* nop / xchg r8,rax */
2144 if (!(c->rex_prefix & 1)) { /* nop */
2145 c->dst.type = OP_NONE;
2146 break;
2147 }
2148 case 0x91 ... 0x97: /* xchg reg,rax */
2149 c->src.type = c->dst.type = OP_REG;
2150 c->src.bytes = c->dst.bytes = c->op_bytes;
2151 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2152 c->src.val = *(c->src.ptr);
2153 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002154 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002155 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002156 emulate_push(ctxt);
2157 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002158 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002159 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002160 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002161 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002162 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2163 if (rc != X86EMUL_CONTINUE)
2164 goto done;
2165 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002166 case 0xa0 ... 0xa1: /* mov */
2167 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2168 c->dst.val = c->src.val;
2169 break;
2170 case 0xa2 ... 0xa3: /* mov */
2171 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002174 c->dst.type = OP_MEM;
2175 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002176 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002177 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002178 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002179 rc = ops->read_emulated(register_address(c,
2180 seg_override_base(ctxt, c),
2181 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002182 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002183 c->dst.bytes, ctxt->vcpu);
2184 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002186 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002187 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002188 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002189 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002190 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002191 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 break;
2193 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002194 c->src.type = OP_NONE; /* Disable writeback. */
2195 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002196 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002197 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002198 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002199 rc = ops->read_emulated((unsigned long)c->src.ptr,
2200 &c->src.val,
2201 c->src.bytes,
2202 ctxt->vcpu);
2203 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002204 goto done;
2205
2206 c->dst.type = OP_NONE; /* Disable writeback. */
2207 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002208 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002209 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002210 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002211 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2212 &c->dst.val,
2213 c->dst.bytes,
2214 ctxt->vcpu);
2215 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002216 goto done;
2217
2218 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2219
2220 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2221
Harvey Harrison7a9572752008-02-19 07:40:41 -08002222 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002223 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2224 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002225 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002226 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2227 : c->dst.bytes);
2228
2229 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002231 c->dst.type = OP_MEM;
2232 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002233 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002234 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002235 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002236 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002237 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002238 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002239 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 break;
2241 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002242 c->dst.type = OP_REG;
2243 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2244 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002245 rc = ops->read_emulated(register_address(c,
2246 seg_override_base(ctxt, c),
2247 c->regs[VCPU_REGS_RSI]),
2248 &c->dst.val,
2249 c->dst.bytes,
2250 ctxt->vcpu);
2251 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002253 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002254 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002255 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 break;
2257 case 0xae ... 0xaf: /* scas */
2258 DPRINTF("Urk! I don't handle SCAS.\n");
2259 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002260 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002261 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002262 case 0xc0 ... 0xc1:
2263 emulate_grp2(ctxt);
2264 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002265 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002266 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002267 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002268 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002269 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002270 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2271 mov:
2272 c->dst.val = c->src.val;
2273 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002274 case 0xcb: /* ret far */
2275 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002276 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002277 goto done;
2278 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002279 case 0xd0 ... 0xd1: /* Grp2 */
2280 c->src.val = 1;
2281 emulate_grp2(ctxt);
2282 break;
2283 case 0xd2 ... 0xd3: /* Grp2 */
2284 c->src.val = c->regs[VCPU_REGS_RCX];
2285 emulate_grp2(ctxt);
2286 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002287 case 0xe4: /* inb */
2288 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002289 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002290 io_dir_in = 1;
2291 goto do_io;
2292 case 0xe6: /* outb */
2293 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002294 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002295 io_dir_in = 0;
2296 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002297 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002298 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002299 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002300 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002301 emulate_push(ctxt);
2302 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002303 }
2304 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002305 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002306 case 0xea: /* jmp far */
Gleb Natapovc6975182010-02-18 12:15:01 +02002307 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2308 VCPU_SREG_CS))
2309 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002310
Gleb Natapov782b8772009-04-12 13:36:25 +03002311 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002312 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002313 case 0xeb:
2314 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002315 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002316 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002317 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002318 case 0xec: /* in al,dx */
2319 case 0xed: /* in (e/r)ax,dx */
2320 port = c->regs[VCPU_REGS_RDX];
2321 io_dir_in = 1;
2322 goto do_io;
2323 case 0xee: /* out al,dx */
2324 case 0xef: /* out (e/r)ax,dx */
2325 port = c->regs[VCPU_REGS_RDX];
2326 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002327 do_io:
2328 if (!emulator_io_permited(ctxt, ops, port,
2329 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2330 kvm_inject_gp(ctxt->vcpu, 0);
2331 goto done;
2332 }
2333 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002334 (c->d & ByteOp) ? 1 : c->op_bytes,
2335 port) != 0) {
2336 c->eip = saved_eip;
2337 goto cannot_emulate;
2338 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002339 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002340 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002342 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002343 case 0xf5: /* cmc */
2344 /* complement carry flag from eflags reg */
2345 ctxt->eflags ^= EFLG_CF;
2346 c->dst.type = OP_NONE; /* Disable writeback. */
2347 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002348 case 0xf6 ... 0xf7: /* Grp3 */
2349 rc = emulate_grp3(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002350 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002351 goto done;
2352 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002353 case 0xf8: /* clc */
2354 ctxt->eflags &= ~EFLG_CF;
2355 c->dst.type = OP_NONE; /* Disable writeback. */
2356 break;
2357 case 0xfa: /* cli */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002358 if (emulator_bad_iopl(ctxt))
2359 kvm_inject_gp(ctxt->vcpu, 0);
2360 else {
2361 ctxt->eflags &= ~X86_EFLAGS_IF;
2362 c->dst.type = OP_NONE; /* Disable writeback. */
2363 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002364 break;
2365 case 0xfb: /* sti */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002366 if (emulator_bad_iopl(ctxt))
2367 kvm_inject_gp(ctxt->vcpu, 0);
2368 else {
2369 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
2370 ctxt->eflags |= X86_EFLAGS_IF;
2371 c->dst.type = OP_NONE; /* Disable writeback. */
2372 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002373 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002374 case 0xfc: /* cld */
2375 ctxt->eflags &= ~EFLG_DF;
2376 c->dst.type = OP_NONE; /* Disable writeback. */
2377 break;
2378 case 0xfd: /* std */
2379 ctxt->eflags |= EFLG_DF;
2380 c->dst.type = OP_NONE; /* Disable writeback. */
2381 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002382 case 0xfe ... 0xff: /* Grp4/Grp5 */
2383 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002384 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002385 goto done;
2386 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002388
2389writeback:
2390 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002391 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002392 goto done;
2393
2394 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002395 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002396 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002397
2398done:
2399 if (rc == X86EMUL_UNHANDLEABLE) {
2400 c->eip = saved_eip;
2401 return -1;
2402 }
2403 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404
2405twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002406 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002408 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 u16 size;
2410 unsigned long address;
2411
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002412 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002413 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002414 goto cannot_emulate;
2415
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002416 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002417 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002418 goto done;
2419
Avi Kivity33e38852008-05-21 15:34:25 +03002420 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002421 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002422 /* Disable writeback. */
2423 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002424 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002426 rc = read_descriptor(ctxt, ops, c->src.ptr,
2427 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002428 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 goto done;
2430 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002431 /* Disable writeback. */
2432 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002434 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002435 if (c->modrm_mod == 3) {
2436 switch (c->modrm_rm) {
2437 case 1:
2438 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002439 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002440 goto done;
2441 break;
2442 default:
2443 goto cannot_emulate;
2444 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002445 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002446 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002447 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002448 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002449 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002450 goto done;
2451 realmode_lidt(ctxt->vcpu, size, address);
2452 }
Avi Kivity16286d02008-04-14 14:40:50 +03002453 /* Disable writeback. */
2454 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 break;
2456 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002457 c->dst.bytes = 2;
2458 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 break;
2460 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002461 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2462 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002463 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 break;
2465 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002466 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002467 /* Disable writeback. */
2468 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 break;
2470 default:
2471 goto cannot_emulate;
2472 }
2473 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002474 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002475 rc = emulate_syscall(ctxt);
2476 if (rc != X86EMUL_CONTINUE)
2477 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002478 else
2479 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002480 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002481 case 0x06:
2482 emulate_clts(ctxt->vcpu);
2483 c->dst.type = OP_NONE;
2484 break;
2485 case 0x08: /* invd */
2486 case 0x09: /* wbinvd */
2487 case 0x0d: /* GrpP (prefetch) */
2488 case 0x18: /* Grp16 (prefetch/nop) */
2489 c->dst.type = OP_NONE;
2490 break;
2491 case 0x20: /* mov cr, reg */
2492 if (c->modrm_mod != 3)
2493 goto cannot_emulate;
2494 c->regs[c->modrm_rm] =
2495 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2496 c->dst.type = OP_NONE; /* no writeback */
2497 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002499 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002501 if (emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002502 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002503 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002504 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002506 case 0x22: /* mov reg, cr */
2507 if (c->modrm_mod != 3)
2508 goto cannot_emulate;
2509 realmode_set_cr(ctxt->vcpu,
2510 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2511 c->dst.type = OP_NONE;
2512 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002514 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002516 if (emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002517 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002518 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002519 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002521 case 0x30:
2522 /* wrmsr */
2523 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2524 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002525 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002526 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002527 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002528 }
2529 rc = X86EMUL_CONTINUE;
2530 c->dst.type = OP_NONE;
2531 break;
2532 case 0x32:
2533 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002534 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002535 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002536 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002537 } else {
2538 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2539 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2540 }
2541 rc = X86EMUL_CONTINUE;
2542 c->dst.type = OP_NONE;
2543 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002544 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002545 rc = emulate_sysenter(ctxt);
2546 if (rc != X86EMUL_CONTINUE)
2547 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002548 else
2549 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002550 break;
2551 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002552 rc = emulate_sysexit(ctxt);
2553 if (rc != X86EMUL_CONTINUE)
2554 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002555 else
2556 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002557 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002559 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002560 if (!test_cc(c->b, ctxt->eflags))
2561 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002563 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002564 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002565 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002566 c->dst.type = OP_NONE;
2567 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002568 case 0xa0: /* push fs */
2569 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2570 break;
2571 case 0xa1: /* pop fs */
2572 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002573 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002574 goto done;
2575 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002576 case 0xa3:
2577 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002578 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002579 /* only subword offset */
2580 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002581 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002582 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002583 case 0xa4: /* shld imm8, r, r/m */
2584 case 0xa5: /* shld cl, r, r/m */
2585 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2586 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002587 case 0xa8: /* push gs */
2588 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2589 break;
2590 case 0xa9: /* pop gs */
2591 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002592 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002593 goto done;
2594 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002595 case 0xab:
2596 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002597 /* only subword offset */
2598 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002599 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002600 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002601 case 0xac: /* shrd imm8, r, r/m */
2602 case 0xad: /* shrd cl, r, r/m */
2603 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2604 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002605 case 0xae: /* clflush */
2606 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 case 0xb0 ... 0xb1: /* cmpxchg */
2608 /*
2609 * Save real source value, then compare EAX against
2610 * destination.
2611 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002612 c->src.orig_val = c->src.val;
2613 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002614 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2615 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002617 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 } else {
2619 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002620 c->dst.type = OP_REG;
2621 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622 }
2623 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 case 0xb3:
2625 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002626 /* only subword offset */
2627 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002628 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002631 c->dst.bytes = c->op_bytes;
2632 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2633 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002636 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 case 0:
2638 goto bt;
2639 case 1:
2640 goto bts;
2641 case 2:
2642 goto btr;
2643 case 3:
2644 goto btc;
2645 }
2646 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002647 case 0xbb:
2648 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002649 /* only subword offset */
2650 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002651 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002652 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002654 c->dst.bytes = c->op_bytes;
2655 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2656 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002658 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002659 c->dst.bytes = c->op_bytes;
2660 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2661 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002662 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002664 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002665 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002666 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002667 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002668 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 }
2670 goto writeback;
2671
2672cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002673 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002674 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 return -1;
2676}