blob: c3b9334eb248fa584e28104fbc058d01bc93af47 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020079#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020080#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082/* Source 2 operand type */
83#define Src2None (0<<29)
84#define Src2CL (1<<29)
85#define Src2ImmByte (2<<29)
86#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030087#define Src2Imm16 (4<<29)
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 Natapovea79849d2010-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 Kivityc9eaf20f2009-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 Kivityc9eaf20f2009-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;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001400 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401
1402 switch (c->modrm_reg) {
1403 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001404 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001405 break;
1406 case 2: /* not */
1407 c->dst.val = ~c->dst.val;
1408 break;
1409 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001410 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001411 break;
1412 default:
1413 DPRINTF("Cannot emulate %02x\n", c->b);
1414 rc = X86EMUL_UNHANDLEABLE;
1415 break;
1416 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001417 return rc;
1418}
1419
1420static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001421 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422{
1423 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001424
1425 switch (c->modrm_reg) {
1426 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001427 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428 break;
1429 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001430 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001431 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001432 case 2: /* call near abs */ {
1433 long int old_eip;
1434 old_eip = c->eip;
1435 c->eip = c->src.val;
1436 c->src.val = old_eip;
1437 emulate_push(ctxt);
1438 break;
1439 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001441 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442 break;
1443 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001444 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001446 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001447 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001448}
1449
1450static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1451 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001452 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001453{
1454 struct decode_cache *c = &ctxt->decode;
1455 u64 old, new;
1456 int rc;
1457
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001458 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001459 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001460 return rc;
1461
1462 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1463 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1464
1465 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1466 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001467 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468
1469 } else {
1470 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1471 (u32) c->regs[VCPU_REGS_RBX];
1472
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001473 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001474 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001476 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001477 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001478 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001479}
1480
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001481static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1482 struct x86_emulate_ops *ops)
1483{
1484 struct decode_cache *c = &ctxt->decode;
1485 int rc;
1486 unsigned long cs;
1487
1488 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001489 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001490 return rc;
1491 if (c->op_bytes == 4)
1492 c->eip = (u32)c->eip;
1493 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001494 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001495 return rc;
Gleb Natapovc6975182010-02-18 12:15:01 +02001496 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001497 return rc;
1498}
1499
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001500static inline int writeback(struct x86_emulate_ctxt *ctxt,
1501 struct x86_emulate_ops *ops)
1502{
1503 int rc;
1504 struct decode_cache *c = &ctxt->decode;
1505
1506 switch (c->dst.type) {
1507 case OP_REG:
1508 /* The 4-byte case *is* correct:
1509 * in 64-bit mode we zero-extend.
1510 */
1511 switch (c->dst.bytes) {
1512 case 1:
1513 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1514 break;
1515 case 2:
1516 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1517 break;
1518 case 4:
1519 *c->dst.ptr = (u32)c->dst.val;
1520 break; /* 64b: zero-ext */
1521 case 8:
1522 *c->dst.ptr = c->dst.val;
1523 break;
1524 }
1525 break;
1526 case OP_MEM:
1527 if (c->lock_prefix)
1528 rc = ops->cmpxchg_emulated(
1529 (unsigned long)c->dst.ptr,
1530 &c->dst.orig_val,
1531 &c->dst.val,
1532 c->dst.bytes,
1533 ctxt->vcpu);
1534 else
1535 rc = ops->write_emulated(
1536 (unsigned long)c->dst.ptr,
1537 &c->dst.val,
1538 c->dst.bytes,
1539 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001540 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001541 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001542 break;
1543 case OP_NONE:
1544 /* no writeback */
1545 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001546 default:
1547 break;
1548 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001549 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001550}
1551
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301552static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001553{
1554 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1555 /*
1556 * an sti; sti; sequence only disable interrupts for the first
1557 * instruction. So, if the last instruction, be it emulated or
1558 * not, left the system with the INT_STI flag enabled, it
1559 * means that the last instruction is an sti. We should not
1560 * leave the flag on in this case. The same goes for mov ss
1561 */
1562 if (!(int_shadow & mask))
1563 ctxt->interruptibility = mask;
1564}
1565
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001566static inline void
1567setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1568 struct kvm_segment *cs, struct kvm_segment *ss)
1569{
1570 memset(cs, 0, sizeof(struct kvm_segment));
1571 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1572 memset(ss, 0, sizeof(struct kvm_segment));
1573
1574 cs->l = 0; /* will be adjusted later */
1575 cs->base = 0; /* flat segment */
1576 cs->g = 1; /* 4kb granularity */
1577 cs->limit = 0xffffffff; /* 4GB limit */
1578 cs->type = 0x0b; /* Read, Execute, Accessed */
1579 cs->s = 1;
1580 cs->dpl = 0; /* will be adjusted later */
1581 cs->present = 1;
1582 cs->db = 1;
1583
1584 ss->unusable = 0;
1585 ss->base = 0; /* flat segment */
1586 ss->limit = 0xffffffff; /* 4GB limit */
1587 ss->g = 1; /* 4kb granularity */
1588 ss->s = 1;
1589 ss->type = 0x03; /* Read/Write, Accessed */
1590 ss->db = 1; /* 32bit stack segment */
1591 ss->dpl = 0;
1592 ss->present = 1;
1593}
1594
1595static int
1596emulate_syscall(struct x86_emulate_ctxt *ctxt)
1597{
1598 struct decode_cache *c = &ctxt->decode;
1599 struct kvm_segment cs, ss;
1600 u64 msr_data;
1601
1602 /* syscall is not available in real mode */
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001603 if (ctxt->mode == X86EMUL_MODE_REAL || ctxt->mode == X86EMUL_MODE_VM86)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001604 return X86EMUL_UNHANDLEABLE;
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);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001654 return X86EMUL_UNHANDLEABLE;
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 */
1660 if (ctxt->mode == X86EMUL_MODE_PROT64)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001661 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001662
1663 setup_syscalls_segments(ctxt, &cs, &ss);
1664
1665 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1666 switch (ctxt->mode) {
1667 case X86EMUL_MODE_PROT32:
1668 if ((msr_data & 0xfffc) == 0x0) {
1669 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001670 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001671 }
1672 break;
1673 case X86EMUL_MODE_PROT64:
1674 if (msr_data == 0x0) {
1675 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001676 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001677 }
1678 break;
1679 }
1680
1681 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1682 cs.selector = (u16)msr_data;
1683 cs.selector &= ~SELECTOR_RPL_MASK;
1684 ss.selector = cs.selector + 8;
1685 ss.selector &= ~SELECTOR_RPL_MASK;
1686 if (ctxt->mode == X86EMUL_MODE_PROT64
1687 || is_long_mode(ctxt->vcpu)) {
1688 cs.db = 0;
1689 cs.l = 1;
1690 }
1691
1692 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1693 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1694
1695 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1696 c->eip = msr_data;
1697
1698 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1699 c->regs[VCPU_REGS_RSP] = msr_data;
1700
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001701 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001702}
1703
Andre Przywara4668f052009-06-18 12:56:02 +02001704static int
1705emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1706{
1707 struct decode_cache *c = &ctxt->decode;
1708 struct kvm_segment cs, ss;
1709 u64 msr_data;
1710 int usermode;
1711
Gleb Natapova0044752010-02-10 14:21:31 +02001712 /* inject #GP if in real mode or Virtual 8086 mode */
1713 if (ctxt->mode == X86EMUL_MODE_REAL ||
1714 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001715 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001716 return X86EMUL_UNHANDLEABLE;
Andre Przywara4668f052009-06-18 12:56:02 +02001717 }
1718
Andre Przywara4668f052009-06-18 12:56:02 +02001719 setup_syscalls_segments(ctxt, &cs, &ss);
1720
1721 if ((c->rex_prefix & 0x8) != 0x0)
1722 usermode = X86EMUL_MODE_PROT64;
1723 else
1724 usermode = X86EMUL_MODE_PROT32;
1725
1726 cs.dpl = 3;
1727 ss.dpl = 3;
1728 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1729 switch (usermode) {
1730 case X86EMUL_MODE_PROT32:
1731 cs.selector = (u16)(msr_data + 16);
1732 if ((msr_data & 0xfffc) == 0x0) {
1733 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001734 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001735 }
1736 ss.selector = (u16)(msr_data + 24);
1737 break;
1738 case X86EMUL_MODE_PROT64:
1739 cs.selector = (u16)(msr_data + 32);
1740 if (msr_data == 0x0) {
1741 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001742 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001743 }
1744 ss.selector = cs.selector + 8;
1745 cs.db = 0;
1746 cs.l = 1;
1747 break;
1748 }
1749 cs.selector |= SELECTOR_RPL_MASK;
1750 ss.selector |= SELECTOR_RPL_MASK;
1751
1752 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1753 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1754
1755 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1756 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1757
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001758 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001759}
1760
Gleb Natapov9c537242010-03-18 15:20:05 +02001761static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1762 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001763{
1764 int iopl;
1765 if (ctxt->mode == X86EMUL_MODE_REAL)
1766 return false;
1767 if (ctxt->mode == X86EMUL_MODE_VM86)
1768 return true;
1769 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001770 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001771}
1772
1773static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1774 struct x86_emulate_ops *ops,
1775 u16 port, u16 len)
1776{
1777 struct kvm_segment tr_seg;
1778 int r;
1779 u16 io_bitmap_ptr;
1780 u8 perm, bit_idx = port & 0x7;
1781 unsigned mask = (1 << len) - 1;
1782
1783 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1784 if (tr_seg.unusable)
1785 return false;
1786 if (tr_seg.limit < 103)
1787 return false;
1788 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1789 NULL);
1790 if (r != X86EMUL_CONTINUE)
1791 return false;
1792 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1793 return false;
1794 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1795 ctxt->vcpu, NULL);
1796 if (r != X86EMUL_CONTINUE)
1797 return false;
1798 if ((perm >> bit_idx) & mask)
1799 return false;
1800 return true;
1801}
1802
1803static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1804 struct x86_emulate_ops *ops,
1805 u16 port, u16 len)
1806{
Gleb Natapov9c537242010-03-18 15:20:05 +02001807 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001808 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1809 return false;
1810 return true;
1811}
1812
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001813int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001814x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001815{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001816 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001817 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001818 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001819 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001820 unsigned int port;
1821 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001822 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001823
Glauber Costa310b5d32009-05-12 16:21:06 -04001824 ctxt->interruptibility = 0;
1825
Laurent Vivier34273182007-09-18 11:27:37 +02001826 /* Shadow copy of register state. Committed on successful emulation.
1827 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1828 * modify them.
1829 */
1830
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001831 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001832 saved_eip = c->eip;
1833
Gleb Natapov11616242010-02-11 14:43:14 +02001834 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
1835 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1836 goto done;
1837 }
1838
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001839 /* LOCK prefix is allowed only with some instructions */
1840 if (c->lock_prefix && !(c->d & Lock)) {
1841 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1842 goto done;
1843 }
1844
Gleb Natapove92805a2010-02-10 14:21:35 +02001845 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02001846 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02001847 kvm_inject_gp(ctxt->vcpu, 0);
1848 goto done;
1849 }
1850
Avi Kivityc7e75a32007-10-28 16:34:25 +02001851 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001852 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001853
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001854 if (c->rep_prefix && (c->d & String)) {
1855 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02001856 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001857 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001858 goto done;
1859 }
1860 /* The second termination condition only applies for REPE
1861 * and REPNE. Test if the repeat string operation prefix is
1862 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1863 * corresponding termination condition according to:
1864 * - if REPE/REPZ and ZF = 0 then done
1865 * - if REPNE/REPNZ and ZF = 1 then done
1866 */
1867 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1868 (c->b == 0xae) || (c->b == 0xaf)) {
1869 if ((c->rep_prefix == REPE_PREFIX) &&
1870 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001871 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001872 goto done;
1873 }
1874 if ((c->rep_prefix == REPNE_PREFIX) &&
1875 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001876 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001877 goto done;
1878 }
1879 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02001880 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02001881 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001882 }
1883
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001884 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001885 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001886 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001887 rc = ops->read_emulated((unsigned long)c->src.ptr,
1888 &c->src.val,
1889 c->src.bytes,
1890 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001891 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001892 goto done;
1893 c->src.orig_val = c->src.val;
1894 }
1895
Gleb Natapove35b7b92010-02-25 16:36:42 +02001896 if (c->src2.type == OP_MEM) {
1897 c->src2.ptr = (unsigned long *)(memop + c->src.bytes);
1898 c->src2.val = 0;
1899 rc = ops->read_emulated((unsigned long)c->src2.ptr,
1900 &c->src2.val,
1901 c->src2.bytes,
1902 ctxt->vcpu);
1903 if (rc != X86EMUL_CONTINUE)
1904 goto done;
1905 }
1906
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001907 if ((c->d & DstMask) == ImplicitOps)
1908 goto special_insn;
1909
1910
1911 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001912 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001913 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1914 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001915 if (c->d & BitOp) {
1916 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001917
Laurent Viviere4e03de2007-09-18 11:52:50 +02001918 c->dst.ptr = (void *)c->dst.ptr +
1919 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001920 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001921 if (!(c->d & Mov)) {
1922 /* optimisation - avoid slow emulated read */
1923 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1924 &c->dst.val,
1925 c->dst.bytes,
1926 ctxt->vcpu);
1927 if (rc != X86EMUL_CONTINUE)
1928 goto done;
1929 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001930 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001931 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001932
Avi Kivity018a98d2007-11-27 19:30:56 +02001933special_insn:
1934
Laurent Viviere4e03de2007-09-18 11:52:50 +02001935 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 goto twobyte_insn;
1937
Laurent Viviere4e03de2007-09-18 11:52:50 +02001938 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 case 0x00 ... 0x05:
1940 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001941 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001943 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001944 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1945 break;
1946 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001947 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001948 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001949 goto done;
1950 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951 case 0x08 ... 0x0d:
1952 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001953 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001955 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001956 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1957 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 case 0x10 ... 0x15:
1959 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001960 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001962 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001963 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1964 break;
1965 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001966 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001967 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001968 goto done;
1969 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 case 0x18 ... 0x1d:
1971 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001972 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001974 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001975 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1976 break;
1977 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001978 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001979 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001980 goto done;
1981 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001982 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001984 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 break;
1986 case 0x28 ... 0x2d:
1987 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001988 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 break;
1990 case 0x30 ... 0x35:
1991 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001992 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993 break;
1994 case 0x38 ... 0x3d:
1995 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001996 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001998 case 0x40 ... 0x47: /* inc r16/r32 */
1999 emulate_1op("inc", c->dst, ctxt->eflags);
2000 break;
2001 case 0x48 ... 0x4f: /* dec r16/r32 */
2002 emulate_1op("dec", c->dst, ctxt->eflags);
2003 break;
2004 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002005 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002006 break;
2007 case 0x58 ... 0x5f: /* pop reg */
2008 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002009 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002010 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002011 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002012 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002013 case 0x60: /* pusha */
2014 emulate_pusha(ctxt);
2015 break;
2016 case 0x61: /* popa */
2017 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002018 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002019 goto done;
2020 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002022 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002024 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002026 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002027 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002028 emulate_push(ctxt);
2029 break;
2030 case 0x6c: /* insb */
2031 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002032 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2033 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2034 kvm_inject_gp(ctxt->vcpu, 0);
2035 goto done;
2036 }
2037 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002038 1,
2039 (c->d & ByteOp) ? 1 : c->op_bytes,
2040 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002041 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002042 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002043 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002044 c->regs[VCPU_REGS_RDI]),
2045 c->rep_prefix,
2046 c->regs[VCPU_REGS_RDX]) == 0) {
2047 c->eip = saved_eip;
2048 return -1;
2049 }
2050 return 0;
2051 case 0x6e: /* outsb */
2052 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002053 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2054 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2055 kvm_inject_gp(ctxt->vcpu, 0);
2056 goto done;
2057 }
Avi Kivity851ba692009-08-24 11:10:17 +03002058 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002059 0,
2060 (c->d & ByteOp) ? 1 : c->op_bytes,
2061 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002062 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002063 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002064 register_address(c,
2065 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002066 c->regs[VCPU_REGS_RSI]),
2067 c->rep_prefix,
2068 c->regs[VCPU_REGS_RDX]) == 0) {
2069 c->eip = saved_eip;
2070 return -1;
2071 }
2072 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002073 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002074 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002075 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002078 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 case 0:
2080 goto add;
2081 case 1:
2082 goto or;
2083 case 2:
2084 goto adc;
2085 case 3:
2086 goto sbb;
2087 case 4:
2088 goto and;
2089 case 5:
2090 goto sub;
2091 case 6:
2092 goto xor;
2093 case 7:
2094 goto cmp;
2095 }
2096 break;
2097 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002098 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 break;
2100 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002101 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002103 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002105 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 break;
2107 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002108 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 break;
2110 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002111 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 break; /* 64b reg: zero-extend */
2113 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002114 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 break;
2116 }
2117 /*
2118 * Write back the memory destination with implicit LOCK
2119 * prefix.
2120 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002121 c->dst.val = c->src.val;
2122 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002125 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002126 case 0x8c: { /* mov r/m, sreg */
2127 struct kvm_segment segreg;
2128
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002129 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002130 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2131 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002132 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2133 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002134 }
2135 c->dst.val = segreg.selector;
2136 break;
2137 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002138 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002139 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002140 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002141 case 0x8e: { /* mov seg, r/m16 */
2142 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002143
2144 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002145
Gleb Natapovc6975182010-02-18 12:15:01 +02002146 if (c->modrm_reg == VCPU_SREG_CS ||
2147 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002148 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2149 goto done;
2150 }
2151
Glauber Costa310b5d32009-05-12 16:21:06 -04002152 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002153 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002154
Gleb Natapovc6975182010-02-18 12:15:01 +02002155 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002156
2157 c->dst.type = OP_NONE; /* Disable writeback. */
2158 break;
2159 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002162 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002165 case 0x90: /* nop / xchg r8,rax */
2166 if (!(c->rex_prefix & 1)) { /* nop */
2167 c->dst.type = OP_NONE;
2168 break;
2169 }
2170 case 0x91 ... 0x97: /* xchg reg,rax */
2171 c->src.type = c->dst.type = OP_REG;
2172 c->src.bytes = c->dst.bytes = c->op_bytes;
2173 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2174 c->src.val = *(c->src.ptr);
2175 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002176 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002177 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178 emulate_push(ctxt);
2179 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002180 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002181 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002182 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002183 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002184 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2185 if (rc != X86EMUL_CONTINUE)
2186 goto done;
2187 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002188 case 0xa0 ... 0xa1: /* mov */
2189 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2190 c->dst.val = c->src.val;
2191 break;
2192 case 0xa2 ... 0xa3: /* mov */
2193 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2194 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002196 c->dst.type = OP_MEM;
2197 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002198 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002199 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002200 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002201 rc = ops->read_emulated(register_address(c,
2202 seg_override_base(ctxt, c),
2203 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002204 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002205 c->dst.bytes, ctxt->vcpu);
2206 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002208 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002209 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002210 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002211 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002212 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002213 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214 break;
2215 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002216 c->src.type = OP_NONE; /* Disable writeback. */
2217 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002218 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002219 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002220 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002221 rc = ops->read_emulated((unsigned long)c->src.ptr,
2222 &c->src.val,
2223 c->src.bytes,
2224 ctxt->vcpu);
2225 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002226 goto done;
2227
2228 c->dst.type = OP_NONE; /* Disable writeback. */
2229 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002230 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002231 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002232 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002233 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2234 &c->dst.val,
2235 c->dst.bytes,
2236 ctxt->vcpu);
2237 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002238 goto done;
2239
2240 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2241
2242 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2243
Harvey Harrison7a9572752008-02-19 07:40:41 -08002244 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002245 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2246 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002247 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002248 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2249 : c->dst.bytes);
2250
2251 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002253 c->dst.type = OP_MEM;
2254 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002255 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002256 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002257 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002258 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002259 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002260 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002261 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 break;
2263 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002264 c->dst.type = OP_REG;
2265 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2266 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002267 rc = ops->read_emulated(register_address(c,
2268 seg_override_base(ctxt, c),
2269 c->regs[VCPU_REGS_RSI]),
2270 &c->dst.val,
2271 c->dst.bytes,
2272 ctxt->vcpu);
2273 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002275 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002276 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002277 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 break;
2279 case 0xae ... 0xaf: /* scas */
2280 DPRINTF("Urk! I don't handle SCAS.\n");
2281 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002282 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002283 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002284 case 0xc0 ... 0xc1:
2285 emulate_grp2(ctxt);
2286 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002287 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002288 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002289 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002290 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002291 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002292 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2293 mov:
2294 c->dst.val = c->src.val;
2295 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002296 case 0xcb: /* ret far */
2297 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002298 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002299 goto done;
2300 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002301 case 0xd0 ... 0xd1: /* Grp2 */
2302 c->src.val = 1;
2303 emulate_grp2(ctxt);
2304 break;
2305 case 0xd2 ... 0xd3: /* Grp2 */
2306 c->src.val = c->regs[VCPU_REGS_RCX];
2307 emulate_grp2(ctxt);
2308 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002309 case 0xe4: /* inb */
2310 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002311 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002312 io_dir_in = 1;
2313 goto do_io;
2314 case 0xe6: /* outb */
2315 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002316 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002317 io_dir_in = 0;
2318 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002319 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002320 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002321 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002322 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002323 emulate_push(ctxt);
2324 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002325 }
2326 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002327 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002328 case 0xea: /* jmp far */
Gleb Natapovea79849d2010-02-25 16:36:43 +02002329 jump_far:
Gleb Natapovc6975182010-02-18 12:15:01 +02002330 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2331 VCPU_SREG_CS))
2332 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002333
Gleb Natapov782b8772009-04-12 13:36:25 +03002334 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002335 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002336 case 0xeb:
2337 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002338 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002339 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002340 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002341 case 0xec: /* in al,dx */
2342 case 0xed: /* in (e/r)ax,dx */
2343 port = c->regs[VCPU_REGS_RDX];
2344 io_dir_in = 1;
2345 goto do_io;
2346 case 0xee: /* out al,dx */
2347 case 0xef: /* out (e/r)ax,dx */
2348 port = c->regs[VCPU_REGS_RDX];
2349 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002350 do_io:
2351 if (!emulator_io_permited(ctxt, ops, port,
2352 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2353 kvm_inject_gp(ctxt->vcpu, 0);
2354 goto done;
2355 }
2356 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002357 (c->d & ByteOp) ? 1 : c->op_bytes,
2358 port) != 0) {
2359 c->eip = saved_eip;
2360 goto cannot_emulate;
2361 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002362 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002363 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002364 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002365 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002366 case 0xf5: /* cmc */
2367 /* complement carry flag from eflags reg */
2368 ctxt->eflags ^= EFLG_CF;
2369 c->dst.type = OP_NONE; /* Disable writeback. */
2370 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002371 case 0xf6 ... 0xf7: /* Grp3 */
2372 rc = emulate_grp3(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002373 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002374 goto done;
2375 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002376 case 0xf8: /* clc */
2377 ctxt->eflags &= ~EFLG_CF;
2378 c->dst.type = OP_NONE; /* Disable writeback. */
2379 break;
2380 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002381 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002382 kvm_inject_gp(ctxt->vcpu, 0);
2383 else {
2384 ctxt->eflags &= ~X86_EFLAGS_IF;
2385 c->dst.type = OP_NONE; /* Disable writeback. */
2386 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002387 break;
2388 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002389 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002390 kvm_inject_gp(ctxt->vcpu, 0);
2391 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002392 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002393 ctxt->eflags |= X86_EFLAGS_IF;
2394 c->dst.type = OP_NONE; /* Disable writeback. */
2395 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002396 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002397 case 0xfc: /* cld */
2398 ctxt->eflags &= ~EFLG_DF;
2399 c->dst.type = OP_NONE; /* Disable writeback. */
2400 break;
2401 case 0xfd: /* std */
2402 ctxt->eflags |= EFLG_DF;
2403 c->dst.type = OP_NONE; /* Disable writeback. */
2404 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002405 case 0xfe: /* Grp4 */
2406 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002407 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002408 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002409 goto done;
2410 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002411 case 0xff: /* Grp5 */
2412 if (c->modrm_reg == 5)
2413 goto jump_far;
2414 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002416
2417writeback:
2418 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002419 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002420 goto done;
2421
2422 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002423 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002424 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002425
2426done:
2427 if (rc == X86EMUL_UNHANDLEABLE) {
2428 c->eip = saved_eip;
2429 return -1;
2430 }
2431 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432
2433twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002434 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002436 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 u16 size;
2438 unsigned long address;
2439
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002440 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002441 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002442 goto cannot_emulate;
2443
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002444 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002445 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002446 goto done;
2447
Avi Kivity33e38852008-05-21 15:34:25 +03002448 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002449 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002450 /* Disable writeback. */
2451 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002454 rc = read_descriptor(ctxt, ops, c->src.ptr,
2455 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002456 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 goto done;
2458 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002459 /* Disable writeback. */
2460 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002462 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002463 if (c->modrm_mod == 3) {
2464 switch (c->modrm_rm) {
2465 case 1:
2466 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002467 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002468 goto done;
2469 break;
2470 default:
2471 goto cannot_emulate;
2472 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002473 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002474 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002475 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002476 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002477 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002478 goto done;
2479 realmode_lidt(ctxt->vcpu, size, address);
2480 }
Avi Kivity16286d02008-04-14 14:40:50 +03002481 /* Disable writeback. */
2482 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 break;
2484 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002485 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02002486 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 break;
2488 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02002489 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
2490 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002491 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 break;
2493 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002494 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002495 /* Disable writeback. */
2496 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 break;
2498 default:
2499 goto cannot_emulate;
2500 }
2501 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002502 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002503 rc = emulate_syscall(ctxt);
2504 if (rc != X86EMUL_CONTINUE)
2505 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002506 else
2507 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002508 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002509 case 0x06:
2510 emulate_clts(ctxt->vcpu);
2511 c->dst.type = OP_NONE;
2512 break;
2513 case 0x08: /* invd */
2514 case 0x09: /* wbinvd */
2515 case 0x0d: /* GrpP (prefetch) */
2516 case 0x18: /* Grp16 (prefetch/nop) */
2517 c->dst.type = OP_NONE;
2518 break;
2519 case 0x20: /* mov cr, reg */
2520 if (c->modrm_mod != 3)
2521 goto cannot_emulate;
Gleb Natapov52a46612010-03-18 15:20:03 +02002522 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002523 c->dst.type = OP_NONE; /* no writeback */
2524 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002526 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002528 if (emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002529 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002530 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002531 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002533 case 0x22: /* mov reg, cr */
2534 if (c->modrm_mod != 3)
2535 goto cannot_emulate;
Gleb Natapov52a46612010-03-18 15:20:03 +02002536 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002537 c->dst.type = OP_NONE;
2538 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002540 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002542 if (emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]))
Laurent Viviera01af5e2007-09-24 11:10:56 +02002543 goto cannot_emulate;
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002544 rc = X86EMUL_CONTINUE;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002545 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002547 case 0x30:
2548 /* wrmsr */
2549 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2550 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002551 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002552 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov063db062010-03-18 15:20:06 +02002553 c->eip = ctxt->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02002554 }
2555 rc = X86EMUL_CONTINUE;
2556 c->dst.type = OP_NONE;
2557 break;
2558 case 0x32:
2559 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002560 if (kvm_get_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 Natapov063db062010-03-18 15:20:06 +02002562 c->eip = ctxt->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02002563 } else {
2564 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2565 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2566 }
2567 rc = X86EMUL_CONTINUE;
2568 c->dst.type = OP_NONE;
2569 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002570 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002571 rc = emulate_sysenter(ctxt);
2572 if (rc != X86EMUL_CONTINUE)
2573 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002574 else
2575 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002576 break;
2577 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002578 rc = emulate_sysexit(ctxt);
2579 if (rc != X86EMUL_CONTINUE)
2580 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002581 else
2582 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002585 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002586 if (!test_cc(c->b, ctxt->eflags))
2587 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002589 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002590 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002591 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002592 c->dst.type = OP_NONE;
2593 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002594 case 0xa0: /* push fs */
2595 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2596 break;
2597 case 0xa1: /* pop fs */
2598 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002599 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002600 goto done;
2601 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002602 case 0xa3:
2603 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002604 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002605 /* only subword offset */
2606 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002607 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002608 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002609 case 0xa4: /* shld imm8, r, r/m */
2610 case 0xa5: /* shld cl, r, r/m */
2611 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2612 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002613 case 0xa8: /* push gs */
2614 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2615 break;
2616 case 0xa9: /* pop gs */
2617 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002618 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002619 goto done;
2620 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002621 case 0xab:
2622 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002623 /* only subword offset */
2624 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002625 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002626 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002627 case 0xac: /* shrd imm8, r, r/m */
2628 case 0xad: /* shrd cl, r, r/m */
2629 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2630 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002631 case 0xae: /* clflush */
2632 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 case 0xb0 ... 0xb1: /* cmpxchg */
2634 /*
2635 * Save real source value, then compare EAX against
2636 * destination.
2637 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002638 c->src.orig_val = c->src.val;
2639 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002640 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2641 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002643 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 } else {
2645 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002646 c->dst.type = OP_REG;
2647 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 }
2649 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 case 0xb3:
2651 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002652 /* only subword offset */
2653 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002654 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002657 c->dst.bytes = c->op_bytes;
2658 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2659 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002662 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 case 0:
2664 goto bt;
2665 case 1:
2666 goto bts;
2667 case 2:
2668 goto btr;
2669 case 3:
2670 goto btc;
2671 }
2672 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002673 case 0xbb:
2674 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002675 /* only subword offset */
2676 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002677 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002678 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002680 c->dst.bytes = c->op_bytes;
2681 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2682 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002684 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002685 c->dst.bytes = c->op_bytes;
2686 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2687 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002688 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002690 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002691 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002692 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002693 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002694 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 }
2696 goto writeback;
2697
2698cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002699 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002700 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 return -1;
2702}