blob: 46a7ee3040a02ca540e571828515b4f8a0e999de [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020079#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020080#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082/* Source 2 operand type */
83#define Src2None (0<<29)
84#define Src2CL (1<<29)
85#define Src2ImmByte (2<<29)
86#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030087#define Src2Imm16 (4<<29)
Gleb Natapove35b7b92010-02-25 16:36:42 +020088#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
89 in memory and second argument is located
90 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
Avi Kivity43bb19c2008-01-18 12:46:50 +020093enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020094 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020095 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020096 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020097};
98
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010099static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200101 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300103 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300104 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200106 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200108 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
109 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200111 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300113 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300114 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200116 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300118 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300119 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200121 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200123 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200125 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
127 0, 0, 0, 0,
128 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200129 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
131 0, 0, 0, 0,
132 /* 0x38 - 0x3F */
133 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
134 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200135 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
136 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700137 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200138 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700139 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200140 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300141 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200142 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
143 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300144 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200145 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
146 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700147 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200148 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
149 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700150 0, 0, 0, 0,
151 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300152 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300153 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
154 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300155 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300156 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300158 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300159 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200162 Group | Group1_80, Group | Group1_81,
163 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200165 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0x88 - 0x8F */
167 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
168 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200169 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200170 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300171 /* 0x90 - 0x97 */
172 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
173 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300174 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300175 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200177 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
178 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200179 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
180 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200182 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
183 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
184 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300185 /* 0xB0 - 0xB7 */
186 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
187 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
188 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 /* 0xB8 - 0xBF */
191 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
192 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
193 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300196 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200197 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300198 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300200 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300201 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xD0 - 0xD7 */
203 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 0, 0, 0, 0,
206 /* 0xD8 - 0xDF */
207 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300208 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300209 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300210 ByteOp | SrcImmUByte, SrcImmUByte,
211 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300212 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300213 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300214 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300215 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
216 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xF0 - 0xF7 */
218 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200219 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700221 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300222 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223};
224
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100225static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200227 0, Group | GroupDual | Group7, 0, 0,
228 0, ImplicitOps, ImplicitOps | Priv, 0,
229 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
230 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 /* 0x10 - 0x1F */
232 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200234 ModRM | ImplicitOps | Priv, ModRM | Priv,
235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 0, 0, 0, 0, 0, 0, 0, 0,
238 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200239 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
240 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200241 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 /* 0x40 - 0x47 */
243 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
244 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 /* 0x48 - 0x4F */
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 /* 0x50 - 0x5F */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 /* 0x60 - 0x6F */
255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256 /* 0x70 - 0x7F */
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300259 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
260 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261 /* 0x90 - 0x9F */
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
263 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300264 ImplicitOps | Stack, ImplicitOps | Stack,
265 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100266 DstMem | SrcReg | Src2ImmByte | ModRM,
267 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300269 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200270 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100271 DstMem | SrcReg | Src2ImmByte | ModRM,
272 DstMem | SrcReg | Src2CL | ModRM,
273 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200275 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
276 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
278 DstReg | SrcMem16 | ModRM | Mov,
279 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200280 0, 0,
281 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
283 DstReg | SrcMem16 | ModRM | Mov,
284 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200285 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
286 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800287 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 /* 0xD0 - 0xDF */
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 /* 0xE0 - 0xEF */
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292 /* 0xF0 - 0xFF */
293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
294};
295
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100296static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200297 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200298 ByteOp | DstMem | SrcImm | ModRM | Lock,
299 ByteOp | DstMem | SrcImm | ModRM | Lock,
300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200306 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200307 DstMem | SrcImm | ModRM | Lock,
308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200315 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200316 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
317 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
318 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200324 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200325 DstMem | SrcImmByte | ModRM | Lock,
326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200333 [Group1A*8] =
334 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200335 [Group3_Byte*8] =
336 ByteOp | SrcImm | DstMem | ModRM, 0,
337 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
338 0, 0, 0, 0,
339 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400340 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300341 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200342 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200343 [Group4*8] =
344 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
345 0, 0, 0, 0, 0, 0,
346 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300347 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
348 SrcMem | ModRM | Stack, 0,
Gleb Natapovea798492010-02-25 16:36:43 +0200349 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
350 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200351 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200352 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300353 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200354 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200355 [Group8*8] =
356 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200357 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
358 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200359 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200360 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200361};
362
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100363static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200364 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200365 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300366 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200367 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200368 [Group9*8] =
369 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200370};
371
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200373#define EFLG_ID (1<<21)
374#define EFLG_VIP (1<<20)
375#define EFLG_VIF (1<<19)
376#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200377#define EFLG_VM (1<<17)
378#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200379#define EFLG_IOPL (3<<12)
380#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381#define EFLG_OF (1<<11)
382#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200383#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200384#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385#define EFLG_SF (1<<7)
386#define EFLG_ZF (1<<6)
387#define EFLG_AF (1<<4)
388#define EFLG_PF (1<<2)
389#define EFLG_CF (1<<0)
390
391/*
392 * Instruction emulation:
393 * Most instructions are emulated directly via a fragment of inline assembly
394 * code. This allows us to save/restore EFLAGS and thus very easily pick up
395 * any modified flags.
396 */
397
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800398#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399#define _LO32 "k" /* force 32-bit operand */
400#define _STK "%%rsp" /* stack pointer */
401#elif defined(__i386__)
402#define _LO32 "" /* force 32-bit operand */
403#define _STK "%%esp" /* stack pointer */
404#endif
405
406/*
407 * These EFLAGS bits are restored from saved value during emulation, and
408 * any changes are written back to the saved value after emulation.
409 */
410#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
411
412/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200413#define _PRE_EFLAGS(_sav, _msk, _tmp) \
414 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
415 "movl %"_sav",%"_LO32 _tmp"; " \
416 "push %"_tmp"; " \
417 "push %"_tmp"; " \
418 "movl %"_msk",%"_LO32 _tmp"; " \
419 "andl %"_LO32 _tmp",("_STK"); " \
420 "pushf; " \
421 "notl %"_LO32 _tmp"; " \
422 "andl %"_LO32 _tmp",("_STK"); " \
423 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
424 "pop %"_tmp"; " \
425 "orl %"_LO32 _tmp",("_STK"); " \
426 "popf; " \
427 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428
429/* After executing instruction: write-back necessary bits in EFLAGS. */
430#define _POST_EFLAGS(_sav, _msk, _tmp) \
431 /* _sav |= EFLAGS & _msk; */ \
432 "pushf; " \
433 "pop %"_tmp"; " \
434 "andl %"_msk",%"_LO32 _tmp"; " \
435 "orl %"_LO32 _tmp",%"_sav"; "
436
Avi Kivitydda96d82008-11-26 15:14:10 +0200437#ifdef CONFIG_X86_64
438#define ON64(x) x
439#else
440#define ON64(x)
441#endif
442
Avi Kivity6b7ad612008-11-26 15:30:45 +0200443#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
444 do { \
445 __asm__ __volatile__ ( \
446 _PRE_EFLAGS("0", "4", "2") \
447 _op _suffix " %"_x"3,%1; " \
448 _POST_EFLAGS("0", "4", "2") \
449 : "=m" (_eflags), "=m" ((_dst).val), \
450 "=&r" (_tmp) \
451 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200452 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200453
454
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455/* Raw emulation: instruction has two explicit operands. */
456#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200457 do { \
458 unsigned long _tmp; \
459 \
460 switch ((_dst).bytes) { \
461 case 2: \
462 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
463 break; \
464 case 4: \
465 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
466 break; \
467 case 8: \
468 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
469 break; \
470 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 } while (0)
472
473#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
474 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200475 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400476 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200478 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 break; \
480 default: \
481 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
482 _wx, _wy, _lx, _ly, _qx, _qy); \
483 break; \
484 } \
485 } while (0)
486
487/* Source operand is byte-sized and may be restricted to just %cl. */
488#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
489 __emulate_2op(_op, _src, _dst, _eflags, \
490 "b", "c", "b", "c", "b", "c", "b", "c")
491
492/* Source operand is byte, word, long or quad sized. */
493#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
494 __emulate_2op(_op, _src, _dst, _eflags, \
495 "b", "q", "w", "r", _LO32, "r", "", "r")
496
497/* Source operand is word, long or quad sized. */
498#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
499 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
500 "w", "r", _LO32, "r", "", "r")
501
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100502/* Instruction has three operands and one operand is stored in ECX register */
503#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
504 do { \
505 unsigned long _tmp; \
506 _type _clv = (_cl).val; \
507 _type _srcv = (_src).val; \
508 _type _dstv = (_dst).val; \
509 \
510 __asm__ __volatile__ ( \
511 _PRE_EFLAGS("0", "5", "2") \
512 _op _suffix " %4,%1 \n" \
513 _POST_EFLAGS("0", "5", "2") \
514 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
515 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
516 ); \
517 \
518 (_cl).val = (unsigned long) _clv; \
519 (_src).val = (unsigned long) _srcv; \
520 (_dst).val = (unsigned long) _dstv; \
521 } while (0)
522
523#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
524 do { \
525 switch ((_dst).bytes) { \
526 case 2: \
527 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
528 "w", unsigned short); \
529 break; \
530 case 4: \
531 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
532 "l", unsigned int); \
533 break; \
534 case 8: \
535 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
536 "q", unsigned long)); \
537 break; \
538 } \
539 } while (0)
540
Avi Kivitydda96d82008-11-26 15:14:10 +0200541#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 do { \
543 unsigned long _tmp; \
544 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200545 __asm__ __volatile__ ( \
546 _PRE_EFLAGS("0", "3", "2") \
547 _op _suffix " %1; " \
548 _POST_EFLAGS("0", "3", "2") \
549 : "=m" (_eflags), "+m" ((_dst).val), \
550 "=&r" (_tmp) \
551 : "i" (EFLAGS_MASK)); \
552 } while (0)
553
554/* Instruction has only one explicit operand (no source operand). */
555#define emulate_1op(_op, _dst, _eflags) \
556 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400557 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200558 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
559 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
560 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
561 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562 } \
563 } while (0)
564
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565/* Fetch next part of the instruction being emulated. */
566#define insn_fetch(_type, _size, _eip) \
567({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200568 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200569 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 goto done; \
571 (_eip) += (_size); \
572 (_type)_x; \
573})
574
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800575static inline unsigned long ad_mask(struct decode_cache *c)
576{
577 return (1UL << (c->ad_bytes << 3)) - 1;
578}
579
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800581static inline unsigned long
582address_mask(struct decode_cache *c, unsigned long reg)
583{
584 if (c->ad_bytes == sizeof(unsigned long))
585 return reg;
586 else
587 return reg & ad_mask(c);
588}
589
590static inline unsigned long
591register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
592{
593 return base + address_mask(c, reg);
594}
595
Harvey Harrison7a9572752008-02-19 07:40:41 -0800596static inline void
597register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
598{
599 if (c->ad_bytes == sizeof(unsigned long))
600 *reg += inc;
601 else
602 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
603}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604
Harvey Harrison7a9572752008-02-19 07:40:41 -0800605static inline void jmp_rel(struct decode_cache *c, int rel)
606{
607 register_address_increment(c, &c->eip, rel);
608}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300609
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300610static void set_seg_override(struct decode_cache *c, int seg)
611{
612 c->has_seg_override = true;
613 c->seg_override = seg;
614}
615
616static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
617{
618 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
619 return 0;
620
621 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
622}
623
624static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
625 struct decode_cache *c)
626{
627 if (!c->has_seg_override)
628 return 0;
629
630 return seg_base(ctxt, c->seg_override);
631}
632
633static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
634{
635 return seg_base(ctxt, VCPU_SREG_ES);
636}
637
638static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
639{
640 return seg_base(ctxt, VCPU_SREG_SS);
641}
642
Avi Kivity62266862007-11-20 13:15:52 +0200643static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
644 struct x86_emulate_ops *ops,
645 unsigned long linear, u8 *dest)
646{
647 struct fetch_cache *fc = &ctxt->decode.fetch;
648 int rc;
649 int size;
650
651 if (linear < fc->start || linear >= fc->end) {
652 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200653 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900654 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200655 return rc;
656 fc->start = linear;
657 fc->end = linear + size;
658 }
659 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900660 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200661}
662
663static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
664 struct x86_emulate_ops *ops,
665 unsigned long eip, void *dest, unsigned size)
666{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900667 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200668
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200669 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200670 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200671 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200672 eip += ctxt->cs_base;
673 while (size--) {
674 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900675 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200676 return rc;
677 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200679}
680
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000681/*
682 * Given the 'reg' portion of a ModRM byte, and a register block, return a
683 * pointer into the block that addresses the relevant register.
684 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
685 */
686static void *decode_register(u8 modrm_reg, unsigned long *regs,
687 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
689 void *p;
690
691 p = &regs[modrm_reg];
692 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
693 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
694 return p;
695}
696
697static int read_descriptor(struct x86_emulate_ctxt *ctxt,
698 struct x86_emulate_ops *ops,
699 void *ptr,
700 u16 *size, unsigned long *address, int op_bytes)
701{
702 int rc;
703
704 if (op_bytes == 2)
705 op_bytes = 3;
706 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300707 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200708 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900709 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300711 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200712 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 return rc;
714}
715
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300716static int test_cc(unsigned int condition, unsigned int flags)
717{
718 int rc = 0;
719
720 switch ((condition & 15) >> 1) {
721 case 0: /* o */
722 rc |= (flags & EFLG_OF);
723 break;
724 case 1: /* b/c/nae */
725 rc |= (flags & EFLG_CF);
726 break;
727 case 2: /* z/e */
728 rc |= (flags & EFLG_ZF);
729 break;
730 case 3: /* be/na */
731 rc |= (flags & (EFLG_CF|EFLG_ZF));
732 break;
733 case 4: /* s */
734 rc |= (flags & EFLG_SF);
735 break;
736 case 5: /* p/pe */
737 rc |= (flags & EFLG_PF);
738 break;
739 case 7: /* le/ng */
740 rc |= (flags & EFLG_ZF);
741 /* fall through */
742 case 6: /* l/nge */
743 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
744 break;
745 }
746
747 /* Odd condition identifiers (lsb == 1) have inverted sense. */
748 return (!!rc ^ (condition & 1));
749}
750
Avi Kivity3c118e22007-10-31 10:27:04 +0200751static void decode_register_operand(struct operand *op,
752 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200753 int inhibit_bytereg)
754{
Avi Kivity33615aa2007-10-31 11:15:56 +0200755 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200756 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200757
758 if (!(c->d & ModRM))
759 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200760 op->type = OP_REG;
761 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200762 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->val = *(u8 *)op->ptr;
764 op->bytes = 1;
765 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200766 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200767 op->bytes = c->op_bytes;
768 switch (op->bytes) {
769 case 2:
770 op->val = *(u16 *)op->ptr;
771 break;
772 case 4:
773 op->val = *(u32 *)op->ptr;
774 break;
775 case 8:
776 op->val = *(u64 *) op->ptr;
777 break;
778 }
779 }
780 op->orig_val = op->val;
781}
782
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783static int decode_modrm(struct x86_emulate_ctxt *ctxt,
784 struct x86_emulate_ops *ops)
785{
786 struct decode_cache *c = &ctxt->decode;
787 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700788 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900789 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790
791 if (c->rex_prefix) {
792 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
793 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
794 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
795 }
796
797 c->modrm = insn_fetch(u8, 1, c->eip);
798 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
799 c->modrm_reg |= (c->modrm & 0x38) >> 3;
800 c->modrm_rm |= (c->modrm & 0x07);
801 c->modrm_ea = 0;
802 c->use_modrm_ea = 1;
803
804 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300805 c->modrm_ptr = decode_register(c->modrm_rm,
806 c->regs, c->d & ByteOp);
807 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808 return rc;
809 }
810
811 if (c->ad_bytes == 2) {
812 unsigned bx = c->regs[VCPU_REGS_RBX];
813 unsigned bp = c->regs[VCPU_REGS_RBP];
814 unsigned si = c->regs[VCPU_REGS_RSI];
815 unsigned di = c->regs[VCPU_REGS_RDI];
816
817 /* 16-bit ModR/M decode. */
818 switch (c->modrm_mod) {
819 case 0:
820 if (c->modrm_rm == 6)
821 c->modrm_ea += insn_fetch(u16, 2, c->eip);
822 break;
823 case 1:
824 c->modrm_ea += insn_fetch(s8, 1, c->eip);
825 break;
826 case 2:
827 c->modrm_ea += insn_fetch(u16, 2, c->eip);
828 break;
829 }
830 switch (c->modrm_rm) {
831 case 0:
832 c->modrm_ea += bx + si;
833 break;
834 case 1:
835 c->modrm_ea += bx + di;
836 break;
837 case 2:
838 c->modrm_ea += bp + si;
839 break;
840 case 3:
841 c->modrm_ea += bp + di;
842 break;
843 case 4:
844 c->modrm_ea += si;
845 break;
846 case 5:
847 c->modrm_ea += di;
848 break;
849 case 6:
850 if (c->modrm_mod != 0)
851 c->modrm_ea += bp;
852 break;
853 case 7:
854 c->modrm_ea += bx;
855 break;
856 }
857 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
858 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300859 if (!c->has_seg_override)
860 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200861 c->modrm_ea = (u16)c->modrm_ea;
862 } else {
863 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700864 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200865 sib = insn_fetch(u8, 1, c->eip);
866 index_reg |= (sib >> 3) & 7;
867 base_reg |= sib & 7;
868 scale = sib >> 6;
869
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700870 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
871 c->modrm_ea += insn_fetch(s32, 4, c->eip);
872 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700874 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200875 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700876 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
877 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700878 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700879 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200880 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 switch (c->modrm_mod) {
882 case 0:
883 if (c->modrm_rm == 5)
884 c->modrm_ea += insn_fetch(s32, 4, c->eip);
885 break;
886 case 1:
887 c->modrm_ea += insn_fetch(s8, 1, c->eip);
888 break;
889 case 2:
890 c->modrm_ea += insn_fetch(s32, 4, c->eip);
891 break;
892 }
893 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894done:
895 return rc;
896}
897
898static int decode_abs(struct x86_emulate_ctxt *ctxt,
899 struct x86_emulate_ops *ops)
900{
901 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900902 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200903
904 switch (c->ad_bytes) {
905 case 2:
906 c->modrm_ea = insn_fetch(u16, 2, c->eip);
907 break;
908 case 4:
909 c->modrm_ea = insn_fetch(u32, 4, c->eip);
910 break;
911 case 8:
912 c->modrm_ea = insn_fetch(u64, 8, c->eip);
913 break;
914 }
915done:
916 return rc;
917}
918
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200920x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200922 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200925 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
927 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Laurent Viviere4e03de2007-09-18 11:52:50 +0200929 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200930 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300931 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800932 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
934 switch (mode) {
935 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200936 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200938 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 break;
940 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800943#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200945 def_op_bytes = 4;
946 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 break;
948#endif
949 default:
950 return -1;
951 }
952
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200953 c->op_bytes = def_op_bytes;
954 c->ad_bytes = def_ad_bytes;
955
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200957 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200958 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200960 /* switch between 2/4 bytes */
961 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 break;
963 case 0x67: /* address-size override */
964 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200965 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200966 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300972 case 0x2e: /* CS override */
973 case 0x36: /* SS override */
974 case 0x3e: /* DS override */
975 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
977 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300979 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200981 case 0x40 ... 0x4f: /* REX */
982 if (mode != X86EMUL_MODE_PROT64)
983 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200984 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200985 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200987 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200989 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100990 c->rep_prefix = REPNE_PREFIX;
991 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100993 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 default:
996 goto done_prefixes;
997 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200998
999 /* Any legacy prefix after a REX prefix nullifies its effect. */
1000
Avi Kivity33615aa2007-10-31 11:15:56 +02001001 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 }
1003
1004done_prefixes:
1005
1006 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001008 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
1011 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->d = opcode_table[c->b];
1013 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 if (c->b == 0x0f) {
1016 c->twobyte = 1;
1017 c->b = insn_fetch(u8, 1, c->eip);
1018 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Avi Kivitye09d0822008-01-18 12:38:59 +02001022 if (c->d & Group) {
1023 group = c->d & GroupMask;
1024 c->modrm = insn_fetch(u8, 1, c->eip);
1025 --c->eip;
1026
1027 group = (group << 3) + ((c->modrm >> 3) & 7);
1028 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1029 c->d = group2_table[group];
1030 else
1031 c->d = group_table[group];
1032 }
1033
1034 /* Unrecognised? */
1035 if (c->d == 0) {
1036 DPRINTF("Cannot emulate %02x\n", c->b);
1037 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 }
1039
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001040 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1041 c->op_bytes = 8;
1042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001044 if (c->d & ModRM)
1045 rc = decode_modrm(ctxt, ops);
1046 else if (c->d & MemAbs)
1047 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001048 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001049 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001051 if (!c->has_seg_override)
1052 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001053
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001054 if (!(!c->twobyte && c->b == 0x8d))
1055 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001056
1057 if (c->ad_bytes != 8)
1058 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 /*
1060 * Decode and fetch the source operand: register, memory
1061 * or immediate.
1062 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001063 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 case SrcNone:
1065 break;
1066 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001067 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 break;
1069 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 goto srcmem_common;
1072 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001073 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 goto srcmem_common;
1075 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001076 c->src.bytes = (c->d & ByteOp) ? 1 :
1077 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001078 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001079 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001080 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001081 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001082 /*
1083 * For instructions with a ModR/M byte, switch to register
1084 * access if Mod = 3.
1085 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001086 if ((c->d & ModRM) && c->modrm_mod == 3) {
1087 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001088 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001089 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001090 break;
1091 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001092 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 break;
1094 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001095 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001096 c->src.type = OP_IMM;
1097 c->src.ptr = (unsigned long *)c->eip;
1098 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1099 if (c->src.bytes == 8)
1100 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001102 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001104 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 break;
1106 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001107 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 break;
1109 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001110 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 break;
1112 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001113 if ((c->d & SrcMask) == SrcImmU) {
1114 switch (c->src.bytes) {
1115 case 1:
1116 c->src.val &= 0xff;
1117 break;
1118 case 2:
1119 c->src.val &= 0xffff;
1120 break;
1121 case 4:
1122 c->src.val &= 0xffffffff;
1123 break;
1124 }
1125 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126 break;
1127 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001128 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001129 c->src.type = OP_IMM;
1130 c->src.ptr = (unsigned long *)c->eip;
1131 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001132 if ((c->d & SrcMask) == SrcImmByte)
1133 c->src.val = insn_fetch(s8, 1, c->eip);
1134 else
1135 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001137 case SrcOne:
1138 c->src.bytes = 1;
1139 c->src.val = 1;
1140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 }
1142
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001143 /*
1144 * Decode and fetch the second source operand: register, memory
1145 * or immediate.
1146 */
1147 switch (c->d & Src2Mask) {
1148 case Src2None:
1149 break;
1150 case Src2CL:
1151 c->src2.bytes = 1;
1152 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1153 break;
1154 case Src2ImmByte:
1155 c->src2.type = OP_IMM;
1156 c->src2.ptr = (unsigned long *)c->eip;
1157 c->src2.bytes = 1;
1158 c->src2.val = insn_fetch(u8, 1, c->eip);
1159 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001160 case Src2Imm16:
1161 c->src2.type = OP_IMM;
1162 c->src2.ptr = (unsigned long *)c->eip;
1163 c->src2.bytes = 2;
1164 c->src2.val = insn_fetch(u16, 2, c->eip);
1165 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001166 case Src2One:
1167 c->src2.bytes = 1;
1168 c->src2.val = 1;
1169 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001170 case Src2Mem16:
1171 c->src2.bytes = 2;
1172 c->src2.type = OP_MEM;
1173 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001174 }
1175
Avi Kivity038e51d2007-01-22 20:40:40 -08001176 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001177 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001178 case ImplicitOps:
1179 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001180 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001181 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001182 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001183 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001184 break;
1185 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001186 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001187 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001188 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001189 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001190 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001191 break;
1192 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001193 c->dst.type = OP_MEM;
1194 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001195 case DstAcc:
1196 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001197 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001198 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001199 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001200 case 1:
1201 c->dst.val = *(u8 *)c->dst.ptr;
1202 break;
1203 case 2:
1204 c->dst.val = *(u16 *)c->dst.ptr;
1205 break;
1206 case 4:
1207 c->dst.val = *(u32 *)c->dst.ptr;
1208 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001209 case 8:
1210 c->dst.val = *(u64 *)c->dst.ptr;
1211 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001212 }
1213 c->dst.orig_val = c->dst.val;
1214 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001215 }
1216
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001217 if (c->rip_relative)
1218 c->modrm_ea += c->eip;
1219
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001220done:
1221 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1222}
1223
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001224static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1225{
1226 struct decode_cache *c = &ctxt->decode;
1227
1228 c->dst.type = OP_MEM;
1229 c->dst.bytes = c->op_bytes;
1230 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001231 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001232 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233 c->regs[VCPU_REGS_RSP]);
1234}
1235
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001236static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001237 struct x86_emulate_ops *ops,
1238 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239{
1240 struct decode_cache *c = &ctxt->decode;
1241 int rc;
1242
Avi Kivity781d0ed2008-11-27 18:00:28 +02001243 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1244 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001245 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001246 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001247 return rc;
1248
Avi Kivity350f69d2009-01-05 11:12:40 +02001249 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001250 return rc;
1251}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001252
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001253static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1254 struct x86_emulate_ops *ops,
1255 void *dest, int len)
1256{
1257 int rc;
1258 unsigned long val, change_mask;
1259 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001260 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001261
1262 rc = emulate_pop(ctxt, ops, &val, len);
1263 if (rc != X86EMUL_CONTINUE)
1264 return rc;
1265
1266 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1267 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1268
1269 switch(ctxt->mode) {
1270 case X86EMUL_MODE_PROT64:
1271 case X86EMUL_MODE_PROT32:
1272 case X86EMUL_MODE_PROT16:
1273 if (cpl == 0)
1274 change_mask |= EFLG_IOPL;
1275 if (cpl <= iopl)
1276 change_mask |= EFLG_IF;
1277 break;
1278 case X86EMUL_MODE_VM86:
1279 if (iopl < 3) {
1280 kvm_inject_gp(ctxt->vcpu, 0);
1281 return X86EMUL_PROPAGATE_FAULT;
1282 }
1283 change_mask |= EFLG_IF;
1284 break;
1285 default: /* real mode */
1286 change_mask |= (EFLG_IOPL | EFLG_IF);
1287 break;
1288 }
1289
1290 *(unsigned long *)dest =
1291 (ctxt->eflags & ~change_mask) | (val & change_mask);
1292
1293 return rc;
1294}
1295
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001296static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1297{
1298 struct decode_cache *c = &ctxt->decode;
1299 struct kvm_segment segment;
1300
1301 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1302
1303 c->src.val = segment.selector;
1304 emulate_push(ctxt);
1305}
1306
1307static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1308 struct x86_emulate_ops *ops, int seg)
1309{
1310 struct decode_cache *c = &ctxt->decode;
1311 unsigned long selector;
1312 int rc;
1313
1314 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001315 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001316 return rc;
1317
Gleb Natapovc6975182010-02-18 12:15:01 +02001318 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001319 return rc;
1320}
1321
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001322static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1323{
1324 struct decode_cache *c = &ctxt->decode;
1325 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1326 int reg = VCPU_REGS_RAX;
1327
1328 while (reg <= VCPU_REGS_RDI) {
1329 (reg == VCPU_REGS_RSP) ?
1330 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1331
1332 emulate_push(ctxt);
1333 ++reg;
1334 }
1335}
1336
1337static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1338 struct x86_emulate_ops *ops)
1339{
1340 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001341 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001342 int reg = VCPU_REGS_RDI;
1343
1344 while (reg >= VCPU_REGS_RAX) {
1345 if (reg == VCPU_REGS_RSP) {
1346 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1347 c->op_bytes);
1348 --reg;
1349 }
1350
1351 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001352 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001353 break;
1354 --reg;
1355 }
1356 return rc;
1357}
1358
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001359static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1360 struct x86_emulate_ops *ops)
1361{
1362 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001363
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001364 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365}
1366
Laurent Vivier05f086f2007-09-24 11:10:55 +02001367static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001369 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370 switch (c->modrm_reg) {
1371 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001372 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373 break;
1374 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001375 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376 break;
1377 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001378 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001379 break;
1380 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382 break;
1383 case 4: /* sal/shl */
1384 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386 break;
1387 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389 break;
1390 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001391 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392 break;
1393 }
1394}
1395
1396static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398{
1399 struct decode_cache *c = &ctxt->decode;
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 Natapov2e901c42010-03-18 15:20:12 +02001603 if (ctxt->mode == X86EMUL_MODE_REAL ||
1604 ctxt->mode == X86EMUL_MODE_VM86) {
1605 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1606 return X86EMUL_PROPAGATE_FAULT;
1607 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001608
1609 setup_syscalls_segments(ctxt, &cs, &ss);
1610
1611 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1612 msr_data >>= 32;
1613 cs.selector = (u16)(msr_data & 0xfffc);
1614 ss.selector = (u16)(msr_data + 8);
1615
1616 if (is_long_mode(ctxt->vcpu)) {
1617 cs.db = 0;
1618 cs.l = 1;
1619 }
1620 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1621 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1622
1623 c->regs[VCPU_REGS_RCX] = c->eip;
1624 if (is_long_mode(ctxt->vcpu)) {
1625#ifdef CONFIG_X86_64
1626 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1627
1628 kvm_x86_ops->get_msr(ctxt->vcpu,
1629 ctxt->mode == X86EMUL_MODE_PROT64 ?
1630 MSR_LSTAR : MSR_CSTAR, &msr_data);
1631 c->eip = msr_data;
1632
1633 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1634 ctxt->eflags &= ~(msr_data | EFLG_RF);
1635#endif
1636 } else {
1637 /* legacy mode */
1638 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1639 c->eip = (u32)msr_data;
1640
1641 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1642 }
1643
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001644 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001645}
1646
Andre Przywara8c604352009-06-18 12:56:01 +02001647static int
1648emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1649{
1650 struct decode_cache *c = &ctxt->decode;
1651 struct kvm_segment cs, ss;
1652 u64 msr_data;
1653
Gleb Natapova0044752010-02-10 14:21:31 +02001654 /* inject #GP if in real mode */
1655 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001656 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001657 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001658 }
1659
1660 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1661 * Therefore, we inject an #UD.
1662 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001663 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1664 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1665 return X86EMUL_PROPAGATE_FAULT;
1666 }
Andre Przywara8c604352009-06-18 12:56:01 +02001667
1668 setup_syscalls_segments(ctxt, &cs, &ss);
1669
1670 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1671 switch (ctxt->mode) {
1672 case X86EMUL_MODE_PROT32:
1673 if ((msr_data & 0xfffc) == 0x0) {
1674 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001675 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001676 }
1677 break;
1678 case X86EMUL_MODE_PROT64:
1679 if (msr_data == 0x0) {
1680 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001681 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001682 }
1683 break;
1684 }
1685
1686 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1687 cs.selector = (u16)msr_data;
1688 cs.selector &= ~SELECTOR_RPL_MASK;
1689 ss.selector = cs.selector + 8;
1690 ss.selector &= ~SELECTOR_RPL_MASK;
1691 if (ctxt->mode == X86EMUL_MODE_PROT64
1692 || is_long_mode(ctxt->vcpu)) {
1693 cs.db = 0;
1694 cs.l = 1;
1695 }
1696
1697 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1698 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1699
1700 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1701 c->eip = msr_data;
1702
1703 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1704 c->regs[VCPU_REGS_RSP] = msr_data;
1705
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001706 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001707}
1708
Andre Przywara4668f052009-06-18 12:56:02 +02001709static int
1710emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1711{
1712 struct decode_cache *c = &ctxt->decode;
1713 struct kvm_segment cs, ss;
1714 u64 msr_data;
1715 int usermode;
1716
Gleb Natapova0044752010-02-10 14:21:31 +02001717 /* inject #GP if in real mode or Virtual 8086 mode */
1718 if (ctxt->mode == X86EMUL_MODE_REAL ||
1719 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001720 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001721 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001722 }
1723
Andre Przywara4668f052009-06-18 12:56:02 +02001724 setup_syscalls_segments(ctxt, &cs, &ss);
1725
1726 if ((c->rex_prefix & 0x8) != 0x0)
1727 usermode = X86EMUL_MODE_PROT64;
1728 else
1729 usermode = X86EMUL_MODE_PROT32;
1730
1731 cs.dpl = 3;
1732 ss.dpl = 3;
1733 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1734 switch (usermode) {
1735 case X86EMUL_MODE_PROT32:
1736 cs.selector = (u16)(msr_data + 16);
1737 if ((msr_data & 0xfffc) == 0x0) {
1738 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001739 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001740 }
1741 ss.selector = (u16)(msr_data + 24);
1742 break;
1743 case X86EMUL_MODE_PROT64:
1744 cs.selector = (u16)(msr_data + 32);
1745 if (msr_data == 0x0) {
1746 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001747 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001748 }
1749 ss.selector = cs.selector + 8;
1750 cs.db = 0;
1751 cs.l = 1;
1752 break;
1753 }
1754 cs.selector |= SELECTOR_RPL_MASK;
1755 ss.selector |= SELECTOR_RPL_MASK;
1756
1757 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1758 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1759
1760 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1761 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1762
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001763 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001764}
1765
Gleb Natapov9c537242010-03-18 15:20:05 +02001766static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1767 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001768{
1769 int iopl;
1770 if (ctxt->mode == X86EMUL_MODE_REAL)
1771 return false;
1772 if (ctxt->mode == X86EMUL_MODE_VM86)
1773 return true;
1774 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001775 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001776}
1777
1778static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1779 struct x86_emulate_ops *ops,
1780 u16 port, u16 len)
1781{
1782 struct kvm_segment tr_seg;
1783 int r;
1784 u16 io_bitmap_ptr;
1785 u8 perm, bit_idx = port & 0x7;
1786 unsigned mask = (1 << len) - 1;
1787
1788 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1789 if (tr_seg.unusable)
1790 return false;
1791 if (tr_seg.limit < 103)
1792 return false;
1793 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1794 NULL);
1795 if (r != X86EMUL_CONTINUE)
1796 return false;
1797 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1798 return false;
1799 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1800 ctxt->vcpu, NULL);
1801 if (r != X86EMUL_CONTINUE)
1802 return false;
1803 if ((perm >> bit_idx) & mask)
1804 return false;
1805 return true;
1806}
1807
1808static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1809 struct x86_emulate_ops *ops,
1810 u16 port, u16 len)
1811{
Gleb Natapov9c537242010-03-18 15:20:05 +02001812 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001813 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1814 return false;
1815 return true;
1816}
1817
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001818int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001819x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001820{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001821 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001822 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001823 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001824 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001825 unsigned int port;
1826 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001827 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001828
Glauber Costa310b5d32009-05-12 16:21:06 -04001829 ctxt->interruptibility = 0;
1830
Laurent Vivier34273182007-09-18 11:27:37 +02001831 /* Shadow copy of register state. Committed on successful emulation.
1832 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1833 * modify them.
1834 */
1835
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001836 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001837 saved_eip = c->eip;
1838
Gleb Natapov11616242010-02-11 14:43:14 +02001839 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
1840 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1841 goto done;
1842 }
1843
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001844 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02001845 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001846 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1847 goto done;
1848 }
1849
Gleb Natapove92805a2010-02-10 14:21:35 +02001850 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02001851 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02001852 kvm_inject_gp(ctxt->vcpu, 0);
1853 goto done;
1854 }
1855
Avi Kivityc7e75a32007-10-28 16:34:25 +02001856 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001857 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001858
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001859 if (c->rep_prefix && (c->d & String)) {
1860 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02001861 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001862 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001863 goto done;
1864 }
1865 /* The second termination condition only applies for REPE
1866 * and REPNE. Test if the repeat string operation prefix is
1867 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1868 * corresponding termination condition according to:
1869 * - if REPE/REPZ and ZF = 0 then done
1870 * - if REPNE/REPNZ and ZF = 1 then done
1871 */
1872 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1873 (c->b == 0xae) || (c->b == 0xaf)) {
1874 if ((c->rep_prefix == REPE_PREFIX) &&
1875 ((ctxt->eflags & EFLG_ZF) == 0)) {
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 if ((c->rep_prefix == REPNE_PREFIX) &&
1880 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001881 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001882 goto done;
1883 }
1884 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02001885 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02001886 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001887 }
1888
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001889 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001890 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001891 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001892 rc = ops->read_emulated((unsigned long)c->src.ptr,
1893 &c->src.val,
1894 c->src.bytes,
1895 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001896 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001897 goto done;
1898 c->src.orig_val = c->src.val;
1899 }
1900
Gleb Natapove35b7b92010-02-25 16:36:42 +02001901 if (c->src2.type == OP_MEM) {
1902 c->src2.ptr = (unsigned long *)(memop + c->src.bytes);
1903 c->src2.val = 0;
1904 rc = ops->read_emulated((unsigned long)c->src2.ptr,
1905 &c->src2.val,
1906 c->src2.bytes,
1907 ctxt->vcpu);
1908 if (rc != X86EMUL_CONTINUE)
1909 goto done;
1910 }
1911
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001912 if ((c->d & DstMask) == ImplicitOps)
1913 goto special_insn;
1914
1915
1916 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001917 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001918 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1919 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001920 if (c->d & BitOp) {
1921 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001922
Laurent Viviere4e03de2007-09-18 11:52:50 +02001923 c->dst.ptr = (void *)c->dst.ptr +
1924 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001925 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001926 if (!(c->d & Mov)) {
1927 /* optimisation - avoid slow emulated read */
1928 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1929 &c->dst.val,
1930 c->dst.bytes,
1931 ctxt->vcpu);
1932 if (rc != X86EMUL_CONTINUE)
1933 goto done;
1934 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001935 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001936 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001937
Avi Kivity018a98d2007-11-27 19:30:56 +02001938special_insn:
1939
Laurent Viviere4e03de2007-09-18 11:52:50 +02001940 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 goto twobyte_insn;
1942
Laurent Viviere4e03de2007-09-18 11:52:50 +02001943 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 case 0x00 ... 0x05:
1945 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001946 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001948 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001949 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1950 break;
1951 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001952 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001953 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001954 goto done;
1955 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 case 0x08 ... 0x0d:
1957 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001958 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001960 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001961 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1962 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 case 0x10 ... 0x15:
1964 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001965 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001967 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001968 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1969 break;
1970 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001971 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001972 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001973 goto done;
1974 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 case 0x18 ... 0x1d:
1976 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001977 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001979 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001980 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1981 break;
1982 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001983 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001984 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001985 goto done;
1986 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001987 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001989 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 break;
1991 case 0x28 ... 0x2d:
1992 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001993 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001994 break;
1995 case 0x30 ... 0x35:
1996 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001997 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998 break;
1999 case 0x38 ... 0x3d:
2000 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002001 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002003 case 0x40 ... 0x47: /* inc r16/r32 */
2004 emulate_1op("inc", c->dst, ctxt->eflags);
2005 break;
2006 case 0x48 ... 0x4f: /* dec r16/r32 */
2007 emulate_1op("dec", c->dst, ctxt->eflags);
2008 break;
2009 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002010 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002011 break;
2012 case 0x58 ... 0x5f: /* pop reg */
2013 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002014 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002015 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002016 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002017 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002018 case 0x60: /* pusha */
2019 emulate_pusha(ctxt);
2020 break;
2021 case 0x61: /* popa */
2022 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002023 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002024 goto done;
2025 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002027 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002029 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002031 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002032 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002033 emulate_push(ctxt);
2034 break;
2035 case 0x6c: /* insb */
2036 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2038 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2039 kvm_inject_gp(ctxt->vcpu, 0);
2040 goto done;
2041 }
2042 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002043 1,
2044 (c->d & ByteOp) ? 1 : c->op_bytes,
2045 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002046 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002047 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002048 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002049 c->regs[VCPU_REGS_RDI]),
2050 c->rep_prefix,
2051 c->regs[VCPU_REGS_RDX]) == 0) {
2052 c->eip = saved_eip;
2053 return -1;
2054 }
2055 return 0;
2056 case 0x6e: /* outsb */
2057 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2059 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2060 kvm_inject_gp(ctxt->vcpu, 0);
2061 goto done;
2062 }
Avi Kivity851ba692009-08-24 11:10:17 +03002063 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002064 0,
2065 (c->d & ByteOp) ? 1 : c->op_bytes,
2066 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002067 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002068 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002069 register_address(c,
2070 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002071 c->regs[VCPU_REGS_RSI]),
2072 c->rep_prefix,
2073 c->regs[VCPU_REGS_RDX]) == 0) {
2074 c->eip = saved_eip;
2075 return -1;
2076 }
2077 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002078 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002079 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002080 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002081 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002083 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084 case 0:
2085 goto add;
2086 case 1:
2087 goto or;
2088 case 2:
2089 goto adc;
2090 case 3:
2091 goto sbb;
2092 case 4:
2093 goto and;
2094 case 5:
2095 goto sub;
2096 case 6:
2097 goto xor;
2098 case 7:
2099 goto cmp;
2100 }
2101 break;
2102 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002103 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 break;
2105 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002106 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002108 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002110 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 break;
2112 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002113 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break;
2115 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 break; /* 64b reg: zero-extend */
2118 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002119 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 break;
2121 }
2122 /*
2123 * Write back the memory destination with implicit LOCK
2124 * prefix.
2125 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002126 c->dst.val = c->src.val;
2127 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002130 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002131 case 0x8c: { /* mov r/m, sreg */
2132 struct kvm_segment segreg;
2133
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002134 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002135 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2136 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002137 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2138 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002139 }
2140 c->dst.val = segreg.selector;
2141 break;
2142 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002143 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002144 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002145 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002146 case 0x8e: { /* mov seg, r/m16 */
2147 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002148
2149 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002150
Gleb Natapovc6975182010-02-18 12:15:01 +02002151 if (c->modrm_reg == VCPU_SREG_CS ||
2152 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002153 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2154 goto done;
2155 }
2156
Glauber Costa310b5d32009-05-12 16:21:06 -04002157 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002158 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002159
Gleb Natapovc6975182010-02-18 12:15:01 +02002160 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002161
2162 c->dst.type = OP_NONE; /* Disable writeback. */
2163 break;
2164 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002166 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002167 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002170 case 0x90: /* nop / xchg r8,rax */
2171 if (!(c->rex_prefix & 1)) { /* nop */
2172 c->dst.type = OP_NONE;
2173 break;
2174 }
2175 case 0x91 ... 0x97: /* xchg reg,rax */
2176 c->src.type = c->dst.type = OP_REG;
2177 c->src.bytes = c->dst.bytes = c->op_bytes;
2178 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2179 c->src.val = *(c->src.ptr);
2180 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002181 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002182 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002183 emulate_push(ctxt);
2184 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002185 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002186 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002187 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002188 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002189 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2190 if (rc != X86EMUL_CONTINUE)
2191 goto done;
2192 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002193 case 0xa0 ... 0xa1: /* mov */
2194 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2195 c->dst.val = c->src.val;
2196 break;
2197 case 0xa2 ... 0xa3: /* mov */
2198 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2199 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002201 c->dst.type = OP_MEM;
2202 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002203 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002204 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002205 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002206 rc = ops->read_emulated(register_address(c,
2207 seg_override_base(ctxt, c),
2208 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002209 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002210 c->dst.bytes, ctxt->vcpu);
2211 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002213 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002214 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002215 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002216 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002217 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002218 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 break;
2220 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002221 c->src.type = OP_NONE; /* Disable writeback. */
2222 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002223 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002224 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002225 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002226 rc = ops->read_emulated((unsigned long)c->src.ptr,
2227 &c->src.val,
2228 c->src.bytes,
2229 ctxt->vcpu);
2230 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002231 goto done;
2232
2233 c->dst.type = OP_NONE; /* Disable writeback. */
2234 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002235 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002236 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002237 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002238 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2239 &c->dst.val,
2240 c->dst.bytes,
2241 ctxt->vcpu);
2242 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002243 goto done;
2244
2245 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2246
2247 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2248
Harvey Harrison7a9572752008-02-19 07:40:41 -08002249 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002250 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2251 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002252 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002253 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2254 : c->dst.bytes);
2255
2256 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002258 c->dst.type = OP_MEM;
2259 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002260 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002261 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002262 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002263 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002264 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002265 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002266 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267 break;
2268 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002269 c->dst.type = OP_REG;
2270 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2271 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002272 rc = ops->read_emulated(register_address(c,
2273 seg_override_base(ctxt, c),
2274 c->regs[VCPU_REGS_RSI]),
2275 &c->dst.val,
2276 c->dst.bytes,
2277 ctxt->vcpu);
2278 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002280 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002281 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002282 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 break;
2284 case 0xae ... 0xaf: /* scas */
2285 DPRINTF("Urk! I don't handle SCAS.\n");
2286 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002287 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002288 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002289 case 0xc0 ... 0xc1:
2290 emulate_grp2(ctxt);
2291 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002292 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002293 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002294 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002295 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002296 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002297 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2298 mov:
2299 c->dst.val = c->src.val;
2300 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002301 case 0xcb: /* ret far */
2302 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002303 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002304 goto done;
2305 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002306 case 0xd0 ... 0xd1: /* Grp2 */
2307 c->src.val = 1;
2308 emulate_grp2(ctxt);
2309 break;
2310 case 0xd2 ... 0xd3: /* Grp2 */
2311 c->src.val = c->regs[VCPU_REGS_RCX];
2312 emulate_grp2(ctxt);
2313 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002314 case 0xe4: /* inb */
2315 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002316 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002317 io_dir_in = 1;
2318 goto do_io;
2319 case 0xe6: /* outb */
2320 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002321 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002322 io_dir_in = 0;
2323 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002324 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002325 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002326 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002327 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002328 emulate_push(ctxt);
2329 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002330 }
2331 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002332 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002333 case 0xea: /* jmp far */
Gleb Natapovea798492010-02-25 16:36:43 +02002334 jump_far:
Gleb Natapovc6975182010-02-18 12:15:01 +02002335 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2336 VCPU_SREG_CS))
2337 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002338
Gleb Natapov782b8772009-04-12 13:36:25 +03002339 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002340 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002341 case 0xeb:
2342 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002343 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002344 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002345 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002346 case 0xec: /* in al,dx */
2347 case 0xed: /* in (e/r)ax,dx */
2348 port = c->regs[VCPU_REGS_RDX];
2349 io_dir_in = 1;
2350 goto do_io;
2351 case 0xee: /* out al,dx */
2352 case 0xef: /* out (e/r)ax,dx */
2353 port = c->regs[VCPU_REGS_RDX];
2354 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002355 do_io:
2356 if (!emulator_io_permited(ctxt, ops, port,
2357 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2358 kvm_inject_gp(ctxt->vcpu, 0);
2359 goto done;
2360 }
2361 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002362 (c->d & ByteOp) ? 1 : c->op_bytes,
2363 port) != 0) {
2364 c->eip = saved_eip;
2365 goto cannot_emulate;
2366 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002367 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002368 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002369 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002370 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002371 case 0xf5: /* cmc */
2372 /* complement carry flag from eflags reg */
2373 ctxt->eflags ^= EFLG_CF;
2374 c->dst.type = OP_NONE; /* Disable writeback. */
2375 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002376 case 0xf6 ... 0xf7: /* Grp3 */
2377 rc = emulate_grp3(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002378 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002379 goto done;
2380 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002381 case 0xf8: /* clc */
2382 ctxt->eflags &= ~EFLG_CF;
2383 c->dst.type = OP_NONE; /* Disable writeback. */
2384 break;
2385 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002386 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002387 kvm_inject_gp(ctxt->vcpu, 0);
2388 else {
2389 ctxt->eflags &= ~X86_EFLAGS_IF;
2390 c->dst.type = OP_NONE; /* Disable writeback. */
2391 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002392 break;
2393 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002394 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002395 kvm_inject_gp(ctxt->vcpu, 0);
2396 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002397 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002398 ctxt->eflags |= X86_EFLAGS_IF;
2399 c->dst.type = OP_NONE; /* Disable writeback. */
2400 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002401 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002402 case 0xfc: /* cld */
2403 ctxt->eflags &= ~EFLG_DF;
2404 c->dst.type = OP_NONE; /* Disable writeback. */
2405 break;
2406 case 0xfd: /* std */
2407 ctxt->eflags |= EFLG_DF;
2408 c->dst.type = OP_NONE; /* Disable writeback. */
2409 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002410 case 0xfe: /* Grp4 */
2411 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002412 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002413 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002414 goto done;
2415 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002416 case 0xff: /* Grp5 */
2417 if (c->modrm_reg == 5)
2418 goto jump_far;
2419 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002421
2422writeback:
2423 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002424 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002425 goto done;
2426
2427 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002429 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002430
2431done:
2432 if (rc == X86EMUL_UNHANDLEABLE) {
2433 c->eip = saved_eip;
2434 return -1;
2435 }
2436 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437
2438twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002439 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002441 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 u16 size;
2443 unsigned long address;
2444
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002445 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002446 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002447 goto cannot_emulate;
2448
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002449 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002450 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002451 goto done;
2452
Avi Kivity33e38852008-05-21 15:34:25 +03002453 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002454 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002455 /* Disable writeback. */
2456 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002457 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002459 rc = read_descriptor(ctxt, ops, c->src.ptr,
2460 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002461 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 goto done;
2463 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002464 /* Disable writeback. */
2465 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002467 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002468 if (c->modrm_mod == 3) {
2469 switch (c->modrm_rm) {
2470 case 1:
2471 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002472 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002473 goto done;
2474 break;
2475 default:
2476 goto cannot_emulate;
2477 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002478 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002479 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002480 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002481 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002482 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002483 goto done;
2484 realmode_lidt(ctxt->vcpu, size, address);
2485 }
Avi Kivity16286d02008-04-14 14:40:50 +03002486 /* Disable writeback. */
2487 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 break;
2489 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002490 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02002491 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 break;
2493 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02002494 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
2495 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002496 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02002498 case 5: /* not defined */
2499 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2500 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002502 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002503 /* Disable writeback. */
2504 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 break;
2506 default:
2507 goto cannot_emulate;
2508 }
2509 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002510 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002511 rc = emulate_syscall(ctxt);
2512 if (rc != X86EMUL_CONTINUE)
2513 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002514 else
2515 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002516 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002517 case 0x06:
2518 emulate_clts(ctxt->vcpu);
2519 c->dst.type = OP_NONE;
2520 break;
2521 case 0x08: /* invd */
2522 case 0x09: /* wbinvd */
2523 case 0x0d: /* GrpP (prefetch) */
2524 case 0x18: /* Grp16 (prefetch/nop) */
2525 c->dst.type = OP_NONE;
2526 break;
2527 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02002528 switch (c->modrm_reg) {
2529 case 1:
2530 case 5 ... 7:
2531 case 9 ... 15:
2532 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2533 goto done;
2534 }
Gleb Natapov52a46612010-03-18 15:20:03 +02002535 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002536 c->dst.type = OP_NONE; /* no writeback */
2537 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02002539 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
2540 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
2541 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2542 goto done;
2543 }
2544 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
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 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02002548 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002549 c->dst.type = OP_NONE;
2550 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02002552 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
2553 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
2554 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2555 goto done;
2556 }
2557 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002558 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002560 case 0x30:
2561 /* wrmsr */
2562 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2563 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002564 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002565 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02002566 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002567 }
2568 rc = X86EMUL_CONTINUE;
2569 c->dst.type = OP_NONE;
2570 break;
2571 case 0x32:
2572 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09002573 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002574 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02002575 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002576 } else {
2577 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2578 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2579 }
2580 rc = X86EMUL_CONTINUE;
2581 c->dst.type = OP_NONE;
2582 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002583 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002584 rc = emulate_sysenter(ctxt);
2585 if (rc != X86EMUL_CONTINUE)
2586 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002587 else
2588 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002589 break;
2590 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002591 rc = emulate_sysexit(ctxt);
2592 if (rc != X86EMUL_CONTINUE)
2593 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002594 else
2595 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002596 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002598 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002599 if (!test_cc(c->b, ctxt->eflags))
2600 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002602 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002603 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002604 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002605 c->dst.type = OP_NONE;
2606 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002607 case 0xa0: /* push fs */
2608 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2609 break;
2610 case 0xa1: /* pop fs */
2611 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002612 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002613 goto done;
2614 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002615 case 0xa3:
2616 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002617 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002618 /* only subword offset */
2619 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002620 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002621 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002622 case 0xa4: /* shld imm8, r, r/m */
2623 case 0xa5: /* shld cl, r, r/m */
2624 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2625 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002626 case 0xa8: /* push gs */
2627 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2628 break;
2629 case 0xa9: /* pop gs */
2630 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002631 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002632 goto done;
2633 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002634 case 0xab:
2635 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002636 /* only subword offset */
2637 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002638 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002639 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002640 case 0xac: /* shrd imm8, r, r/m */
2641 case 0xad: /* shrd cl, r, r/m */
2642 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2643 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002644 case 0xae: /* clflush */
2645 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 case 0xb0 ... 0xb1: /* cmpxchg */
2647 /*
2648 * Save real source value, then compare EAX against
2649 * destination.
2650 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002651 c->src.orig_val = c->src.val;
2652 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002653 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2654 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002656 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 } else {
2658 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002659 c->dst.type = OP_REG;
2660 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 }
2662 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 case 0xb3:
2664 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002665 /* only subword offset */
2666 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002667 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002670 c->dst.bytes = c->op_bytes;
2671 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2672 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002675 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 case 0:
2677 goto bt;
2678 case 1:
2679 goto bts;
2680 case 2:
2681 goto btr;
2682 case 3:
2683 goto btc;
2684 }
2685 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002686 case 0xbb:
2687 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002688 /* only subword offset */
2689 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002690 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002693 c->dst.bytes = c->op_bytes;
2694 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2695 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002697 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002698 c->dst.bytes = c->op_bytes;
2699 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2700 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002703 rc = emulate_grp9(ctxt, ops, memop);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002704 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002705 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002706 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002707 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 }
2709 goto writeback;
2710
2711cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002712 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002713 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 return -1;
2715}