blob: 3b32270a20db3b418acb7ca3ecca5b26eb1bc1d1 [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"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020036#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038/*
39 * Opcode effective-address decode tables.
40 * Note that we only emulate instructions that have at least one memory
41 * operand (excluding implicit stack references). We assume that stack
42 * references and instruction fetches will never occur in special memory
43 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
44 * not be handled.
45 */
46
47/* Operand sizes: 8-bit operands or specified/overridden size. */
48#define ByteOp (1<<0) /* 8-bit operands. */
49/* Destination operand type. */
50#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
51#define DstReg (2<<1) /* Register operand. */
52#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define DstAcc (4<<1) /* Destination Accumulator */
Gleb Natapova682e352010-03-18 15:20:21 +020054#define DstDI (5<<1) /* Destination is in ES:(E)DI */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080056/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020057#define SrcNone (0<<4) /* No source operand. */
58#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
59#define SrcReg (1<<4) /* Register operand. */
60#define SrcMem (2<<4) /* Memory operand. */
61#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
62#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
63#define SrcImm (5<<4) /* Immediate operand. */
64#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010065#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030067#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020068#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030073#define Mov (1<<9)
74#define BitOp (1<<10)
75#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020076#define String (1<<12) /* String instruction (rep capable) */
77#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020078#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
79#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
80#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020082#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020083#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010085/* Source 2 operand type */
86#define Src2None (0<<29)
87#define Src2CL (1<<29)
88#define Src2ImmByte (2<<29)
89#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030090#define Src2Imm16 (4<<29)
Gleb Natapove35b7b92010-02-25 16:36:42 +020091#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
92 in memory and second argument is located
93 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010094#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Avi Kivity43bb19c2008-01-18 12:46:50 +020096enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020097 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020098 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020099 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200100};
101
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100102static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200104 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300106 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300107 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200109 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200111 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
112 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200114 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300116 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300117 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200119 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300122 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200124 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200126 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200128 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
130 0, 0, 0, 0,
131 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200132 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
134 0, 0, 0, 0,
135 /* 0x38 - 0x3F */
136 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
137 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200138 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
139 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700140 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200141 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700142 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200143 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300144 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200145 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
146 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300147 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200148 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
149 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700150 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200151 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
152 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700153 0, 0, 0, 0,
154 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300155 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300156 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
157 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300158 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300159 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300161 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300162 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
163 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200165 Group | Group1_80, Group | Group1_81,
166 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200168 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0x88 - 0x8F */
170 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
171 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200172 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200173 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300174 /* 0x90 - 0x97 */
175 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
176 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300177 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300178 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200180 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
181 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200182 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
183 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 /* 0xA8 - 0xAF */
Gleb Natapova682e352010-03-18 15:20:21 +0200185 0, 0, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
186 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
187 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300188 /* 0xB0 - 0xB7 */
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
191 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
192 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
193 /* 0xB8 - 0xBF */
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
195 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
196 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
197 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300199 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200200 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300201 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300203 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300204 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xD0 - 0xD7 */
206 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
207 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
208 0, 0, 0, 0,
209 /* 0xD8 - 0xDF */
210 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300211 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300212 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300213 ByteOp | SrcImmUByte, SrcImmUByte,
214 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300215 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300216 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300217 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300218 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
219 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xF0 - 0xF7 */
221 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200222 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700224 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300225 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226};
227
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100228static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200230 0, Group | GroupDual | Group7, 0, 0,
231 0, ImplicitOps, ImplicitOps | Priv, 0,
232 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
233 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0x10 - 0x1F */
235 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
236 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200237 ModRM | ImplicitOps | Priv, ModRM | Priv,
238 ModRM | ImplicitOps | Priv, ModRM | Priv,
239 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 0, 0, 0, 0, 0, 0, 0, 0,
241 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200242 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
243 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200244 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 /* 0x40 - 0x47 */
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 /* 0x48 - 0x4F */
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
253 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
254 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
255 /* 0x50 - 0x5F */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 /* 0x60 - 0x6F */
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259 /* 0x70 - 0x7F */
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
261 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300262 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
263 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264 /* 0x90 - 0x9F */
265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300267 ImplicitOps | Stack, ImplicitOps | Stack,
268 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100269 DstMem | SrcReg | Src2ImmByte | ModRM,
270 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300272 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200273 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100274 DstMem | SrcReg | Src2ImmByte | ModRM,
275 DstMem | SrcReg | Src2CL | ModRM,
276 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200278 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
279 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
281 DstReg | SrcMem16 | ModRM | Mov,
282 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200283 0, 0,
284 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
286 DstReg | SrcMem16 | ModRM | Mov,
287 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200288 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
289 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800290 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 /* 0xD0 - 0xDF */
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293 /* 0xE0 - 0xEF */
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
295 /* 0xF0 - 0xFF */
296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
297};
298
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100299static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200300 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200301 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 | Lock,
306 ByteOp | DstMem | SrcImm | ModRM | Lock,
307 ByteOp | DstMem | SrcImm | ModRM | Lock,
308 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200309 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM | Lock,
315 DstMem | SrcImm | ModRM | Lock,
316 DstMem | SrcImm | ModRM | Lock,
317 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200318 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200319 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 | Lock,
324 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
325 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
326 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200327 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM | Lock,
333 DstMem | SrcImmByte | ModRM | Lock,
334 DstMem | SrcImmByte | ModRM | Lock,
335 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200336 [Group1A*8] =
337 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200338 [Group3_Byte*8] =
339 ByteOp | SrcImm | DstMem | ModRM, 0,
340 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
341 0, 0, 0, 0,
342 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400343 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300344 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200345 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200346 [Group4*8] =
347 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
348 0, 0, 0, 0, 0, 0,
349 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300350 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
351 SrcMem | ModRM | Stack, 0,
Gleb Natapovea798492010-02-25 16:36:43 +0200352 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
353 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200354 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200355 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300356 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200357 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200358 [Group8*8] =
359 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200360 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
361 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200362 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200363 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200364};
365
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100366static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200367 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200368 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300369 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200370 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200371 [Group9*8] =
372 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200373};
374
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200376#define EFLG_ID (1<<21)
377#define EFLG_VIP (1<<20)
378#define EFLG_VIF (1<<19)
379#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200380#define EFLG_VM (1<<17)
381#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200382#define EFLG_IOPL (3<<12)
383#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384#define EFLG_OF (1<<11)
385#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200386#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200387#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388#define EFLG_SF (1<<7)
389#define EFLG_ZF (1<<6)
390#define EFLG_AF (1<<4)
391#define EFLG_PF (1<<2)
392#define EFLG_CF (1<<0)
393
394/*
395 * Instruction emulation:
396 * Most instructions are emulated directly via a fragment of inline assembly
397 * code. This allows us to save/restore EFLAGS and thus very easily pick up
398 * any modified flags.
399 */
400
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800401#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402#define _LO32 "k" /* force 32-bit operand */
403#define _STK "%%rsp" /* stack pointer */
404#elif defined(__i386__)
405#define _LO32 "" /* force 32-bit operand */
406#define _STK "%%esp" /* stack pointer */
407#endif
408
409/*
410 * These EFLAGS bits are restored from saved value during emulation, and
411 * any changes are written back to the saved value after emulation.
412 */
413#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
414
415/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200416#define _PRE_EFLAGS(_sav, _msk, _tmp) \
417 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
418 "movl %"_sav",%"_LO32 _tmp"; " \
419 "push %"_tmp"; " \
420 "push %"_tmp"; " \
421 "movl %"_msk",%"_LO32 _tmp"; " \
422 "andl %"_LO32 _tmp",("_STK"); " \
423 "pushf; " \
424 "notl %"_LO32 _tmp"; " \
425 "andl %"_LO32 _tmp",("_STK"); " \
426 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
427 "pop %"_tmp"; " \
428 "orl %"_LO32 _tmp",("_STK"); " \
429 "popf; " \
430 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431
432/* After executing instruction: write-back necessary bits in EFLAGS. */
433#define _POST_EFLAGS(_sav, _msk, _tmp) \
434 /* _sav |= EFLAGS & _msk; */ \
435 "pushf; " \
436 "pop %"_tmp"; " \
437 "andl %"_msk",%"_LO32 _tmp"; " \
438 "orl %"_LO32 _tmp",%"_sav"; "
439
Avi Kivitydda96d82008-11-26 15:14:10 +0200440#ifdef CONFIG_X86_64
441#define ON64(x) x
442#else
443#define ON64(x)
444#endif
445
Avi Kivity6b7ad612008-11-26 15:30:45 +0200446#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
447 do { \
448 __asm__ __volatile__ ( \
449 _PRE_EFLAGS("0", "4", "2") \
450 _op _suffix " %"_x"3,%1; " \
451 _POST_EFLAGS("0", "4", "2") \
452 : "=m" (_eflags), "=m" ((_dst).val), \
453 "=&r" (_tmp) \
454 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200455 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200456
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458/* Raw emulation: instruction has two explicit operands. */
459#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200460 do { \
461 unsigned long _tmp; \
462 \
463 switch ((_dst).bytes) { \
464 case 2: \
465 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
466 break; \
467 case 4: \
468 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
469 break; \
470 case 8: \
471 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
472 break; \
473 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 } while (0)
475
476#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
477 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200478 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400479 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200481 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 break; \
483 default: \
484 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
485 _wx, _wy, _lx, _ly, _qx, _qy); \
486 break; \
487 } \
488 } while (0)
489
490/* Source operand is byte-sized and may be restricted to just %cl. */
491#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
492 __emulate_2op(_op, _src, _dst, _eflags, \
493 "b", "c", "b", "c", "b", "c", "b", "c")
494
495/* Source operand is byte, word, long or quad sized. */
496#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
497 __emulate_2op(_op, _src, _dst, _eflags, \
498 "b", "q", "w", "r", _LO32, "r", "", "r")
499
500/* Source operand is word, long or quad sized. */
501#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
502 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
503 "w", "r", _LO32, "r", "", "r")
504
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100505/* Instruction has three operands and one operand is stored in ECX register */
506#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
507 do { \
508 unsigned long _tmp; \
509 _type _clv = (_cl).val; \
510 _type _srcv = (_src).val; \
511 _type _dstv = (_dst).val; \
512 \
513 __asm__ __volatile__ ( \
514 _PRE_EFLAGS("0", "5", "2") \
515 _op _suffix " %4,%1 \n" \
516 _POST_EFLAGS("0", "5", "2") \
517 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
518 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
519 ); \
520 \
521 (_cl).val = (unsigned long) _clv; \
522 (_src).val = (unsigned long) _srcv; \
523 (_dst).val = (unsigned long) _dstv; \
524 } while (0)
525
526#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
527 do { \
528 switch ((_dst).bytes) { \
529 case 2: \
530 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
531 "w", unsigned short); \
532 break; \
533 case 4: \
534 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
535 "l", unsigned int); \
536 break; \
537 case 8: \
538 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
539 "q", unsigned long)); \
540 break; \
541 } \
542 } while (0)
543
Avi Kivitydda96d82008-11-26 15:14:10 +0200544#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 do { \
546 unsigned long _tmp; \
547 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200548 __asm__ __volatile__ ( \
549 _PRE_EFLAGS("0", "3", "2") \
550 _op _suffix " %1; " \
551 _POST_EFLAGS("0", "3", "2") \
552 : "=m" (_eflags), "+m" ((_dst).val), \
553 "=&r" (_tmp) \
554 : "i" (EFLAGS_MASK)); \
555 } while (0)
556
557/* Instruction has only one explicit operand (no source operand). */
558#define emulate_1op(_op, _dst, _eflags) \
559 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400560 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200561 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
562 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
563 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
564 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 } \
566 } while (0)
567
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568/* Fetch next part of the instruction being emulated. */
569#define insn_fetch(_type, _size, _eip) \
570({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200571 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200572 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 goto done; \
574 (_eip) += (_size); \
575 (_type)_x; \
576})
577
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800578static inline unsigned long ad_mask(struct decode_cache *c)
579{
580 return (1UL << (c->ad_bytes << 3)) - 1;
581}
582
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800584static inline unsigned long
585address_mask(struct decode_cache *c, unsigned long reg)
586{
587 if (c->ad_bytes == sizeof(unsigned long))
588 return reg;
589 else
590 return reg & ad_mask(c);
591}
592
593static inline unsigned long
594register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
595{
596 return base + address_mask(c, reg);
597}
598
Harvey Harrison7a9572752008-02-19 07:40:41 -0800599static inline void
600register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
601{
602 if (c->ad_bytes == sizeof(unsigned long))
603 *reg += inc;
604 else
605 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
606}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607
Harvey Harrison7a9572752008-02-19 07:40:41 -0800608static inline void jmp_rel(struct decode_cache *c, int rel)
609{
610 register_address_increment(c, &c->eip, rel);
611}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300612
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300613static void set_seg_override(struct decode_cache *c, int seg)
614{
615 c->has_seg_override = true;
616 c->seg_override = seg;
617}
618
619static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
620{
621 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
622 return 0;
623
624 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
625}
626
627static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
628 struct decode_cache *c)
629{
630 if (!c->has_seg_override)
631 return 0;
632
633 return seg_base(ctxt, c->seg_override);
634}
635
636static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
637{
638 return seg_base(ctxt, VCPU_SREG_ES);
639}
640
641static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
642{
643 return seg_base(ctxt, VCPU_SREG_SS);
644}
645
Avi Kivity62266862007-11-20 13:15:52 +0200646static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
647 struct x86_emulate_ops *ops,
648 unsigned long linear, u8 *dest)
649{
650 struct fetch_cache *fc = &ctxt->decode.fetch;
651 int rc;
652 int size;
653
654 if (linear < fc->start || linear >= fc->end) {
655 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200656 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900657 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200658 return rc;
659 fc->start = linear;
660 fc->end = linear + size;
661 }
662 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200664}
665
666static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
667 struct x86_emulate_ops *ops,
668 unsigned long eip, void *dest, unsigned size)
669{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900670 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200671
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200673 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200674 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200675 eip += ctxt->cs_base;
676 while (size--) {
677 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200679 return rc;
680 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900681 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200682}
683
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000684/*
685 * Given the 'reg' portion of a ModRM byte, and a register block, return a
686 * pointer into the block that addresses the relevant register.
687 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
688 */
689static void *decode_register(u8 modrm_reg, unsigned long *regs,
690 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691{
692 void *p;
693
694 p = &regs[modrm_reg];
695 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
696 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
697 return p;
698}
699
700static int read_descriptor(struct x86_emulate_ctxt *ctxt,
701 struct x86_emulate_ops *ops,
702 void *ptr,
703 u16 *size, unsigned long *address, int op_bytes)
704{
705 int rc;
706
707 if (op_bytes == 2)
708 op_bytes = 3;
709 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300710 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200711 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900712 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300714 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200715 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 return rc;
717}
718
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300719static int test_cc(unsigned int condition, unsigned int flags)
720{
721 int rc = 0;
722
723 switch ((condition & 15) >> 1) {
724 case 0: /* o */
725 rc |= (flags & EFLG_OF);
726 break;
727 case 1: /* b/c/nae */
728 rc |= (flags & EFLG_CF);
729 break;
730 case 2: /* z/e */
731 rc |= (flags & EFLG_ZF);
732 break;
733 case 3: /* be/na */
734 rc |= (flags & (EFLG_CF|EFLG_ZF));
735 break;
736 case 4: /* s */
737 rc |= (flags & EFLG_SF);
738 break;
739 case 5: /* p/pe */
740 rc |= (flags & EFLG_PF);
741 break;
742 case 7: /* le/ng */
743 rc |= (flags & EFLG_ZF);
744 /* fall through */
745 case 6: /* l/nge */
746 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
747 break;
748 }
749
750 /* Odd condition identifiers (lsb == 1) have inverted sense. */
751 return (!!rc ^ (condition & 1));
752}
753
Avi Kivity3c118e22007-10-31 10:27:04 +0200754static void decode_register_operand(struct operand *op,
755 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200756 int inhibit_bytereg)
757{
Avi Kivity33615aa2007-10-31 11:15:56 +0200758 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200759 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200760
761 if (!(c->d & ModRM))
762 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->type = OP_REG;
764 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200765 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200766 op->val = *(u8 *)op->ptr;
767 op->bytes = 1;
768 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200769 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200770 op->bytes = c->op_bytes;
771 switch (op->bytes) {
772 case 2:
773 op->val = *(u16 *)op->ptr;
774 break;
775 case 4:
776 op->val = *(u32 *)op->ptr;
777 break;
778 case 8:
779 op->val = *(u64 *) op->ptr;
780 break;
781 }
782 }
783 op->orig_val = op->val;
784}
785
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786static int decode_modrm(struct x86_emulate_ctxt *ctxt,
787 struct x86_emulate_ops *ops)
788{
789 struct decode_cache *c = &ctxt->decode;
790 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700791 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900792 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200793
794 if (c->rex_prefix) {
795 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
796 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
797 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
798 }
799
800 c->modrm = insn_fetch(u8, 1, c->eip);
801 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
802 c->modrm_reg |= (c->modrm & 0x38) >> 3;
803 c->modrm_rm |= (c->modrm & 0x07);
804 c->modrm_ea = 0;
805 c->use_modrm_ea = 1;
806
807 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300808 c->modrm_ptr = decode_register(c->modrm_rm,
809 c->regs, c->d & ByteOp);
810 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200811 return rc;
812 }
813
814 if (c->ad_bytes == 2) {
815 unsigned bx = c->regs[VCPU_REGS_RBX];
816 unsigned bp = c->regs[VCPU_REGS_RBP];
817 unsigned si = c->regs[VCPU_REGS_RSI];
818 unsigned di = c->regs[VCPU_REGS_RDI];
819
820 /* 16-bit ModR/M decode. */
821 switch (c->modrm_mod) {
822 case 0:
823 if (c->modrm_rm == 6)
824 c->modrm_ea += insn_fetch(u16, 2, c->eip);
825 break;
826 case 1:
827 c->modrm_ea += insn_fetch(s8, 1, c->eip);
828 break;
829 case 2:
830 c->modrm_ea += insn_fetch(u16, 2, c->eip);
831 break;
832 }
833 switch (c->modrm_rm) {
834 case 0:
835 c->modrm_ea += bx + si;
836 break;
837 case 1:
838 c->modrm_ea += bx + di;
839 break;
840 case 2:
841 c->modrm_ea += bp + si;
842 break;
843 case 3:
844 c->modrm_ea += bp + di;
845 break;
846 case 4:
847 c->modrm_ea += si;
848 break;
849 case 5:
850 c->modrm_ea += di;
851 break;
852 case 6:
853 if (c->modrm_mod != 0)
854 c->modrm_ea += bp;
855 break;
856 case 7:
857 c->modrm_ea += bx;
858 break;
859 }
860 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
861 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300862 if (!c->has_seg_override)
863 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200864 c->modrm_ea = (u16)c->modrm_ea;
865 } else {
866 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700867 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200868 sib = insn_fetch(u8, 1, c->eip);
869 index_reg |= (sib >> 3) & 7;
870 base_reg |= sib & 7;
871 scale = sib >> 6;
872
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700873 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
874 c->modrm_ea += insn_fetch(s32, 4, c->eip);
875 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700877 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200878 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700879 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
880 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700881 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700882 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200883 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200884 switch (c->modrm_mod) {
885 case 0:
886 if (c->modrm_rm == 5)
887 c->modrm_ea += insn_fetch(s32, 4, c->eip);
888 break;
889 case 1:
890 c->modrm_ea += insn_fetch(s8, 1, c->eip);
891 break;
892 case 2:
893 c->modrm_ea += insn_fetch(s32, 4, c->eip);
894 break;
895 }
896 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897done:
898 return rc;
899}
900
901static int decode_abs(struct x86_emulate_ctxt *ctxt,
902 struct x86_emulate_ops *ops)
903{
904 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900905 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906
907 switch (c->ad_bytes) {
908 case 2:
909 c->modrm_ea = insn_fetch(u16, 2, c->eip);
910 break;
911 case 4:
912 c->modrm_ea = insn_fetch(u32, 4, c->eip);
913 break;
914 case 8:
915 c->modrm_ea = insn_fetch(u64, 8, c->eip);
916 break;
917 }
918done:
919 return rc;
920}
921
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200923x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200925 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900926 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200928 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931
Laurent Viviere4e03de2007-09-18 11:52:50 +0200932 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200933 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300934 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800935 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
937 switch (mode) {
938 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200939 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
943 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200944 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800946#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200948 def_op_bytes = 4;
949 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 break;
951#endif
952 default:
953 return -1;
954 }
955
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200956 c->op_bytes = def_op_bytes;
957 c->ad_bytes = def_ad_bytes;
958
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200960 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200961 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200963 /* switch between 2/4 bytes */
964 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 break;
966 case 0x67: /* address-size override */
967 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200971 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200972 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300975 case 0x2e: /* CS override */
976 case 0x36: /* SS override */
977 case 0x3e: /* DS override */
978 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 break;
980 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300982 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200984 case 0x40 ... 0x4f: /* REX */
985 if (mode != X86EMUL_MODE_PROT64)
986 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200987 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200988 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200990 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200992 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100993 c->rep_prefix = REPNE_PREFIX;
994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100996 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 default:
999 goto done_prefixes;
1000 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001001
1002 /* Any legacy prefix after a REX prefix nullifies its effect. */
1003
Avi Kivity33615aa2007-10-31 11:15:56 +02001004 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 }
1006
1007done_prefixes:
1008
1009 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001010 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001011 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013
1014 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->d = opcode_table[c->b];
1016 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 if (c->b == 0x0f) {
1019 c->twobyte = 1;
1020 c->b = insn_fetch(u8, 1, c->eip);
1021 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Avi Kivitye09d0822008-01-18 12:38:59 +02001025 if (c->d & Group) {
1026 group = c->d & GroupMask;
1027 c->modrm = insn_fetch(u8, 1, c->eip);
1028 --c->eip;
1029
1030 group = (group << 3) + ((c->modrm >> 3) & 7);
1031 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1032 c->d = group2_table[group];
1033 else
1034 c->d = group_table[group];
1035 }
1036
1037 /* Unrecognised? */
1038 if (c->d == 0) {
1039 DPRINTF("Cannot emulate %02x\n", c->b);
1040 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 }
1042
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001043 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1044 c->op_bytes = 8;
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001047 if (c->d & ModRM)
1048 rc = decode_modrm(ctxt, ops);
1049 else if (c->d & MemAbs)
1050 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001051 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001052 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001054 if (!c->has_seg_override)
1055 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001056
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001057 if (!(!c->twobyte && c->b == 0x8d))
1058 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001059
1060 if (c->ad_bytes != 8)
1061 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001062
1063 if (c->rip_relative)
1064 c->modrm_ea += c->eip;
1065
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 /*
1067 * Decode and fetch the source operand: register, memory
1068 * or immediate.
1069 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 case SrcNone:
1072 break;
1073 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001074 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 break;
1076 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 goto srcmem_common;
1079 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001080 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 goto srcmem_common;
1082 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001083 c->src.bytes = (c->d & ByteOp) ? 1 :
1084 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001085 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001086 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001087 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001088 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001089 /*
1090 * For instructions with a ModR/M byte, switch to register
1091 * access if Mod = 3.
1092 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 if ((c->d & ModRM) && c->modrm_mod == 3) {
1094 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001095 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001096 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001097 break;
1098 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001099 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001100 c->src.ptr = (unsigned long *)c->modrm_ea;
1101 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 break;
1103 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001104 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001105 c->src.type = OP_IMM;
1106 c->src.ptr = (unsigned long *)c->eip;
1107 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1108 if (c->src.bytes == 8)
1109 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 break;
1115 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 break;
1118 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 break;
1121 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001122 if ((c->d & SrcMask) == SrcImmU) {
1123 switch (c->src.bytes) {
1124 case 1:
1125 c->src.val &= 0xff;
1126 break;
1127 case 2:
1128 c->src.val &= 0xffff;
1129 break;
1130 case 4:
1131 c->src.val &= 0xffffffff;
1132 break;
1133 }
1134 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 break;
1136 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001137 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001138 c->src.type = OP_IMM;
1139 c->src.ptr = (unsigned long *)c->eip;
1140 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001141 if ((c->d & SrcMask) == SrcImmByte)
1142 c->src.val = insn_fetch(s8, 1, c->eip);
1143 else
1144 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001146 case SrcOne:
1147 c->src.bytes = 1;
1148 c->src.val = 1;
1149 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001150 case SrcSI:
1151 c->src.type = OP_MEM;
1152 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1153 c->src.ptr = (unsigned long *)
1154 register_address(c, seg_override_base(ctxt, c),
1155 c->regs[VCPU_REGS_RSI]);
1156 c->src.val = 0;
1157 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 }
1159
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001160 /*
1161 * Decode and fetch the second source operand: register, memory
1162 * or immediate.
1163 */
1164 switch (c->d & Src2Mask) {
1165 case Src2None:
1166 break;
1167 case Src2CL:
1168 c->src2.bytes = 1;
1169 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1170 break;
1171 case Src2ImmByte:
1172 c->src2.type = OP_IMM;
1173 c->src2.ptr = (unsigned long *)c->eip;
1174 c->src2.bytes = 1;
1175 c->src2.val = insn_fetch(u8, 1, c->eip);
1176 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001177 case Src2Imm16:
1178 c->src2.type = OP_IMM;
1179 c->src2.ptr = (unsigned long *)c->eip;
1180 c->src2.bytes = 2;
1181 c->src2.val = insn_fetch(u16, 2, c->eip);
1182 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001183 case Src2One:
1184 c->src2.bytes = 1;
1185 c->src2.val = 1;
1186 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001187 case Src2Mem16:
Gleb Natapove35b7b92010-02-25 16:36:42 +02001188 c->src2.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001189 c->src2.bytes = 2;
1190 c->src2.ptr = (unsigned long *)(c->modrm_ea + c->src.bytes);
1191 c->src2.val = 0;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001192 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001193 }
1194
Avi Kivity038e51d2007-01-22 20:40:40 -08001195 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001196 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001197 case ImplicitOps:
1198 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001199 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001200 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001201 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001202 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001203 break;
1204 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001205 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001206 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001207 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001208 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001209 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001210 break;
1211 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001212 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001213 c->dst.ptr = (unsigned long *)c->modrm_ea;
1214 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1215 c->dst.val = 0;
1216 if (c->d & BitOp) {
1217 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1218
1219 c->dst.ptr = (void *)c->dst.ptr +
1220 (c->src.val & mask) / 8;
1221 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001222 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001223 case DstAcc:
1224 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001225 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001226 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001227 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001228 case 1:
1229 c->dst.val = *(u8 *)c->dst.ptr;
1230 break;
1231 case 2:
1232 c->dst.val = *(u16 *)c->dst.ptr;
1233 break;
1234 case 4:
1235 c->dst.val = *(u32 *)c->dst.ptr;
1236 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001237 case 8:
1238 c->dst.val = *(u64 *)c->dst.ptr;
1239 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001240 }
1241 c->dst.orig_val = c->dst.val;
1242 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001243 case DstDI:
1244 c->dst.type = OP_MEM;
1245 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1246 c->dst.ptr = (unsigned long *)
1247 register_address(c, es_base(ctxt),
1248 c->regs[VCPU_REGS_RDI]);
1249 c->dst.val = 0;
1250 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001251 }
1252
1253done:
1254 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1255}
1256
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001257static u32 desc_limit_scaled(struct desc_struct *desc)
1258{
1259 u32 limit = get_desc_limit(desc);
1260
1261 return desc->g ? (limit << 12) | 0xfff : limit;
1262}
1263
1264static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1265 struct x86_emulate_ops *ops,
1266 u16 selector, struct desc_ptr *dt)
1267{
1268 if (selector & 1 << 2) {
1269 struct desc_struct desc;
1270 memset (dt, 0, sizeof *dt);
1271 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1272 return;
1273
1274 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1275 dt->address = get_desc_base(&desc);
1276 } else
1277 ops->get_gdt(dt, ctxt->vcpu);
1278}
1279
1280/* allowed just for 8 bytes segments */
1281static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1282 struct x86_emulate_ops *ops,
1283 u16 selector, struct desc_struct *desc)
1284{
1285 struct desc_ptr dt;
1286 u16 index = selector >> 3;
1287 int ret;
1288 u32 err;
1289 ulong addr;
1290
1291 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1292
1293 if (dt.size < index * 8 + 7) {
1294 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1295 return X86EMUL_PROPAGATE_FAULT;
1296 }
1297 addr = dt.address + index * 8;
1298 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1299 if (ret == X86EMUL_PROPAGATE_FAULT)
1300 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1301
1302 return ret;
1303}
1304
1305/* allowed just for 8 bytes segments */
1306static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1307 struct x86_emulate_ops *ops,
1308 u16 selector, struct desc_struct *desc)
1309{
1310 struct desc_ptr dt;
1311 u16 index = selector >> 3;
1312 u32 err;
1313 ulong addr;
1314 int ret;
1315
1316 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1317
1318 if (dt.size < index * 8 + 7) {
1319 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1320 return X86EMUL_PROPAGATE_FAULT;
1321 }
1322
1323 addr = dt.address + index * 8;
1324 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1325 if (ret == X86EMUL_PROPAGATE_FAULT)
1326 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1327
1328 return ret;
1329}
1330
1331static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1332 struct x86_emulate_ops *ops,
1333 u16 selector, int seg)
1334{
1335 struct desc_struct seg_desc;
1336 u8 dpl, rpl, cpl;
1337 unsigned err_vec = GP_VECTOR;
1338 u32 err_code = 0;
1339 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1340 int ret;
1341
1342 memset(&seg_desc, 0, sizeof seg_desc);
1343
1344 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1345 || ctxt->mode == X86EMUL_MODE_REAL) {
1346 /* set real mode segment descriptor */
1347 set_desc_base(&seg_desc, selector << 4);
1348 set_desc_limit(&seg_desc, 0xffff);
1349 seg_desc.type = 3;
1350 seg_desc.p = 1;
1351 seg_desc.s = 1;
1352 goto load;
1353 }
1354
1355 /* NULL selector is not valid for TR, CS and SS */
1356 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1357 && null_selector)
1358 goto exception;
1359
1360 /* TR should be in GDT only */
1361 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1362 goto exception;
1363
1364 if (null_selector) /* for NULL selector skip all following checks */
1365 goto load;
1366
1367 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1368 if (ret != X86EMUL_CONTINUE)
1369 return ret;
1370
1371 err_code = selector & 0xfffc;
1372 err_vec = GP_VECTOR;
1373
1374 /* can't load system descriptor into segment selecor */
1375 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1376 goto exception;
1377
1378 if (!seg_desc.p) {
1379 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1380 goto exception;
1381 }
1382
1383 rpl = selector & 3;
1384 dpl = seg_desc.dpl;
1385 cpl = ops->cpl(ctxt->vcpu);
1386
1387 switch (seg) {
1388 case VCPU_SREG_SS:
1389 /*
1390 * segment is not a writable data segment or segment
1391 * selector's RPL != CPL or segment selector's RPL != CPL
1392 */
1393 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1394 goto exception;
1395 break;
1396 case VCPU_SREG_CS:
1397 if (!(seg_desc.type & 8))
1398 goto exception;
1399
1400 if (seg_desc.type & 4) {
1401 /* conforming */
1402 if (dpl > cpl)
1403 goto exception;
1404 } else {
1405 /* nonconforming */
1406 if (rpl > cpl || dpl != cpl)
1407 goto exception;
1408 }
1409 /* CS(RPL) <- CPL */
1410 selector = (selector & 0xfffc) | cpl;
1411 break;
1412 case VCPU_SREG_TR:
1413 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1414 goto exception;
1415 break;
1416 case VCPU_SREG_LDTR:
1417 if (seg_desc.s || seg_desc.type != 2)
1418 goto exception;
1419 break;
1420 default: /* DS, ES, FS, or GS */
1421 /*
1422 * segment is not a data or readable code segment or
1423 * ((segment is a data or nonconforming code segment)
1424 * and (both RPL and CPL > DPL))
1425 */
1426 if ((seg_desc.type & 0xa) == 0x8 ||
1427 (((seg_desc.type & 0xc) != 0xc) &&
1428 (rpl > dpl && cpl > dpl)))
1429 goto exception;
1430 break;
1431 }
1432
1433 if (seg_desc.s) {
1434 /* mark segment as accessed */
1435 seg_desc.type |= 1;
1436 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1437 if (ret != X86EMUL_CONTINUE)
1438 return ret;
1439 }
1440load:
1441 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1442 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1443 return X86EMUL_CONTINUE;
1444exception:
1445 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1446 return X86EMUL_PROPAGATE_FAULT;
1447}
1448
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001449static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1450{
1451 struct decode_cache *c = &ctxt->decode;
1452
1453 c->dst.type = OP_MEM;
1454 c->dst.bytes = c->op_bytes;
1455 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001456 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001457 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001458 c->regs[VCPU_REGS_RSP]);
1459}
1460
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001461static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001462 struct x86_emulate_ops *ops,
1463 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001464{
1465 struct decode_cache *c = &ctxt->decode;
1466 int rc;
1467
Avi Kivity781d0ed2008-11-27 18:00:28 +02001468 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1469 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001470 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001471 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001472 return rc;
1473
Avi Kivity350f69d2009-01-05 11:12:40 +02001474 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001475 return rc;
1476}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001477
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001478static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1479 struct x86_emulate_ops *ops,
1480 void *dest, int len)
1481{
1482 int rc;
1483 unsigned long val, change_mask;
1484 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001485 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001486
1487 rc = emulate_pop(ctxt, ops, &val, len);
1488 if (rc != X86EMUL_CONTINUE)
1489 return rc;
1490
1491 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1492 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1493
1494 switch(ctxt->mode) {
1495 case X86EMUL_MODE_PROT64:
1496 case X86EMUL_MODE_PROT32:
1497 case X86EMUL_MODE_PROT16:
1498 if (cpl == 0)
1499 change_mask |= EFLG_IOPL;
1500 if (cpl <= iopl)
1501 change_mask |= EFLG_IF;
1502 break;
1503 case X86EMUL_MODE_VM86:
1504 if (iopl < 3) {
1505 kvm_inject_gp(ctxt->vcpu, 0);
1506 return X86EMUL_PROPAGATE_FAULT;
1507 }
1508 change_mask |= EFLG_IF;
1509 break;
1510 default: /* real mode */
1511 change_mask |= (EFLG_IOPL | EFLG_IF);
1512 break;
1513 }
1514
1515 *(unsigned long *)dest =
1516 (ctxt->eflags & ~change_mask) | (val & change_mask);
1517
1518 return rc;
1519}
1520
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001521static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1522{
1523 struct decode_cache *c = &ctxt->decode;
1524 struct kvm_segment segment;
1525
1526 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1527
1528 c->src.val = segment.selector;
1529 emulate_push(ctxt);
1530}
1531
1532static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1533 struct x86_emulate_ops *ops, int seg)
1534{
1535 struct decode_cache *c = &ctxt->decode;
1536 unsigned long selector;
1537 int rc;
1538
1539 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001540 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001541 return rc;
1542
Gleb Natapov2e873022010-03-18 15:20:18 +02001543 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001544 return rc;
1545}
1546
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001547static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1548{
1549 struct decode_cache *c = &ctxt->decode;
1550 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1551 int reg = VCPU_REGS_RAX;
1552
1553 while (reg <= VCPU_REGS_RDI) {
1554 (reg == VCPU_REGS_RSP) ?
1555 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1556
1557 emulate_push(ctxt);
1558 ++reg;
1559 }
1560}
1561
1562static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1563 struct x86_emulate_ops *ops)
1564{
1565 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001566 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001567 int reg = VCPU_REGS_RDI;
1568
1569 while (reg >= VCPU_REGS_RAX) {
1570 if (reg == VCPU_REGS_RSP) {
1571 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1572 c->op_bytes);
1573 --reg;
1574 }
1575
1576 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001577 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001578 break;
1579 --reg;
1580 }
1581 return rc;
1582}
1583
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001584static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1585 struct x86_emulate_ops *ops)
1586{
1587 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001588
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001589 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001590}
1591
Laurent Vivier05f086f2007-09-24 11:10:55 +02001592static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001593{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001594 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001595 switch (c->modrm_reg) {
1596 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001597 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001598 break;
1599 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001600 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001601 break;
1602 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001603 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001604 break;
1605 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001606 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001607 break;
1608 case 4: /* sal/shl */
1609 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001610 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001611 break;
1612 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001613 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614 break;
1615 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001616 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001617 break;
1618 }
1619}
1620
1621static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001622 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001623{
1624 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001625
1626 switch (c->modrm_reg) {
1627 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001628 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001629 break;
1630 case 2: /* not */
1631 c->dst.val = ~c->dst.val;
1632 break;
1633 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001634 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635 break;
1636 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001637 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001639 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640}
1641
1642static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001643 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001644{
1645 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646
1647 switch (c->modrm_reg) {
1648 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001649 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 break;
1651 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001652 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001654 case 2: /* call near abs */ {
1655 long int old_eip;
1656 old_eip = c->eip;
1657 c->eip = c->src.val;
1658 c->src.val = old_eip;
1659 emulate_push(ctxt);
1660 break;
1661 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001662 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001663 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001664 break;
1665 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001666 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001668 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001669 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001670}
1671
1672static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001673 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001674{
1675 struct decode_cache *c = &ctxt->decode;
1676 u64 old, new;
1677 int rc;
1678
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001679 rc = ops->read_emulated(c->modrm_ea, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001680 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 return rc;
1682
1683 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1684 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1685
1686 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1687 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001688 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689
1690 } else {
1691 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1692 (u32) c->regs[VCPU_REGS_RBX];
1693
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001694 rc = ops->cmpxchg_emulated(c->modrm_ea, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001695 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001697 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001698 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001699 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700}
1701
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001702static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1703 struct x86_emulate_ops *ops)
1704{
1705 struct decode_cache *c = &ctxt->decode;
1706 int rc;
1707 unsigned long cs;
1708
1709 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001710 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001711 return rc;
1712 if (c->op_bytes == 4)
1713 c->eip = (u32)c->eip;
1714 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001715 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001716 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001717 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001718 return rc;
1719}
1720
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721static inline int writeback(struct x86_emulate_ctxt *ctxt,
1722 struct x86_emulate_ops *ops)
1723{
1724 int rc;
1725 struct decode_cache *c = &ctxt->decode;
1726
1727 switch (c->dst.type) {
1728 case OP_REG:
1729 /* The 4-byte case *is* correct:
1730 * in 64-bit mode we zero-extend.
1731 */
1732 switch (c->dst.bytes) {
1733 case 1:
1734 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1735 break;
1736 case 2:
1737 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1738 break;
1739 case 4:
1740 *c->dst.ptr = (u32)c->dst.val;
1741 break; /* 64b: zero-ext */
1742 case 8:
1743 *c->dst.ptr = c->dst.val;
1744 break;
1745 }
1746 break;
1747 case OP_MEM:
1748 if (c->lock_prefix)
1749 rc = ops->cmpxchg_emulated(
1750 (unsigned long)c->dst.ptr,
1751 &c->dst.orig_val,
1752 &c->dst.val,
1753 c->dst.bytes,
1754 ctxt->vcpu);
1755 else
1756 rc = ops->write_emulated(
1757 (unsigned long)c->dst.ptr,
1758 &c->dst.val,
1759 c->dst.bytes,
1760 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001761 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001762 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001763 break;
1764 case OP_NONE:
1765 /* no writeback */
1766 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767 default:
1768 break;
1769 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001770 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771}
1772
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301773static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001774{
1775 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1776 /*
1777 * an sti; sti; sequence only disable interrupts for the first
1778 * instruction. So, if the last instruction, be it emulated or
1779 * not, left the system with the INT_STI flag enabled, it
1780 * means that the last instruction is an sti. We should not
1781 * leave the flag on in this case. The same goes for mov ss
1782 */
1783 if (!(int_shadow & mask))
1784 ctxt->interruptibility = mask;
1785}
1786
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001787static inline void
1788setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1789 struct kvm_segment *cs, struct kvm_segment *ss)
1790{
1791 memset(cs, 0, sizeof(struct kvm_segment));
1792 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1793 memset(ss, 0, sizeof(struct kvm_segment));
1794
1795 cs->l = 0; /* will be adjusted later */
1796 cs->base = 0; /* flat segment */
1797 cs->g = 1; /* 4kb granularity */
1798 cs->limit = 0xffffffff; /* 4GB limit */
1799 cs->type = 0x0b; /* Read, Execute, Accessed */
1800 cs->s = 1;
1801 cs->dpl = 0; /* will be adjusted later */
1802 cs->present = 1;
1803 cs->db = 1;
1804
1805 ss->unusable = 0;
1806 ss->base = 0; /* flat segment */
1807 ss->limit = 0xffffffff; /* 4GB limit */
1808 ss->g = 1; /* 4kb granularity */
1809 ss->s = 1;
1810 ss->type = 0x03; /* Read/Write, Accessed */
1811 ss->db = 1; /* 32bit stack segment */
1812 ss->dpl = 0;
1813 ss->present = 1;
1814}
1815
1816static int
1817emulate_syscall(struct x86_emulate_ctxt *ctxt)
1818{
1819 struct decode_cache *c = &ctxt->decode;
1820 struct kvm_segment cs, ss;
1821 u64 msr_data;
1822
1823 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001824 if (ctxt->mode == X86EMUL_MODE_REAL ||
1825 ctxt->mode == X86EMUL_MODE_VM86) {
1826 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1827 return X86EMUL_PROPAGATE_FAULT;
1828 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001829
1830 setup_syscalls_segments(ctxt, &cs, &ss);
1831
1832 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1833 msr_data >>= 32;
1834 cs.selector = (u16)(msr_data & 0xfffc);
1835 ss.selector = (u16)(msr_data + 8);
1836
1837 if (is_long_mode(ctxt->vcpu)) {
1838 cs.db = 0;
1839 cs.l = 1;
1840 }
1841 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1842 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1843
1844 c->regs[VCPU_REGS_RCX] = c->eip;
1845 if (is_long_mode(ctxt->vcpu)) {
1846#ifdef CONFIG_X86_64
1847 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1848
1849 kvm_x86_ops->get_msr(ctxt->vcpu,
1850 ctxt->mode == X86EMUL_MODE_PROT64 ?
1851 MSR_LSTAR : MSR_CSTAR, &msr_data);
1852 c->eip = msr_data;
1853
1854 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1855 ctxt->eflags &= ~(msr_data | EFLG_RF);
1856#endif
1857 } else {
1858 /* legacy mode */
1859 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1860 c->eip = (u32)msr_data;
1861
1862 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1863 }
1864
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001865 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866}
1867
Andre Przywara8c604352009-06-18 12:56:01 +02001868static int
1869emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1870{
1871 struct decode_cache *c = &ctxt->decode;
1872 struct kvm_segment cs, ss;
1873 u64 msr_data;
1874
Gleb Natapova0044752010-02-10 14:21:31 +02001875 /* inject #GP if in real mode */
1876 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001877 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001878 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001879 }
1880
1881 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1882 * Therefore, we inject an #UD.
1883 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001884 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1885 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1886 return X86EMUL_PROPAGATE_FAULT;
1887 }
Andre Przywara8c604352009-06-18 12:56:01 +02001888
1889 setup_syscalls_segments(ctxt, &cs, &ss);
1890
1891 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1892 switch (ctxt->mode) {
1893 case X86EMUL_MODE_PROT32:
1894 if ((msr_data & 0xfffc) == 0x0) {
1895 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001896 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001897 }
1898 break;
1899 case X86EMUL_MODE_PROT64:
1900 if (msr_data == 0x0) {
1901 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001902 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001903 }
1904 break;
1905 }
1906
1907 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1908 cs.selector = (u16)msr_data;
1909 cs.selector &= ~SELECTOR_RPL_MASK;
1910 ss.selector = cs.selector + 8;
1911 ss.selector &= ~SELECTOR_RPL_MASK;
1912 if (ctxt->mode == X86EMUL_MODE_PROT64
1913 || is_long_mode(ctxt->vcpu)) {
1914 cs.db = 0;
1915 cs.l = 1;
1916 }
1917
1918 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1919 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1920
1921 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1922 c->eip = msr_data;
1923
1924 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1925 c->regs[VCPU_REGS_RSP] = msr_data;
1926
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001927 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001928}
1929
Andre Przywara4668f052009-06-18 12:56:02 +02001930static int
1931emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1932{
1933 struct decode_cache *c = &ctxt->decode;
1934 struct kvm_segment cs, ss;
1935 u64 msr_data;
1936 int usermode;
1937
Gleb Natapova0044752010-02-10 14:21:31 +02001938 /* inject #GP if in real mode or Virtual 8086 mode */
1939 if (ctxt->mode == X86EMUL_MODE_REAL ||
1940 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001941 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001942 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001943 }
1944
Andre Przywara4668f052009-06-18 12:56:02 +02001945 setup_syscalls_segments(ctxt, &cs, &ss);
1946
1947 if ((c->rex_prefix & 0x8) != 0x0)
1948 usermode = X86EMUL_MODE_PROT64;
1949 else
1950 usermode = X86EMUL_MODE_PROT32;
1951
1952 cs.dpl = 3;
1953 ss.dpl = 3;
1954 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1955 switch (usermode) {
1956 case X86EMUL_MODE_PROT32:
1957 cs.selector = (u16)(msr_data + 16);
1958 if ((msr_data & 0xfffc) == 0x0) {
1959 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001960 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001961 }
1962 ss.selector = (u16)(msr_data + 24);
1963 break;
1964 case X86EMUL_MODE_PROT64:
1965 cs.selector = (u16)(msr_data + 32);
1966 if (msr_data == 0x0) {
1967 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001968 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001969 }
1970 ss.selector = cs.selector + 8;
1971 cs.db = 0;
1972 cs.l = 1;
1973 break;
1974 }
1975 cs.selector |= SELECTOR_RPL_MASK;
1976 ss.selector |= SELECTOR_RPL_MASK;
1977
1978 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1979 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1980
1981 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1982 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1983
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001984 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001985}
1986
Gleb Natapov9c537242010-03-18 15:20:05 +02001987static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1988 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001989{
1990 int iopl;
1991 if (ctxt->mode == X86EMUL_MODE_REAL)
1992 return false;
1993 if (ctxt->mode == X86EMUL_MODE_VM86)
1994 return true;
1995 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001996 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001997}
1998
1999static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2000 struct x86_emulate_ops *ops,
2001 u16 port, u16 len)
2002{
2003 struct kvm_segment tr_seg;
2004 int r;
2005 u16 io_bitmap_ptr;
2006 u8 perm, bit_idx = port & 0x7;
2007 unsigned mask = (1 << len) - 1;
2008
2009 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
2010 if (tr_seg.unusable)
2011 return false;
2012 if (tr_seg.limit < 103)
2013 return false;
2014 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
2015 NULL);
2016 if (r != X86EMUL_CONTINUE)
2017 return false;
2018 if (io_bitmap_ptr + port/8 > tr_seg.limit)
2019 return false;
2020 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
2021 ctxt->vcpu, NULL);
2022 if (r != X86EMUL_CONTINUE)
2023 return false;
2024 if ((perm >> bit_idx) & mask)
2025 return false;
2026 return true;
2027}
2028
2029static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2030 struct x86_emulate_ops *ops,
2031 u16 port, u16 len)
2032{
Gleb Natapov9c537242010-03-18 15:20:05 +02002033 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2035 return false;
2036 return true;
2037}
2038
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002039static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt,
2040 struct x86_emulate_ops *ops,
2041 int seg)
2042{
2043 struct desc_struct desc;
2044 if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu))
2045 return get_desc_base(&desc);
2046 else
2047 return ~0;
2048}
2049
2050static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2051 struct x86_emulate_ops *ops,
2052 struct tss_segment_16 *tss)
2053{
2054 struct decode_cache *c = &ctxt->decode;
2055
2056 tss->ip = c->eip;
2057 tss->flag = ctxt->eflags;
2058 tss->ax = c->regs[VCPU_REGS_RAX];
2059 tss->cx = c->regs[VCPU_REGS_RCX];
2060 tss->dx = c->regs[VCPU_REGS_RDX];
2061 tss->bx = c->regs[VCPU_REGS_RBX];
2062 tss->sp = c->regs[VCPU_REGS_RSP];
2063 tss->bp = c->regs[VCPU_REGS_RBP];
2064 tss->si = c->regs[VCPU_REGS_RSI];
2065 tss->di = c->regs[VCPU_REGS_RDI];
2066
2067 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2068 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2069 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2070 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2071 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2072}
2073
2074static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2075 struct x86_emulate_ops *ops,
2076 struct tss_segment_16 *tss)
2077{
2078 struct decode_cache *c = &ctxt->decode;
2079 int ret;
2080
2081 c->eip = tss->ip;
2082 ctxt->eflags = tss->flag | 2;
2083 c->regs[VCPU_REGS_RAX] = tss->ax;
2084 c->regs[VCPU_REGS_RCX] = tss->cx;
2085 c->regs[VCPU_REGS_RDX] = tss->dx;
2086 c->regs[VCPU_REGS_RBX] = tss->bx;
2087 c->regs[VCPU_REGS_RSP] = tss->sp;
2088 c->regs[VCPU_REGS_RBP] = tss->bp;
2089 c->regs[VCPU_REGS_RSI] = tss->si;
2090 c->regs[VCPU_REGS_RDI] = tss->di;
2091
2092 /*
2093 * SDM says that segment selectors are loaded before segment
2094 * descriptors
2095 */
2096 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2097 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2098 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2099 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2100 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2101
2102 /*
2103 * Now load segment descriptors. If fault happenes at this stage
2104 * it is handled in a context of new task
2105 */
2106 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2107 if (ret != X86EMUL_CONTINUE)
2108 return ret;
2109 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2110 if (ret != X86EMUL_CONTINUE)
2111 return ret;
2112 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2113 if (ret != X86EMUL_CONTINUE)
2114 return ret;
2115 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2116 if (ret != X86EMUL_CONTINUE)
2117 return ret;
2118 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2119 if (ret != X86EMUL_CONTINUE)
2120 return ret;
2121
2122 return X86EMUL_CONTINUE;
2123}
2124
2125static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2126 struct x86_emulate_ops *ops,
2127 u16 tss_selector, u16 old_tss_sel,
2128 ulong old_tss_base, struct desc_struct *new_desc)
2129{
2130 struct tss_segment_16 tss_seg;
2131 int ret;
2132 u32 err, new_tss_base = get_desc_base(new_desc);
2133
2134 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2135 &err);
2136 if (ret == X86EMUL_PROPAGATE_FAULT) {
2137 /* FIXME: need to provide precise fault address */
2138 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2139 return ret;
2140 }
2141
2142 save_state_to_tss16(ctxt, ops, &tss_seg);
2143
2144 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2145 &err);
2146 if (ret == X86EMUL_PROPAGATE_FAULT) {
2147 /* FIXME: need to provide precise fault address */
2148 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2149 return ret;
2150 }
2151
2152 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2153 &err);
2154 if (ret == X86EMUL_PROPAGATE_FAULT) {
2155 /* FIXME: need to provide precise fault address */
2156 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2157 return ret;
2158 }
2159
2160 if (old_tss_sel != 0xffff) {
2161 tss_seg.prev_task_link = old_tss_sel;
2162
2163 ret = ops->write_std(new_tss_base,
2164 &tss_seg.prev_task_link,
2165 sizeof tss_seg.prev_task_link,
2166 ctxt->vcpu, &err);
2167 if (ret == X86EMUL_PROPAGATE_FAULT) {
2168 /* FIXME: need to provide precise fault address */
2169 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2170 return ret;
2171 }
2172 }
2173
2174 return load_state_from_tss16(ctxt, ops, &tss_seg);
2175}
2176
2177static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2178 struct x86_emulate_ops *ops,
2179 struct tss_segment_32 *tss)
2180{
2181 struct decode_cache *c = &ctxt->decode;
2182
2183 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2184 tss->eip = c->eip;
2185 tss->eflags = ctxt->eflags;
2186 tss->eax = c->regs[VCPU_REGS_RAX];
2187 tss->ecx = c->regs[VCPU_REGS_RCX];
2188 tss->edx = c->regs[VCPU_REGS_RDX];
2189 tss->ebx = c->regs[VCPU_REGS_RBX];
2190 tss->esp = c->regs[VCPU_REGS_RSP];
2191 tss->ebp = c->regs[VCPU_REGS_RBP];
2192 tss->esi = c->regs[VCPU_REGS_RSI];
2193 tss->edi = c->regs[VCPU_REGS_RDI];
2194
2195 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2196 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2197 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2198 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2199 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2200 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2201 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2202}
2203
2204static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2205 struct x86_emulate_ops *ops,
2206 struct tss_segment_32 *tss)
2207{
2208 struct decode_cache *c = &ctxt->decode;
2209 int ret;
2210
2211 ops->set_cr(3, tss->cr3, ctxt->vcpu);
2212 c->eip = tss->eip;
2213 ctxt->eflags = tss->eflags | 2;
2214 c->regs[VCPU_REGS_RAX] = tss->eax;
2215 c->regs[VCPU_REGS_RCX] = tss->ecx;
2216 c->regs[VCPU_REGS_RDX] = tss->edx;
2217 c->regs[VCPU_REGS_RBX] = tss->ebx;
2218 c->regs[VCPU_REGS_RSP] = tss->esp;
2219 c->regs[VCPU_REGS_RBP] = tss->ebp;
2220 c->regs[VCPU_REGS_RSI] = tss->esi;
2221 c->regs[VCPU_REGS_RDI] = tss->edi;
2222
2223 /*
2224 * SDM says that segment selectors are loaded before segment
2225 * descriptors
2226 */
2227 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2228 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2229 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2230 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2231 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2232 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2233 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2234
2235 /*
2236 * Now load segment descriptors. If fault happenes at this stage
2237 * it is handled in a context of new task
2238 */
2239 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2240 if (ret != X86EMUL_CONTINUE)
2241 return ret;
2242 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2243 if (ret != X86EMUL_CONTINUE)
2244 return ret;
2245 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2246 if (ret != X86EMUL_CONTINUE)
2247 return ret;
2248 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2249 if (ret != X86EMUL_CONTINUE)
2250 return ret;
2251 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2252 if (ret != X86EMUL_CONTINUE)
2253 return ret;
2254 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2255 if (ret != X86EMUL_CONTINUE)
2256 return ret;
2257 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2258 if (ret != X86EMUL_CONTINUE)
2259 return ret;
2260
2261 return X86EMUL_CONTINUE;
2262}
2263
2264static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2265 struct x86_emulate_ops *ops,
2266 u16 tss_selector, u16 old_tss_sel,
2267 ulong old_tss_base, struct desc_struct *new_desc)
2268{
2269 struct tss_segment_32 tss_seg;
2270 int ret;
2271 u32 err, new_tss_base = get_desc_base(new_desc);
2272
2273 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2274 &err);
2275 if (ret == X86EMUL_PROPAGATE_FAULT) {
2276 /* FIXME: need to provide precise fault address */
2277 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2278 return ret;
2279 }
2280
2281 save_state_to_tss32(ctxt, ops, &tss_seg);
2282
2283 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2284 &err);
2285 if (ret == X86EMUL_PROPAGATE_FAULT) {
2286 /* FIXME: need to provide precise fault address */
2287 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2288 return ret;
2289 }
2290
2291 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2292 &err);
2293 if (ret == X86EMUL_PROPAGATE_FAULT) {
2294 /* FIXME: need to provide precise fault address */
2295 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2296 return ret;
2297 }
2298
2299 if (old_tss_sel != 0xffff) {
2300 tss_seg.prev_task_link = old_tss_sel;
2301
2302 ret = ops->write_std(new_tss_base,
2303 &tss_seg.prev_task_link,
2304 sizeof tss_seg.prev_task_link,
2305 ctxt->vcpu, &err);
2306 if (ret == X86EMUL_PROPAGATE_FAULT) {
2307 /* FIXME: need to provide precise fault address */
2308 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2309 return ret;
2310 }
2311 }
2312
2313 return load_state_from_tss32(ctxt, ops, &tss_seg);
2314}
2315
2316static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2317 struct x86_emulate_ops *ops,
2318 u16 tss_selector, int reason)
2319{
2320 struct desc_struct curr_tss_desc, next_tss_desc;
2321 int ret;
2322 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2323 ulong old_tss_base =
2324 get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002325 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326
2327 /* FIXME: old_tss_base == ~0 ? */
2328
2329 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2330 if (ret != X86EMUL_CONTINUE)
2331 return ret;
2332 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2333 if (ret != X86EMUL_CONTINUE)
2334 return ret;
2335
2336 /* FIXME: check that next_tss_desc is tss */
2337
2338 if (reason != TASK_SWITCH_IRET) {
2339 if ((tss_selector & 3) > next_tss_desc.dpl ||
2340 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2341 kvm_inject_gp(ctxt->vcpu, 0);
2342 return X86EMUL_PROPAGATE_FAULT;
2343 }
2344 }
2345
Gleb Natapovceffb452010-03-18 15:20:19 +02002346 desc_limit = desc_limit_scaled(&next_tss_desc);
2347 if (!next_tss_desc.p ||
2348 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2349 desc_limit < 0x2b)) {
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2351 tss_selector & 0xfffc);
2352 return X86EMUL_PROPAGATE_FAULT;
2353 }
2354
2355 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2356 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2357 write_segment_descriptor(ctxt, ops, old_tss_sel,
2358 &curr_tss_desc);
2359 }
2360
2361 if (reason == TASK_SWITCH_IRET)
2362 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2363
2364 /* set back link to prev task only if NT bit is set in eflags
2365 note that old_tss_sel is not used afetr this point */
2366 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2367 old_tss_sel = 0xffff;
2368
2369 if (next_tss_desc.type & 8)
2370 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2371 old_tss_base, &next_tss_desc);
2372 else
2373 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2374 old_tss_base, &next_tss_desc);
2375
2376 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2377 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2378
2379 if (reason != TASK_SWITCH_IRET) {
2380 next_tss_desc.type |= (1 << 1); /* set busy flag */
2381 write_segment_descriptor(ctxt, ops, tss_selector,
2382 &next_tss_desc);
2383 }
2384
2385 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2386 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2387 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2388
2389 return ret;
2390}
2391
2392int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2393 struct x86_emulate_ops *ops,
2394 u16 tss_selector, int reason)
2395{
2396 struct decode_cache *c = &ctxt->decode;
2397 int rc;
2398
2399 memset(c, 0, sizeof(struct decode_cache));
2400 c->eip = ctxt->eip;
2401 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
2402
2403 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason);
2404
2405 if (rc == X86EMUL_CONTINUE) {
2406 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
2407 kvm_rip_write(ctxt->vcpu, c->eip);
2408 }
2409
2410 return rc;
2411}
2412
Gleb Natapova682e352010-03-18 15:20:21 +02002413static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
2414 int reg, unsigned long **ptr)
2415{
2416 struct decode_cache *c = &ctxt->decode;
2417 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2418
2419 register_address_increment(c, &c->regs[reg], df * c->src.bytes);
2420 *ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
2421}
2422
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002423int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002424x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002425{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002426 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02002427 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002428 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002429 unsigned int port;
2430 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002431 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002432
Glauber Costa310b5d32009-05-12 16:21:06 -04002433 ctxt->interruptibility = 0;
2434
Laurent Vivier34273182007-09-18 11:27:37 +02002435 /* Shadow copy of register state. Committed on successful emulation.
2436 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
2437 * modify them.
2438 */
2439
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02002441 saved_eip = c->eip;
2442
Gleb Natapov11616242010-02-11 14:43:14 +02002443 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2444 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2445 goto done;
2446 }
2447
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002448 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002449 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002450 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2451 goto done;
2452 }
2453
Gleb Natapove92805a2010-02-10 14:21:35 +02002454 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002455 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002456 kvm_inject_gp(ctxt->vcpu, 0);
2457 goto done;
2458 }
2459
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002460 if (c->rep_prefix && (c->d & String)) {
2461 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002462 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002463 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002464 goto done;
2465 }
2466 /* The second termination condition only applies for REPE
2467 * and REPNE. Test if the repeat string operation prefix is
2468 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2469 * corresponding termination condition according to:
2470 * - if REPE/REPZ and ZF = 0 then done
2471 * - if REPNE/REPNZ and ZF = 1 then done
2472 */
2473 if ((c->b == 0xa6) || (c->b == 0xa7) ||
2474 (c->b == 0xae) || (c->b == 0xaf)) {
2475 if ((c->rep_prefix == REPE_PREFIX) &&
2476 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002477 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002478 goto done;
2479 }
2480 if ((c->rep_prefix == REPNE_PREFIX) &&
2481 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002482 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002483 goto done;
2484 }
2485 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02002486 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02002487 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002488 }
2489
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002490 if (c->src.type == OP_MEM) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002491 rc = ops->read_emulated((unsigned long)c->src.ptr,
2492 &c->src.val,
2493 c->src.bytes,
2494 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002495 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002496 goto done;
2497 c->src.orig_val = c->src.val;
2498 }
2499
Gleb Natapove35b7b92010-02-25 16:36:42 +02002500 if (c->src2.type == OP_MEM) {
Gleb Natapove35b7b92010-02-25 16:36:42 +02002501 rc = ops->read_emulated((unsigned long)c->src2.ptr,
2502 &c->src2.val,
2503 c->src2.bytes,
2504 ctxt->vcpu);
2505 if (rc != X86EMUL_CONTINUE)
2506 goto done;
2507 }
2508
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002509 if ((c->d & DstMask) == ImplicitOps)
2510 goto special_insn;
2511
2512
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002513 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2514 /* optimisation - avoid slow emulated read if Mov */
2515 rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val,
2516 c->dst.bytes, ctxt->vcpu);
2517 if (rc != X86EMUL_CONTINUE)
2518 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002519 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002520 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002521
Avi Kivity018a98d2007-11-27 19:30:56 +02002522special_insn:
2523
Laurent Viviere4e03de2007-09-18 11:52:50 +02002524 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 goto twobyte_insn;
2526
Laurent Viviere4e03de2007-09-18 11:52:50 +02002527 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 case 0x00 ... 0x05:
2529 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002530 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002532 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002533 emulate_push_sreg(ctxt, VCPU_SREG_ES);
2534 break;
2535 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002536 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002537 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002538 goto done;
2539 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 case 0x08 ... 0x0d:
2541 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002542 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002544 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002545 emulate_push_sreg(ctxt, VCPU_SREG_CS);
2546 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 case 0x10 ... 0x15:
2548 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002549 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002551 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002552 emulate_push_sreg(ctxt, VCPU_SREG_SS);
2553 break;
2554 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002555 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002556 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002557 goto done;
2558 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 case 0x18 ... 0x1d:
2560 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002561 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002563 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002564 emulate_push_sreg(ctxt, VCPU_SREG_DS);
2565 break;
2566 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002567 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002568 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002569 goto done;
2570 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002571 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002573 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 break;
2575 case 0x28 ... 0x2d:
2576 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002577 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 break;
2579 case 0x30 ... 0x35:
2580 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002581 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 break;
2583 case 0x38 ... 0x3d:
2584 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002585 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002587 case 0x40 ... 0x47: /* inc r16/r32 */
2588 emulate_1op("inc", c->dst, ctxt->eflags);
2589 break;
2590 case 0x48 ... 0x4f: /* dec r16/r32 */
2591 emulate_1op("dec", c->dst, ctxt->eflags);
2592 break;
2593 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002594 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002595 break;
2596 case 0x58 ... 0x5f: /* pop reg */
2597 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002598 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002599 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002600 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002601 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002602 case 0x60: /* pusha */
2603 emulate_pusha(ctxt);
2604 break;
2605 case 0x61: /* popa */
2606 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002607 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002608 goto done;
2609 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002611 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002613 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002615 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002616 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002617 emulate_push(ctxt);
2618 break;
2619 case 0x6c: /* insb */
2620 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002621 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2622 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2623 kvm_inject_gp(ctxt->vcpu, 0);
2624 goto done;
2625 }
2626 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002627 1,
2628 (c->d & ByteOp) ? 1 : c->op_bytes,
2629 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002630 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002631 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002632 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002633 c->regs[VCPU_REGS_RDI]),
2634 c->rep_prefix,
2635 c->regs[VCPU_REGS_RDX]) == 0) {
2636 c->eip = saved_eip;
2637 return -1;
2638 }
2639 return 0;
2640 case 0x6e: /* outsb */
2641 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002642 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2643 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2644 kvm_inject_gp(ctxt->vcpu, 0);
2645 goto done;
2646 }
Avi Kivity851ba692009-08-24 11:10:17 +03002647 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002648 0,
2649 (c->d & ByteOp) ? 1 : c->op_bytes,
2650 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002651 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002652 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002653 register_address(c,
2654 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002655 c->regs[VCPU_REGS_RSI]),
2656 c->rep_prefix,
2657 c->regs[VCPU_REGS_RDX]) == 0) {
2658 c->eip = saved_eip;
2659 return -1;
2660 }
2661 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002662 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002663 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002664 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002665 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002667 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 case 0:
2669 goto add;
2670 case 1:
2671 goto or;
2672 case 2:
2673 goto adc;
2674 case 3:
2675 goto sbb;
2676 case 4:
2677 goto and;
2678 case 5:
2679 goto sub;
2680 case 6:
2681 goto xor;
2682 case 7:
2683 goto cmp;
2684 }
2685 break;
2686 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002687 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 break;
2689 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002690 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002692 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002694 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 break;
2696 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002697 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 break;
2699 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002700 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 break; /* 64b reg: zero-extend */
2702 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002703 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 break;
2705 }
2706 /*
2707 * Write back the memory destination with implicit LOCK
2708 * prefix.
2709 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002710 c->dst.val = c->src.val;
2711 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002714 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002715 case 0x8c: { /* mov r/m, sreg */
2716 struct kvm_segment segreg;
2717
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002718 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002719 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2720 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002721 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2722 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002723 }
2724 c->dst.val = segreg.selector;
2725 break;
2726 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002727 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002728 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002729 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002730 case 0x8e: { /* mov seg, r/m16 */
2731 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002732
2733 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002734
Gleb Natapovc6975182010-02-18 12:15:01 +02002735 if (c->modrm_reg == VCPU_SREG_CS ||
2736 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002737 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2738 goto done;
2739 }
2740
Glauber Costa310b5d32009-05-12 16:21:06 -04002741 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002742 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002743
Gleb Natapov2e873022010-03-18 15:20:18 +02002744 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002745
2746 c->dst.type = OP_NONE; /* Disable writeback. */
2747 break;
2748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002750 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002751 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002754 case 0x90: /* nop / xchg r8,rax */
2755 if (!(c->rex_prefix & 1)) { /* nop */
2756 c->dst.type = OP_NONE;
2757 break;
2758 }
2759 case 0x91 ... 0x97: /* xchg reg,rax */
2760 c->src.type = c->dst.type = OP_REG;
2761 c->src.bytes = c->dst.bytes = c->op_bytes;
2762 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2763 c->src.val = *(c->src.ptr);
2764 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002765 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002766 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002767 emulate_push(ctxt);
2768 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002769 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002770 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002771 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002772 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002773 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2774 if (rc != X86EMUL_CONTINUE)
2775 goto done;
2776 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002777 case 0xa0 ... 0xa1: /* mov */
2778 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2779 c->dst.val = c->src.val;
2780 break;
2781 case 0xa2 ... 0xa3: /* mov */
2782 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2783 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002785 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002787 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002788 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002789 goto cmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002791 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 break;
2793 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002794 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 case 0xae ... 0xaf: /* scas */
2796 DPRINTF("Urk! I don't handle SCAS.\n");
2797 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002798 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002799 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002800 case 0xc0 ... 0xc1:
2801 emulate_grp2(ctxt);
2802 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002803 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002804 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002805 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002806 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002807 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002808 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2809 mov:
2810 c->dst.val = c->src.val;
2811 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002812 case 0xcb: /* ret far */
2813 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002814 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002815 goto done;
2816 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002817 case 0xd0 ... 0xd1: /* Grp2 */
2818 c->src.val = 1;
2819 emulate_grp2(ctxt);
2820 break;
2821 case 0xd2 ... 0xd3: /* Grp2 */
2822 c->src.val = c->regs[VCPU_REGS_RCX];
2823 emulate_grp2(ctxt);
2824 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002825 case 0xe4: /* inb */
2826 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002827 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002828 io_dir_in = 1;
2829 goto do_io;
2830 case 0xe6: /* outb */
2831 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002832 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002833 io_dir_in = 0;
2834 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002835 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002836 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002837 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002838 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002839 emulate_push(ctxt);
2840 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002841 }
2842 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002843 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002844 case 0xea: /* jmp far */
Gleb Natapovea798492010-02-25 16:36:43 +02002845 jump_far:
Gleb Natapov2e873022010-03-18 15:20:18 +02002846 if (load_segment_descriptor(ctxt, ops, c->src2.val,
2847 VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002848 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002849
Gleb Natapov782b8772009-04-12 13:36:25 +03002850 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002851 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002852 case 0xeb:
2853 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002854 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002855 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002856 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002857 case 0xec: /* in al,dx */
2858 case 0xed: /* in (e/r)ax,dx */
2859 port = c->regs[VCPU_REGS_RDX];
2860 io_dir_in = 1;
2861 goto do_io;
2862 case 0xee: /* out al,dx */
2863 case 0xef: /* out (e/r)ax,dx */
2864 port = c->regs[VCPU_REGS_RDX];
2865 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002866 do_io:
2867 if (!emulator_io_permited(ctxt, ops, port,
2868 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2869 kvm_inject_gp(ctxt->vcpu, 0);
2870 goto done;
2871 }
2872 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002873 (c->d & ByteOp) ? 1 : c->op_bytes,
2874 port) != 0) {
2875 c->eip = saved_eip;
2876 goto cannot_emulate;
2877 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002878 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002879 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002880 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002881 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002882 case 0xf5: /* cmc */
2883 /* complement carry flag from eflags reg */
2884 ctxt->eflags ^= EFLG_CF;
2885 c->dst.type = OP_NONE; /* Disable writeback. */
2886 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002887 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002888 if (!emulate_grp3(ctxt, ops))
2889 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002890 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002891 case 0xf8: /* clc */
2892 ctxt->eflags &= ~EFLG_CF;
2893 c->dst.type = OP_NONE; /* Disable writeback. */
2894 break;
2895 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002896 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002897 kvm_inject_gp(ctxt->vcpu, 0);
2898 else {
2899 ctxt->eflags &= ~X86_EFLAGS_IF;
2900 c->dst.type = OP_NONE; /* Disable writeback. */
2901 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002902 break;
2903 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002904 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002905 kvm_inject_gp(ctxt->vcpu, 0);
2906 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002907 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002908 ctxt->eflags |= X86_EFLAGS_IF;
2909 c->dst.type = OP_NONE; /* Disable writeback. */
2910 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002911 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002912 case 0xfc: /* cld */
2913 ctxt->eflags &= ~EFLG_DF;
2914 c->dst.type = OP_NONE; /* Disable writeback. */
2915 break;
2916 case 0xfd: /* std */
2917 ctxt->eflags |= EFLG_DF;
2918 c->dst.type = OP_NONE; /* Disable writeback. */
2919 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002920 case 0xfe: /* Grp4 */
2921 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002922 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002923 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002924 goto done;
2925 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002926 case 0xff: /* Grp5 */
2927 if (c->modrm_reg == 5)
2928 goto jump_far;
2929 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002931
2932writeback:
2933 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002934 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002935 goto done;
2936
Gleb Natapova682e352010-03-18 15:20:21 +02002937 if ((c->d & SrcMask) == SrcSI)
2938 string_addr_inc(ctxt, seg_override_base(ctxt, c), VCPU_REGS_RSI,
2939 &c->src.ptr);
2940
2941 if ((c->d & DstMask) == DstDI)
2942 string_addr_inc(ctxt, es_base(ctxt), VCPU_REGS_RDI,
2943 &c->dst.ptr);
2944
Avi Kivity018a98d2007-11-27 19:30:56 +02002945 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002946 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002947 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002948
2949done:
2950 if (rc == X86EMUL_UNHANDLEABLE) {
2951 c->eip = saved_eip;
2952 return -1;
2953 }
2954 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955
2956twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002957 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002959 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 u16 size;
2961 unsigned long address;
2962
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002963 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002964 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002965 goto cannot_emulate;
2966
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002967 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002968 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002969 goto done;
2970
Avi Kivity33e38852008-05-21 15:34:25 +03002971 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002972 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002973 /* Disable writeback. */
2974 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002975 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002977 rc = read_descriptor(ctxt, ops, c->src.ptr,
2978 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002979 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 goto done;
2981 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002982 /* Disable writeback. */
2983 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002985 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002986 if (c->modrm_mod == 3) {
2987 switch (c->modrm_rm) {
2988 case 1:
2989 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002990 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002991 goto done;
2992 break;
2993 default:
2994 goto cannot_emulate;
2995 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002996 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002997 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002998 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002999 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003000 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003001 goto done;
3002 realmode_lidt(ctxt->vcpu, size, address);
3003 }
Avi Kivity16286d02008-04-14 14:40:50 +03003004 /* Disable writeback. */
3005 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 break;
3007 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003008 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003009 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 break;
3011 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003012 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3013 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003014 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003016 case 5: /* not defined */
3017 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3018 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003020 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003021 /* Disable writeback. */
3022 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023 break;
3024 default:
3025 goto cannot_emulate;
3026 }
3027 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003028 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003029 rc = emulate_syscall(ctxt);
3030 if (rc != X86EMUL_CONTINUE)
3031 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003032 else
3033 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003034 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003035 case 0x06:
3036 emulate_clts(ctxt->vcpu);
3037 c->dst.type = OP_NONE;
3038 break;
3039 case 0x08: /* invd */
3040 case 0x09: /* wbinvd */
3041 case 0x0d: /* GrpP (prefetch) */
3042 case 0x18: /* Grp16 (prefetch/nop) */
3043 c->dst.type = OP_NONE;
3044 break;
3045 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003046 switch (c->modrm_reg) {
3047 case 1:
3048 case 5 ... 7:
3049 case 9 ... 15:
3050 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3051 goto done;
3052 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003053 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003054 c->dst.type = OP_NONE; /* no writeback */
3055 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003057 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3058 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3059 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3060 goto done;
3061 }
3062 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003063 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003065 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02003066 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003067 c->dst.type = OP_NONE;
3068 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003070 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3071 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3072 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3073 goto done;
3074 }
3075 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003076 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003078 case 0x30:
3079 /* wrmsr */
3080 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3081 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003082 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003083 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003084 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003085 }
3086 rc = X86EMUL_CONTINUE;
3087 c->dst.type = OP_NONE;
3088 break;
3089 case 0x32:
3090 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003091 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003092 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003093 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003094 } else {
3095 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3096 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3097 }
3098 rc = X86EMUL_CONTINUE;
3099 c->dst.type = OP_NONE;
3100 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003101 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003102 rc = emulate_sysenter(ctxt);
3103 if (rc != X86EMUL_CONTINUE)
3104 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003105 else
3106 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003107 break;
3108 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003109 rc = emulate_sysexit(ctxt);
3110 if (rc != X86EMUL_CONTINUE)
3111 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003112 else
3113 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003114 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003116 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003117 if (!test_cc(c->b, ctxt->eflags))
3118 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003120 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003121 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003122 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003123 c->dst.type = OP_NONE;
3124 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003125 case 0xa0: /* push fs */
3126 emulate_push_sreg(ctxt, VCPU_SREG_FS);
3127 break;
3128 case 0xa1: /* pop fs */
3129 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003130 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003131 goto done;
3132 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003133 case 0xa3:
3134 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003135 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003136 /* only subword offset */
3137 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003138 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003139 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003140 case 0xa4: /* shld imm8, r, r/m */
3141 case 0xa5: /* shld cl, r, r/m */
3142 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3143 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003144 case 0xa8: /* push gs */
3145 emulate_push_sreg(ctxt, VCPU_SREG_GS);
3146 break;
3147 case 0xa9: /* pop gs */
3148 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003149 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003150 goto done;
3151 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003152 case 0xab:
3153 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003154 /* only subword offset */
3155 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003156 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003157 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003158 case 0xac: /* shrd imm8, r, r/m */
3159 case 0xad: /* shrd cl, r, r/m */
3160 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3161 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003162 case 0xae: /* clflush */
3163 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164 case 0xb0 ... 0xb1: /* cmpxchg */
3165 /*
3166 * Save real source value, then compare EAX against
3167 * destination.
3168 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003169 c->src.orig_val = c->src.val;
3170 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003171 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3172 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003174 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 } else {
3176 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003177 c->dst.type = OP_REG;
3178 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 }
3180 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 case 0xb3:
3182 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003183 /* only subword offset */
3184 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003185 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003188 c->dst.bytes = c->op_bytes;
3189 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3190 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003193 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 case 0:
3195 goto bt;
3196 case 1:
3197 goto bts;
3198 case 2:
3199 goto btr;
3200 case 3:
3201 goto btc;
3202 }
3203 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003204 case 0xbb:
3205 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003206 /* only subword offset */
3207 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003208 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003209 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003211 c->dst.bytes = c->op_bytes;
3212 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3213 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003215 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003216 c->dst.bytes = c->op_bytes;
3217 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3218 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003219 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003221 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003222 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003223 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003224 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003225 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 }
3227 goto writeback;
3228
3229cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003230 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02003231 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 return -1;
3233}