blob: d696cbd6ff7a40ea03b8caba18d91679320e2616 [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,
Gleb Natapovea798492010-02-25 16:36:43 +0200349 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
350 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200351 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200352 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300353 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200354 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200355 [Group8*8] =
356 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200357 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
358 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200359 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200360 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200361};
362
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100363static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200364 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200365 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300366 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200367 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200368 [Group9*8] =
369 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200370};
371
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200373#define EFLG_ID (1<<21)
374#define EFLG_VIP (1<<20)
375#define EFLG_VIF (1<<19)
376#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200377#define EFLG_VM (1<<17)
378#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200379#define EFLG_IOPL (3<<12)
380#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381#define EFLG_OF (1<<11)
382#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200383#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200384#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385#define EFLG_SF (1<<7)
386#define EFLG_ZF (1<<6)
387#define EFLG_AF (1<<4)
388#define EFLG_PF (1<<2)
389#define EFLG_CF (1<<0)
390
391/*
392 * Instruction emulation:
393 * Most instructions are emulated directly via a fragment of inline assembly
394 * code. This allows us to save/restore EFLAGS and thus very easily pick up
395 * any modified flags.
396 */
397
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800398#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399#define _LO32 "k" /* force 32-bit operand */
400#define _STK "%%rsp" /* stack pointer */
401#elif defined(__i386__)
402#define _LO32 "" /* force 32-bit operand */
403#define _STK "%%esp" /* stack pointer */
404#endif
405
406/*
407 * These EFLAGS bits are restored from saved value during emulation, and
408 * any changes are written back to the saved value after emulation.
409 */
410#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
411
412/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200413#define _PRE_EFLAGS(_sav, _msk, _tmp) \
414 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
415 "movl %"_sav",%"_LO32 _tmp"; " \
416 "push %"_tmp"; " \
417 "push %"_tmp"; " \
418 "movl %"_msk",%"_LO32 _tmp"; " \
419 "andl %"_LO32 _tmp",("_STK"); " \
420 "pushf; " \
421 "notl %"_LO32 _tmp"; " \
422 "andl %"_LO32 _tmp",("_STK"); " \
423 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
424 "pop %"_tmp"; " \
425 "orl %"_LO32 _tmp",("_STK"); " \
426 "popf; " \
427 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428
429/* After executing instruction: write-back necessary bits in EFLAGS. */
430#define _POST_EFLAGS(_sav, _msk, _tmp) \
431 /* _sav |= EFLAGS & _msk; */ \
432 "pushf; " \
433 "pop %"_tmp"; " \
434 "andl %"_msk",%"_LO32 _tmp"; " \
435 "orl %"_LO32 _tmp",%"_sav"; "
436
Avi Kivitydda96d82008-11-26 15:14:10 +0200437#ifdef CONFIG_X86_64
438#define ON64(x) x
439#else
440#define ON64(x)
441#endif
442
Avi Kivity6b7ad612008-11-26 15:30:45 +0200443#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
444 do { \
445 __asm__ __volatile__ ( \
446 _PRE_EFLAGS("0", "4", "2") \
447 _op _suffix " %"_x"3,%1; " \
448 _POST_EFLAGS("0", "4", "2") \
449 : "=m" (_eflags), "=m" ((_dst).val), \
450 "=&r" (_tmp) \
451 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200452 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200453
454
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455/* Raw emulation: instruction has two explicit operands. */
456#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200457 do { \
458 unsigned long _tmp; \
459 \
460 switch ((_dst).bytes) { \
461 case 2: \
462 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
463 break; \
464 case 4: \
465 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
466 break; \
467 case 8: \
468 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
469 break; \
470 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 } while (0)
472
473#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
474 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200475 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400476 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200478 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 break; \
480 default: \
481 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
482 _wx, _wy, _lx, _ly, _qx, _qy); \
483 break; \
484 } \
485 } while (0)
486
487/* Source operand is byte-sized and may be restricted to just %cl. */
488#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
489 __emulate_2op(_op, _src, _dst, _eflags, \
490 "b", "c", "b", "c", "b", "c", "b", "c")
491
492/* Source operand is byte, word, long or quad sized. */
493#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
494 __emulate_2op(_op, _src, _dst, _eflags, \
495 "b", "q", "w", "r", _LO32, "r", "", "r")
496
497/* Source operand is word, long or quad sized. */
498#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
499 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
500 "w", "r", _LO32, "r", "", "r")
501
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100502/* Instruction has three operands and one operand is stored in ECX register */
503#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
504 do { \
505 unsigned long _tmp; \
506 _type _clv = (_cl).val; \
507 _type _srcv = (_src).val; \
508 _type _dstv = (_dst).val; \
509 \
510 __asm__ __volatile__ ( \
511 _PRE_EFLAGS("0", "5", "2") \
512 _op _suffix " %4,%1 \n" \
513 _POST_EFLAGS("0", "5", "2") \
514 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
515 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
516 ); \
517 \
518 (_cl).val = (unsigned long) _clv; \
519 (_src).val = (unsigned long) _srcv; \
520 (_dst).val = (unsigned long) _dstv; \
521 } while (0)
522
523#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
524 do { \
525 switch ((_dst).bytes) { \
526 case 2: \
527 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
528 "w", unsigned short); \
529 break; \
530 case 4: \
531 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
532 "l", unsigned int); \
533 break; \
534 case 8: \
535 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
536 "q", unsigned long)); \
537 break; \
538 } \
539 } while (0)
540
Avi Kivitydda96d82008-11-26 15:14:10 +0200541#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 do { \
543 unsigned long _tmp; \
544 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200545 __asm__ __volatile__ ( \
546 _PRE_EFLAGS("0", "3", "2") \
547 _op _suffix " %1; " \
548 _POST_EFLAGS("0", "3", "2") \
549 : "=m" (_eflags), "+m" ((_dst).val), \
550 "=&r" (_tmp) \
551 : "i" (EFLAGS_MASK)); \
552 } while (0)
553
554/* Instruction has only one explicit operand (no source operand). */
555#define emulate_1op(_op, _dst, _eflags) \
556 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400557 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200558 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
559 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
560 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
561 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562 } \
563 } while (0)
564
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565/* Fetch next part of the instruction being emulated. */
566#define insn_fetch(_type, _size, _eip) \
567({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200568 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200569 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 goto done; \
571 (_eip) += (_size); \
572 (_type)_x; \
573})
574
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800575static inline unsigned long ad_mask(struct decode_cache *c)
576{
577 return (1UL << (c->ad_bytes << 3)) - 1;
578}
579
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800581static inline unsigned long
582address_mask(struct decode_cache *c, unsigned long reg)
583{
584 if (c->ad_bytes == sizeof(unsigned long))
585 return reg;
586 else
587 return reg & ad_mask(c);
588}
589
590static inline unsigned long
591register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
592{
593 return base + address_mask(c, reg);
594}
595
Harvey Harrison7a9572752008-02-19 07:40:41 -0800596static inline void
597register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
598{
599 if (c->ad_bytes == sizeof(unsigned long))
600 *reg += inc;
601 else
602 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
603}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604
Harvey Harrison7a9572752008-02-19 07:40:41 -0800605static inline void jmp_rel(struct decode_cache *c, int rel)
606{
607 register_address_increment(c, &c->eip, rel);
608}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300609
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300610static void set_seg_override(struct decode_cache *c, int seg)
611{
612 c->has_seg_override = true;
613 c->seg_override = seg;
614}
615
616static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
617{
618 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
619 return 0;
620
621 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
622}
623
624static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
625 struct decode_cache *c)
626{
627 if (!c->has_seg_override)
628 return 0;
629
630 return seg_base(ctxt, c->seg_override);
631}
632
633static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
634{
635 return seg_base(ctxt, VCPU_SREG_ES);
636}
637
638static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
639{
640 return seg_base(ctxt, VCPU_SREG_SS);
641}
642
Avi Kivity62266862007-11-20 13:15:52 +0200643static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
644 struct x86_emulate_ops *ops,
645 unsigned long linear, u8 *dest)
646{
647 struct fetch_cache *fc = &ctxt->decode.fetch;
648 int rc;
649 int size;
650
651 if (linear < fc->start || linear >= fc->end) {
652 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200653 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900654 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200655 return rc;
656 fc->start = linear;
657 fc->end = linear + size;
658 }
659 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900660 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200661}
662
663static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
664 struct x86_emulate_ops *ops,
665 unsigned long eip, void *dest, unsigned size)
666{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900667 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200668
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200669 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200670 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200671 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200672 eip += ctxt->cs_base;
673 while (size--) {
674 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900675 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200676 return rc;
677 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200679}
680
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000681/*
682 * Given the 'reg' portion of a ModRM byte, and a register block, return a
683 * pointer into the block that addresses the relevant register.
684 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
685 */
686static void *decode_register(u8 modrm_reg, unsigned long *regs,
687 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
689 void *p;
690
691 p = &regs[modrm_reg];
692 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
693 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
694 return p;
695}
696
697static int read_descriptor(struct x86_emulate_ctxt *ctxt,
698 struct x86_emulate_ops *ops,
699 void *ptr,
700 u16 *size, unsigned long *address, int op_bytes)
701{
702 int rc;
703
704 if (op_bytes == 2)
705 op_bytes = 3;
706 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300707 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200708 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900709 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300711 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200712 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 return rc;
714}
715
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300716static int test_cc(unsigned int condition, unsigned int flags)
717{
718 int rc = 0;
719
720 switch ((condition & 15) >> 1) {
721 case 0: /* o */
722 rc |= (flags & EFLG_OF);
723 break;
724 case 1: /* b/c/nae */
725 rc |= (flags & EFLG_CF);
726 break;
727 case 2: /* z/e */
728 rc |= (flags & EFLG_ZF);
729 break;
730 case 3: /* be/na */
731 rc |= (flags & (EFLG_CF|EFLG_ZF));
732 break;
733 case 4: /* s */
734 rc |= (flags & EFLG_SF);
735 break;
736 case 5: /* p/pe */
737 rc |= (flags & EFLG_PF);
738 break;
739 case 7: /* le/ng */
740 rc |= (flags & EFLG_ZF);
741 /* fall through */
742 case 6: /* l/nge */
743 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
744 break;
745 }
746
747 /* Odd condition identifiers (lsb == 1) have inverted sense. */
748 return (!!rc ^ (condition & 1));
749}
750
Avi Kivity3c118e22007-10-31 10:27:04 +0200751static void decode_register_operand(struct operand *op,
752 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200753 int inhibit_bytereg)
754{
Avi Kivity33615aa2007-10-31 11:15:56 +0200755 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200756 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200757
758 if (!(c->d & ModRM))
759 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200760 op->type = OP_REG;
761 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200762 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->val = *(u8 *)op->ptr;
764 op->bytes = 1;
765 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200766 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200767 op->bytes = c->op_bytes;
768 switch (op->bytes) {
769 case 2:
770 op->val = *(u16 *)op->ptr;
771 break;
772 case 4:
773 op->val = *(u32 *)op->ptr;
774 break;
775 case 8:
776 op->val = *(u64 *) op->ptr;
777 break;
778 }
779 }
780 op->orig_val = op->val;
781}
782
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783static int decode_modrm(struct x86_emulate_ctxt *ctxt,
784 struct x86_emulate_ops *ops)
785{
786 struct decode_cache *c = &ctxt->decode;
787 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700788 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900789 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790
791 if (c->rex_prefix) {
792 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
793 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
794 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
795 }
796
797 c->modrm = insn_fetch(u8, 1, c->eip);
798 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
799 c->modrm_reg |= (c->modrm & 0x38) >> 3;
800 c->modrm_rm |= (c->modrm & 0x07);
801 c->modrm_ea = 0;
802 c->use_modrm_ea = 1;
803
804 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300805 c->modrm_ptr = decode_register(c->modrm_rm,
806 c->regs, c->d & ByteOp);
807 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808 return rc;
809 }
810
811 if (c->ad_bytes == 2) {
812 unsigned bx = c->regs[VCPU_REGS_RBX];
813 unsigned bp = c->regs[VCPU_REGS_RBP];
814 unsigned si = c->regs[VCPU_REGS_RSI];
815 unsigned di = c->regs[VCPU_REGS_RDI];
816
817 /* 16-bit ModR/M decode. */
818 switch (c->modrm_mod) {
819 case 0:
820 if (c->modrm_rm == 6)
821 c->modrm_ea += insn_fetch(u16, 2, c->eip);
822 break;
823 case 1:
824 c->modrm_ea += insn_fetch(s8, 1, c->eip);
825 break;
826 case 2:
827 c->modrm_ea += insn_fetch(u16, 2, c->eip);
828 break;
829 }
830 switch (c->modrm_rm) {
831 case 0:
832 c->modrm_ea += bx + si;
833 break;
834 case 1:
835 c->modrm_ea += bx + di;
836 break;
837 case 2:
838 c->modrm_ea += bp + si;
839 break;
840 case 3:
841 c->modrm_ea += bp + di;
842 break;
843 case 4:
844 c->modrm_ea += si;
845 break;
846 case 5:
847 c->modrm_ea += di;
848 break;
849 case 6:
850 if (c->modrm_mod != 0)
851 c->modrm_ea += bp;
852 break;
853 case 7:
854 c->modrm_ea += bx;
855 break;
856 }
857 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
858 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300859 if (!c->has_seg_override)
860 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200861 c->modrm_ea = (u16)c->modrm_ea;
862 } else {
863 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700864 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200865 sib = insn_fetch(u8, 1, c->eip);
866 index_reg |= (sib >> 3) & 7;
867 base_reg |= sib & 7;
868 scale = sib >> 6;
869
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700870 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
871 c->modrm_ea += insn_fetch(s32, 4, c->eip);
872 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700874 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200875 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700876 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
877 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700878 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700879 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200880 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 switch (c->modrm_mod) {
882 case 0:
883 if (c->modrm_rm == 5)
884 c->modrm_ea += insn_fetch(s32, 4, c->eip);
885 break;
886 case 1:
887 c->modrm_ea += insn_fetch(s8, 1, c->eip);
888 break;
889 case 2:
890 c->modrm_ea += insn_fetch(s32, 4, c->eip);
891 break;
892 }
893 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894done:
895 return rc;
896}
897
898static int decode_abs(struct x86_emulate_ctxt *ctxt,
899 struct x86_emulate_ops *ops)
900{
901 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900902 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200903
904 switch (c->ad_bytes) {
905 case 2:
906 c->modrm_ea = insn_fetch(u16, 2, c->eip);
907 break;
908 case 4:
909 c->modrm_ea = insn_fetch(u32, 4, c->eip);
910 break;
911 case 8:
912 c->modrm_ea = insn_fetch(u64, 8, c->eip);
913 break;
914 }
915done:
916 return rc;
917}
918
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200920x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200922 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200925 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
927 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Laurent Viviere4e03de2007-09-18 11:52:50 +0200929 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200930 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300931 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800932 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
934 switch (mode) {
935 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200936 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200938 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 break;
940 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800943#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200945 def_op_bytes = 4;
946 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 break;
948#endif
949 default:
950 return -1;
951 }
952
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200953 c->op_bytes = def_op_bytes;
954 c->ad_bytes = def_ad_bytes;
955
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200957 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200958 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200960 /* switch between 2/4 bytes */
961 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 break;
963 case 0x67: /* address-size override */
964 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200965 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200966 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300972 case 0x2e: /* CS override */
973 case 0x36: /* SS override */
974 case 0x3e: /* DS override */
975 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
977 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300979 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200981 case 0x40 ... 0x4f: /* REX */
982 if (mode != X86EMUL_MODE_PROT64)
983 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200984 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200985 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200987 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200989 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100990 c->rep_prefix = REPNE_PREFIX;
991 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100993 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 default:
996 goto done_prefixes;
997 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200998
999 /* Any legacy prefix after a REX prefix nullifies its effect. */
1000
Avi Kivity33615aa2007-10-31 11:15:56 +02001001 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 }
1003
1004done_prefixes:
1005
1006 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001008 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
1011 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->d = opcode_table[c->b];
1013 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 if (c->b == 0x0f) {
1016 c->twobyte = 1;
1017 c->b = insn_fetch(u8, 1, c->eip);
1018 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Avi Kivitye09d0822008-01-18 12:38:59 +02001022 if (c->d & Group) {
1023 group = c->d & GroupMask;
1024 c->modrm = insn_fetch(u8, 1, c->eip);
1025 --c->eip;
1026
1027 group = (group << 3) + ((c->modrm >> 3) & 7);
1028 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1029 c->d = group2_table[group];
1030 else
1031 c->d = group_table[group];
1032 }
1033
1034 /* Unrecognised? */
1035 if (c->d == 0) {
1036 DPRINTF("Cannot emulate %02x\n", c->b);
1037 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 }
1039
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001040 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1041 c->op_bytes = 8;
1042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001044 if (c->d & ModRM)
1045 rc = decode_modrm(ctxt, ops);
1046 else if (c->d & MemAbs)
1047 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001048 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001049 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001051 if (!c->has_seg_override)
1052 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001053
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001054 if (!(!c->twobyte && c->b == 0x8d))
1055 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001056
1057 if (c->ad_bytes != 8)
1058 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 /*
1060 * Decode and fetch the source operand: register, memory
1061 * or immediate.
1062 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001063 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 case SrcNone:
1065 break;
1066 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001067 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 break;
1069 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 goto srcmem_common;
1072 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001073 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 goto srcmem_common;
1075 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001076 c->src.bytes = (c->d & ByteOp) ? 1 :
1077 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001078 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001079 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001080 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001081 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001082 /*
1083 * For instructions with a ModR/M byte, switch to register
1084 * access if Mod = 3.
1085 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001086 if ((c->d & ModRM) && c->modrm_mod == 3) {
1087 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001088 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001089 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001090 break;
1091 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001092 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 break;
1094 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001095 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001096 c->src.type = OP_IMM;
1097 c->src.ptr = (unsigned long *)c->eip;
1098 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1099 if (c->src.bytes == 8)
1100 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001102 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001104 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 break;
1106 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001107 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 break;
1109 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001110 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 break;
1112 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001113 if ((c->d & SrcMask) == SrcImmU) {
1114 switch (c->src.bytes) {
1115 case 1:
1116 c->src.val &= 0xff;
1117 break;
1118 case 2:
1119 c->src.val &= 0xffff;
1120 break;
1121 case 4:
1122 c->src.val &= 0xffffffff;
1123 break;
1124 }
1125 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126 break;
1127 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001128 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001129 c->src.type = OP_IMM;
1130 c->src.ptr = (unsigned long *)c->eip;
1131 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001132 if ((c->d & SrcMask) == SrcImmByte)
1133 c->src.val = insn_fetch(s8, 1, c->eip);
1134 else
1135 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001137 case SrcOne:
1138 c->src.bytes = 1;
1139 c->src.val = 1;
1140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 }
1142
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001143 /*
1144 * Decode and fetch the second source operand: register, memory
1145 * or immediate.
1146 */
1147 switch (c->d & Src2Mask) {
1148 case Src2None:
1149 break;
1150 case Src2CL:
1151 c->src2.bytes = 1;
1152 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1153 break;
1154 case Src2ImmByte:
1155 c->src2.type = OP_IMM;
1156 c->src2.ptr = (unsigned long *)c->eip;
1157 c->src2.bytes = 1;
1158 c->src2.val = insn_fetch(u8, 1, c->eip);
1159 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001160 case Src2Imm16:
1161 c->src2.type = OP_IMM;
1162 c->src2.ptr = (unsigned long *)c->eip;
1163 c->src2.bytes = 2;
1164 c->src2.val = insn_fetch(u16, 2, c->eip);
1165 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001166 case Src2One:
1167 c->src2.bytes = 1;
1168 c->src2.val = 1;
1169 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001170 case Src2Mem16:
1171 c->src2.bytes = 2;
1172 c->src2.type = OP_MEM;
1173 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001174 }
1175
Avi Kivity038e51d2007-01-22 20:40:40 -08001176 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001177 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001178 case ImplicitOps:
1179 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001180 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001181 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001182 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001183 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001184 break;
1185 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001186 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001187 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001188 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001189 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001190 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001191 break;
1192 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001193 c->dst.type = OP_MEM;
1194 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001195 case DstAcc:
1196 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001197 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001198 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001199 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001200 case 1:
1201 c->dst.val = *(u8 *)c->dst.ptr;
1202 break;
1203 case 2:
1204 c->dst.val = *(u16 *)c->dst.ptr;
1205 break;
1206 case 4:
1207 c->dst.val = *(u32 *)c->dst.ptr;
1208 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001209 case 8:
1210 c->dst.val = *(u64 *)c->dst.ptr;
1211 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001212 }
1213 c->dst.orig_val = c->dst.val;
1214 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001215 }
1216
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001217 if (c->rip_relative)
1218 c->modrm_ea += c->eip;
1219
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001220done:
1221 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1222}
1223
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001224static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1225{
1226 struct decode_cache *c = &ctxt->decode;
1227
1228 c->dst.type = OP_MEM;
1229 c->dst.bytes = c->op_bytes;
1230 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001231 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001232 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233 c->regs[VCPU_REGS_RSP]);
1234}
1235
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001236static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001237 struct x86_emulate_ops *ops,
1238 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239{
1240 struct decode_cache *c = &ctxt->decode;
1241 int rc;
1242
Avi Kivity781d0ed2008-11-27 18:00:28 +02001243 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1244 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001245 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001246 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001247 return rc;
1248
Avi Kivity350f69d2009-01-05 11:12:40 +02001249 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001250 return rc;
1251}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001252
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001253static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1254 struct x86_emulate_ops *ops,
1255 void *dest, int len)
1256{
1257 int rc;
1258 unsigned long val, change_mask;
1259 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001260 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001261
1262 rc = emulate_pop(ctxt, ops, &val, len);
1263 if (rc != X86EMUL_CONTINUE)
1264 return rc;
1265
1266 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1267 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1268
1269 switch(ctxt->mode) {
1270 case X86EMUL_MODE_PROT64:
1271 case X86EMUL_MODE_PROT32:
1272 case X86EMUL_MODE_PROT16:
1273 if (cpl == 0)
1274 change_mask |= EFLG_IOPL;
1275 if (cpl <= iopl)
1276 change_mask |= EFLG_IF;
1277 break;
1278 case X86EMUL_MODE_VM86:
1279 if (iopl < 3) {
1280 kvm_inject_gp(ctxt->vcpu, 0);
1281 return X86EMUL_PROPAGATE_FAULT;
1282 }
1283 change_mask |= EFLG_IF;
1284 break;
1285 default: /* real mode */
1286 change_mask |= (EFLG_IOPL | EFLG_IF);
1287 break;
1288 }
1289
1290 *(unsigned long *)dest =
1291 (ctxt->eflags & ~change_mask) | (val & change_mask);
1292
1293 return rc;
1294}
1295
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001296static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1297{
1298 struct decode_cache *c = &ctxt->decode;
1299 struct kvm_segment segment;
1300
1301 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1302
1303 c->src.val = segment.selector;
1304 emulate_push(ctxt);
1305}
1306
1307static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1308 struct x86_emulate_ops *ops, int seg)
1309{
1310 struct decode_cache *c = &ctxt->decode;
1311 unsigned long selector;
1312 int rc;
1313
1314 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001315 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001316 return rc;
1317
Gleb Natapovc6975182010-02-18 12:15:01 +02001318 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001319 return rc;
1320}
1321
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001322static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1323{
1324 struct decode_cache *c = &ctxt->decode;
1325 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1326 int reg = VCPU_REGS_RAX;
1327
1328 while (reg <= VCPU_REGS_RDI) {
1329 (reg == VCPU_REGS_RSP) ?
1330 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1331
1332 emulate_push(ctxt);
1333 ++reg;
1334 }
1335}
1336
1337static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1338 struct x86_emulate_ops *ops)
1339{
1340 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001341 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001342 int reg = VCPU_REGS_RDI;
1343
1344 while (reg >= VCPU_REGS_RAX) {
1345 if (reg == VCPU_REGS_RSP) {
1346 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1347 c->op_bytes);
1348 --reg;
1349 }
1350
1351 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001352 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001353 break;
1354 --reg;
1355 }
1356 return rc;
1357}
1358
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001359static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1360 struct x86_emulate_ops *ops)
1361{
1362 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001363
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001364 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365}
1366
Laurent Vivier05f086f2007-09-24 11:10:55 +02001367static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001369 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370 switch (c->modrm_reg) {
1371 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001372 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373 break;
1374 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001375 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376 break;
1377 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001378 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001379 break;
1380 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382 break;
1383 case 4: /* sal/shl */
1384 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386 break;
1387 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389 break;
1390 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001391 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392 break;
1393 }
1394}
1395
1396static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398{
1399 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001400
1401 switch (c->modrm_reg) {
1402 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001403 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001404 break;
1405 case 2: /* not */
1406 c->dst.val = ~c->dst.val;
1407 break;
1408 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001409 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001410 break;
1411 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001412 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001414 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001415}
1416
1417static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001418 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001419{
1420 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001421
1422 switch (c->modrm_reg) {
1423 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001424 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001425 break;
1426 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001427 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001429 case 2: /* call near abs */ {
1430 long int old_eip;
1431 old_eip = c->eip;
1432 c->eip = c->src.val;
1433 c->src.val = old_eip;
1434 emulate_push(ctxt);
1435 break;
1436 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001437 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001438 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001439 break;
1440 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001441 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001444 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445}
1446
1447static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1448 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001449 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450{
1451 struct decode_cache *c = &ctxt->decode;
1452 u64 old, new;
1453 int rc;
1454
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001455 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001456 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001457 return rc;
1458
1459 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1460 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1461
1462 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1463 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001464 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001465
1466 } else {
1467 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1468 (u32) c->regs[VCPU_REGS_RBX];
1469
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001470 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001471 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001472 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001473 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001474 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001475 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001476}
1477
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001478static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1479 struct x86_emulate_ops *ops)
1480{
1481 struct decode_cache *c = &ctxt->decode;
1482 int rc;
1483 unsigned long cs;
1484
1485 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001486 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001487 return rc;
1488 if (c->op_bytes == 4)
1489 c->eip = (u32)c->eip;
1490 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001491 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001492 return rc;
Gleb Natapovc6975182010-02-18 12:15:01 +02001493 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001494 return rc;
1495}
1496
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001497static inline int writeback(struct x86_emulate_ctxt *ctxt,
1498 struct x86_emulate_ops *ops)
1499{
1500 int rc;
1501 struct decode_cache *c = &ctxt->decode;
1502
1503 switch (c->dst.type) {
1504 case OP_REG:
1505 /* The 4-byte case *is* correct:
1506 * in 64-bit mode we zero-extend.
1507 */
1508 switch (c->dst.bytes) {
1509 case 1:
1510 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1511 break;
1512 case 2:
1513 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1514 break;
1515 case 4:
1516 *c->dst.ptr = (u32)c->dst.val;
1517 break; /* 64b: zero-ext */
1518 case 8:
1519 *c->dst.ptr = c->dst.val;
1520 break;
1521 }
1522 break;
1523 case OP_MEM:
1524 if (c->lock_prefix)
1525 rc = ops->cmpxchg_emulated(
1526 (unsigned long)c->dst.ptr,
1527 &c->dst.orig_val,
1528 &c->dst.val,
1529 c->dst.bytes,
1530 ctxt->vcpu);
1531 else
1532 rc = ops->write_emulated(
1533 (unsigned long)c->dst.ptr,
1534 &c->dst.val,
1535 c->dst.bytes,
1536 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001537 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001539 break;
1540 case OP_NONE:
1541 /* no writeback */
1542 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001543 default:
1544 break;
1545 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001546 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001547}
1548
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301549static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001550{
1551 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1552 /*
1553 * an sti; sti; sequence only disable interrupts for the first
1554 * instruction. So, if the last instruction, be it emulated or
1555 * not, left the system with the INT_STI flag enabled, it
1556 * means that the last instruction is an sti. We should not
1557 * leave the flag on in this case. The same goes for mov ss
1558 */
1559 if (!(int_shadow & mask))
1560 ctxt->interruptibility = mask;
1561}
1562
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001563static inline void
1564setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1565 struct kvm_segment *cs, struct kvm_segment *ss)
1566{
1567 memset(cs, 0, sizeof(struct kvm_segment));
1568 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1569 memset(ss, 0, sizeof(struct kvm_segment));
1570
1571 cs->l = 0; /* will be adjusted later */
1572 cs->base = 0; /* flat segment */
1573 cs->g = 1; /* 4kb granularity */
1574 cs->limit = 0xffffffff; /* 4GB limit */
1575 cs->type = 0x0b; /* Read, Execute, Accessed */
1576 cs->s = 1;
1577 cs->dpl = 0; /* will be adjusted later */
1578 cs->present = 1;
1579 cs->db = 1;
1580
1581 ss->unusable = 0;
1582 ss->base = 0; /* flat segment */
1583 ss->limit = 0xffffffff; /* 4GB limit */
1584 ss->g = 1; /* 4kb granularity */
1585 ss->s = 1;
1586 ss->type = 0x03; /* Read/Write, Accessed */
1587 ss->db = 1; /* 32bit stack segment */
1588 ss->dpl = 0;
1589 ss->present = 1;
1590}
1591
1592static int
1593emulate_syscall(struct x86_emulate_ctxt *ctxt)
1594{
1595 struct decode_cache *c = &ctxt->decode;
1596 struct kvm_segment cs, ss;
1597 u64 msr_data;
1598
1599 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001600 if (ctxt->mode == X86EMUL_MODE_REAL ||
1601 ctxt->mode == X86EMUL_MODE_VM86) {
1602 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1603 return X86EMUL_PROPAGATE_FAULT;
1604 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001605
1606 setup_syscalls_segments(ctxt, &cs, &ss);
1607
1608 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1609 msr_data >>= 32;
1610 cs.selector = (u16)(msr_data & 0xfffc);
1611 ss.selector = (u16)(msr_data + 8);
1612
1613 if (is_long_mode(ctxt->vcpu)) {
1614 cs.db = 0;
1615 cs.l = 1;
1616 }
1617 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1618 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1619
1620 c->regs[VCPU_REGS_RCX] = c->eip;
1621 if (is_long_mode(ctxt->vcpu)) {
1622#ifdef CONFIG_X86_64
1623 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1624
1625 kvm_x86_ops->get_msr(ctxt->vcpu,
1626 ctxt->mode == X86EMUL_MODE_PROT64 ?
1627 MSR_LSTAR : MSR_CSTAR, &msr_data);
1628 c->eip = msr_data;
1629
1630 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1631 ctxt->eflags &= ~(msr_data | EFLG_RF);
1632#endif
1633 } else {
1634 /* legacy mode */
1635 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1636 c->eip = (u32)msr_data;
1637
1638 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1639 }
1640
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001641 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001642}
1643
Andre Przywara8c604352009-06-18 12:56:01 +02001644static int
1645emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1646{
1647 struct decode_cache *c = &ctxt->decode;
1648 struct kvm_segment cs, ss;
1649 u64 msr_data;
1650
Gleb Natapova0044752010-02-10 14:21:31 +02001651 /* inject #GP if in real mode */
1652 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001653 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001654 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001655 }
1656
1657 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1658 * Therefore, we inject an #UD.
1659 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001660 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1661 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1662 return X86EMUL_PROPAGATE_FAULT;
1663 }
Andre Przywara8c604352009-06-18 12:56:01 +02001664
1665 setup_syscalls_segments(ctxt, &cs, &ss);
1666
1667 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1668 switch (ctxt->mode) {
1669 case X86EMUL_MODE_PROT32:
1670 if ((msr_data & 0xfffc) == 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 case X86EMUL_MODE_PROT64:
1676 if (msr_data == 0x0) {
1677 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001678 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001679 }
1680 break;
1681 }
1682
1683 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1684 cs.selector = (u16)msr_data;
1685 cs.selector &= ~SELECTOR_RPL_MASK;
1686 ss.selector = cs.selector + 8;
1687 ss.selector &= ~SELECTOR_RPL_MASK;
1688 if (ctxt->mode == X86EMUL_MODE_PROT64
1689 || is_long_mode(ctxt->vcpu)) {
1690 cs.db = 0;
1691 cs.l = 1;
1692 }
1693
1694 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1695 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1696
1697 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1698 c->eip = msr_data;
1699
1700 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1701 c->regs[VCPU_REGS_RSP] = msr_data;
1702
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001703 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001704}
1705
Andre Przywara4668f052009-06-18 12:56:02 +02001706static int
1707emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1708{
1709 struct decode_cache *c = &ctxt->decode;
1710 struct kvm_segment cs, ss;
1711 u64 msr_data;
1712 int usermode;
1713
Gleb Natapova0044752010-02-10 14:21:31 +02001714 /* inject #GP if in real mode or Virtual 8086 mode */
1715 if (ctxt->mode == X86EMUL_MODE_REAL ||
1716 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001717 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001718 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001719 }
1720
Andre Przywara4668f052009-06-18 12:56:02 +02001721 setup_syscalls_segments(ctxt, &cs, &ss);
1722
1723 if ((c->rex_prefix & 0x8) != 0x0)
1724 usermode = X86EMUL_MODE_PROT64;
1725 else
1726 usermode = X86EMUL_MODE_PROT32;
1727
1728 cs.dpl = 3;
1729 ss.dpl = 3;
1730 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1731 switch (usermode) {
1732 case X86EMUL_MODE_PROT32:
1733 cs.selector = (u16)(msr_data + 16);
1734 if ((msr_data & 0xfffc) == 0x0) {
1735 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001736 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001737 }
1738 ss.selector = (u16)(msr_data + 24);
1739 break;
1740 case X86EMUL_MODE_PROT64:
1741 cs.selector = (u16)(msr_data + 32);
1742 if (msr_data == 0x0) {
1743 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001744 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001745 }
1746 ss.selector = cs.selector + 8;
1747 cs.db = 0;
1748 cs.l = 1;
1749 break;
1750 }
1751 cs.selector |= SELECTOR_RPL_MASK;
1752 ss.selector |= SELECTOR_RPL_MASK;
1753
1754 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1755 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1756
1757 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1758 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1759
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001760 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001761}
1762
Gleb Natapov9c537242010-03-18 15:20:05 +02001763static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1764 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001765{
1766 int iopl;
1767 if (ctxt->mode == X86EMUL_MODE_REAL)
1768 return false;
1769 if (ctxt->mode == X86EMUL_MODE_VM86)
1770 return true;
1771 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001772 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001773}
1774
1775static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1776 struct x86_emulate_ops *ops,
1777 u16 port, u16 len)
1778{
1779 struct kvm_segment tr_seg;
1780 int r;
1781 u16 io_bitmap_ptr;
1782 u8 perm, bit_idx = port & 0x7;
1783 unsigned mask = (1 << len) - 1;
1784
1785 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1786 if (tr_seg.unusable)
1787 return false;
1788 if (tr_seg.limit < 103)
1789 return false;
1790 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1791 NULL);
1792 if (r != X86EMUL_CONTINUE)
1793 return false;
1794 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1795 return false;
1796 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1797 ctxt->vcpu, NULL);
1798 if (r != X86EMUL_CONTINUE)
1799 return false;
1800 if ((perm >> bit_idx) & mask)
1801 return false;
1802 return true;
1803}
1804
1805static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1806 struct x86_emulate_ops *ops,
1807 u16 port, u16 len)
1808{
Gleb Natapov9c537242010-03-18 15:20:05 +02001809 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001810 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1811 return false;
1812 return true;
1813}
1814
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001815int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001816x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001817{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001818 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001819 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001820 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001821 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001822 unsigned int port;
1823 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001824 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001825
Glauber Costa310b5d32009-05-12 16:21:06 -04001826 ctxt->interruptibility = 0;
1827
Laurent Vivier34273182007-09-18 11:27:37 +02001828 /* Shadow copy of register state. Committed on successful emulation.
1829 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1830 * modify them.
1831 */
1832
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001833 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001834 saved_eip = c->eip;
1835
Gleb Natapov11616242010-02-11 14:43:14 +02001836 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
1837 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1838 goto done;
1839 }
1840
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001841 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02001842 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001843 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1844 goto done;
1845 }
1846
Gleb Natapove92805a2010-02-10 14:21:35 +02001847 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02001848 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02001849 kvm_inject_gp(ctxt->vcpu, 0);
1850 goto done;
1851 }
1852
Avi Kivityc7e75a32007-10-28 16:34:25 +02001853 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001854 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001855
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001856 if (c->rep_prefix && (c->d & String)) {
1857 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02001858 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001859 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001860 goto done;
1861 }
1862 /* The second termination condition only applies for REPE
1863 * and REPNE. Test if the repeat string operation prefix is
1864 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1865 * corresponding termination condition according to:
1866 * - if REPE/REPZ and ZF = 0 then done
1867 * - if REPNE/REPNZ and ZF = 1 then done
1868 */
1869 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1870 (c->b == 0xae) || (c->b == 0xaf)) {
1871 if ((c->rep_prefix == REPE_PREFIX) &&
1872 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001873 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001874 goto done;
1875 }
1876 if ((c->rep_prefix == REPNE_PREFIX) &&
1877 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001878 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001879 goto done;
1880 }
1881 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02001882 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02001883 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001884 }
1885
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001886 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001887 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001888 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001889 rc = ops->read_emulated((unsigned long)c->src.ptr,
1890 &c->src.val,
1891 c->src.bytes,
1892 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001893 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001894 goto done;
1895 c->src.orig_val = c->src.val;
1896 }
1897
Gleb Natapove35b7b92010-02-25 16:36:42 +02001898 if (c->src2.type == OP_MEM) {
1899 c->src2.ptr = (unsigned long *)(memop + c->src.bytes);
1900 c->src2.val = 0;
1901 rc = ops->read_emulated((unsigned long)c->src2.ptr,
1902 &c->src2.val,
1903 c->src2.bytes,
1904 ctxt->vcpu);
1905 if (rc != X86EMUL_CONTINUE)
1906 goto done;
1907 }
1908
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001909 if ((c->d & DstMask) == ImplicitOps)
1910 goto special_insn;
1911
1912
1913 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001914 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001915 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1916 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001917 if (c->d & BitOp) {
1918 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001919
Laurent Viviere4e03de2007-09-18 11:52:50 +02001920 c->dst.ptr = (void *)c->dst.ptr +
1921 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001922 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001923 if (!(c->d & Mov)) {
1924 /* optimisation - avoid slow emulated read */
1925 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1926 &c->dst.val,
1927 c->dst.bytes,
1928 ctxt->vcpu);
1929 if (rc != X86EMUL_CONTINUE)
1930 goto done;
1931 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001932 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001933 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001934
Avi Kivity018a98d2007-11-27 19:30:56 +02001935special_insn:
1936
Laurent Viviere4e03de2007-09-18 11:52:50 +02001937 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 goto twobyte_insn;
1939
Laurent Viviere4e03de2007-09-18 11:52:50 +02001940 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 case 0x00 ... 0x05:
1942 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001943 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001945 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001946 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1947 break;
1948 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001949 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001950 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001951 goto done;
1952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 case 0x08 ... 0x0d:
1954 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001955 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001957 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001958 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1959 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 case 0x10 ... 0x15:
1961 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001962 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001964 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001965 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1966 break;
1967 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001968 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001969 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001970 goto done;
1971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 case 0x18 ... 0x1d:
1973 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001974 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001976 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001977 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1978 break;
1979 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001980 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001981 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001982 goto done;
1983 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001984 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001986 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987 break;
1988 case 0x28 ... 0x2d:
1989 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001990 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 break;
1992 case 0x30 ... 0x35:
1993 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001994 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 break;
1996 case 0x38 ... 0x3d:
1997 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001998 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002000 case 0x40 ... 0x47: /* inc r16/r32 */
2001 emulate_1op("inc", c->dst, ctxt->eflags);
2002 break;
2003 case 0x48 ... 0x4f: /* dec r16/r32 */
2004 emulate_1op("dec", c->dst, ctxt->eflags);
2005 break;
2006 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002007 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002008 break;
2009 case 0x58 ... 0x5f: /* pop reg */
2010 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002011 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002012 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002013 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002014 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002015 case 0x60: /* pusha */
2016 emulate_pusha(ctxt);
2017 break;
2018 case 0x61: /* popa */
2019 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002020 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002021 goto done;
2022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002024 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002026 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002028 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002029 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002030 emulate_push(ctxt);
2031 break;
2032 case 0x6c: /* insb */
2033 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2035 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2036 kvm_inject_gp(ctxt->vcpu, 0);
2037 goto done;
2038 }
2039 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002040 1,
2041 (c->d & ByteOp) ? 1 : c->op_bytes,
2042 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002043 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002044 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002045 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002046 c->regs[VCPU_REGS_RDI]),
2047 c->rep_prefix,
2048 c->regs[VCPU_REGS_RDX]) == 0) {
2049 c->eip = saved_eip;
2050 return -1;
2051 }
2052 return 0;
2053 case 0x6e: /* outsb */
2054 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002055 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2056 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2057 kvm_inject_gp(ctxt->vcpu, 0);
2058 goto done;
2059 }
Avi Kivity851ba692009-08-24 11:10:17 +03002060 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002061 0,
2062 (c->d & ByteOp) ? 1 : c->op_bytes,
2063 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002064 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002065 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002066 register_address(c,
2067 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002068 c->regs[VCPU_REGS_RSI]),
2069 c->rep_prefix,
2070 c->regs[VCPU_REGS_RDX]) == 0) {
2071 c->eip = saved_eip;
2072 return -1;
2073 }
2074 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002075 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002076 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002077 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002080 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 case 0:
2082 goto add;
2083 case 1:
2084 goto or;
2085 case 2:
2086 goto adc;
2087 case 3:
2088 goto sbb;
2089 case 4:
2090 goto and;
2091 case 5:
2092 goto sub;
2093 case 6:
2094 goto xor;
2095 case 7:
2096 goto cmp;
2097 }
2098 break;
2099 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002100 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 break;
2102 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002103 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002105 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002107 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 break;
2109 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002110 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 break;
2112 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002113 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break; /* 64b reg: zero-extend */
2115 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 break;
2118 }
2119 /*
2120 * Write back the memory destination with implicit LOCK
2121 * prefix.
2122 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002123 c->dst.val = c->src.val;
2124 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002127 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002128 case 0x8c: { /* mov r/m, sreg */
2129 struct kvm_segment segreg;
2130
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002131 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002132 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2133 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002134 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2135 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002136 }
2137 c->dst.val = segreg.selector;
2138 break;
2139 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002140 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002141 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002142 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002143 case 0x8e: { /* mov seg, r/m16 */
2144 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002145
2146 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002147
Gleb Natapovc6975182010-02-18 12:15:01 +02002148 if (c->modrm_reg == VCPU_SREG_CS ||
2149 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002150 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2151 goto done;
2152 }
2153
Glauber Costa310b5d32009-05-12 16:21:06 -04002154 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002155 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002156
Gleb Natapovc6975182010-02-18 12:15:01 +02002157 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002158
2159 c->dst.type = OP_NONE; /* Disable writeback. */
2160 break;
2161 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002164 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002167 case 0x90: /* nop / xchg r8,rax */
2168 if (!(c->rex_prefix & 1)) { /* nop */
2169 c->dst.type = OP_NONE;
2170 break;
2171 }
2172 case 0x91 ... 0x97: /* xchg reg,rax */
2173 c->src.type = c->dst.type = OP_REG;
2174 c->src.bytes = c->dst.bytes = c->op_bytes;
2175 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2176 c->src.val = *(c->src.ptr);
2177 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002178 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002179 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002180 emulate_push(ctxt);
2181 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002182 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002183 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002184 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002185 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002186 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2187 if (rc != X86EMUL_CONTINUE)
2188 goto done;
2189 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002190 case 0xa0 ... 0xa1: /* mov */
2191 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2192 c->dst.val = c->src.val;
2193 break;
2194 case 0xa2 ... 0xa3: /* mov */
2195 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2196 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002198 c->dst.type = OP_MEM;
2199 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002200 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002201 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002202 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002203 rc = ops->read_emulated(register_address(c,
2204 seg_override_base(ctxt, c),
2205 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002206 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002207 c->dst.bytes, ctxt->vcpu);
2208 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002210 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002211 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002212 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002213 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002214 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002215 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 break;
2217 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002218 c->src.type = OP_NONE; /* Disable writeback. */
2219 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002220 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002221 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002222 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002223 rc = ops->read_emulated((unsigned long)c->src.ptr,
2224 &c->src.val,
2225 c->src.bytes,
2226 ctxt->vcpu);
2227 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002228 goto done;
2229
2230 c->dst.type = OP_NONE; /* Disable writeback. */
2231 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002232 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002233 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002234 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002235 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2236 &c->dst.val,
2237 c->dst.bytes,
2238 ctxt->vcpu);
2239 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002240 goto done;
2241
2242 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2243
2244 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2245
Harvey Harrison7a9572752008-02-19 07:40:41 -08002246 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002247 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2248 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002249 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002250 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2251 : c->dst.bytes);
2252
2253 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002255 c->dst.type = OP_MEM;
2256 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002257 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002258 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002259 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002260 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002261 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002262 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002263 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264 break;
2265 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002266 c->dst.type = OP_REG;
2267 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2268 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002269 rc = ops->read_emulated(register_address(c,
2270 seg_override_base(ctxt, c),
2271 c->regs[VCPU_REGS_RSI]),
2272 &c->dst.val,
2273 c->dst.bytes,
2274 ctxt->vcpu);
2275 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002277 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002278 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002279 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 break;
2281 case 0xae ... 0xaf: /* scas */
2282 DPRINTF("Urk! I don't handle SCAS.\n");
2283 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002284 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002285 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002286 case 0xc0 ... 0xc1:
2287 emulate_grp2(ctxt);
2288 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002289 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002290 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002291 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002292 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002293 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002294 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2295 mov:
2296 c->dst.val = c->src.val;
2297 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002298 case 0xcb: /* ret far */
2299 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002300 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002301 goto done;
2302 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002303 case 0xd0 ... 0xd1: /* Grp2 */
2304 c->src.val = 1;
2305 emulate_grp2(ctxt);
2306 break;
2307 case 0xd2 ... 0xd3: /* Grp2 */
2308 c->src.val = c->regs[VCPU_REGS_RCX];
2309 emulate_grp2(ctxt);
2310 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002311 case 0xe4: /* inb */
2312 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002313 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002314 io_dir_in = 1;
2315 goto do_io;
2316 case 0xe6: /* outb */
2317 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002318 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002319 io_dir_in = 0;
2320 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002321 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002322 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002323 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002324 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002325 emulate_push(ctxt);
2326 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002327 }
2328 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002329 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002330 case 0xea: /* jmp far */
Gleb Natapovea798492010-02-25 16:36:43 +02002331 jump_far:
Gleb Natapovc6975182010-02-18 12:15:01 +02002332 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2333 VCPU_SREG_CS))
2334 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002335
Gleb Natapov782b8772009-04-12 13:36:25 +03002336 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002337 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002338 case 0xeb:
2339 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002340 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002341 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002342 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002343 case 0xec: /* in al,dx */
2344 case 0xed: /* in (e/r)ax,dx */
2345 port = c->regs[VCPU_REGS_RDX];
2346 io_dir_in = 1;
2347 goto do_io;
2348 case 0xee: /* out al,dx */
2349 case 0xef: /* out (e/r)ax,dx */
2350 port = c->regs[VCPU_REGS_RDX];
2351 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002352 do_io:
2353 if (!emulator_io_permited(ctxt, ops, port,
2354 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2355 kvm_inject_gp(ctxt->vcpu, 0);
2356 goto done;
2357 }
2358 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002359 (c->d & ByteOp) ? 1 : c->op_bytes,
2360 port) != 0) {
2361 c->eip = saved_eip;
2362 goto cannot_emulate;
2363 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002364 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002365 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002366 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002367 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002368 case 0xf5: /* cmc */
2369 /* complement carry flag from eflags reg */
2370 ctxt->eflags ^= EFLG_CF;
2371 c->dst.type = OP_NONE; /* Disable writeback. */
2372 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002373 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002374 if (!emulate_grp3(ctxt, ops))
2375 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002376 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002377 case 0xf8: /* clc */
2378 ctxt->eflags &= ~EFLG_CF;
2379 c->dst.type = OP_NONE; /* Disable writeback. */
2380 break;
2381 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002382 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002383 kvm_inject_gp(ctxt->vcpu, 0);
2384 else {
2385 ctxt->eflags &= ~X86_EFLAGS_IF;
2386 c->dst.type = OP_NONE; /* Disable writeback. */
2387 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002388 break;
2389 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002390 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002391 kvm_inject_gp(ctxt->vcpu, 0);
2392 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002393 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002394 ctxt->eflags |= X86_EFLAGS_IF;
2395 c->dst.type = OP_NONE; /* Disable writeback. */
2396 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002397 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002398 case 0xfc: /* cld */
2399 ctxt->eflags &= ~EFLG_DF;
2400 c->dst.type = OP_NONE; /* Disable writeback. */
2401 break;
2402 case 0xfd: /* std */
2403 ctxt->eflags |= EFLG_DF;
2404 c->dst.type = OP_NONE; /* Disable writeback. */
2405 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002406 case 0xfe: /* Grp4 */
2407 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002408 rc = emulate_grp45(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 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002412 case 0xff: /* Grp5 */
2413 if (c->modrm_reg == 5)
2414 goto jump_far;
2415 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002417
2418writeback:
2419 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002420 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002421 goto done;
2422
2423 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002424 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002425 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002426
2427done:
2428 if (rc == X86EMUL_UNHANDLEABLE) {
2429 c->eip = saved_eip;
2430 return -1;
2431 }
2432 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433
2434twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002435 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002437 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 u16 size;
2439 unsigned long address;
2440
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002441 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002442 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002443 goto cannot_emulate;
2444
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002445 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002446 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002447 goto done;
2448
Avi Kivity33e38852008-05-21 15:34:25 +03002449 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002450 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002451 /* Disable writeback. */
2452 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002453 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002455 rc = read_descriptor(ctxt, ops, c->src.ptr,
2456 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002457 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 goto done;
2459 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002460 /* Disable writeback. */
2461 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002463 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002464 if (c->modrm_mod == 3) {
2465 switch (c->modrm_rm) {
2466 case 1:
2467 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002468 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002469 goto done;
2470 break;
2471 default:
2472 goto cannot_emulate;
2473 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002474 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002475 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002476 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002477 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002478 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002479 goto done;
2480 realmode_lidt(ctxt->vcpu, size, address);
2481 }
Avi Kivity16286d02008-04-14 14:40:50 +03002482 /* Disable writeback. */
2483 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 break;
2485 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002486 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02002487 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 break;
2489 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02002490 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
2491 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002492 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02002494 case 5: /* not defined */
2495 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2496 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002498 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002499 /* Disable writeback. */
2500 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 break;
2502 default:
2503 goto cannot_emulate;
2504 }
2505 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002506 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002507 rc = emulate_syscall(ctxt);
2508 if (rc != X86EMUL_CONTINUE)
2509 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002510 else
2511 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002512 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002513 case 0x06:
2514 emulate_clts(ctxt->vcpu);
2515 c->dst.type = OP_NONE;
2516 break;
2517 case 0x08: /* invd */
2518 case 0x09: /* wbinvd */
2519 case 0x0d: /* GrpP (prefetch) */
2520 case 0x18: /* Grp16 (prefetch/nop) */
2521 c->dst.type = OP_NONE;
2522 break;
2523 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02002524 switch (c->modrm_reg) {
2525 case 1:
2526 case 5 ... 7:
2527 case 9 ... 15:
2528 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2529 goto done;
2530 }
Gleb Natapov52a46612010-03-18 15:20:03 +02002531 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002532 c->dst.type = OP_NONE; /* no writeback */
2533 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02002535 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
2536 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
2537 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2538 goto done;
2539 }
2540 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002541 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002543 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02002544 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002545 c->dst.type = OP_NONE;
2546 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02002548 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
2549 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
2550 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2551 goto done;
2552 }
2553 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002554 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002556 case 0x30:
2557 /* wrmsr */
2558 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2559 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002560 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002561 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02002562 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002563 }
2564 rc = X86EMUL_CONTINUE;
2565 c->dst.type = OP_NONE;
2566 break;
2567 case 0x32:
2568 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002569 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002570 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02002571 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002572 } else {
2573 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2574 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2575 }
2576 rc = X86EMUL_CONTINUE;
2577 c->dst.type = OP_NONE;
2578 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002579 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002580 rc = emulate_sysenter(ctxt);
2581 if (rc != X86EMUL_CONTINUE)
2582 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002583 else
2584 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002585 break;
2586 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002587 rc = emulate_sysexit(ctxt);
2588 if (rc != X86EMUL_CONTINUE)
2589 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002590 else
2591 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002592 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002594 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002595 if (!test_cc(c->b, ctxt->eflags))
2596 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002598 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002599 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002600 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002601 c->dst.type = OP_NONE;
2602 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002603 case 0xa0: /* push fs */
2604 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2605 break;
2606 case 0xa1: /* pop fs */
2607 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002608 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002609 goto done;
2610 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002611 case 0xa3:
2612 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002613 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002614 /* only subword offset */
2615 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002616 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002617 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002618 case 0xa4: /* shld imm8, r, r/m */
2619 case 0xa5: /* shld cl, r, r/m */
2620 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2621 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002622 case 0xa8: /* push gs */
2623 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2624 break;
2625 case 0xa9: /* pop gs */
2626 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002627 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002628 goto done;
2629 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002630 case 0xab:
2631 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002632 /* only subword offset */
2633 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002634 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002635 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002636 case 0xac: /* shrd imm8, r, r/m */
2637 case 0xad: /* shrd cl, r, r/m */
2638 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2639 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002640 case 0xae: /* clflush */
2641 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case 0xb0 ... 0xb1: /* cmpxchg */
2643 /*
2644 * Save real source value, then compare EAX against
2645 * destination.
2646 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002647 c->src.orig_val = c->src.val;
2648 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002649 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2650 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002652 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 } else {
2654 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002655 c->dst.type = OP_REG;
2656 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 }
2658 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 case 0xb3:
2660 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002661 /* only subword offset */
2662 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002663 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002666 c->dst.bytes = c->op_bytes;
2667 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2668 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002671 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 case 0:
2673 goto bt;
2674 case 1:
2675 goto bts;
2676 case 2:
2677 goto btr;
2678 case 3:
2679 goto btc;
2680 }
2681 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002682 case 0xbb:
2683 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002684 /* only subword offset */
2685 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002686 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002687 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002689 c->dst.bytes = c->op_bytes;
2690 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2691 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002693 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002694 c->dst.bytes = c->op_bytes;
2695 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2696 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002697 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002699 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002700 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002701 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002702 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002703 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 }
2705 goto writeback;
2706
2707cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002708 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002709 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 return -1;
2711}