blob: 0eed6839619f465147f97e2897c4c5e2ed8a35c2 [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 */
54#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080055/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020056#define SrcNone (0<<4) /* No source operand. */
57#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
58#define SrcReg (1<<4) /* Register operand. */
59#define SrcMem (2<<4) /* Memory operand. */
60#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
61#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
62#define SrcImm (5<<4) /* Immediate operand. */
63#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010064#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030065#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030066#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
78#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030079/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020080#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020081#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030082#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010083/* Source 2 operand type */
84#define Src2None (0<<29)
85#define Src2CL (1<<29)
86#define Src2ImmByte (2<<29)
87#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030088#define Src2Imm16 (4<<29)
Gleb Natapove35b7b92010-02-25 16:36:42 +020089#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
90 in memory and second argument is located
91 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010092#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093
Avi Kivity43bb19c2008-01-18 12:46:50 +020094enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020095 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020096 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020097 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020098};
99
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100100static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200102 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300104 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300105 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200107 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200109 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
110 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200112 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300114 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300115 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200117 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300119 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300120 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200122 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200124 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200126 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
128 0, 0, 0, 0,
129 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200130 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
132 0, 0, 0, 0,
133 /* 0x38 - 0x3F */
134 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
135 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200136 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
137 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700138 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200139 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700140 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200141 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300142 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200143 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
144 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300145 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200146 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
147 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700148 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200149 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
150 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700151 0, 0, 0, 0,
152 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300153 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300154 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
155 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300156 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
158 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300159 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
161 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200163 Group | Group1_80, Group | Group1_81,
164 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200166 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 /* 0x88 - 0x8F */
168 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
169 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200170 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200171 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300172 /* 0x90 - 0x97 */
173 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
174 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300175 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300176 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200178 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
179 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200180 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
181 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200183 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
184 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
185 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300186 /* 0xB0 - 0xB7 */
187 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
188 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
191 /* 0xB8 - 0xBF */
192 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
193 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
195 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300197 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200198 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300199 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300201 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300202 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xD0 - 0xD7 */
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
206 0, 0, 0, 0,
207 /* 0xD8 - 0xDF */
208 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300209 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300210 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300211 ByteOp | SrcImmUByte, SrcImmUByte,
212 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300213 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300214 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300215 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300216 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
217 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0xF0 - 0xF7 */
219 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200220 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700222 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300223 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224};
225
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100226static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200228 0, Group | GroupDual | Group7, 0, 0,
229 0, ImplicitOps, ImplicitOps | Priv, 0,
230 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
231 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0x10 - 0x1F */
233 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
234 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 ModRM | ImplicitOps | Priv, ModRM | Priv,
237 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200240 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
241 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200242 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0x40 - 0x47 */
244 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 /* 0x48 - 0x4F */
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
253 /* 0x50 - 0x5F */
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 /* 0x60 - 0x6F */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 /* 0x70 - 0x7F */
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300260 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
261 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0x90 - 0x9F */
263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300265 ImplicitOps | Stack, ImplicitOps | Stack,
266 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100267 DstMem | SrcReg | Src2ImmByte | ModRM,
268 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300270 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200271 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100272 DstMem | SrcReg | Src2ImmByte | ModRM,
273 DstMem | SrcReg | Src2CL | ModRM,
274 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200276 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
277 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
279 DstReg | SrcMem16 | ModRM | Mov,
280 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200281 0, 0,
282 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
284 DstReg | SrcMem16 | ModRM | Mov,
285 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200286 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
287 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800288 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 /* 0xD0 - 0xDF */
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291 /* 0xE0 - 0xEF */
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293 /* 0xF0 - 0xFF */
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
295};
296
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100297static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200298 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200299 ByteOp | DstMem | SrcImm | ModRM | Lock,
300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM | Lock,
306 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200307 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM | Lock,
315 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200316 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200317 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
318 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
324 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200325 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM | Lock,
333 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200334 [Group1A*8] =
335 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200336 [Group3_Byte*8] =
337 ByteOp | SrcImm | DstMem | ModRM, 0,
338 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
339 0, 0, 0, 0,
340 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400341 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300342 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200343 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200344 [Group4*8] =
345 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
346 0, 0, 0, 0, 0, 0,
347 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300348 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
349 SrcMem | ModRM | Stack, 0,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200350 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
351 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200352 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200353 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300354 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200355 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200356 [Group8*8] =
357 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200358 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
359 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200360 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200361 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200362};
363
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100364static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200365 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200366 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300367 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200368 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200369 [Group9*8] =
370 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200371};
372
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200374#define EFLG_ID (1<<21)
375#define EFLG_VIP (1<<20)
376#define EFLG_VIF (1<<19)
377#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200378#define EFLG_VM (1<<17)
379#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200380#define EFLG_IOPL (3<<12)
381#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382#define EFLG_OF (1<<11)
383#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200384#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200385#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386#define EFLG_SF (1<<7)
387#define EFLG_ZF (1<<6)
388#define EFLG_AF (1<<4)
389#define EFLG_PF (1<<2)
390#define EFLG_CF (1<<0)
391
392/*
393 * Instruction emulation:
394 * Most instructions are emulated directly via a fragment of inline assembly
395 * code. This allows us to save/restore EFLAGS and thus very easily pick up
396 * any modified flags.
397 */
398
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800399#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400#define _LO32 "k" /* force 32-bit operand */
401#define _STK "%%rsp" /* stack pointer */
402#elif defined(__i386__)
403#define _LO32 "" /* force 32-bit operand */
404#define _STK "%%esp" /* stack pointer */
405#endif
406
407/*
408 * These EFLAGS bits are restored from saved value during emulation, and
409 * any changes are written back to the saved value after emulation.
410 */
411#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
412
413/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200414#define _PRE_EFLAGS(_sav, _msk, _tmp) \
415 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
416 "movl %"_sav",%"_LO32 _tmp"; " \
417 "push %"_tmp"; " \
418 "push %"_tmp"; " \
419 "movl %"_msk",%"_LO32 _tmp"; " \
420 "andl %"_LO32 _tmp",("_STK"); " \
421 "pushf; " \
422 "notl %"_LO32 _tmp"; " \
423 "andl %"_LO32 _tmp",("_STK"); " \
424 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
425 "pop %"_tmp"; " \
426 "orl %"_LO32 _tmp",("_STK"); " \
427 "popf; " \
428 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
430/* After executing instruction: write-back necessary bits in EFLAGS. */
431#define _POST_EFLAGS(_sav, _msk, _tmp) \
432 /* _sav |= EFLAGS & _msk; */ \
433 "pushf; " \
434 "pop %"_tmp"; " \
435 "andl %"_msk",%"_LO32 _tmp"; " \
436 "orl %"_LO32 _tmp",%"_sav"; "
437
Avi Kivitydda96d82008-11-26 15:14:10 +0200438#ifdef CONFIG_X86_64
439#define ON64(x) x
440#else
441#define ON64(x)
442#endif
443
Avi Kivity6b7ad612008-11-26 15:30:45 +0200444#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
445 do { \
446 __asm__ __volatile__ ( \
447 _PRE_EFLAGS("0", "4", "2") \
448 _op _suffix " %"_x"3,%1; " \
449 _POST_EFLAGS("0", "4", "2") \
450 : "=m" (_eflags), "=m" ((_dst).val), \
451 "=&r" (_tmp) \
452 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200453 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200454
455
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456/* Raw emulation: instruction has two explicit operands. */
457#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200458 do { \
459 unsigned long _tmp; \
460 \
461 switch ((_dst).bytes) { \
462 case 2: \
463 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
464 break; \
465 case 4: \
466 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
467 break; \
468 case 8: \
469 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
470 break; \
471 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 } while (0)
473
474#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
475 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200476 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400477 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200479 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 break; \
481 default: \
482 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
483 _wx, _wy, _lx, _ly, _qx, _qy); \
484 break; \
485 } \
486 } while (0)
487
488/* Source operand is byte-sized and may be restricted to just %cl. */
489#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
490 __emulate_2op(_op, _src, _dst, _eflags, \
491 "b", "c", "b", "c", "b", "c", "b", "c")
492
493/* Source operand is byte, word, long or quad sized. */
494#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
495 __emulate_2op(_op, _src, _dst, _eflags, \
496 "b", "q", "w", "r", _LO32, "r", "", "r")
497
498/* Source operand is word, long or quad sized. */
499#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
500 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
501 "w", "r", _LO32, "r", "", "r")
502
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100503/* Instruction has three operands and one operand is stored in ECX register */
504#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
505 do { \
506 unsigned long _tmp; \
507 _type _clv = (_cl).val; \
508 _type _srcv = (_src).val; \
509 _type _dstv = (_dst).val; \
510 \
511 __asm__ __volatile__ ( \
512 _PRE_EFLAGS("0", "5", "2") \
513 _op _suffix " %4,%1 \n" \
514 _POST_EFLAGS("0", "5", "2") \
515 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
516 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
517 ); \
518 \
519 (_cl).val = (unsigned long) _clv; \
520 (_src).val = (unsigned long) _srcv; \
521 (_dst).val = (unsigned long) _dstv; \
522 } while (0)
523
524#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
525 do { \
526 switch ((_dst).bytes) { \
527 case 2: \
528 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
529 "w", unsigned short); \
530 break; \
531 case 4: \
532 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
533 "l", unsigned int); \
534 break; \
535 case 8: \
536 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
537 "q", unsigned long)); \
538 break; \
539 } \
540 } while (0)
541
Avi Kivitydda96d82008-11-26 15:14:10 +0200542#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 do { \
544 unsigned long _tmp; \
545 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200546 __asm__ __volatile__ ( \
547 _PRE_EFLAGS("0", "3", "2") \
548 _op _suffix " %1; " \
549 _POST_EFLAGS("0", "3", "2") \
550 : "=m" (_eflags), "+m" ((_dst).val), \
551 "=&r" (_tmp) \
552 : "i" (EFLAGS_MASK)); \
553 } while (0)
554
555/* Instruction has only one explicit operand (no source operand). */
556#define emulate_1op(_op, _dst, _eflags) \
557 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400558 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200559 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
560 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
561 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
562 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 } \
564 } while (0)
565
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566/* Fetch next part of the instruction being emulated. */
567#define insn_fetch(_type, _size, _eip) \
568({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200569 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200570 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 goto done; \
572 (_eip) += (_size); \
573 (_type)_x; \
574})
575
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800576static inline unsigned long ad_mask(struct decode_cache *c)
577{
578 return (1UL << (c->ad_bytes << 3)) - 1;
579}
580
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800582static inline unsigned long
583address_mask(struct decode_cache *c, unsigned long reg)
584{
585 if (c->ad_bytes == sizeof(unsigned long))
586 return reg;
587 else
588 return reg & ad_mask(c);
589}
590
591static inline unsigned long
592register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
593{
594 return base + address_mask(c, reg);
595}
596
Harvey Harrison7a9572752008-02-19 07:40:41 -0800597static inline void
598register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
599{
600 if (c->ad_bytes == sizeof(unsigned long))
601 *reg += inc;
602 else
603 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
604}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605
Harvey Harrison7a9572752008-02-19 07:40:41 -0800606static inline void jmp_rel(struct decode_cache *c, int rel)
607{
608 register_address_increment(c, &c->eip, rel);
609}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300610
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300611static void set_seg_override(struct decode_cache *c, int seg)
612{
613 c->has_seg_override = true;
614 c->seg_override = seg;
615}
616
617static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
618{
619 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
620 return 0;
621
622 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
623}
624
625static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
626 struct decode_cache *c)
627{
628 if (!c->has_seg_override)
629 return 0;
630
631 return seg_base(ctxt, c->seg_override);
632}
633
634static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
635{
636 return seg_base(ctxt, VCPU_SREG_ES);
637}
638
639static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
640{
641 return seg_base(ctxt, VCPU_SREG_SS);
642}
643
Avi Kivity62266862007-11-20 13:15:52 +0200644static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
645 struct x86_emulate_ops *ops,
646 unsigned long linear, u8 *dest)
647{
648 struct fetch_cache *fc = &ctxt->decode.fetch;
649 int rc;
650 int size;
651
652 if (linear < fc->start || linear >= fc->end) {
653 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200654 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900655 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200656 return rc;
657 fc->start = linear;
658 fc->end = linear + size;
659 }
660 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900661 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200662}
663
664static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
665 struct x86_emulate_ops *ops,
666 unsigned long eip, void *dest, unsigned size)
667{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900668 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200669
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200670 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200671 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200673 eip += ctxt->cs_base;
674 while (size--) {
675 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900676 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200677 return rc;
678 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900679 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200680}
681
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000682/*
683 * Given the 'reg' portion of a ModRM byte, and a register block, return a
684 * pointer into the block that addresses the relevant register.
685 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
686 */
687static void *decode_register(u8 modrm_reg, unsigned long *regs,
688 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689{
690 void *p;
691
692 p = &regs[modrm_reg];
693 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
694 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
695 return p;
696}
697
698static int read_descriptor(struct x86_emulate_ctxt *ctxt,
699 struct x86_emulate_ops *ops,
700 void *ptr,
701 u16 *size, unsigned long *address, int op_bytes)
702{
703 int rc;
704
705 if (op_bytes == 2)
706 op_bytes = 3;
707 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300708 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200709 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900710 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300712 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200713 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 return rc;
715}
716
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300717static int test_cc(unsigned int condition, unsigned int flags)
718{
719 int rc = 0;
720
721 switch ((condition & 15) >> 1) {
722 case 0: /* o */
723 rc |= (flags & EFLG_OF);
724 break;
725 case 1: /* b/c/nae */
726 rc |= (flags & EFLG_CF);
727 break;
728 case 2: /* z/e */
729 rc |= (flags & EFLG_ZF);
730 break;
731 case 3: /* be/na */
732 rc |= (flags & (EFLG_CF|EFLG_ZF));
733 break;
734 case 4: /* s */
735 rc |= (flags & EFLG_SF);
736 break;
737 case 5: /* p/pe */
738 rc |= (flags & EFLG_PF);
739 break;
740 case 7: /* le/ng */
741 rc |= (flags & EFLG_ZF);
742 /* fall through */
743 case 6: /* l/nge */
744 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
745 break;
746 }
747
748 /* Odd condition identifiers (lsb == 1) have inverted sense. */
749 return (!!rc ^ (condition & 1));
750}
751
Avi Kivity3c118e22007-10-31 10:27:04 +0200752static void decode_register_operand(struct operand *op,
753 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200754 int inhibit_bytereg)
755{
Avi Kivity33615aa2007-10-31 11:15:56 +0200756 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200757 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200758
759 if (!(c->d & ModRM))
760 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200761 op->type = OP_REG;
762 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200763 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200764 op->val = *(u8 *)op->ptr;
765 op->bytes = 1;
766 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200767 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200768 op->bytes = c->op_bytes;
769 switch (op->bytes) {
770 case 2:
771 op->val = *(u16 *)op->ptr;
772 break;
773 case 4:
774 op->val = *(u32 *)op->ptr;
775 break;
776 case 8:
777 op->val = *(u64 *) op->ptr;
778 break;
779 }
780 }
781 op->orig_val = op->val;
782}
783
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784static int decode_modrm(struct x86_emulate_ctxt *ctxt,
785 struct x86_emulate_ops *ops)
786{
787 struct decode_cache *c = &ctxt->decode;
788 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700789 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900790 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791
792 if (c->rex_prefix) {
793 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
794 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
795 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
796 }
797
798 c->modrm = insn_fetch(u8, 1, c->eip);
799 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
800 c->modrm_reg |= (c->modrm & 0x38) >> 3;
801 c->modrm_rm |= (c->modrm & 0x07);
802 c->modrm_ea = 0;
803 c->use_modrm_ea = 1;
804
805 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300806 c->modrm_ptr = decode_register(c->modrm_rm,
807 c->regs, c->d & ByteOp);
808 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 return rc;
810 }
811
812 if (c->ad_bytes == 2) {
813 unsigned bx = c->regs[VCPU_REGS_RBX];
814 unsigned bp = c->regs[VCPU_REGS_RBP];
815 unsigned si = c->regs[VCPU_REGS_RSI];
816 unsigned di = c->regs[VCPU_REGS_RDI];
817
818 /* 16-bit ModR/M decode. */
819 switch (c->modrm_mod) {
820 case 0:
821 if (c->modrm_rm == 6)
822 c->modrm_ea += insn_fetch(u16, 2, c->eip);
823 break;
824 case 1:
825 c->modrm_ea += insn_fetch(s8, 1, c->eip);
826 break;
827 case 2:
828 c->modrm_ea += insn_fetch(u16, 2, c->eip);
829 break;
830 }
831 switch (c->modrm_rm) {
832 case 0:
833 c->modrm_ea += bx + si;
834 break;
835 case 1:
836 c->modrm_ea += bx + di;
837 break;
838 case 2:
839 c->modrm_ea += bp + si;
840 break;
841 case 3:
842 c->modrm_ea += bp + di;
843 break;
844 case 4:
845 c->modrm_ea += si;
846 break;
847 case 5:
848 c->modrm_ea += di;
849 break;
850 case 6:
851 if (c->modrm_mod != 0)
852 c->modrm_ea += bp;
853 break;
854 case 7:
855 c->modrm_ea += bx;
856 break;
857 }
858 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
859 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300860 if (!c->has_seg_override)
861 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200862 c->modrm_ea = (u16)c->modrm_ea;
863 } else {
864 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700865 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200866 sib = insn_fetch(u8, 1, c->eip);
867 index_reg |= (sib >> 3) & 7;
868 base_reg |= sib & 7;
869 scale = sib >> 6;
870
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700871 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
872 c->modrm_ea += insn_fetch(s32, 4, c->eip);
873 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700875 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700877 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
878 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700879 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700880 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200882 switch (c->modrm_mod) {
883 case 0:
884 if (c->modrm_rm == 5)
885 c->modrm_ea += insn_fetch(s32, 4, c->eip);
886 break;
887 case 1:
888 c->modrm_ea += insn_fetch(s8, 1, c->eip);
889 break;
890 case 2:
891 c->modrm_ea += insn_fetch(s32, 4, c->eip);
892 break;
893 }
894 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200895done:
896 return rc;
897}
898
899static int decode_abs(struct x86_emulate_ctxt *ctxt,
900 struct x86_emulate_ops *ops)
901{
902 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900903 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200904
905 switch (c->ad_bytes) {
906 case 2:
907 c->modrm_ea = insn_fetch(u16, 2, c->eip);
908 break;
909 case 4:
910 c->modrm_ea = insn_fetch(u32, 4, c->eip);
911 break;
912 case 8:
913 c->modrm_ea = insn_fetch(u64, 8, c->eip);
914 break;
915 }
916done:
917 return rc;
918}
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200921x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200923 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900924 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200926 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200931 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300932 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800933 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 switch (mode) {
936 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200937 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200939 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 break;
941 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200942 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800944#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200946 def_op_bytes = 4;
947 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 break;
949#endif
950 default:
951 return -1;
952 }
953
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200954 c->op_bytes = def_op_bytes;
955 c->ad_bytes = def_ad_bytes;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200958 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200959 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200961 /* switch between 2/4 bytes */
962 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 break;
964 case 0x67: /* address-size override */
965 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200966 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200969 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200970 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300973 case 0x2e: /* CS override */
974 case 0x36: /* SS override */
975 case 0x3e: /* DS override */
976 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 break;
978 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300980 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200982 case 0x40 ... 0x4f: /* REX */
983 if (mode != X86EMUL_MODE_PROT64)
984 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200985 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200986 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200988 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200990 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100991 c->rep_prefix = REPNE_PREFIX;
992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100994 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 default:
997 goto done_prefixes;
998 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200999
1000 /* Any legacy prefix after a REX prefix nullifies its effect. */
1001
Avi Kivity33615aa2007-10-31 11:15:56 +02001002 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 }
1004
1005done_prefixes:
1006
1007 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001009 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->d = opcode_table[c->b];
1014 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 if (c->b == 0x0f) {
1017 c->twobyte = 1;
1018 c->b = insn_fetch(u8, 1, c->eip);
1019 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 if (c->d & Group) {
1024 group = c->d & GroupMask;
1025 c->modrm = insn_fetch(u8, 1, c->eip);
1026 --c->eip;
1027
1028 group = (group << 3) + ((c->modrm >> 3) & 7);
1029 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1030 c->d = group2_table[group];
1031 else
1032 c->d = group_table[group];
1033 }
1034
1035 /* Unrecognised? */
1036 if (c->d == 0) {
1037 DPRINTF("Cannot emulate %02x\n", c->b);
1038 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 }
1040
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001041 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1042 c->op_bytes = 8;
1043
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 if (c->d & ModRM)
1046 rc = decode_modrm(ctxt, ops);
1047 else if (c->d & MemAbs)
1048 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001049 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001050 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001052 if (!c->has_seg_override)
1053 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001054
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001055 if (!(!c->twobyte && c->b == 0x8d))
1056 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001057
1058 if (c->ad_bytes != 8)
1059 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001060
1061 if (c->rip_relative)
1062 c->modrm_ea += c->eip;
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 /*
1065 * Decode and fetch the source operand: register, memory
1066 * or immediate.
1067 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001068 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 case SrcNone:
1070 break;
1071 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001072 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 break;
1074 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001075 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 goto srcmem_common;
1077 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001078 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 goto srcmem_common;
1080 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001081 c->src.bytes = (c->d & ByteOp) ? 1 :
1082 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001083 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001084 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001085 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001086 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001087 /*
1088 * For instructions with a ModR/M byte, switch to register
1089 * access if Mod = 3.
1090 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 if ((c->d & ModRM) && c->modrm_mod == 3) {
1092 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001093 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001094 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001095 break;
1096 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001097 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001098 c->src.ptr = (unsigned long *)c->modrm_ea;
1099 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 break;
1101 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001102 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 c->src.type = OP_IMM;
1104 c->src.ptr = (unsigned long *)c->eip;
1105 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1106 if (c->src.bytes == 8)
1107 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001109 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001114 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 break;
1116 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001117 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 break;
1119 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001120 if ((c->d & SrcMask) == SrcImmU) {
1121 switch (c->src.bytes) {
1122 case 1:
1123 c->src.val &= 0xff;
1124 break;
1125 case 2:
1126 c->src.val &= 0xffff;
1127 break;
1128 case 4:
1129 c->src.val &= 0xffffffff;
1130 break;
1131 }
1132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 break;
1134 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001135 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 c->src.type = OP_IMM;
1137 c->src.ptr = (unsigned long *)c->eip;
1138 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001139 if ((c->d & SrcMask) == SrcImmByte)
1140 c->src.val = insn_fetch(s8, 1, c->eip);
1141 else
1142 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001144 case SrcOne:
1145 c->src.bytes = 1;
1146 c->src.val = 1;
1147 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 }
1149
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001150 /*
1151 * Decode and fetch the second source operand: register, memory
1152 * or immediate.
1153 */
1154 switch (c->d & Src2Mask) {
1155 case Src2None:
1156 break;
1157 case Src2CL:
1158 c->src2.bytes = 1;
1159 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1160 break;
1161 case Src2ImmByte:
1162 c->src2.type = OP_IMM;
1163 c->src2.ptr = (unsigned long *)c->eip;
1164 c->src2.bytes = 1;
1165 c->src2.val = insn_fetch(u8, 1, c->eip);
1166 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001167 case Src2Imm16:
1168 c->src2.type = OP_IMM;
1169 c->src2.ptr = (unsigned long *)c->eip;
1170 c->src2.bytes = 2;
1171 c->src2.val = insn_fetch(u16, 2, c->eip);
1172 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001173 case Src2One:
1174 c->src2.bytes = 1;
1175 c->src2.val = 1;
1176 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001177 case Src2Mem16:
Gleb Natapove35b7b92010-02-25 16:36:42 +02001178 c->src2.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001179 c->src2.bytes = 2;
1180 c->src2.ptr = (unsigned long *)(c->modrm_ea + c->src.bytes);
1181 c->src2.val = 0;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001182 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001183 }
1184
Avi Kivity038e51d2007-01-22 20:40:40 -08001185 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001186 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001187 case ImplicitOps:
1188 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001189 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001190 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001191 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001192 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001193 break;
1194 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001195 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001196 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001197 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001198 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001199 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001200 break;
1201 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001202 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001203 c->dst.ptr = (unsigned long *)c->modrm_ea;
1204 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1205 c->dst.val = 0;
1206 if (c->d & BitOp) {
1207 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1208
1209 c->dst.ptr = (void *)c->dst.ptr +
1210 (c->src.val & mask) / 8;
1211 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001212 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001213 case DstAcc:
1214 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001215 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001216 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001217 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001218 case 1:
1219 c->dst.val = *(u8 *)c->dst.ptr;
1220 break;
1221 case 2:
1222 c->dst.val = *(u16 *)c->dst.ptr;
1223 break;
1224 case 4:
1225 c->dst.val = *(u32 *)c->dst.ptr;
1226 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001227 case 8:
1228 c->dst.val = *(u64 *)c->dst.ptr;
1229 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001230 }
1231 c->dst.orig_val = c->dst.val;
1232 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001233 }
1234
1235done:
1236 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1237}
1238
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001239static u32 desc_limit_scaled(struct desc_struct *desc)
1240{
1241 u32 limit = get_desc_limit(desc);
1242
1243 return desc->g ? (limit << 12) | 0xfff : limit;
1244}
1245
1246static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1247 struct x86_emulate_ops *ops,
1248 u16 selector, struct desc_ptr *dt)
1249{
1250 if (selector & 1 << 2) {
1251 struct desc_struct desc;
1252 memset (dt, 0, sizeof *dt);
1253 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1254 return;
1255
1256 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1257 dt->address = get_desc_base(&desc);
1258 } else
1259 ops->get_gdt(dt, ctxt->vcpu);
1260}
1261
1262/* allowed just for 8 bytes segments */
1263static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1264 struct x86_emulate_ops *ops,
1265 u16 selector, struct desc_struct *desc)
1266{
1267 struct desc_ptr dt;
1268 u16 index = selector >> 3;
1269 int ret;
1270 u32 err;
1271 ulong addr;
1272
1273 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1274
1275 if (dt.size < index * 8 + 7) {
1276 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1277 return X86EMUL_PROPAGATE_FAULT;
1278 }
1279 addr = dt.address + index * 8;
1280 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1281 if (ret == X86EMUL_PROPAGATE_FAULT)
1282 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1283
1284 return ret;
1285}
1286
1287/* allowed just for 8 bytes segments */
1288static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1289 struct x86_emulate_ops *ops,
1290 u16 selector, struct desc_struct *desc)
1291{
1292 struct desc_ptr dt;
1293 u16 index = selector >> 3;
1294 u32 err;
1295 ulong addr;
1296 int ret;
1297
1298 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1299
1300 if (dt.size < index * 8 + 7) {
1301 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1302 return X86EMUL_PROPAGATE_FAULT;
1303 }
1304
1305 addr = dt.address + index * 8;
1306 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1307 if (ret == X86EMUL_PROPAGATE_FAULT)
1308 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1309
1310 return ret;
1311}
1312
1313static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1314 struct x86_emulate_ops *ops,
1315 u16 selector, int seg)
1316{
1317 struct desc_struct seg_desc;
1318 u8 dpl, rpl, cpl;
1319 unsigned err_vec = GP_VECTOR;
1320 u32 err_code = 0;
1321 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1322 int ret;
1323
1324 memset(&seg_desc, 0, sizeof seg_desc);
1325
1326 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1327 || ctxt->mode == X86EMUL_MODE_REAL) {
1328 /* set real mode segment descriptor */
1329 set_desc_base(&seg_desc, selector << 4);
1330 set_desc_limit(&seg_desc, 0xffff);
1331 seg_desc.type = 3;
1332 seg_desc.p = 1;
1333 seg_desc.s = 1;
1334 goto load;
1335 }
1336
1337 /* NULL selector is not valid for TR, CS and SS */
1338 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1339 && null_selector)
1340 goto exception;
1341
1342 /* TR should be in GDT only */
1343 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1344 goto exception;
1345
1346 if (null_selector) /* for NULL selector skip all following checks */
1347 goto load;
1348
1349 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1350 if (ret != X86EMUL_CONTINUE)
1351 return ret;
1352
1353 err_code = selector & 0xfffc;
1354 err_vec = GP_VECTOR;
1355
1356 /* can't load system descriptor into segment selecor */
1357 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1358 goto exception;
1359
1360 if (!seg_desc.p) {
1361 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1362 goto exception;
1363 }
1364
1365 rpl = selector & 3;
1366 dpl = seg_desc.dpl;
1367 cpl = ops->cpl(ctxt->vcpu);
1368
1369 switch (seg) {
1370 case VCPU_SREG_SS:
1371 /*
1372 * segment is not a writable data segment or segment
1373 * selector's RPL != CPL or segment selector's RPL != CPL
1374 */
1375 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1376 goto exception;
1377 break;
1378 case VCPU_SREG_CS:
1379 if (!(seg_desc.type & 8))
1380 goto exception;
1381
1382 if (seg_desc.type & 4) {
1383 /* conforming */
1384 if (dpl > cpl)
1385 goto exception;
1386 } else {
1387 /* nonconforming */
1388 if (rpl > cpl || dpl != cpl)
1389 goto exception;
1390 }
1391 /* CS(RPL) <- CPL */
1392 selector = (selector & 0xfffc) | cpl;
1393 break;
1394 case VCPU_SREG_TR:
1395 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1396 goto exception;
1397 break;
1398 case VCPU_SREG_LDTR:
1399 if (seg_desc.s || seg_desc.type != 2)
1400 goto exception;
1401 break;
1402 default: /* DS, ES, FS, or GS */
1403 /*
1404 * segment is not a data or readable code segment or
1405 * ((segment is a data or nonconforming code segment)
1406 * and (both RPL and CPL > DPL))
1407 */
1408 if ((seg_desc.type & 0xa) == 0x8 ||
1409 (((seg_desc.type & 0xc) != 0xc) &&
1410 (rpl > dpl && cpl > dpl)))
1411 goto exception;
1412 break;
1413 }
1414
1415 if (seg_desc.s) {
1416 /* mark segment as accessed */
1417 seg_desc.type |= 1;
1418 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1419 if (ret != X86EMUL_CONTINUE)
1420 return ret;
1421 }
1422load:
1423 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1424 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1425 return X86EMUL_CONTINUE;
1426exception:
1427 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1428 return X86EMUL_PROPAGATE_FAULT;
1429}
1430
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001431static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1432{
1433 struct decode_cache *c = &ctxt->decode;
1434
1435 c->dst.type = OP_MEM;
1436 c->dst.bytes = c->op_bytes;
1437 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001438 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001439 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 c->regs[VCPU_REGS_RSP]);
1441}
1442
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001443static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001444 struct x86_emulate_ops *ops,
1445 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001446{
1447 struct decode_cache *c = &ctxt->decode;
1448 int rc;
1449
Avi Kivity781d0ed2008-11-27 18:00:28 +02001450 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1451 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001452 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001453 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001454 return rc;
1455
Avi Kivity350f69d2009-01-05 11:12:40 +02001456 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001457 return rc;
1458}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001459
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001460static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1461 struct x86_emulate_ops *ops,
1462 void *dest, int len)
1463{
1464 int rc;
1465 unsigned long val, change_mask;
1466 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001467 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001468
1469 rc = emulate_pop(ctxt, ops, &val, len);
1470 if (rc != X86EMUL_CONTINUE)
1471 return rc;
1472
1473 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1474 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1475
1476 switch(ctxt->mode) {
1477 case X86EMUL_MODE_PROT64:
1478 case X86EMUL_MODE_PROT32:
1479 case X86EMUL_MODE_PROT16:
1480 if (cpl == 0)
1481 change_mask |= EFLG_IOPL;
1482 if (cpl <= iopl)
1483 change_mask |= EFLG_IF;
1484 break;
1485 case X86EMUL_MODE_VM86:
1486 if (iopl < 3) {
1487 kvm_inject_gp(ctxt->vcpu, 0);
1488 return X86EMUL_PROPAGATE_FAULT;
1489 }
1490 change_mask |= EFLG_IF;
1491 break;
1492 default: /* real mode */
1493 change_mask |= (EFLG_IOPL | EFLG_IF);
1494 break;
1495 }
1496
1497 *(unsigned long *)dest =
1498 (ctxt->eflags & ~change_mask) | (val & change_mask);
1499
1500 return rc;
1501}
1502
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001503static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1504{
1505 struct decode_cache *c = &ctxt->decode;
1506 struct kvm_segment segment;
1507
1508 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1509
1510 c->src.val = segment.selector;
1511 emulate_push(ctxt);
1512}
1513
1514static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1515 struct x86_emulate_ops *ops, int seg)
1516{
1517 struct decode_cache *c = &ctxt->decode;
1518 unsigned long selector;
1519 int rc;
1520
1521 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001522 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001523 return rc;
1524
Gleb Natapov2e873022010-03-18 15:20:18 +02001525 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001526 return rc;
1527}
1528
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001529static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1530{
1531 struct decode_cache *c = &ctxt->decode;
1532 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1533 int reg = VCPU_REGS_RAX;
1534
1535 while (reg <= VCPU_REGS_RDI) {
1536 (reg == VCPU_REGS_RSP) ?
1537 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1538
1539 emulate_push(ctxt);
1540 ++reg;
1541 }
1542}
1543
1544static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1545 struct x86_emulate_ops *ops)
1546{
1547 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001548 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001549 int reg = VCPU_REGS_RDI;
1550
1551 while (reg >= VCPU_REGS_RAX) {
1552 if (reg == VCPU_REGS_RSP) {
1553 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1554 c->op_bytes);
1555 --reg;
1556 }
1557
1558 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001559 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001560 break;
1561 --reg;
1562 }
1563 return rc;
1564}
1565
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001566static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1567 struct x86_emulate_ops *ops)
1568{
1569 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001570
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001571 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001572}
1573
Laurent Vivier05f086f2007-09-24 11:10:55 +02001574static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001575{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001576 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001577 switch (c->modrm_reg) {
1578 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001579 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001580 break;
1581 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001582 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001583 break;
1584 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001585 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001586 break;
1587 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001588 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001589 break;
1590 case 4: /* sal/shl */
1591 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001592 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001593 break;
1594 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001595 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001596 break;
1597 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001598 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001599 break;
1600 }
1601}
1602
1603static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001604 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001605{
1606 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001607
1608 switch (c->modrm_reg) {
1609 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001610 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001611 break;
1612 case 2: /* not */
1613 c->dst.val = ~c->dst.val;
1614 break;
1615 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001616 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001617 break;
1618 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001619 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001620 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001621 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001622}
1623
1624static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001625 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001626{
1627 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628
1629 switch (c->modrm_reg) {
1630 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001631 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001632 break;
1633 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001634 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001636 case 2: /* call near abs */ {
1637 long int old_eip;
1638 old_eip = c->eip;
1639 c->eip = c->src.val;
1640 c->src.val = old_eip;
1641 emulate_push(ctxt);
1642 break;
1643 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001644 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001645 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646 break;
1647 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001648 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001649 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001651 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001652}
1653
1654static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001655 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656{
1657 struct decode_cache *c = &ctxt->decode;
1658 u64 old, new;
1659 int rc;
1660
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001661 rc = ops->read_emulated(c->modrm_ea, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001662 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001663 return rc;
1664
1665 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1666 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1667
1668 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1669 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001670 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671
1672 } else {
1673 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1674 (u32) c->regs[VCPU_REGS_RBX];
1675
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001676 rc = ops->cmpxchg_emulated(c->modrm_ea, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001677 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001679 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001681 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001682}
1683
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001684static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1685 struct x86_emulate_ops *ops)
1686{
1687 struct decode_cache *c = &ctxt->decode;
1688 int rc;
1689 unsigned long cs;
1690
1691 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001692 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001693 return rc;
1694 if (c->op_bytes == 4)
1695 c->eip = (u32)c->eip;
1696 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001697 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001698 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001699 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001700 return rc;
1701}
1702
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703static inline int writeback(struct x86_emulate_ctxt *ctxt,
1704 struct x86_emulate_ops *ops)
1705{
1706 int rc;
1707 struct decode_cache *c = &ctxt->decode;
1708
1709 switch (c->dst.type) {
1710 case OP_REG:
1711 /* The 4-byte case *is* correct:
1712 * in 64-bit mode we zero-extend.
1713 */
1714 switch (c->dst.bytes) {
1715 case 1:
1716 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1717 break;
1718 case 2:
1719 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1720 break;
1721 case 4:
1722 *c->dst.ptr = (u32)c->dst.val;
1723 break; /* 64b: zero-ext */
1724 case 8:
1725 *c->dst.ptr = c->dst.val;
1726 break;
1727 }
1728 break;
1729 case OP_MEM:
1730 if (c->lock_prefix)
1731 rc = ops->cmpxchg_emulated(
1732 (unsigned long)c->dst.ptr,
1733 &c->dst.orig_val,
1734 &c->dst.val,
1735 c->dst.bytes,
1736 ctxt->vcpu);
1737 else
1738 rc = ops->write_emulated(
1739 (unsigned long)c->dst.ptr,
1740 &c->dst.val,
1741 c->dst.bytes,
1742 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001743 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001745 break;
1746 case OP_NONE:
1747 /* no writeback */
1748 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001749 default:
1750 break;
1751 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001752 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001753}
1754
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301755static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001756{
1757 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1758 /*
1759 * an sti; sti; sequence only disable interrupts for the first
1760 * instruction. So, if the last instruction, be it emulated or
1761 * not, left the system with the INT_STI flag enabled, it
1762 * means that the last instruction is an sti. We should not
1763 * leave the flag on in this case. The same goes for mov ss
1764 */
1765 if (!(int_shadow & mask))
1766 ctxt->interruptibility = mask;
1767}
1768
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001769static inline void
1770setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1771 struct kvm_segment *cs, struct kvm_segment *ss)
1772{
1773 memset(cs, 0, sizeof(struct kvm_segment));
1774 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1775 memset(ss, 0, sizeof(struct kvm_segment));
1776
1777 cs->l = 0; /* will be adjusted later */
1778 cs->base = 0; /* flat segment */
1779 cs->g = 1; /* 4kb granularity */
1780 cs->limit = 0xffffffff; /* 4GB limit */
1781 cs->type = 0x0b; /* Read, Execute, Accessed */
1782 cs->s = 1;
1783 cs->dpl = 0; /* will be adjusted later */
1784 cs->present = 1;
1785 cs->db = 1;
1786
1787 ss->unusable = 0;
1788 ss->base = 0; /* flat segment */
1789 ss->limit = 0xffffffff; /* 4GB limit */
1790 ss->g = 1; /* 4kb granularity */
1791 ss->s = 1;
1792 ss->type = 0x03; /* Read/Write, Accessed */
1793 ss->db = 1; /* 32bit stack segment */
1794 ss->dpl = 0;
1795 ss->present = 1;
1796}
1797
1798static int
1799emulate_syscall(struct x86_emulate_ctxt *ctxt)
1800{
1801 struct decode_cache *c = &ctxt->decode;
1802 struct kvm_segment cs, ss;
1803 u64 msr_data;
1804
1805 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001806 if (ctxt->mode == X86EMUL_MODE_REAL ||
1807 ctxt->mode == X86EMUL_MODE_VM86) {
1808 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1809 return X86EMUL_PROPAGATE_FAULT;
1810 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001811
1812 setup_syscalls_segments(ctxt, &cs, &ss);
1813
1814 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1815 msr_data >>= 32;
1816 cs.selector = (u16)(msr_data & 0xfffc);
1817 ss.selector = (u16)(msr_data + 8);
1818
1819 if (is_long_mode(ctxt->vcpu)) {
1820 cs.db = 0;
1821 cs.l = 1;
1822 }
1823 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1824 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1825
1826 c->regs[VCPU_REGS_RCX] = c->eip;
1827 if (is_long_mode(ctxt->vcpu)) {
1828#ifdef CONFIG_X86_64
1829 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1830
1831 kvm_x86_ops->get_msr(ctxt->vcpu,
1832 ctxt->mode == X86EMUL_MODE_PROT64 ?
1833 MSR_LSTAR : MSR_CSTAR, &msr_data);
1834 c->eip = msr_data;
1835
1836 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1837 ctxt->eflags &= ~(msr_data | EFLG_RF);
1838#endif
1839 } else {
1840 /* legacy mode */
1841 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1842 c->eip = (u32)msr_data;
1843
1844 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1845 }
1846
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001847 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001848}
1849
Andre Przywara8c604352009-06-18 12:56:01 +02001850static int
1851emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1852{
1853 struct decode_cache *c = &ctxt->decode;
1854 struct kvm_segment cs, ss;
1855 u64 msr_data;
1856
Gleb Natapova0044752010-02-10 14:21:31 +02001857 /* inject #GP if in real mode */
1858 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001859 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001860 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001861 }
1862
1863 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1864 * Therefore, we inject an #UD.
1865 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001866 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1867 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1868 return X86EMUL_PROPAGATE_FAULT;
1869 }
Andre Przywara8c604352009-06-18 12:56:01 +02001870
1871 setup_syscalls_segments(ctxt, &cs, &ss);
1872
1873 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1874 switch (ctxt->mode) {
1875 case X86EMUL_MODE_PROT32:
1876 if ((msr_data & 0xfffc) == 0x0) {
1877 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001878 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001879 }
1880 break;
1881 case X86EMUL_MODE_PROT64:
1882 if (msr_data == 0x0) {
1883 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001884 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001885 }
1886 break;
1887 }
1888
1889 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1890 cs.selector = (u16)msr_data;
1891 cs.selector &= ~SELECTOR_RPL_MASK;
1892 ss.selector = cs.selector + 8;
1893 ss.selector &= ~SELECTOR_RPL_MASK;
1894 if (ctxt->mode == X86EMUL_MODE_PROT64
1895 || is_long_mode(ctxt->vcpu)) {
1896 cs.db = 0;
1897 cs.l = 1;
1898 }
1899
1900 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1901 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1902
1903 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1904 c->eip = msr_data;
1905
1906 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1907 c->regs[VCPU_REGS_RSP] = msr_data;
1908
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001909 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001910}
1911
Andre Przywara4668f052009-06-18 12:56:02 +02001912static int
1913emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1914{
1915 struct decode_cache *c = &ctxt->decode;
1916 struct kvm_segment cs, ss;
1917 u64 msr_data;
1918 int usermode;
1919
Gleb Natapova0044752010-02-10 14:21:31 +02001920 /* inject #GP if in real mode or Virtual 8086 mode */
1921 if (ctxt->mode == X86EMUL_MODE_REAL ||
1922 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001923 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001924 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001925 }
1926
Andre Przywara4668f052009-06-18 12:56:02 +02001927 setup_syscalls_segments(ctxt, &cs, &ss);
1928
1929 if ((c->rex_prefix & 0x8) != 0x0)
1930 usermode = X86EMUL_MODE_PROT64;
1931 else
1932 usermode = X86EMUL_MODE_PROT32;
1933
1934 cs.dpl = 3;
1935 ss.dpl = 3;
1936 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1937 switch (usermode) {
1938 case X86EMUL_MODE_PROT32:
1939 cs.selector = (u16)(msr_data + 16);
1940 if ((msr_data & 0xfffc) == 0x0) {
1941 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001942 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001943 }
1944 ss.selector = (u16)(msr_data + 24);
1945 break;
1946 case X86EMUL_MODE_PROT64:
1947 cs.selector = (u16)(msr_data + 32);
1948 if (msr_data == 0x0) {
1949 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001950 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001951 }
1952 ss.selector = cs.selector + 8;
1953 cs.db = 0;
1954 cs.l = 1;
1955 break;
1956 }
1957 cs.selector |= SELECTOR_RPL_MASK;
1958 ss.selector |= SELECTOR_RPL_MASK;
1959
1960 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1961 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1962
1963 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1964 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1965
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001966 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001967}
1968
Gleb Natapov9c537242010-03-18 15:20:05 +02001969static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1970 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001971{
1972 int iopl;
1973 if (ctxt->mode == X86EMUL_MODE_REAL)
1974 return false;
1975 if (ctxt->mode == X86EMUL_MODE_VM86)
1976 return true;
1977 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001978 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001979}
1980
1981static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1982 struct x86_emulate_ops *ops,
1983 u16 port, u16 len)
1984{
1985 struct kvm_segment tr_seg;
1986 int r;
1987 u16 io_bitmap_ptr;
1988 u8 perm, bit_idx = port & 0x7;
1989 unsigned mask = (1 << len) - 1;
1990
1991 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1992 if (tr_seg.unusable)
1993 return false;
1994 if (tr_seg.limit < 103)
1995 return false;
1996 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1997 NULL);
1998 if (r != X86EMUL_CONTINUE)
1999 return false;
2000 if (io_bitmap_ptr + port/8 > tr_seg.limit)
2001 return false;
2002 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
2003 ctxt->vcpu, NULL);
2004 if (r != X86EMUL_CONTINUE)
2005 return false;
2006 if ((perm >> bit_idx) & mask)
2007 return false;
2008 return true;
2009}
2010
2011static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2012 struct x86_emulate_ops *ops,
2013 u16 port, u16 len)
2014{
Gleb Natapov9c537242010-03-18 15:20:05 +02002015 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002016 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2017 return false;
2018 return true;
2019}
2020
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002021static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt,
2022 struct x86_emulate_ops *ops,
2023 int seg)
2024{
2025 struct desc_struct desc;
2026 if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu))
2027 return get_desc_base(&desc);
2028 else
2029 return ~0;
2030}
2031
2032static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2033 struct x86_emulate_ops *ops,
2034 struct tss_segment_16 *tss)
2035{
2036 struct decode_cache *c = &ctxt->decode;
2037
2038 tss->ip = c->eip;
2039 tss->flag = ctxt->eflags;
2040 tss->ax = c->regs[VCPU_REGS_RAX];
2041 tss->cx = c->regs[VCPU_REGS_RCX];
2042 tss->dx = c->regs[VCPU_REGS_RDX];
2043 tss->bx = c->regs[VCPU_REGS_RBX];
2044 tss->sp = c->regs[VCPU_REGS_RSP];
2045 tss->bp = c->regs[VCPU_REGS_RBP];
2046 tss->si = c->regs[VCPU_REGS_RSI];
2047 tss->di = c->regs[VCPU_REGS_RDI];
2048
2049 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2050 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2051 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2052 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2053 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2054}
2055
2056static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2057 struct x86_emulate_ops *ops,
2058 struct tss_segment_16 *tss)
2059{
2060 struct decode_cache *c = &ctxt->decode;
2061 int ret;
2062
2063 c->eip = tss->ip;
2064 ctxt->eflags = tss->flag | 2;
2065 c->regs[VCPU_REGS_RAX] = tss->ax;
2066 c->regs[VCPU_REGS_RCX] = tss->cx;
2067 c->regs[VCPU_REGS_RDX] = tss->dx;
2068 c->regs[VCPU_REGS_RBX] = tss->bx;
2069 c->regs[VCPU_REGS_RSP] = tss->sp;
2070 c->regs[VCPU_REGS_RBP] = tss->bp;
2071 c->regs[VCPU_REGS_RSI] = tss->si;
2072 c->regs[VCPU_REGS_RDI] = tss->di;
2073
2074 /*
2075 * SDM says that segment selectors are loaded before segment
2076 * descriptors
2077 */
2078 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2079 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2080 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2081 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2082 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2083
2084 /*
2085 * Now load segment descriptors. If fault happenes at this stage
2086 * it is handled in a context of new task
2087 */
2088 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2089 if (ret != X86EMUL_CONTINUE)
2090 return ret;
2091 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2092 if (ret != X86EMUL_CONTINUE)
2093 return ret;
2094 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2095 if (ret != X86EMUL_CONTINUE)
2096 return ret;
2097 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2098 if (ret != X86EMUL_CONTINUE)
2099 return ret;
2100 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2101 if (ret != X86EMUL_CONTINUE)
2102 return ret;
2103
2104 return X86EMUL_CONTINUE;
2105}
2106
2107static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2108 struct x86_emulate_ops *ops,
2109 u16 tss_selector, u16 old_tss_sel,
2110 ulong old_tss_base, struct desc_struct *new_desc)
2111{
2112 struct tss_segment_16 tss_seg;
2113 int ret;
2114 u32 err, new_tss_base = get_desc_base(new_desc);
2115
2116 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2117 &err);
2118 if (ret == X86EMUL_PROPAGATE_FAULT) {
2119 /* FIXME: need to provide precise fault address */
2120 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2121 return ret;
2122 }
2123
2124 save_state_to_tss16(ctxt, ops, &tss_seg);
2125
2126 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2127 &err);
2128 if (ret == X86EMUL_PROPAGATE_FAULT) {
2129 /* FIXME: need to provide precise fault address */
2130 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2131 return ret;
2132 }
2133
2134 ret = ops->read_std(new_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, new_tss_base, err);
2139 return ret;
2140 }
2141
2142 if (old_tss_sel != 0xffff) {
2143 tss_seg.prev_task_link = old_tss_sel;
2144
2145 ret = ops->write_std(new_tss_base,
2146 &tss_seg.prev_task_link,
2147 sizeof tss_seg.prev_task_link,
2148 ctxt->vcpu, &err);
2149 if (ret == X86EMUL_PROPAGATE_FAULT) {
2150 /* FIXME: need to provide precise fault address */
2151 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2152 return ret;
2153 }
2154 }
2155
2156 return load_state_from_tss16(ctxt, ops, &tss_seg);
2157}
2158
2159static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2160 struct x86_emulate_ops *ops,
2161 struct tss_segment_32 *tss)
2162{
2163 struct decode_cache *c = &ctxt->decode;
2164
2165 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2166 tss->eip = c->eip;
2167 tss->eflags = ctxt->eflags;
2168 tss->eax = c->regs[VCPU_REGS_RAX];
2169 tss->ecx = c->regs[VCPU_REGS_RCX];
2170 tss->edx = c->regs[VCPU_REGS_RDX];
2171 tss->ebx = c->regs[VCPU_REGS_RBX];
2172 tss->esp = c->regs[VCPU_REGS_RSP];
2173 tss->ebp = c->regs[VCPU_REGS_RBP];
2174 tss->esi = c->regs[VCPU_REGS_RSI];
2175 tss->edi = c->regs[VCPU_REGS_RDI];
2176
2177 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2178 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2179 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2180 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2181 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2182 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2183 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2184}
2185
2186static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2187 struct x86_emulate_ops *ops,
2188 struct tss_segment_32 *tss)
2189{
2190 struct decode_cache *c = &ctxt->decode;
2191 int ret;
2192
2193 ops->set_cr(3, tss->cr3, ctxt->vcpu);
2194 c->eip = tss->eip;
2195 ctxt->eflags = tss->eflags | 2;
2196 c->regs[VCPU_REGS_RAX] = tss->eax;
2197 c->regs[VCPU_REGS_RCX] = tss->ecx;
2198 c->regs[VCPU_REGS_RDX] = tss->edx;
2199 c->regs[VCPU_REGS_RBX] = tss->ebx;
2200 c->regs[VCPU_REGS_RSP] = tss->esp;
2201 c->regs[VCPU_REGS_RBP] = tss->ebp;
2202 c->regs[VCPU_REGS_RSI] = tss->esi;
2203 c->regs[VCPU_REGS_RDI] = tss->edi;
2204
2205 /*
2206 * SDM says that segment selectors are loaded before segment
2207 * descriptors
2208 */
2209 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2210 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2211 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2212 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2213 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2214 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2215 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2216
2217 /*
2218 * Now load segment descriptors. If fault happenes at this stage
2219 * it is handled in a context of new task
2220 */
2221 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2222 if (ret != X86EMUL_CONTINUE)
2223 return ret;
2224 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2225 if (ret != X86EMUL_CONTINUE)
2226 return ret;
2227 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2228 if (ret != X86EMUL_CONTINUE)
2229 return ret;
2230 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2231 if (ret != X86EMUL_CONTINUE)
2232 return ret;
2233 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2234 if (ret != X86EMUL_CONTINUE)
2235 return ret;
2236 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2237 if (ret != X86EMUL_CONTINUE)
2238 return ret;
2239 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2240 if (ret != X86EMUL_CONTINUE)
2241 return ret;
2242
2243 return X86EMUL_CONTINUE;
2244}
2245
2246static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2247 struct x86_emulate_ops *ops,
2248 u16 tss_selector, u16 old_tss_sel,
2249 ulong old_tss_base, struct desc_struct *new_desc)
2250{
2251 struct tss_segment_32 tss_seg;
2252 int ret;
2253 u32 err, new_tss_base = get_desc_base(new_desc);
2254
2255 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2256 &err);
2257 if (ret == X86EMUL_PROPAGATE_FAULT) {
2258 /* FIXME: need to provide precise fault address */
2259 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2260 return ret;
2261 }
2262
2263 save_state_to_tss32(ctxt, ops, &tss_seg);
2264
2265 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2266 &err);
2267 if (ret == X86EMUL_PROPAGATE_FAULT) {
2268 /* FIXME: need to provide precise fault address */
2269 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2270 return ret;
2271 }
2272
2273 ret = ops->read_std(new_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, new_tss_base, err);
2278 return ret;
2279 }
2280
2281 if (old_tss_sel != 0xffff) {
2282 tss_seg.prev_task_link = old_tss_sel;
2283
2284 ret = ops->write_std(new_tss_base,
2285 &tss_seg.prev_task_link,
2286 sizeof tss_seg.prev_task_link,
2287 ctxt->vcpu, &err);
2288 if (ret == X86EMUL_PROPAGATE_FAULT) {
2289 /* FIXME: need to provide precise fault address */
2290 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2291 return ret;
2292 }
2293 }
2294
2295 return load_state_from_tss32(ctxt, ops, &tss_seg);
2296}
2297
2298static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2299 struct x86_emulate_ops *ops,
2300 u16 tss_selector, int reason)
2301{
2302 struct desc_struct curr_tss_desc, next_tss_desc;
2303 int ret;
2304 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2305 ulong old_tss_base =
2306 get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002307 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308
2309 /* FIXME: old_tss_base == ~0 ? */
2310
2311 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2312 if (ret != X86EMUL_CONTINUE)
2313 return ret;
2314 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2315 if (ret != X86EMUL_CONTINUE)
2316 return ret;
2317
2318 /* FIXME: check that next_tss_desc is tss */
2319
2320 if (reason != TASK_SWITCH_IRET) {
2321 if ((tss_selector & 3) > next_tss_desc.dpl ||
2322 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2323 kvm_inject_gp(ctxt->vcpu, 0);
2324 return X86EMUL_PROPAGATE_FAULT;
2325 }
2326 }
2327
Gleb Natapovceffb452010-03-18 15:20:19 +02002328 desc_limit = desc_limit_scaled(&next_tss_desc);
2329 if (!next_tss_desc.p ||
2330 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2331 desc_limit < 0x2b)) {
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2333 tss_selector & 0xfffc);
2334 return X86EMUL_PROPAGATE_FAULT;
2335 }
2336
2337 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2338 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2339 write_segment_descriptor(ctxt, ops, old_tss_sel,
2340 &curr_tss_desc);
2341 }
2342
2343 if (reason == TASK_SWITCH_IRET)
2344 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2345
2346 /* set back link to prev task only if NT bit is set in eflags
2347 note that old_tss_sel is not used afetr this point */
2348 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2349 old_tss_sel = 0xffff;
2350
2351 if (next_tss_desc.type & 8)
2352 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2353 old_tss_base, &next_tss_desc);
2354 else
2355 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2356 old_tss_base, &next_tss_desc);
2357
2358 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2359 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2360
2361 if (reason != TASK_SWITCH_IRET) {
2362 next_tss_desc.type |= (1 << 1); /* set busy flag */
2363 write_segment_descriptor(ctxt, ops, tss_selector,
2364 &next_tss_desc);
2365 }
2366
2367 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2368 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2369 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2370
2371 return ret;
2372}
2373
2374int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2375 struct x86_emulate_ops *ops,
2376 u16 tss_selector, int reason)
2377{
2378 struct decode_cache *c = &ctxt->decode;
2379 int rc;
2380
2381 memset(c, 0, sizeof(struct decode_cache));
2382 c->eip = ctxt->eip;
2383 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
2384
2385 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason);
2386
2387 if (rc == X86EMUL_CONTINUE) {
2388 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
2389 kvm_rip_write(ctxt->vcpu, c->eip);
2390 }
2391
2392 return rc;
2393}
2394
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002395int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002396x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002397{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002398 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02002399 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002400 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002401 unsigned int port;
2402 int io_dir_in;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002403 int rc = X86EMUL_CONTINUE;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002404
Glauber Costa310b5d32009-05-12 16:21:06 -04002405 ctxt->interruptibility = 0;
2406
Laurent Vivier34273182007-09-18 11:27:37 +02002407 /* Shadow copy of register state. Committed on successful emulation.
2408 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
2409 * modify them.
2410 */
2411
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002412 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02002413 saved_eip = c->eip;
2414
Gleb Natapov11616242010-02-11 14:43:14 +02002415 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2416 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2417 goto done;
2418 }
2419
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002420 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002421 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002422 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2423 goto done;
2424 }
2425
Gleb Natapove92805a2010-02-10 14:21:35 +02002426 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002427 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002428 kvm_inject_gp(ctxt->vcpu, 0);
2429 goto done;
2430 }
2431
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002432 if (c->rep_prefix && (c->d & String)) {
2433 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002434 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002435 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002436 goto done;
2437 }
2438 /* The second termination condition only applies for REPE
2439 * and REPNE. Test if the repeat string operation prefix is
2440 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2441 * corresponding termination condition according to:
2442 * - if REPE/REPZ and ZF = 0 then done
2443 * - if REPNE/REPNZ and ZF = 1 then done
2444 */
2445 if ((c->b == 0xa6) || (c->b == 0xa7) ||
2446 (c->b == 0xae) || (c->b == 0xaf)) {
2447 if ((c->rep_prefix == REPE_PREFIX) &&
2448 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002449 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002450 goto done;
2451 }
2452 if ((c->rep_prefix == REPNE_PREFIX) &&
2453 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002454 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002455 goto done;
2456 }
2457 }
Gleb Natapovc73e1972010-03-15 16:38:29 +02002458 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov063db062010-03-18 15:20:06 +02002459 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002460 }
2461
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002462 if (c->src.type == OP_MEM) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002463 rc = ops->read_emulated((unsigned long)c->src.ptr,
2464 &c->src.val,
2465 c->src.bytes,
2466 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002467 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002468 goto done;
2469 c->src.orig_val = c->src.val;
2470 }
2471
Gleb Natapove35b7b92010-02-25 16:36:42 +02002472 if (c->src2.type == OP_MEM) {
Gleb Natapove35b7b92010-02-25 16:36:42 +02002473 rc = ops->read_emulated((unsigned long)c->src2.ptr,
2474 &c->src2.val,
2475 c->src2.bytes,
2476 ctxt->vcpu);
2477 if (rc != X86EMUL_CONTINUE)
2478 goto done;
2479 }
2480
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002481 if ((c->d & DstMask) == ImplicitOps)
2482 goto special_insn;
2483
2484
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002485 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2486 /* optimisation - avoid slow emulated read if Mov */
2487 rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val,
2488 c->dst.bytes, ctxt->vcpu);
2489 if (rc != X86EMUL_CONTINUE)
2490 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002491 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002492 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002493
Avi Kivity018a98d2007-11-27 19:30:56 +02002494special_insn:
2495
Laurent Viviere4e03de2007-09-18 11:52:50 +02002496 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 goto twobyte_insn;
2498
Laurent Viviere4e03de2007-09-18 11:52:50 +02002499 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 case 0x00 ... 0x05:
2501 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002502 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002504 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002505 emulate_push_sreg(ctxt, VCPU_SREG_ES);
2506 break;
2507 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002508 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002509 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002510 goto done;
2511 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 case 0x08 ... 0x0d:
2513 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002514 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002516 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002517 emulate_push_sreg(ctxt, VCPU_SREG_CS);
2518 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 case 0x10 ... 0x15:
2520 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002521 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002523 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002524 emulate_push_sreg(ctxt, VCPU_SREG_SS);
2525 break;
2526 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002527 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002528 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002529 goto done;
2530 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 case 0x18 ... 0x1d:
2532 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002533 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002535 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002536 emulate_push_sreg(ctxt, VCPU_SREG_DS);
2537 break;
2538 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002539 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002540 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002541 goto done;
2542 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002543 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002545 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 break;
2547 case 0x28 ... 0x2d:
2548 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002549 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 break;
2551 case 0x30 ... 0x35:
2552 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002553 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 break;
2555 case 0x38 ... 0x3d:
2556 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002557 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002559 case 0x40 ... 0x47: /* inc r16/r32 */
2560 emulate_1op("inc", c->dst, ctxt->eflags);
2561 break;
2562 case 0x48 ... 0x4f: /* dec r16/r32 */
2563 emulate_1op("dec", c->dst, ctxt->eflags);
2564 break;
2565 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002566 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002567 break;
2568 case 0x58 ... 0x5f: /* pop reg */
2569 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002570 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002571 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002572 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002573 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002574 case 0x60: /* pusha */
2575 emulate_pusha(ctxt);
2576 break;
2577 case 0x61: /* popa */
2578 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002579 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002580 goto done;
2581 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002583 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002585 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002587 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002588 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002589 emulate_push(ctxt);
2590 break;
2591 case 0x6c: /* insb */
2592 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002593 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2594 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2595 kvm_inject_gp(ctxt->vcpu, 0);
2596 goto done;
2597 }
2598 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002599 1,
2600 (c->d & ByteOp) ? 1 : c->op_bytes,
2601 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002602 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002603 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002604 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002605 c->regs[VCPU_REGS_RDI]),
2606 c->rep_prefix,
2607 c->regs[VCPU_REGS_RDX]) == 0) {
2608 c->eip = saved_eip;
2609 return -1;
2610 }
2611 return 0;
2612 case 0x6e: /* outsb */
2613 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002614 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2615 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2616 kvm_inject_gp(ctxt->vcpu, 0);
2617 goto done;
2618 }
Avi Kivity851ba692009-08-24 11:10:17 +03002619 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002620 0,
2621 (c->d & ByteOp) ? 1 : c->op_bytes,
2622 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002623 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002624 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002625 register_address(c,
2626 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002627 c->regs[VCPU_REGS_RSI]),
2628 c->rep_prefix,
2629 c->regs[VCPU_REGS_RDX]) == 0) {
2630 c->eip = saved_eip;
2631 return -1;
2632 }
2633 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002634 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002635 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002636 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002637 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002639 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 case 0:
2641 goto add;
2642 case 1:
2643 goto or;
2644 case 2:
2645 goto adc;
2646 case 3:
2647 goto sbb;
2648 case 4:
2649 goto and;
2650 case 5:
2651 goto sub;
2652 case 6:
2653 goto xor;
2654 case 7:
2655 goto cmp;
2656 }
2657 break;
2658 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002659 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 break;
2661 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002662 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002664 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002666 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 break;
2668 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002669 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 break;
2671 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002672 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 break; /* 64b reg: zero-extend */
2674 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002675 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 break;
2677 }
2678 /*
2679 * Write back the memory destination with implicit LOCK
2680 * prefix.
2681 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002682 c->dst.val = c->src.val;
2683 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002686 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002687 case 0x8c: { /* mov r/m, sreg */
2688 struct kvm_segment segreg;
2689
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002690 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002691 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2692 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002693 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2694 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002695 }
2696 c->dst.val = segreg.selector;
2697 break;
2698 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002699 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002700 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002701 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002702 case 0x8e: { /* mov seg, r/m16 */
2703 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002704
2705 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002706
Gleb Natapovc6975182010-02-18 12:15:01 +02002707 if (c->modrm_reg == VCPU_SREG_CS ||
2708 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002709 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2710 goto done;
2711 }
2712
Glauber Costa310b5d32009-05-12 16:21:06 -04002713 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002714 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002715
Gleb Natapov2e873022010-03-18 15:20:18 +02002716 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002717
2718 c->dst.type = OP_NONE; /* Disable writeback. */
2719 break;
2720 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002722 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002723 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002726 case 0x90: /* nop / xchg r8,rax */
2727 if (!(c->rex_prefix & 1)) { /* nop */
2728 c->dst.type = OP_NONE;
2729 break;
2730 }
2731 case 0x91 ... 0x97: /* xchg reg,rax */
2732 c->src.type = c->dst.type = OP_REG;
2733 c->src.bytes = c->dst.bytes = c->op_bytes;
2734 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2735 c->src.val = *(c->src.ptr);
2736 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002737 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002738 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002739 emulate_push(ctxt);
2740 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002741 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002742 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002743 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002744 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002745 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2746 if (rc != X86EMUL_CONTINUE)
2747 goto done;
2748 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002749 case 0xa0 ... 0xa1: /* mov */
2750 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2751 c->dst.val = c->src.val;
2752 break;
2753 case 0xa2 ... 0xa3: /* mov */
2754 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2755 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002757 c->dst.type = OP_MEM;
2758 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002759 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002760 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002761 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002762 rc = ops->read_emulated(register_address(c,
2763 seg_override_base(ctxt, c),
2764 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002765 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002766 c->dst.bytes, ctxt->vcpu);
2767 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002769 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002770 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002771 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002772 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002773 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002774 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 break;
2776 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002777 c->src.type = OP_NONE; /* Disable writeback. */
2778 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002779 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002780 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002781 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002782 rc = ops->read_emulated((unsigned long)c->src.ptr,
2783 &c->src.val,
2784 c->src.bytes,
2785 ctxt->vcpu);
2786 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002787 goto done;
2788
2789 c->dst.type = OP_NONE; /* Disable writeback. */
2790 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002791 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002792 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002793 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002794 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2795 &c->dst.val,
2796 c->dst.bytes,
2797 ctxt->vcpu);
2798 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002799 goto done;
2800
2801 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2802
2803 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2804
Harvey Harrison7a9572752008-02-19 07:40:41 -08002805 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002806 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2807 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002808 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002809 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2810 : c->dst.bytes);
2811
2812 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002814 c->dst.type = OP_MEM;
2815 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002816 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002817 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002818 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002819 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002820 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002821 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002822 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 break;
2824 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002825 c->dst.type = OP_REG;
2826 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2827 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002828 rc = ops->read_emulated(register_address(c,
2829 seg_override_base(ctxt, c),
2830 c->regs[VCPU_REGS_RSI]),
2831 &c->dst.val,
2832 c->dst.bytes,
2833 ctxt->vcpu);
2834 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002836 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002837 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002838 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 break;
2840 case 0xae ... 0xaf: /* scas */
2841 DPRINTF("Urk! I don't handle SCAS.\n");
2842 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002843 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002844 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002845 case 0xc0 ... 0xc1:
2846 emulate_grp2(ctxt);
2847 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002848 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002849 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002850 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002851 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002852 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002853 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2854 mov:
2855 c->dst.val = c->src.val;
2856 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002857 case 0xcb: /* ret far */
2858 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002859 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002860 goto done;
2861 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002862 case 0xd0 ... 0xd1: /* Grp2 */
2863 c->src.val = 1;
2864 emulate_grp2(ctxt);
2865 break;
2866 case 0xd2 ... 0xd3: /* Grp2 */
2867 c->src.val = c->regs[VCPU_REGS_RCX];
2868 emulate_grp2(ctxt);
2869 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002870 case 0xe4: /* inb */
2871 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002872 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002873 io_dir_in = 1;
2874 goto do_io;
2875 case 0xe6: /* outb */
2876 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002877 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002878 io_dir_in = 0;
2879 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002880 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002881 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002882 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002883 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002884 emulate_push(ctxt);
2885 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002886 }
2887 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002888 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002889 case 0xea: /* jmp far */
Gleb Natapovea79849d2010-02-25 16:36:43 +02002890 jump_far:
Gleb Natapov2e873022010-03-18 15:20:18 +02002891 if (load_segment_descriptor(ctxt, ops, c->src2.val,
2892 VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002893 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002894
Gleb Natapov782b8772009-04-12 13:36:25 +03002895 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002896 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002897 case 0xeb:
2898 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002899 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002900 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002901 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002902 case 0xec: /* in al,dx */
2903 case 0xed: /* in (e/r)ax,dx */
2904 port = c->regs[VCPU_REGS_RDX];
2905 io_dir_in = 1;
2906 goto do_io;
2907 case 0xee: /* out al,dx */
2908 case 0xef: /* out (e/r)ax,dx */
2909 port = c->regs[VCPU_REGS_RDX];
2910 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002911 do_io:
2912 if (!emulator_io_permited(ctxt, ops, port,
2913 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2914 kvm_inject_gp(ctxt->vcpu, 0);
2915 goto done;
2916 }
2917 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002918 (c->d & ByteOp) ? 1 : c->op_bytes,
2919 port) != 0) {
2920 c->eip = saved_eip;
2921 goto cannot_emulate;
2922 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002923 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002924 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002925 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002926 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002927 case 0xf5: /* cmc */
2928 /* complement carry flag from eflags reg */
2929 ctxt->eflags ^= EFLG_CF;
2930 c->dst.type = OP_NONE; /* Disable writeback. */
2931 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002932 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002933 if (!emulate_grp3(ctxt, ops))
2934 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002935 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002936 case 0xf8: /* clc */
2937 ctxt->eflags &= ~EFLG_CF;
2938 c->dst.type = OP_NONE; /* Disable writeback. */
2939 break;
2940 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002941 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002942 kvm_inject_gp(ctxt->vcpu, 0);
2943 else {
2944 ctxt->eflags &= ~X86_EFLAGS_IF;
2945 c->dst.type = OP_NONE; /* Disable writeback. */
2946 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002947 break;
2948 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002949 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002950 kvm_inject_gp(ctxt->vcpu, 0);
2951 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002952 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002953 ctxt->eflags |= X86_EFLAGS_IF;
2954 c->dst.type = OP_NONE; /* Disable writeback. */
2955 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002956 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002957 case 0xfc: /* cld */
2958 ctxt->eflags &= ~EFLG_DF;
2959 c->dst.type = OP_NONE; /* Disable writeback. */
2960 break;
2961 case 0xfd: /* std */
2962 ctxt->eflags |= EFLG_DF;
2963 c->dst.type = OP_NONE; /* Disable writeback. */
2964 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002965 case 0xfe: /* Grp4 */
2966 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002967 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002968 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002969 goto done;
2970 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002971 case 0xff: /* Grp5 */
2972 if (c->modrm_reg == 5)
2973 goto jump_far;
2974 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002976
2977writeback:
2978 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002979 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002980 goto done;
2981
2982 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002983 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002984 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002985
2986done:
2987 if (rc == X86EMUL_UNHANDLEABLE) {
2988 c->eip = saved_eip;
2989 return -1;
2990 }
2991 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992
2993twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002994 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002996 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997 u16 size;
2998 unsigned long address;
2999
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003000 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003001 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003002 goto cannot_emulate;
3003
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003004 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003005 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003006 goto done;
3007
Avi Kivity33e38852008-05-21 15:34:25 +03003008 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003009 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003010 /* Disable writeback. */
3011 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003012 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003014 rc = read_descriptor(ctxt, ops, c->src.ptr,
3015 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003016 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 goto done;
3018 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003019 /* Disable writeback. */
3020 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003022 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003023 if (c->modrm_mod == 3) {
3024 switch (c->modrm_rm) {
3025 case 1:
3026 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003027 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003028 goto done;
3029 break;
3030 default:
3031 goto cannot_emulate;
3032 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003033 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003034 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003035 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003036 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003037 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003038 goto done;
3039 realmode_lidt(ctxt->vcpu, size, address);
3040 }
Avi Kivity16286d02008-04-14 14:40:50 +03003041 /* Disable writeback. */
3042 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 break;
3044 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003045 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003046 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 break;
3048 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003049 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3050 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003051 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003053 case 5: /* not defined */
3054 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3055 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003057 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003058 /* Disable writeback. */
3059 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 break;
3061 default:
3062 goto cannot_emulate;
3063 }
3064 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003065 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003066 rc = emulate_syscall(ctxt);
3067 if (rc != X86EMUL_CONTINUE)
3068 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003069 else
3070 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003071 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003072 case 0x06:
3073 emulate_clts(ctxt->vcpu);
3074 c->dst.type = OP_NONE;
3075 break;
3076 case 0x08: /* invd */
3077 case 0x09: /* wbinvd */
3078 case 0x0d: /* GrpP (prefetch) */
3079 case 0x18: /* Grp16 (prefetch/nop) */
3080 c->dst.type = OP_NONE;
3081 break;
3082 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003083 switch (c->modrm_reg) {
3084 case 1:
3085 case 5 ... 7:
3086 case 9 ... 15:
3087 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3088 goto done;
3089 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003090 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003091 c->dst.type = OP_NONE; /* no writeback */
3092 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003094 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3095 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3096 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3097 goto done;
3098 }
3099 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003100 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003102 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02003103 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003104 c->dst.type = OP_NONE;
3105 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003107 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3108 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3109 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3110 goto done;
3111 }
3112 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003113 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003115 case 0x30:
3116 /* wrmsr */
3117 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3118 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003119 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003120 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003121 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003122 }
3123 rc = X86EMUL_CONTINUE;
3124 c->dst.type = OP_NONE;
3125 break;
3126 case 0x32:
3127 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003128 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003129 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003130 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003131 } else {
3132 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3133 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3134 }
3135 rc = X86EMUL_CONTINUE;
3136 c->dst.type = OP_NONE;
3137 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003138 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003139 rc = emulate_sysenter(ctxt);
3140 if (rc != X86EMUL_CONTINUE)
3141 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003142 else
3143 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003144 break;
3145 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003146 rc = emulate_sysexit(ctxt);
3147 if (rc != X86EMUL_CONTINUE)
3148 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003149 else
3150 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003153 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003154 if (!test_cc(c->b, ctxt->eflags))
3155 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003157 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003158 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003159 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003160 c->dst.type = OP_NONE;
3161 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003162 case 0xa0: /* push fs */
3163 emulate_push_sreg(ctxt, VCPU_SREG_FS);
3164 break;
3165 case 0xa1: /* pop fs */
3166 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003167 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003168 goto done;
3169 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003170 case 0xa3:
3171 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003172 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003173 /* only subword offset */
3174 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003175 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003176 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003177 case 0xa4: /* shld imm8, r, r/m */
3178 case 0xa5: /* shld cl, r, r/m */
3179 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3180 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003181 case 0xa8: /* push gs */
3182 emulate_push_sreg(ctxt, VCPU_SREG_GS);
3183 break;
3184 case 0xa9: /* pop gs */
3185 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003186 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003187 goto done;
3188 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003189 case 0xab:
3190 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003191 /* only subword offset */
3192 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003193 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003194 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003195 case 0xac: /* shrd imm8, r, r/m */
3196 case 0xad: /* shrd cl, r, r/m */
3197 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3198 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003199 case 0xae: /* clflush */
3200 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 case 0xb0 ... 0xb1: /* cmpxchg */
3202 /*
3203 * Save real source value, then compare EAX against
3204 * destination.
3205 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003206 c->src.orig_val = c->src.val;
3207 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003208 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3209 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003211 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 } else {
3213 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003214 c->dst.type = OP_REG;
3215 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 }
3217 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 case 0xb3:
3219 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003220 /* only subword offset */
3221 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003222 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003225 c->dst.bytes = c->op_bytes;
3226 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3227 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003230 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 case 0:
3232 goto bt;
3233 case 1:
3234 goto bts;
3235 case 2:
3236 goto btr;
3237 case 3:
3238 goto btc;
3239 }
3240 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003241 case 0xbb:
3242 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003243 /* only subword offset */
3244 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003245 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003246 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003248 c->dst.bytes = c->op_bytes;
3249 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3250 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003252 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003253 c->dst.bytes = c->op_bytes;
3254 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3255 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003256 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003258 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003259 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003260 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003261 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003262 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 }
3264 goto writeback;
3265
3266cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003267 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02003268 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 return -1;
3270}