blob: 97a740368b307d2916c5064e12f06e5cbfcfde8d [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 Kivityc9eaf202009-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)
Gleb Natapove35b7b92010-02-25 16:36:42 +020088#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
89 in memory and second argument is located
90 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
Avi Kivity43bb19c2008-01-18 12:46:50 +020093enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020094 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020095 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020096 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020097};
98
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010099static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200101 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300103 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300104 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200106 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200108 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
109 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200111 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300113 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300114 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200116 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300118 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300119 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200121 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200123 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200125 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
127 0, 0, 0, 0,
128 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200129 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
131 0, 0, 0, 0,
132 /* 0x38 - 0x3F */
133 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
134 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200135 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
136 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700137 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200138 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700139 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200140 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300141 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200142 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
143 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300144 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200145 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
146 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700147 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200148 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
149 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700150 0, 0, 0, 0,
151 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300152 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300153 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
154 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300155 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300156 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300158 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300159 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200162 Group | Group1_80, Group | Group1_81,
163 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200165 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0x88 - 0x8F */
167 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
168 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200169 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200170 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300171 /* 0x90 - 0x97 */
172 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
173 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300174 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300175 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200177 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
178 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200179 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
180 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200182 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
183 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
184 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300185 /* 0xB0 - 0xB7 */
186 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
187 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
188 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 /* 0xB8 - 0xBF */
191 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
192 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
193 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300196 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200197 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300198 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300200 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300201 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xD0 - 0xD7 */
203 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 0, 0, 0, 0,
206 /* 0xD8 - 0xDF */
207 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300208 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300209 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300210 ByteOp | SrcImmUByte, SrcImmUByte,
211 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300212 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300213 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300214 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300215 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
216 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xF0 - 0xF7 */
218 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200219 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700221 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300222 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223};
224
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100225static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200227 0, Group | GroupDual | Group7, 0, 0,
228 0, ImplicitOps, ImplicitOps | Priv, 0,
229 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
230 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 /* 0x10 - 0x1F */
232 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200234 ModRM | ImplicitOps | Priv, ModRM | Priv,
235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 0, 0, 0, 0, 0, 0, 0, 0,
238 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200239 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
240 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200241 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 /* 0x40 - 0x47 */
243 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
244 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 /* 0x48 - 0x4F */
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 /* 0x50 - 0x5F */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 /* 0x60 - 0x6F */
255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256 /* 0x70 - 0x7F */
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300259 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
260 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261 /* 0x90 - 0x9F */
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
263 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300264 ImplicitOps | Stack, ImplicitOps | Stack,
265 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100266 DstMem | SrcReg | Src2ImmByte | ModRM,
267 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300269 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200270 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100271 DstMem | SrcReg | Src2ImmByte | ModRM,
272 DstMem | SrcReg | Src2CL | ModRM,
273 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200275 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
276 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
278 DstReg | SrcMem16 | ModRM | Mov,
279 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200280 0, 0,
281 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
283 DstReg | SrcMem16 | ModRM | Mov,
284 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200285 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
286 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800287 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 /* 0xD0 - 0xDF */
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 /* 0xE0 - 0xEF */
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292 /* 0xF0 - 0xFF */
293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
294};
295
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100296static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200297 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200298 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 | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200306 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200307 DstMem | SrcImm | ModRM | Lock,
308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200315 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200316 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 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200324 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200325 DstMem | SrcImmByte | ModRM | Lock,
326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200333 [Group1A*8] =
334 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200335 [Group3_Byte*8] =
336 ByteOp | SrcImm | DstMem | ModRM, 0,
337 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
338 0, 0, 0, 0,
339 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400340 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300341 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200342 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200343 [Group4*8] =
344 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
345 0, 0, 0, 0, 0, 0,
346 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300347 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
348 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300349 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200350 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200351 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300352 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200353 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200354 [Group8*8] =
355 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200356 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
357 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200358 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200359 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200360};
361
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100362static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200363 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200364 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300365 SrcNone | ModRM | DstMem | Mov, 0,
366 SrcMem16 | ModRM | Mov, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200367 [Group9*8] =
368 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200369};
370
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200372#define EFLG_ID (1<<21)
373#define EFLG_VIP (1<<20)
374#define EFLG_VIF (1<<19)
375#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200376#define EFLG_VM (1<<17)
377#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200378#define EFLG_IOPL (3<<12)
379#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380#define EFLG_OF (1<<11)
381#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200382#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200383#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384#define EFLG_SF (1<<7)
385#define EFLG_ZF (1<<6)
386#define EFLG_AF (1<<4)
387#define EFLG_PF (1<<2)
388#define EFLG_CF (1<<0)
389
390/*
391 * Instruction emulation:
392 * Most instructions are emulated directly via a fragment of inline assembly
393 * code. This allows us to save/restore EFLAGS and thus very easily pick up
394 * any modified flags.
395 */
396
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800397#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398#define _LO32 "k" /* force 32-bit operand */
399#define _STK "%%rsp" /* stack pointer */
400#elif defined(__i386__)
401#define _LO32 "" /* force 32-bit operand */
402#define _STK "%%esp" /* stack pointer */
403#endif
404
405/*
406 * These EFLAGS bits are restored from saved value during emulation, and
407 * any changes are written back to the saved value after emulation.
408 */
409#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
410
411/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200412#define _PRE_EFLAGS(_sav, _msk, _tmp) \
413 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
414 "movl %"_sav",%"_LO32 _tmp"; " \
415 "push %"_tmp"; " \
416 "push %"_tmp"; " \
417 "movl %"_msk",%"_LO32 _tmp"; " \
418 "andl %"_LO32 _tmp",("_STK"); " \
419 "pushf; " \
420 "notl %"_LO32 _tmp"; " \
421 "andl %"_LO32 _tmp",("_STK"); " \
422 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
423 "pop %"_tmp"; " \
424 "orl %"_LO32 _tmp",("_STK"); " \
425 "popf; " \
426 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427
428/* After executing instruction: write-back necessary bits in EFLAGS. */
429#define _POST_EFLAGS(_sav, _msk, _tmp) \
430 /* _sav |= EFLAGS & _msk; */ \
431 "pushf; " \
432 "pop %"_tmp"; " \
433 "andl %"_msk",%"_LO32 _tmp"; " \
434 "orl %"_LO32 _tmp",%"_sav"; "
435
Avi Kivitydda96d82008-11-26 15:14:10 +0200436#ifdef CONFIG_X86_64
437#define ON64(x) x
438#else
439#define ON64(x)
440#endif
441
Avi Kivity6b7ad612008-11-26 15:30:45 +0200442#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
443 do { \
444 __asm__ __volatile__ ( \
445 _PRE_EFLAGS("0", "4", "2") \
446 _op _suffix " %"_x"3,%1; " \
447 _POST_EFLAGS("0", "4", "2") \
448 : "=m" (_eflags), "=m" ((_dst).val), \
449 "=&r" (_tmp) \
450 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200451 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200452
453
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454/* Raw emulation: instruction has two explicit operands. */
455#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200456 do { \
457 unsigned long _tmp; \
458 \
459 switch ((_dst).bytes) { \
460 case 2: \
461 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
462 break; \
463 case 4: \
464 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
465 break; \
466 case 8: \
467 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
468 break; \
469 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 } while (0)
471
472#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
473 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200474 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400475 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200477 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 break; \
479 default: \
480 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
481 _wx, _wy, _lx, _ly, _qx, _qy); \
482 break; \
483 } \
484 } while (0)
485
486/* Source operand is byte-sized and may be restricted to just %cl. */
487#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
488 __emulate_2op(_op, _src, _dst, _eflags, \
489 "b", "c", "b", "c", "b", "c", "b", "c")
490
491/* Source operand is byte, word, long or quad sized. */
492#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
493 __emulate_2op(_op, _src, _dst, _eflags, \
494 "b", "q", "w", "r", _LO32, "r", "", "r")
495
496/* Source operand is word, long or quad sized. */
497#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
498 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
499 "w", "r", _LO32, "r", "", "r")
500
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100501/* Instruction has three operands and one operand is stored in ECX register */
502#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
503 do { \
504 unsigned long _tmp; \
505 _type _clv = (_cl).val; \
506 _type _srcv = (_src).val; \
507 _type _dstv = (_dst).val; \
508 \
509 __asm__ __volatile__ ( \
510 _PRE_EFLAGS("0", "5", "2") \
511 _op _suffix " %4,%1 \n" \
512 _POST_EFLAGS("0", "5", "2") \
513 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
514 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
515 ); \
516 \
517 (_cl).val = (unsigned long) _clv; \
518 (_src).val = (unsigned long) _srcv; \
519 (_dst).val = (unsigned long) _dstv; \
520 } while (0)
521
522#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
523 do { \
524 switch ((_dst).bytes) { \
525 case 2: \
526 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
527 "w", unsigned short); \
528 break; \
529 case 4: \
530 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
531 "l", unsigned int); \
532 break; \
533 case 8: \
534 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
535 "q", unsigned long)); \
536 break; \
537 } \
538 } while (0)
539
Avi Kivitydda96d82008-11-26 15:14:10 +0200540#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 do { \
542 unsigned long _tmp; \
543 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200544 __asm__ __volatile__ ( \
545 _PRE_EFLAGS("0", "3", "2") \
546 _op _suffix " %1; " \
547 _POST_EFLAGS("0", "3", "2") \
548 : "=m" (_eflags), "+m" ((_dst).val), \
549 "=&r" (_tmp) \
550 : "i" (EFLAGS_MASK)); \
551 } while (0)
552
553/* Instruction has only one explicit operand (no source operand). */
554#define emulate_1op(_op, _dst, _eflags) \
555 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400556 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200557 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
558 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
559 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
560 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 } \
562 } while (0)
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564/* Fetch next part of the instruction being emulated. */
565#define insn_fetch(_type, _size, _eip) \
566({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200567 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400568 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 goto done; \
570 (_eip) += (_size); \
571 (_type)_x; \
572})
573
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800574static inline unsigned long ad_mask(struct decode_cache *c)
575{
576 return (1UL << (c->ad_bytes << 3)) - 1;
577}
578
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800580static inline unsigned long
581address_mask(struct decode_cache *c, unsigned long reg)
582{
583 if (c->ad_bytes == sizeof(unsigned long))
584 return reg;
585 else
586 return reg & ad_mask(c);
587}
588
589static inline unsigned long
590register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
591{
592 return base + address_mask(c, reg);
593}
594
Harvey Harrison7a9572752008-02-19 07:40:41 -0800595static inline void
596register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
597{
598 if (c->ad_bytes == sizeof(unsigned long))
599 *reg += inc;
600 else
601 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
602}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603
Harvey Harrison7a9572752008-02-19 07:40:41 -0800604static inline void jmp_rel(struct decode_cache *c, int rel)
605{
606 register_address_increment(c, &c->eip, rel);
607}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300608
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300609static void set_seg_override(struct decode_cache *c, int seg)
610{
611 c->has_seg_override = true;
612 c->seg_override = seg;
613}
614
615static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
616{
617 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
618 return 0;
619
620 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
621}
622
623static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
624 struct decode_cache *c)
625{
626 if (!c->has_seg_override)
627 return 0;
628
629 return seg_base(ctxt, c->seg_override);
630}
631
632static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
633{
634 return seg_base(ctxt, VCPU_SREG_ES);
635}
636
637static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
638{
639 return seg_base(ctxt, VCPU_SREG_SS);
640}
641
Avi Kivity62266862007-11-20 13:15:52 +0200642static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
643 struct x86_emulate_ops *ops,
644 unsigned long linear, u8 *dest)
645{
646 struct fetch_cache *fc = &ctxt->decode.fetch;
647 int rc;
648 int size;
649
650 if (linear < fc->start || linear >= fc->end) {
651 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200652 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900653 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200654 return rc;
655 fc->start = linear;
656 fc->end = linear + size;
657 }
658 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900659 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200660}
661
662static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
663 struct x86_emulate_ops *ops,
664 unsigned long eip, void *dest, unsigned size)
665{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900666 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200667
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200668 /* x86 instructions are limited to 15 bytes. */
669 if (eip + size - ctxt->decode.eip_orig > 15)
670 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200671 eip += ctxt->cs_base;
672 while (size--) {
673 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900674 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200675 return rc;
676 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900677 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200678}
679
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000680/*
681 * Given the 'reg' portion of a ModRM byte, and a register block, return a
682 * pointer into the block that addresses the relevant register.
683 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
684 */
685static void *decode_register(u8 modrm_reg, unsigned long *regs,
686 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687{
688 void *p;
689
690 p = &regs[modrm_reg];
691 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
692 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
693 return p;
694}
695
696static int read_descriptor(struct x86_emulate_ctxt *ctxt,
697 struct x86_emulate_ops *ops,
698 void *ptr,
699 u16 *size, unsigned long *address, int op_bytes)
700{
701 int rc;
702
703 if (op_bytes == 2)
704 op_bytes = 3;
705 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300706 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200707 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900708 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300710 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200711 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 return rc;
713}
714
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300715static int test_cc(unsigned int condition, unsigned int flags)
716{
717 int rc = 0;
718
719 switch ((condition & 15) >> 1) {
720 case 0: /* o */
721 rc |= (flags & EFLG_OF);
722 break;
723 case 1: /* b/c/nae */
724 rc |= (flags & EFLG_CF);
725 break;
726 case 2: /* z/e */
727 rc |= (flags & EFLG_ZF);
728 break;
729 case 3: /* be/na */
730 rc |= (flags & (EFLG_CF|EFLG_ZF));
731 break;
732 case 4: /* s */
733 rc |= (flags & EFLG_SF);
734 break;
735 case 5: /* p/pe */
736 rc |= (flags & EFLG_PF);
737 break;
738 case 7: /* le/ng */
739 rc |= (flags & EFLG_ZF);
740 /* fall through */
741 case 6: /* l/nge */
742 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
743 break;
744 }
745
746 /* Odd condition identifiers (lsb == 1) have inverted sense. */
747 return (!!rc ^ (condition & 1));
748}
749
Avi Kivity3c118e22007-10-31 10:27:04 +0200750static void decode_register_operand(struct operand *op,
751 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200752 int inhibit_bytereg)
753{
Avi Kivity33615aa2007-10-31 11:15:56 +0200754 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200755 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200756
757 if (!(c->d & ModRM))
758 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200759 op->type = OP_REG;
760 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200761 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200762 op->val = *(u8 *)op->ptr;
763 op->bytes = 1;
764 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200765 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200766 op->bytes = c->op_bytes;
767 switch (op->bytes) {
768 case 2:
769 op->val = *(u16 *)op->ptr;
770 break;
771 case 4:
772 op->val = *(u32 *)op->ptr;
773 break;
774 case 8:
775 op->val = *(u64 *) op->ptr;
776 break;
777 }
778 }
779 op->orig_val = op->val;
780}
781
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200782static int decode_modrm(struct x86_emulate_ctxt *ctxt,
783 struct x86_emulate_ops *ops)
784{
785 struct decode_cache *c = &ctxt->decode;
786 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700787 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900788 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200789
790 if (c->rex_prefix) {
791 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
792 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
793 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
794 }
795
796 c->modrm = insn_fetch(u8, 1, c->eip);
797 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
798 c->modrm_reg |= (c->modrm & 0x38) >> 3;
799 c->modrm_rm |= (c->modrm & 0x07);
800 c->modrm_ea = 0;
801 c->use_modrm_ea = 1;
802
803 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300804 c->modrm_ptr = decode_register(c->modrm_rm,
805 c->regs, c->d & ByteOp);
806 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200807 return rc;
808 }
809
810 if (c->ad_bytes == 2) {
811 unsigned bx = c->regs[VCPU_REGS_RBX];
812 unsigned bp = c->regs[VCPU_REGS_RBP];
813 unsigned si = c->regs[VCPU_REGS_RSI];
814 unsigned di = c->regs[VCPU_REGS_RDI];
815
816 /* 16-bit ModR/M decode. */
817 switch (c->modrm_mod) {
818 case 0:
819 if (c->modrm_rm == 6)
820 c->modrm_ea += insn_fetch(u16, 2, c->eip);
821 break;
822 case 1:
823 c->modrm_ea += insn_fetch(s8, 1, c->eip);
824 break;
825 case 2:
826 c->modrm_ea += insn_fetch(u16, 2, c->eip);
827 break;
828 }
829 switch (c->modrm_rm) {
830 case 0:
831 c->modrm_ea += bx + si;
832 break;
833 case 1:
834 c->modrm_ea += bx + di;
835 break;
836 case 2:
837 c->modrm_ea += bp + si;
838 break;
839 case 3:
840 c->modrm_ea += bp + di;
841 break;
842 case 4:
843 c->modrm_ea += si;
844 break;
845 case 5:
846 c->modrm_ea += di;
847 break;
848 case 6:
849 if (c->modrm_mod != 0)
850 c->modrm_ea += bp;
851 break;
852 case 7:
853 c->modrm_ea += bx;
854 break;
855 }
856 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
857 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300858 if (!c->has_seg_override)
859 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200860 c->modrm_ea = (u16)c->modrm_ea;
861 } else {
862 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700863 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200864 sib = insn_fetch(u8, 1, c->eip);
865 index_reg |= (sib >> 3) & 7;
866 base_reg |= sib & 7;
867 scale = sib >> 6;
868
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700869 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
870 c->modrm_ea += insn_fetch(s32, 4, c->eip);
871 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200872 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700873 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700875 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
876 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700877 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700878 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200879 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200880 switch (c->modrm_mod) {
881 case 0:
882 if (c->modrm_rm == 5)
883 c->modrm_ea += insn_fetch(s32, 4, c->eip);
884 break;
885 case 1:
886 c->modrm_ea += insn_fetch(s8, 1, c->eip);
887 break;
888 case 2:
889 c->modrm_ea += insn_fetch(s32, 4, c->eip);
890 break;
891 }
892 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893done:
894 return rc;
895}
896
897static int decode_abs(struct x86_emulate_ctxt *ctxt,
898 struct x86_emulate_ops *ops)
899{
900 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900901 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902
903 switch (c->ad_bytes) {
904 case 2:
905 c->modrm_ea = insn_fetch(u16, 2, c->eip);
906 break;
907 case 4:
908 c->modrm_ea = insn_fetch(u32, 4, c->eip);
909 break;
910 case 8:
911 c->modrm_ea = insn_fetch(u64, 8, c->eip);
912 break;
913 }
914done:
915 return rc;
916}
917
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200919x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200921 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900922 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200924 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
926 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
Laurent Viviere4e03de2007-09-18 11:52:50 +0200928 memset(c, 0, sizeof(struct decode_cache));
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200929 c->eip = c->eip_orig = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300930 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800931 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
933 switch (mode) {
934 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200935 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200937 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 break;
939 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200940 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800942#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200944 def_op_bytes = 4;
945 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 break;
947#endif
948 default:
949 return -1;
950 }
951
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200952 c->op_bytes = def_op_bytes;
953 c->ad_bytes = def_ad_bytes;
954
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200956 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200957 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200959 /* switch between 2/4 bytes */
960 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 break;
962 case 0x67: /* address-size override */
963 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200965 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200968 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300971 case 0x2e: /* CS override */
972 case 0x36: /* SS override */
973 case 0x3e: /* DS override */
974 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 break;
976 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300978 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200980 case 0x40 ... 0x4f: /* REX */
981 if (mode != X86EMUL_MODE_PROT64)
982 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200983 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200984 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200986 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200988 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100989 c->rep_prefix = REPNE_PREFIX;
990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100992 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 default:
995 goto done_prefixes;
996 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200997
998 /* Any legacy prefix after a REX prefix nullifies its effect. */
999
Avi Kivity33615aa2007-10-31 11:15:56 +02001000 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 }
1002
1003done_prefixes:
1004
1005 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001007 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
1010 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 c->d = opcode_table[c->b];
1012 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 if (c->b == 0x0f) {
1015 c->twobyte = 1;
1016 c->b = insn_fetch(u8, 1, c->eip);
1017 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001019 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
Avi Kivitye09d0822008-01-18 12:38:59 +02001021 if (c->d & Group) {
1022 group = c->d & GroupMask;
1023 c->modrm = insn_fetch(u8, 1, c->eip);
1024 --c->eip;
1025
1026 group = (group << 3) + ((c->modrm >> 3) & 7);
1027 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1028 c->d = group2_table[group];
1029 else
1030 c->d = group_table[group];
1031 }
1032
1033 /* Unrecognised? */
1034 if (c->d == 0) {
1035 DPRINTF("Cannot emulate %02x\n", c->b);
1036 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 }
1038
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001039 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1040 c->op_bytes = 8;
1041
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001043 if (c->d & ModRM)
1044 rc = decode_modrm(ctxt, ops);
1045 else if (c->d & MemAbs)
1046 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001047 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001050 if (!c->has_seg_override)
1051 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001052
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001053 if (!(!c->twobyte && c->b == 0x8d))
1054 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001055
1056 if (c->ad_bytes != 8)
1057 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 /*
1059 * Decode and fetch the source operand: register, memory
1060 * or immediate.
1061 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001062 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 case SrcNone:
1064 break;
1065 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001066 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 break;
1068 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001069 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 goto srcmem_common;
1071 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001072 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 goto srcmem_common;
1074 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001075 c->src.bytes = (c->d & ByteOp) ? 1 :
1076 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001077 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001078 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001079 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001080 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001081 /*
1082 * For instructions with a ModR/M byte, switch to register
1083 * access if Mod = 3.
1084 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001085 if ((c->d & ModRM) && c->modrm_mod == 3) {
1086 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001087 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001088 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001089 break;
1090 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
1093 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001094 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001095 c->src.type = OP_IMM;
1096 c->src.ptr = (unsigned long *)c->eip;
1097 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1098 if (c->src.bytes == 8)
1099 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001101 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 break;
1105 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001106 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 break;
1108 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001109 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 break;
1111 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001112 if ((c->d & SrcMask) == SrcImmU) {
1113 switch (c->src.bytes) {
1114 case 1:
1115 c->src.val &= 0xff;
1116 break;
1117 case 2:
1118 c->src.val &= 0xffff;
1119 break;
1120 case 4:
1121 c->src.val &= 0xffffffff;
1122 break;
1123 }
1124 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 break;
1126 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001127 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001128 c->src.type = OP_IMM;
1129 c->src.ptr = (unsigned long *)c->eip;
1130 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001131 if ((c->d & SrcMask) == SrcImmByte)
1132 c->src.val = insn_fetch(s8, 1, c->eip);
1133 else
1134 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001136 case SrcOne:
1137 c->src.bytes = 1;
1138 c->src.val = 1;
1139 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140 }
1141
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001142 /*
1143 * Decode and fetch the second source operand: register, memory
1144 * or immediate.
1145 */
1146 switch (c->d & Src2Mask) {
1147 case Src2None:
1148 break;
1149 case Src2CL:
1150 c->src2.bytes = 1;
1151 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1152 break;
1153 case Src2ImmByte:
1154 c->src2.type = OP_IMM;
1155 c->src2.ptr = (unsigned long *)c->eip;
1156 c->src2.bytes = 1;
1157 c->src2.val = insn_fetch(u8, 1, c->eip);
1158 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001159 case Src2Imm16:
1160 c->src2.type = OP_IMM;
1161 c->src2.ptr = (unsigned long *)c->eip;
1162 c->src2.bytes = 2;
1163 c->src2.val = insn_fetch(u16, 2, c->eip);
1164 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001165 case Src2One:
1166 c->src2.bytes = 1;
1167 c->src2.val = 1;
1168 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001169 case Src2Mem16:
1170 c->src2.bytes = 2;
1171 c->src2.type = OP_MEM;
1172 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001173 }
1174
Avi Kivity038e51d2007-01-22 20:40:40 -08001175 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001176 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001177 case ImplicitOps:
1178 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001179 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001180 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001181 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001182 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001183 break;
1184 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001185 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001186 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001187 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001188 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001189 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001190 break;
1191 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001192 c->dst.type = OP_MEM;
1193 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001194 case DstAcc:
1195 c->dst.type = OP_REG;
1196 c->dst.bytes = c->op_bytes;
1197 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1198 switch (c->op_bytes) {
1199 case 1:
1200 c->dst.val = *(u8 *)c->dst.ptr;
1201 break;
1202 case 2:
1203 c->dst.val = *(u16 *)c->dst.ptr;
1204 break;
1205 case 4:
1206 c->dst.val = *(u32 *)c->dst.ptr;
1207 break;
1208 }
1209 c->dst.orig_val = c->dst.val;
1210 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001211 }
1212
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001213 if (c->rip_relative)
1214 c->modrm_ea += c->eip;
1215
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001216done:
1217 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1218}
1219
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001220static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1221{
1222 struct decode_cache *c = &ctxt->decode;
1223
1224 c->dst.type = OP_MEM;
1225 c->dst.bytes = c->op_bytes;
1226 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001227 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001228 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001229 c->regs[VCPU_REGS_RSP]);
1230}
1231
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001232static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001233 struct x86_emulate_ops *ops,
1234 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001235{
1236 struct decode_cache *c = &ctxt->decode;
1237 int rc;
1238
Avi Kivity781d0ed2008-11-27 18:00:28 +02001239 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1240 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001241 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001242 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001243 return rc;
1244
Avi Kivity350f69d2009-01-05 11:12:40 +02001245 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001246 return rc;
1247}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001249static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1250 struct x86_emulate_ops *ops,
1251 void *dest, int len)
1252{
1253 int rc;
1254 unsigned long val, change_mask;
1255 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1256 int cpl = kvm_x86_ops->get_cpl(ctxt->vcpu);
1257
1258 rc = emulate_pop(ctxt, ops, &val, len);
1259 if (rc != X86EMUL_CONTINUE)
1260 return rc;
1261
1262 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1263 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1264
1265 switch(ctxt->mode) {
1266 case X86EMUL_MODE_PROT64:
1267 case X86EMUL_MODE_PROT32:
1268 case X86EMUL_MODE_PROT16:
1269 if (cpl == 0)
1270 change_mask |= EFLG_IOPL;
1271 if (cpl <= iopl)
1272 change_mask |= EFLG_IF;
1273 break;
1274 case X86EMUL_MODE_VM86:
1275 if (iopl < 3) {
1276 kvm_inject_gp(ctxt->vcpu, 0);
1277 return X86EMUL_PROPAGATE_FAULT;
1278 }
1279 change_mask |= EFLG_IF;
1280 break;
1281 default: /* real mode */
1282 change_mask |= (EFLG_IOPL | EFLG_IF);
1283 break;
1284 }
1285
1286 *(unsigned long *)dest =
1287 (ctxt->eflags & ~change_mask) | (val & change_mask);
1288
1289 return rc;
1290}
1291
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001292static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1293{
1294 struct decode_cache *c = &ctxt->decode;
1295 struct kvm_segment segment;
1296
1297 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1298
1299 c->src.val = segment.selector;
1300 emulate_push(ctxt);
1301}
1302
1303static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1304 struct x86_emulate_ops *ops, int seg)
1305{
1306 struct decode_cache *c = &ctxt->decode;
1307 unsigned long selector;
1308 int rc;
1309
1310 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001311 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001312 return rc;
1313
Gleb Natapovc6975182010-02-18 12:15:01 +02001314 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001315 return rc;
1316}
1317
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001318static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1319{
1320 struct decode_cache *c = &ctxt->decode;
1321 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1322 int reg = VCPU_REGS_RAX;
1323
1324 while (reg <= VCPU_REGS_RDI) {
1325 (reg == VCPU_REGS_RSP) ?
1326 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1327
1328 emulate_push(ctxt);
1329 ++reg;
1330 }
1331}
1332
1333static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1334 struct x86_emulate_ops *ops)
1335{
1336 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001337 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001338 int reg = VCPU_REGS_RDI;
1339
1340 while (reg >= VCPU_REGS_RAX) {
1341 if (reg == VCPU_REGS_RSP) {
1342 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1343 c->op_bytes);
1344 --reg;
1345 }
1346
1347 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001348 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001349 break;
1350 --reg;
1351 }
1352 return rc;
1353}
1354
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001355static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1356 struct x86_emulate_ops *ops)
1357{
1358 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001359
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001360 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001361}
1362
Laurent Vivier05f086f2007-09-24 11:10:55 +02001363static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001364{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001365 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001366 switch (c->modrm_reg) {
1367 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001368 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001369 break;
1370 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001371 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001372 break;
1373 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001374 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001375 break;
1376 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001377 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378 break;
1379 case 4: /* sal/shl */
1380 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382 break;
1383 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001384 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001385 break;
1386 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001387 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001388 break;
1389 }
1390}
1391
1392static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001393 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394{
1395 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001396 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001397
1398 switch (c->modrm_reg) {
1399 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001400 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401 break;
1402 case 2: /* not */
1403 c->dst.val = ~c->dst.val;
1404 break;
1405 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001406 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001407 break;
1408 default:
1409 DPRINTF("Cannot emulate %02x\n", c->b);
1410 rc = X86EMUL_UNHANDLEABLE;
1411 break;
1412 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413 return rc;
1414}
1415
1416static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001417 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001418{
1419 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001420
1421 switch (c->modrm_reg) {
1422 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001423 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001424 break;
1425 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001426 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001427 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001428 case 2: /* call near abs */ {
1429 long int old_eip;
1430 old_eip = c->eip;
1431 c->eip = c->src.val;
1432 c->src.val = old_eip;
1433 emulate_push(ctxt);
1434 break;
1435 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001436 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001437 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438 break;
1439 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001440 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001441 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001443 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001444}
1445
1446static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1447 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001448 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001449{
1450 struct decode_cache *c = &ctxt->decode;
1451 u64 old, new;
1452 int rc;
1453
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001454 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001455 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001456 return rc;
1457
1458 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1459 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1460
1461 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1462 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001463 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001464
1465 } else {
1466 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1467 (u32) c->regs[VCPU_REGS_RBX];
1468
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001469 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001470 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001471 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001472 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001474 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475}
1476
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001477static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1478 struct x86_emulate_ops *ops)
1479{
1480 struct decode_cache *c = &ctxt->decode;
1481 int rc;
1482 unsigned long cs;
1483
1484 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001485 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001486 return rc;
1487 if (c->op_bytes == 4)
1488 c->eip = (u32)c->eip;
1489 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001490 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001491 return rc;
Gleb Natapovc6975182010-02-18 12:15:01 +02001492 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001493 return rc;
1494}
1495
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001496static inline int writeback(struct x86_emulate_ctxt *ctxt,
1497 struct x86_emulate_ops *ops)
1498{
1499 int rc;
1500 struct decode_cache *c = &ctxt->decode;
1501
1502 switch (c->dst.type) {
1503 case OP_REG:
1504 /* The 4-byte case *is* correct:
1505 * in 64-bit mode we zero-extend.
1506 */
1507 switch (c->dst.bytes) {
1508 case 1:
1509 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1510 break;
1511 case 2:
1512 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1513 break;
1514 case 4:
1515 *c->dst.ptr = (u32)c->dst.val;
1516 break; /* 64b: zero-ext */
1517 case 8:
1518 *c->dst.ptr = c->dst.val;
1519 break;
1520 }
1521 break;
1522 case OP_MEM:
1523 if (c->lock_prefix)
1524 rc = ops->cmpxchg_emulated(
1525 (unsigned long)c->dst.ptr,
1526 &c->dst.orig_val,
1527 &c->dst.val,
1528 c->dst.bytes,
1529 ctxt->vcpu);
1530 else
1531 rc = ops->write_emulated(
1532 (unsigned long)c->dst.ptr,
1533 &c->dst.val,
1534 c->dst.bytes,
1535 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001536 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001537 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001538 break;
1539 case OP_NONE:
1540 /* no writeback */
1541 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001542 default:
1543 break;
1544 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001545 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001546}
1547
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301548static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001549{
1550 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1551 /*
1552 * an sti; sti; sequence only disable interrupts for the first
1553 * instruction. So, if the last instruction, be it emulated or
1554 * not, left the system with the INT_STI flag enabled, it
1555 * means that the last instruction is an sti. We should not
1556 * leave the flag on in this case. The same goes for mov ss
1557 */
1558 if (!(int_shadow & mask))
1559 ctxt->interruptibility = mask;
1560}
1561
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001562static inline void
1563setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1564 struct kvm_segment *cs, struct kvm_segment *ss)
1565{
1566 memset(cs, 0, sizeof(struct kvm_segment));
1567 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1568 memset(ss, 0, sizeof(struct kvm_segment));
1569
1570 cs->l = 0; /* will be adjusted later */
1571 cs->base = 0; /* flat segment */
1572 cs->g = 1; /* 4kb granularity */
1573 cs->limit = 0xffffffff; /* 4GB limit */
1574 cs->type = 0x0b; /* Read, Execute, Accessed */
1575 cs->s = 1;
1576 cs->dpl = 0; /* will be adjusted later */
1577 cs->present = 1;
1578 cs->db = 1;
1579
1580 ss->unusable = 0;
1581 ss->base = 0; /* flat segment */
1582 ss->limit = 0xffffffff; /* 4GB limit */
1583 ss->g = 1; /* 4kb granularity */
1584 ss->s = 1;
1585 ss->type = 0x03; /* Read/Write, Accessed */
1586 ss->db = 1; /* 32bit stack segment */
1587 ss->dpl = 0;
1588 ss->present = 1;
1589}
1590
1591static int
1592emulate_syscall(struct x86_emulate_ctxt *ctxt)
1593{
1594 struct decode_cache *c = &ctxt->decode;
1595 struct kvm_segment cs, ss;
1596 u64 msr_data;
1597
1598 /* syscall is not available in real mode */
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001599 if (ctxt->mode == X86EMUL_MODE_REAL || ctxt->mode == X86EMUL_MODE_VM86)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001600 return X86EMUL_UNHANDLEABLE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001601
1602 setup_syscalls_segments(ctxt, &cs, &ss);
1603
1604 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1605 msr_data >>= 32;
1606 cs.selector = (u16)(msr_data & 0xfffc);
1607 ss.selector = (u16)(msr_data + 8);
1608
1609 if (is_long_mode(ctxt->vcpu)) {
1610 cs.db = 0;
1611 cs.l = 1;
1612 }
1613 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1614 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1615
1616 c->regs[VCPU_REGS_RCX] = c->eip;
1617 if (is_long_mode(ctxt->vcpu)) {
1618#ifdef CONFIG_X86_64
1619 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1620
1621 kvm_x86_ops->get_msr(ctxt->vcpu,
1622 ctxt->mode == X86EMUL_MODE_PROT64 ?
1623 MSR_LSTAR : MSR_CSTAR, &msr_data);
1624 c->eip = msr_data;
1625
1626 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1627 ctxt->eflags &= ~(msr_data | EFLG_RF);
1628#endif
1629 } else {
1630 /* legacy mode */
1631 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1632 c->eip = (u32)msr_data;
1633
1634 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1635 }
1636
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001637 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001638}
1639
Andre Przywara8c604352009-06-18 12:56:01 +02001640static int
1641emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1642{
1643 struct decode_cache *c = &ctxt->decode;
1644 struct kvm_segment cs, ss;
1645 u64 msr_data;
1646
Gleb Natapova0044752010-02-10 14:21:31 +02001647 /* inject #GP if in real mode */
1648 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001649 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001650 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001651 }
1652
1653 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1654 * Therefore, we inject an #UD.
1655 */
1656 if (ctxt->mode == X86EMUL_MODE_PROT64)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001657 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001658
1659 setup_syscalls_segments(ctxt, &cs, &ss);
1660
1661 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1662 switch (ctxt->mode) {
1663 case X86EMUL_MODE_PROT32:
1664 if ((msr_data & 0xfffc) == 0x0) {
1665 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001666 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001667 }
1668 break;
1669 case X86EMUL_MODE_PROT64:
1670 if (msr_data == 0x0) {
1671 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001672 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001673 }
1674 break;
1675 }
1676
1677 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1678 cs.selector = (u16)msr_data;
1679 cs.selector &= ~SELECTOR_RPL_MASK;
1680 ss.selector = cs.selector + 8;
1681 ss.selector &= ~SELECTOR_RPL_MASK;
1682 if (ctxt->mode == X86EMUL_MODE_PROT64
1683 || is_long_mode(ctxt->vcpu)) {
1684 cs.db = 0;
1685 cs.l = 1;
1686 }
1687
1688 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1689 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1690
1691 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1692 c->eip = msr_data;
1693
1694 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1695 c->regs[VCPU_REGS_RSP] = msr_data;
1696
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001697 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001698}
1699
Andre Przywara4668f052009-06-18 12:56:02 +02001700static int
1701emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1702{
1703 struct decode_cache *c = &ctxt->decode;
1704 struct kvm_segment cs, ss;
1705 u64 msr_data;
1706 int usermode;
1707
Gleb Natapova0044752010-02-10 14:21:31 +02001708 /* inject #GP if in real mode or Virtual 8086 mode */
1709 if (ctxt->mode == X86EMUL_MODE_REAL ||
1710 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001711 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001712 return X86EMUL_UNHANDLEABLE;
Andre Przywara4668f052009-06-18 12:56:02 +02001713 }
1714
Andre Przywara4668f052009-06-18 12:56:02 +02001715 setup_syscalls_segments(ctxt, &cs, &ss);
1716
1717 if ((c->rex_prefix & 0x8) != 0x0)
1718 usermode = X86EMUL_MODE_PROT64;
1719 else
1720 usermode = X86EMUL_MODE_PROT32;
1721
1722 cs.dpl = 3;
1723 ss.dpl = 3;
1724 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1725 switch (usermode) {
1726 case X86EMUL_MODE_PROT32:
1727 cs.selector = (u16)(msr_data + 16);
1728 if ((msr_data & 0xfffc) == 0x0) {
1729 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001730 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001731 }
1732 ss.selector = (u16)(msr_data + 24);
1733 break;
1734 case X86EMUL_MODE_PROT64:
1735 cs.selector = (u16)(msr_data + 32);
1736 if (msr_data == 0x0) {
1737 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001738 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001739 }
1740 ss.selector = cs.selector + 8;
1741 cs.db = 0;
1742 cs.l = 1;
1743 break;
1744 }
1745 cs.selector |= SELECTOR_RPL_MASK;
1746 ss.selector |= SELECTOR_RPL_MASK;
1747
1748 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1749 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1750
1751 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1752 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1753
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001754 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001755}
1756
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001757static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
1758{
1759 int iopl;
1760 if (ctxt->mode == X86EMUL_MODE_REAL)
1761 return false;
1762 if (ctxt->mode == X86EMUL_MODE_VM86)
1763 return true;
1764 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1765 return kvm_x86_ops->get_cpl(ctxt->vcpu) > iopl;
1766}
1767
1768static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1769 struct x86_emulate_ops *ops,
1770 u16 port, u16 len)
1771{
1772 struct kvm_segment tr_seg;
1773 int r;
1774 u16 io_bitmap_ptr;
1775 u8 perm, bit_idx = port & 0x7;
1776 unsigned mask = (1 << len) - 1;
1777
1778 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1779 if (tr_seg.unusable)
1780 return false;
1781 if (tr_seg.limit < 103)
1782 return false;
1783 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1784 NULL);
1785 if (r != X86EMUL_CONTINUE)
1786 return false;
1787 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1788 return false;
1789 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1790 ctxt->vcpu, NULL);
1791 if (r != X86EMUL_CONTINUE)
1792 return false;
1793 if ((perm >> bit_idx) & mask)
1794 return false;
1795 return true;
1796}
1797
1798static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1799 struct x86_emulate_ops *ops,
1800 u16 port, u16 len)
1801{
1802 if (emulator_bad_iopl(ctxt))
1803 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1804 return false;
1805 return true;
1806}
1807
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001808int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001809x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001810{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001811 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001812 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001813 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001814 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001815 unsigned int port;
1816 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001817 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001818
Glauber Costa310b5d32009-05-12 16:21:06 -04001819 ctxt->interruptibility = 0;
1820
Laurent Vivier34273182007-09-18 11:27:37 +02001821 /* Shadow copy of register state. Committed on successful emulation.
1822 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1823 * modify them.
1824 */
1825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001826 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001827 saved_eip = c->eip;
1828
Gleb Natapov11616242010-02-11 14:43:14 +02001829 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
1830 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1831 goto done;
1832 }
1833
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001834 /* LOCK prefix is allowed only with some instructions */
1835 if (c->lock_prefix && !(c->d & Lock)) {
1836 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1837 goto done;
1838 }
1839
Gleb Natapove92805a2010-02-10 14:21:35 +02001840 /* Privileged instruction can be executed only in CPL=0 */
1841 if ((c->d & Priv) && kvm_x86_ops->get_cpl(ctxt->vcpu)) {
1842 kvm_inject_gp(ctxt->vcpu, 0);
1843 goto done;
1844 }
1845
Avi Kivityc7e75a32007-10-28 16:34:25 +02001846 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001847 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001848
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001849 if (c->rep_prefix && (c->d & String)) {
1850 /* All REP prefixes have the same first termination condition */
1851 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001852 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001853 goto done;
1854 }
1855 /* The second termination condition only applies for REPE
1856 * and REPNE. Test if the repeat string operation prefix is
1857 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1858 * corresponding termination condition according to:
1859 * - if REPE/REPZ and ZF = 0 then done
1860 * - if REPNE/REPNZ and ZF = 1 then done
1861 */
1862 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1863 (c->b == 0xae) || (c->b == 0xaf)) {
1864 if ((c->rep_prefix == REPE_PREFIX) &&
1865 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001866 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001867 goto done;
1868 }
1869 if ((c->rep_prefix == REPNE_PREFIX) &&
1870 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001871 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001872 goto done;
1873 }
1874 }
1875 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001876 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001877 }
1878
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001879 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001880 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001881 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001882 rc = ops->read_emulated((unsigned long)c->src.ptr,
1883 &c->src.val,
1884 c->src.bytes,
1885 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001886 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001887 goto done;
1888 c->src.orig_val = c->src.val;
1889 }
1890
Gleb Natapove35b7b92010-02-25 16:36:42 +02001891 if (c->src2.type == OP_MEM) {
1892 c->src2.ptr = (unsigned long *)(memop + c->src.bytes);
1893 c->src2.val = 0;
1894 rc = ops->read_emulated((unsigned long)c->src2.ptr,
1895 &c->src2.val,
1896 c->src2.bytes,
1897 ctxt->vcpu);
1898 if (rc != X86EMUL_CONTINUE)
1899 goto done;
1900 }
1901
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001902 if ((c->d & DstMask) == ImplicitOps)
1903 goto special_insn;
1904
1905
1906 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001907 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001908 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1909 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001910 if (c->d & BitOp) {
1911 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001912
Laurent Viviere4e03de2007-09-18 11:52:50 +02001913 c->dst.ptr = (void *)c->dst.ptr +
1914 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001915 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001916 if (!(c->d & Mov)) {
1917 /* optimisation - avoid slow emulated read */
1918 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1919 &c->dst.val,
1920 c->dst.bytes,
1921 ctxt->vcpu);
1922 if (rc != X86EMUL_CONTINUE)
1923 goto done;
1924 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001925 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001927
Avi Kivity018a98d2007-11-27 19:30:56 +02001928special_insn:
1929
Laurent Viviere4e03de2007-09-18 11:52:50 +02001930 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 goto twobyte_insn;
1932
Laurent Viviere4e03de2007-09-18 11:52:50 +02001933 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 case 0x00 ... 0x05:
1935 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001936 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001938 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001939 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1940 break;
1941 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001942 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001943 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001944 goto done;
1945 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 case 0x08 ... 0x0d:
1947 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001948 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001950 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001951 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 case 0x10 ... 0x15:
1954 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001955 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001957 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001958 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1959 break;
1960 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001961 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001962 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001963 goto done;
1964 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 case 0x18 ... 0x1d:
1966 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001967 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001969 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001970 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1971 break;
1972 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001973 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001974 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001975 goto done;
1976 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001977 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001979 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980 break;
1981 case 0x28 ... 0x2d:
1982 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001983 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 break;
1985 case 0x30 ... 0x35:
1986 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001987 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 break;
1989 case 0x38 ... 0x3d:
1990 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001991 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001993 case 0x40 ... 0x47: /* inc r16/r32 */
1994 emulate_1op("inc", c->dst, ctxt->eflags);
1995 break;
1996 case 0x48 ... 0x4f: /* dec r16/r32 */
1997 emulate_1op("dec", c->dst, ctxt->eflags);
1998 break;
1999 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002000 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002001 break;
2002 case 0x58 ... 0x5f: /* pop reg */
2003 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002004 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002005 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002006 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002007 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002008 case 0x60: /* pusha */
2009 emulate_pusha(ctxt);
2010 break;
2011 case 0x61: /* popa */
2012 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002013 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002014 goto done;
2015 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002017 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002019 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002021 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002022 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002023 emulate_push(ctxt);
2024 break;
2025 case 0x6c: /* insb */
2026 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002027 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2028 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2029 kvm_inject_gp(ctxt->vcpu, 0);
2030 goto done;
2031 }
2032 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002033 1,
2034 (c->d & ByteOp) ? 1 : c->op_bytes,
2035 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002036 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002037 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002038 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002039 c->regs[VCPU_REGS_RDI]),
2040 c->rep_prefix,
2041 c->regs[VCPU_REGS_RDX]) == 0) {
2042 c->eip = saved_eip;
2043 return -1;
2044 }
2045 return 0;
2046 case 0x6e: /* outsb */
2047 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002048 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2049 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2050 kvm_inject_gp(ctxt->vcpu, 0);
2051 goto done;
2052 }
Avi Kivity851ba692009-08-24 11:10:17 +03002053 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002054 0,
2055 (c->d & ByteOp) ? 1 : c->op_bytes,
2056 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002057 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002058 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002059 register_address(c,
2060 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002061 c->regs[VCPU_REGS_RSI]),
2062 c->rep_prefix,
2063 c->regs[VCPU_REGS_RDX]) == 0) {
2064 c->eip = saved_eip;
2065 return -1;
2066 }
2067 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002068 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002069 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002070 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002073 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074 case 0:
2075 goto add;
2076 case 1:
2077 goto or;
2078 case 2:
2079 goto adc;
2080 case 3:
2081 goto sbb;
2082 case 4:
2083 goto and;
2084 case 5:
2085 goto sub;
2086 case 6:
2087 goto xor;
2088 case 7:
2089 goto cmp;
2090 }
2091 break;
2092 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002093 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 break;
2095 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002096 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002098 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002100 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 break;
2102 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002103 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 break;
2105 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002106 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 break; /* 64b reg: zero-extend */
2108 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002109 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 break;
2111 }
2112 /*
2113 * Write back the memory destination with implicit LOCK
2114 * prefix.
2115 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 c->dst.val = c->src.val;
2117 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002120 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002121 case 0x8c: { /* mov r/m, sreg */
2122 struct kvm_segment segreg;
2123
2124 if (c->modrm_reg <= 5)
2125 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2126 else {
2127 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
2128 c->modrm);
2129 goto cannot_emulate;
2130 }
2131 c->dst.val = segreg.selector;
2132 break;
2133 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002134 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002135 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002136 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002137 case 0x8e: { /* mov seg, r/m16 */
2138 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002139
2140 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002141
Gleb Natapovc6975182010-02-18 12:15:01 +02002142 if (c->modrm_reg == VCPU_SREG_CS ||
2143 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002144 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2145 goto done;
2146 }
2147
Glauber Costa310b5d32009-05-12 16:21:06 -04002148 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002149 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002150
Gleb Natapovc6975182010-02-18 12:15:01 +02002151 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002152
2153 c->dst.type = OP_NONE; /* Disable writeback. */
2154 break;
2155 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002157 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002158 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002161 case 0x90: /* nop / xchg r8,rax */
2162 if (!(c->rex_prefix & 1)) { /* nop */
2163 c->dst.type = OP_NONE;
2164 break;
2165 }
2166 case 0x91 ... 0x97: /* xchg reg,rax */
2167 c->src.type = c->dst.type = OP_REG;
2168 c->src.bytes = c->dst.bytes = c->op_bytes;
2169 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2170 c->src.val = *(c->src.ptr);
2171 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002172 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002173 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002174 emulate_push(ctxt);
2175 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002176 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002177 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002178 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002179 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002180 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2181 if (rc != X86EMUL_CONTINUE)
2182 goto done;
2183 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002184 case 0xa0 ... 0xa1: /* mov */
2185 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2186 c->dst.val = c->src.val;
2187 break;
2188 case 0xa2 ... 0xa3: /* mov */
2189 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2190 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002192 c->dst.type = OP_MEM;
2193 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002194 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002195 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002196 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002197 rc = ops->read_emulated(register_address(c,
2198 seg_override_base(ctxt, c),
2199 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002200 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002201 c->dst.bytes, ctxt->vcpu);
2202 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002204 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002205 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002206 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002207 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002208 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002209 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210 break;
2211 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002212 c->src.type = OP_NONE; /* Disable writeback. */
2213 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002214 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002215 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002216 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002217 rc = ops->read_emulated((unsigned long)c->src.ptr,
2218 &c->src.val,
2219 c->src.bytes,
2220 ctxt->vcpu);
2221 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002222 goto done;
2223
2224 c->dst.type = OP_NONE; /* Disable writeback. */
2225 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002226 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002227 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002228 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002229 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2230 &c->dst.val,
2231 c->dst.bytes,
2232 ctxt->vcpu);
2233 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002234 goto done;
2235
2236 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2237
2238 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2239
Harvey Harrison7a9572752008-02-19 07:40:41 -08002240 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002241 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2242 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002243 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002244 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2245 : c->dst.bytes);
2246
2247 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002249 c->dst.type = OP_MEM;
2250 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002251 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002252 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002253 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002254 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002255 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002256 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002257 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 break;
2259 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002260 c->dst.type = OP_REG;
2261 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2262 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002263 rc = ops->read_emulated(register_address(c,
2264 seg_override_base(ctxt, c),
2265 c->regs[VCPU_REGS_RSI]),
2266 &c->dst.val,
2267 c->dst.bytes,
2268 ctxt->vcpu);
2269 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002271 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002272 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002273 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 break;
2275 case 0xae ... 0xaf: /* scas */
2276 DPRINTF("Urk! I don't handle SCAS.\n");
2277 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002278 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002279 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002280 case 0xc0 ... 0xc1:
2281 emulate_grp2(ctxt);
2282 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002283 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002284 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002285 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002286 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002287 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002288 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2289 mov:
2290 c->dst.val = c->src.val;
2291 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002292 case 0xcb: /* ret far */
2293 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002294 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002295 goto done;
2296 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002297 case 0xd0 ... 0xd1: /* Grp2 */
2298 c->src.val = 1;
2299 emulate_grp2(ctxt);
2300 break;
2301 case 0xd2 ... 0xd3: /* Grp2 */
2302 c->src.val = c->regs[VCPU_REGS_RCX];
2303 emulate_grp2(ctxt);
2304 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002305 case 0xe4: /* inb */
2306 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002307 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002308 io_dir_in = 1;
2309 goto do_io;
2310 case 0xe6: /* outb */
2311 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002312 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002313 io_dir_in = 0;
2314 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002315 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002316 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002317 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002318 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002319 emulate_push(ctxt);
2320 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002321 }
2322 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002323 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002324 case 0xea: /* jmp far */
Gleb Natapovc6975182010-02-18 12:15:01 +02002325 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2326 VCPU_SREG_CS))
2327 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002328
Gleb Natapov782b8772009-04-12 13:36:25 +03002329 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002330 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002331 case 0xeb:
2332 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002333 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002334 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002335 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002336 case 0xec: /* in al,dx */
2337 case 0xed: /* in (e/r)ax,dx */
2338 port = c->regs[VCPU_REGS_RDX];
2339 io_dir_in = 1;
2340 goto do_io;
2341 case 0xee: /* out al,dx */
2342 case 0xef: /* out (e/r)ax,dx */
2343 port = c->regs[VCPU_REGS_RDX];
2344 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002345 do_io:
2346 if (!emulator_io_permited(ctxt, ops, port,
2347 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2348 kvm_inject_gp(ctxt->vcpu, 0);
2349 goto done;
2350 }
2351 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002352 (c->d & ByteOp) ? 1 : c->op_bytes,
2353 port) != 0) {
2354 c->eip = saved_eip;
2355 goto cannot_emulate;
2356 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002357 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002358 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002359 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002360 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002361 case 0xf5: /* cmc */
2362 /* complement carry flag from eflags reg */
2363 ctxt->eflags ^= EFLG_CF;
2364 c->dst.type = OP_NONE; /* Disable writeback. */
2365 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002366 case 0xf6 ... 0xf7: /* Grp3 */
2367 rc = emulate_grp3(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002368 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002369 goto done;
2370 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002371 case 0xf8: /* clc */
2372 ctxt->eflags &= ~EFLG_CF;
2373 c->dst.type = OP_NONE; /* Disable writeback. */
2374 break;
2375 case 0xfa: /* cli */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002376 if (emulator_bad_iopl(ctxt))
2377 kvm_inject_gp(ctxt->vcpu, 0);
2378 else {
2379 ctxt->eflags &= ~X86_EFLAGS_IF;
2380 c->dst.type = OP_NONE; /* Disable writeback. */
2381 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002382 break;
2383 case 0xfb: /* sti */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002384 if (emulator_bad_iopl(ctxt))
2385 kvm_inject_gp(ctxt->vcpu, 0);
2386 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002387 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002388 ctxt->eflags |= X86_EFLAGS_IF;
2389 c->dst.type = OP_NONE; /* Disable writeback. */
2390 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002391 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002392 case 0xfc: /* cld */
2393 ctxt->eflags &= ~EFLG_DF;
2394 c->dst.type = OP_NONE; /* Disable writeback. */
2395 break;
2396 case 0xfd: /* std */
2397 ctxt->eflags |= EFLG_DF;
2398 c->dst.type = OP_NONE; /* Disable writeback. */
2399 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002400 case 0xfe ... 0xff: /* Grp4/Grp5 */
2401 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002402 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002403 goto done;
2404 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002406
2407writeback:
2408 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002409 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002410 goto done;
2411
2412 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002413 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002414 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002415
2416done:
2417 if (rc == X86EMUL_UNHANDLEABLE) {
2418 c->eip = saved_eip;
2419 return -1;
2420 }
2421 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422
2423twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002424 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002426 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 u16 size;
2428 unsigned long address;
2429
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002430 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002431 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002432 goto cannot_emulate;
2433
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002434 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002435 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002436 goto done;
2437
Avi Kivity33e38852008-05-21 15:34:25 +03002438 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002439 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002440 /* Disable writeback. */
2441 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002442 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002444 rc = read_descriptor(ctxt, ops, c->src.ptr,
2445 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002446 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 goto done;
2448 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002449 /* Disable writeback. */
2450 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002452 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002453 if (c->modrm_mod == 3) {
2454 switch (c->modrm_rm) {
2455 case 1:
2456 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002457 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002458 goto done;
2459 break;
2460 default:
2461 goto cannot_emulate;
2462 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002463 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002464 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002465 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002466 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002467 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002468 goto done;
2469 realmode_lidt(ctxt->vcpu, size, address);
2470 }
Avi Kivity16286d02008-04-14 14:40:50 +03002471 /* Disable writeback. */
2472 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 break;
2474 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002475 c->dst.bytes = 2;
2476 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002477 break;
2478 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002479 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2480 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002481 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482 break;
2483 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002484 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002485 /* Disable writeback. */
2486 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 break;
2488 default:
2489 goto cannot_emulate;
2490 }
2491 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002492 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002493 rc = emulate_syscall(ctxt);
2494 if (rc != X86EMUL_CONTINUE)
2495 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002496 else
2497 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002498 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002499 case 0x06:
2500 emulate_clts(ctxt->vcpu);
2501 c->dst.type = OP_NONE;
2502 break;
2503 case 0x08: /* invd */
2504 case 0x09: /* wbinvd */
2505 case 0x0d: /* GrpP (prefetch) */
2506 case 0x18: /* Grp16 (prefetch/nop) */
2507 c->dst.type = OP_NONE;
2508 break;
2509 case 0x20: /* mov cr, reg */
2510 if (c->modrm_mod != 3)
2511 goto cannot_emulate;
2512 c->regs[c->modrm_rm] =
2513 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2514 c->dst.type = OP_NONE; /* no writeback */
2515 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002517 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002519 if (emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002520 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002521 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002522 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002524 case 0x22: /* mov reg, cr */
2525 if (c->modrm_mod != 3)
2526 goto cannot_emulate;
2527 realmode_set_cr(ctxt->vcpu,
2528 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2529 c->dst.type = OP_NONE;
2530 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002532 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002534 if (emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002535 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002536 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002537 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002539 case 0x30:
2540 /* wrmsr */
2541 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2542 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002543 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002544 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002545 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002546 }
2547 rc = X86EMUL_CONTINUE;
2548 c->dst.type = OP_NONE;
2549 break;
2550 case 0x32:
2551 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002552 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002553 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002554 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002555 } else {
2556 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2557 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2558 }
2559 rc = X86EMUL_CONTINUE;
2560 c->dst.type = OP_NONE;
2561 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002562 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002563 rc = emulate_sysenter(ctxt);
2564 if (rc != X86EMUL_CONTINUE)
2565 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002566 else
2567 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002568 break;
2569 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002570 rc = emulate_sysexit(ctxt);
2571 if (rc != X86EMUL_CONTINUE)
2572 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002573 else
2574 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002575 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002577 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002578 if (!test_cc(c->b, ctxt->eflags))
2579 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002581 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002582 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002583 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002584 c->dst.type = OP_NONE;
2585 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002586 case 0xa0: /* push fs */
2587 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2588 break;
2589 case 0xa1: /* pop fs */
2590 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002591 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002592 goto done;
2593 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002594 case 0xa3:
2595 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002596 c->dst.type = OP_NONE;
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("bt", 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 0xa4: /* shld imm8, r, r/m */
2602 case 0xa5: /* shld cl, r, r/m */
2603 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2604 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002605 case 0xa8: /* push gs */
2606 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2607 break;
2608 case 0xa9: /* pop gs */
2609 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002610 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002611 goto done;
2612 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002613 case 0xab:
2614 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002615 /* only subword offset */
2616 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002617 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002618 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002619 case 0xac: /* shrd imm8, r, r/m */
2620 case 0xad: /* shrd cl, r, r/m */
2621 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2622 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002623 case 0xae: /* clflush */
2624 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 case 0xb0 ... 0xb1: /* cmpxchg */
2626 /*
2627 * Save real source value, then compare EAX against
2628 * destination.
2629 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002630 c->src.orig_val = c->src.val;
2631 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002632 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2633 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002635 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 } else {
2637 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002638 c->dst.type = OP_REG;
2639 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 }
2641 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case 0xb3:
2643 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002644 /* only subword offset */
2645 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002646 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002649 c->dst.bytes = c->op_bytes;
2650 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2651 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002654 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 case 0:
2656 goto bt;
2657 case 1:
2658 goto bts;
2659 case 2:
2660 goto btr;
2661 case 3:
2662 goto btc;
2663 }
2664 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002665 case 0xbb:
2666 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002667 /* only subword offset */
2668 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002669 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002670 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002672 c->dst.bytes = c->op_bytes;
2673 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2674 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002676 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002677 c->dst.bytes = c->op_bytes;
2678 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2679 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002680 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002682 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002683 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002684 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002685 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002686 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 }
2688 goto writeback;
2689
2690cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002691 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002692 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 return -1;
2694}