blob: 6de6ad1610d867793fa5eb0e177c9ee2ae5b2a06 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020036#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038/*
39 * Opcode effective-address decode tables.
40 * Note that we only emulate instructions that have at least one memory
41 * operand (excluding implicit stack references). We assume that stack
42 * references and instruction fetches will never occur in special memory
43 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
44 * not be handled.
45 */
46
47/* Operand sizes: 8-bit operands or specified/overridden size. */
48#define ByteOp (1<<0) /* 8-bit operands. */
49/* Destination operand type. */
50#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
51#define DstReg (2<<1) /* Register operand. */
52#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define DstAcc (4<<1) /* Destination Accumulator */
Gleb Natapova682e352010-03-18 15:20:21 +020054#define DstDI (5<<1) /* Destination is in ES:(E)DI */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080056/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020057#define SrcNone (0<<4) /* No source operand. */
58#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
59#define SrcReg (1<<4) /* Register operand. */
60#define SrcMem (2<<4) /* Memory operand. */
61#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
62#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
63#define SrcImm (5<<4) /* Immediate operand. */
64#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010065#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030067#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020068#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030073#define Mov (1<<9)
74#define BitOp (1<<10)
75#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020076#define String (1<<12) /* String instruction (rep capable) */
77#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020078#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
79#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
80#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020082#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020083#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010085/* Source 2 operand type */
86#define Src2None (0<<29)
87#define Src2CL (1<<29)
88#define Src2ImmByte (2<<29)
89#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030090#define Src2Imm16 (4<<29)
Gleb Natapove35b7b92010-02-25 16:36:42 +020091#define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be
92 in memory and second argument is located
93 immediately after the first one in memory. */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010094#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Avi Kivity43bb19c2008-01-18 12:46:50 +020096enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020097 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020098 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020099 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200100};
101
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100102static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200104 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300106 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300107 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200109 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200111 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
112 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200114 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300116 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300117 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200119 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300122 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200124 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200126 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200128 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
130 0, 0, 0, 0,
131 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200132 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
134 0, 0, 0, 0,
135 /* 0x38 - 0x3F */
136 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
137 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200138 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
139 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700140 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200141 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700142 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200143 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300144 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200145 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
146 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300147 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200148 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
149 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700150 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200151 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
152 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700153 0, 0, 0, 0,
154 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300155 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200156 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
157 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300158 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300159 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
160 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300161 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300162 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
163 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200165 Group | Group1_80, Group | Group1_81,
166 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200168 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0x88 - 0x8F */
170 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
171 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200172 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200173 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300174 /* 0x90 - 0x97 */
175 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
176 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300177 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300178 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200180 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
181 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200182 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
183 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 /* 0xA8 - 0xAF */
Gleb Natapova682e352010-03-18 15:20:21 +0200185 0, 0, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
186 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
187 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300188 /* 0xB0 - 0xB7 */
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
191 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
192 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
193 /* 0xB8 - 0xBF */
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
195 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
196 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
197 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300199 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200200 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300201 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300203 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300204 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xD0 - 0xD7 */
206 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
207 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
208 0, 0, 0, 0,
209 /* 0xD8 - 0xDF */
210 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300211 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300212 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200213 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
214 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300215 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300216 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300217 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200218 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
219 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xF0 - 0xF7 */
221 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200222 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700224 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300225 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226};
227
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100228static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200230 0, Group | GroupDual | Group7, 0, 0,
231 0, ImplicitOps, ImplicitOps | Priv, 0,
232 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
233 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0x10 - 0x1F */
235 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
236 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200237 ModRM | ImplicitOps | Priv, ModRM | Priv,
238 ModRM | ImplicitOps | Priv, ModRM | Priv,
239 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 0, 0, 0, 0, 0, 0, 0, 0,
241 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200242 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
243 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200244 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 /* 0x40 - 0x47 */
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
250 /* 0x48 - 0x4F */
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
253 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
254 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
255 /* 0x50 - 0x5F */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 /* 0x60 - 0x6F */
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259 /* 0x70 - 0x7F */
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
261 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300262 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
263 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264 /* 0x90 - 0x9F */
265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300267 ImplicitOps | Stack, ImplicitOps | Stack,
268 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100269 DstMem | SrcReg | Src2ImmByte | ModRM,
270 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300272 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200273 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100274 DstMem | SrcReg | Src2ImmByte | ModRM,
275 DstMem | SrcReg | Src2CL | ModRM,
276 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200278 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
279 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
281 DstReg | SrcMem16 | ModRM | Mov,
282 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200283 0, 0,
284 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
286 DstReg | SrcMem16 | ModRM | Mov,
287 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200288 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
289 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800290 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 /* 0xD0 - 0xDF */
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293 /* 0xE0 - 0xEF */
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
295 /* 0xF0 - 0xFF */
296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
297};
298
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100299static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200300 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM | Lock,
306 ByteOp | DstMem | SrcImm | ModRM | Lock,
307 ByteOp | DstMem | SrcImm | ModRM | Lock,
308 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200309 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM | Lock,
315 DstMem | SrcImm | ModRM | Lock,
316 DstMem | SrcImm | ModRM | Lock,
317 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200318 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
324 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
325 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
326 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200327 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM | Lock,
333 DstMem | SrcImmByte | ModRM | Lock,
334 DstMem | SrcImmByte | ModRM | Lock,
335 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200336 [Group1A*8] =
337 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200338 [Group3_Byte*8] =
339 ByteOp | SrcImm | DstMem | ModRM, 0,
340 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
341 0, 0, 0, 0,
342 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400343 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300344 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200345 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200346 [Group4*8] =
347 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
348 0, 0, 0, 0, 0, 0,
349 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300350 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
351 SrcMem | ModRM | Stack, 0,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200352 SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps,
353 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200354 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200355 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300356 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200357 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200358 [Group8*8] =
359 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200360 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
361 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200362 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200363 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200364};
365
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100366static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200367 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200368 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300369 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200370 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200371 [Group9*8] =
372 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200373};
374
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200376#define EFLG_ID (1<<21)
377#define EFLG_VIP (1<<20)
378#define EFLG_VIF (1<<19)
379#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200380#define EFLG_VM (1<<17)
381#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200382#define EFLG_IOPL (3<<12)
383#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384#define EFLG_OF (1<<11)
385#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200386#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200387#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388#define EFLG_SF (1<<7)
389#define EFLG_ZF (1<<6)
390#define EFLG_AF (1<<4)
391#define EFLG_PF (1<<2)
392#define EFLG_CF (1<<0)
393
394/*
395 * Instruction emulation:
396 * Most instructions are emulated directly via a fragment of inline assembly
397 * code. This allows us to save/restore EFLAGS and thus very easily pick up
398 * any modified flags.
399 */
400
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800401#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402#define _LO32 "k" /* force 32-bit operand */
403#define _STK "%%rsp" /* stack pointer */
404#elif defined(__i386__)
405#define _LO32 "" /* force 32-bit operand */
406#define _STK "%%esp" /* stack pointer */
407#endif
408
409/*
410 * These EFLAGS bits are restored from saved value during emulation, and
411 * any changes are written back to the saved value after emulation.
412 */
413#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
414
415/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200416#define _PRE_EFLAGS(_sav, _msk, _tmp) \
417 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
418 "movl %"_sav",%"_LO32 _tmp"; " \
419 "push %"_tmp"; " \
420 "push %"_tmp"; " \
421 "movl %"_msk",%"_LO32 _tmp"; " \
422 "andl %"_LO32 _tmp",("_STK"); " \
423 "pushf; " \
424 "notl %"_LO32 _tmp"; " \
425 "andl %"_LO32 _tmp",("_STK"); " \
426 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
427 "pop %"_tmp"; " \
428 "orl %"_LO32 _tmp",("_STK"); " \
429 "popf; " \
430 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431
432/* After executing instruction: write-back necessary bits in EFLAGS. */
433#define _POST_EFLAGS(_sav, _msk, _tmp) \
434 /* _sav |= EFLAGS & _msk; */ \
435 "pushf; " \
436 "pop %"_tmp"; " \
437 "andl %"_msk",%"_LO32 _tmp"; " \
438 "orl %"_LO32 _tmp",%"_sav"; "
439
Avi Kivitydda96d82008-11-26 15:14:10 +0200440#ifdef CONFIG_X86_64
441#define ON64(x) x
442#else
443#define ON64(x)
444#endif
445
Avi Kivity6b7ad612008-11-26 15:30:45 +0200446#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
447 do { \
448 __asm__ __volatile__ ( \
449 _PRE_EFLAGS("0", "4", "2") \
450 _op _suffix " %"_x"3,%1; " \
451 _POST_EFLAGS("0", "4", "2") \
452 : "=m" (_eflags), "=m" ((_dst).val), \
453 "=&r" (_tmp) \
454 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200455 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200456
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458/* Raw emulation: instruction has two explicit operands. */
459#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200460 do { \
461 unsigned long _tmp; \
462 \
463 switch ((_dst).bytes) { \
464 case 2: \
465 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
466 break; \
467 case 4: \
468 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
469 break; \
470 case 8: \
471 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
472 break; \
473 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 } while (0)
475
476#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
477 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200478 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400479 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200481 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 break; \
483 default: \
484 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
485 _wx, _wy, _lx, _ly, _qx, _qy); \
486 break; \
487 } \
488 } while (0)
489
490/* Source operand is byte-sized and may be restricted to just %cl. */
491#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
492 __emulate_2op(_op, _src, _dst, _eflags, \
493 "b", "c", "b", "c", "b", "c", "b", "c")
494
495/* Source operand is byte, word, long or quad sized. */
496#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
497 __emulate_2op(_op, _src, _dst, _eflags, \
498 "b", "q", "w", "r", _LO32, "r", "", "r")
499
500/* Source operand is word, long or quad sized. */
501#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
502 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
503 "w", "r", _LO32, "r", "", "r")
504
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100505/* Instruction has three operands and one operand is stored in ECX register */
506#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
507 do { \
508 unsigned long _tmp; \
509 _type _clv = (_cl).val; \
510 _type _srcv = (_src).val; \
511 _type _dstv = (_dst).val; \
512 \
513 __asm__ __volatile__ ( \
514 _PRE_EFLAGS("0", "5", "2") \
515 _op _suffix " %4,%1 \n" \
516 _POST_EFLAGS("0", "5", "2") \
517 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
518 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
519 ); \
520 \
521 (_cl).val = (unsigned long) _clv; \
522 (_src).val = (unsigned long) _srcv; \
523 (_dst).val = (unsigned long) _dstv; \
524 } while (0)
525
526#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
527 do { \
528 switch ((_dst).bytes) { \
529 case 2: \
530 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
531 "w", unsigned short); \
532 break; \
533 case 4: \
534 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
535 "l", unsigned int); \
536 break; \
537 case 8: \
538 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
539 "q", unsigned long)); \
540 break; \
541 } \
542 } while (0)
543
Avi Kivitydda96d82008-11-26 15:14:10 +0200544#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 do { \
546 unsigned long _tmp; \
547 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200548 __asm__ __volatile__ ( \
549 _PRE_EFLAGS("0", "3", "2") \
550 _op _suffix " %1; " \
551 _POST_EFLAGS("0", "3", "2") \
552 : "=m" (_eflags), "+m" ((_dst).val), \
553 "=&r" (_tmp) \
554 : "i" (EFLAGS_MASK)); \
555 } while (0)
556
557/* Instruction has only one explicit operand (no source operand). */
558#define emulate_1op(_op, _dst, _eflags) \
559 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400560 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200561 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
562 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
563 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
564 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 } \
566 } while (0)
567
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568/* Fetch next part of the instruction being emulated. */
569#define insn_fetch(_type, _size, _eip) \
570({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200571 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200572 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 goto done; \
574 (_eip) += (_size); \
575 (_type)_x; \
576})
577
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800578static inline unsigned long ad_mask(struct decode_cache *c)
579{
580 return (1UL << (c->ad_bytes << 3)) - 1;
581}
582
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800584static inline unsigned long
585address_mask(struct decode_cache *c, unsigned long reg)
586{
587 if (c->ad_bytes == sizeof(unsigned long))
588 return reg;
589 else
590 return reg & ad_mask(c);
591}
592
593static inline unsigned long
594register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
595{
596 return base + address_mask(c, reg);
597}
598
Harvey Harrison7a9572752008-02-19 07:40:41 -0800599static inline void
600register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
601{
602 if (c->ad_bytes == sizeof(unsigned long))
603 *reg += inc;
604 else
605 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
606}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607
Harvey Harrison7a9572752008-02-19 07:40:41 -0800608static inline void jmp_rel(struct decode_cache *c, int rel)
609{
610 register_address_increment(c, &c->eip, rel);
611}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300612
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300613static void set_seg_override(struct decode_cache *c, int seg)
614{
615 c->has_seg_override = true;
616 c->seg_override = seg;
617}
618
619static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
620{
621 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
622 return 0;
623
624 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
625}
626
627static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
628 struct decode_cache *c)
629{
630 if (!c->has_seg_override)
631 return 0;
632
633 return seg_base(ctxt, c->seg_override);
634}
635
636static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
637{
638 return seg_base(ctxt, VCPU_SREG_ES);
639}
640
641static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
642{
643 return seg_base(ctxt, VCPU_SREG_SS);
644}
645
Avi Kivity62266862007-11-20 13:15:52 +0200646static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
647 struct x86_emulate_ops *ops,
648 unsigned long linear, u8 *dest)
649{
650 struct fetch_cache *fc = &ctxt->decode.fetch;
651 int rc;
652 int size;
653
654 if (linear < fc->start || linear >= fc->end) {
655 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200656 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900657 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200658 return rc;
659 fc->start = linear;
660 fc->end = linear + size;
661 }
662 *dest = fc->data[linear - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200664}
665
666static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
667 struct x86_emulate_ops *ops,
668 unsigned long eip, void *dest, unsigned size)
669{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900670 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200671
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200673 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200674 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200675 eip += ctxt->cs_base;
676 while (size--) {
677 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200679 return rc;
680 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900681 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200682}
683
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000684/*
685 * Given the 'reg' portion of a ModRM byte, and a register block, return a
686 * pointer into the block that addresses the relevant register.
687 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
688 */
689static void *decode_register(u8 modrm_reg, unsigned long *regs,
690 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691{
692 void *p;
693
694 p = &regs[modrm_reg];
695 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
696 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
697 return p;
698}
699
700static int read_descriptor(struct x86_emulate_ctxt *ctxt,
701 struct x86_emulate_ops *ops,
702 void *ptr,
703 u16 *size, unsigned long *address, int op_bytes)
704{
705 int rc;
706
707 if (op_bytes == 2)
708 op_bytes = 3;
709 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300710 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200711 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900712 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300714 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200715 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 return rc;
717}
718
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300719static int test_cc(unsigned int condition, unsigned int flags)
720{
721 int rc = 0;
722
723 switch ((condition & 15) >> 1) {
724 case 0: /* o */
725 rc |= (flags & EFLG_OF);
726 break;
727 case 1: /* b/c/nae */
728 rc |= (flags & EFLG_CF);
729 break;
730 case 2: /* z/e */
731 rc |= (flags & EFLG_ZF);
732 break;
733 case 3: /* be/na */
734 rc |= (flags & (EFLG_CF|EFLG_ZF));
735 break;
736 case 4: /* s */
737 rc |= (flags & EFLG_SF);
738 break;
739 case 5: /* p/pe */
740 rc |= (flags & EFLG_PF);
741 break;
742 case 7: /* le/ng */
743 rc |= (flags & EFLG_ZF);
744 /* fall through */
745 case 6: /* l/nge */
746 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
747 break;
748 }
749
750 /* Odd condition identifiers (lsb == 1) have inverted sense. */
751 return (!!rc ^ (condition & 1));
752}
753
Avi Kivity3c118e22007-10-31 10:27:04 +0200754static void decode_register_operand(struct operand *op,
755 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200756 int inhibit_bytereg)
757{
Avi Kivity33615aa2007-10-31 11:15:56 +0200758 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200759 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200760
761 if (!(c->d & ModRM))
762 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->type = OP_REG;
764 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200765 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200766 op->val = *(u8 *)op->ptr;
767 op->bytes = 1;
768 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200769 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200770 op->bytes = c->op_bytes;
771 switch (op->bytes) {
772 case 2:
773 op->val = *(u16 *)op->ptr;
774 break;
775 case 4:
776 op->val = *(u32 *)op->ptr;
777 break;
778 case 8:
779 op->val = *(u64 *) op->ptr;
780 break;
781 }
782 }
783 op->orig_val = op->val;
784}
785
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786static int decode_modrm(struct x86_emulate_ctxt *ctxt,
787 struct x86_emulate_ops *ops)
788{
789 struct decode_cache *c = &ctxt->decode;
790 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700791 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900792 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200793
794 if (c->rex_prefix) {
795 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
796 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
797 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
798 }
799
800 c->modrm = insn_fetch(u8, 1, c->eip);
801 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
802 c->modrm_reg |= (c->modrm & 0x38) >> 3;
803 c->modrm_rm |= (c->modrm & 0x07);
804 c->modrm_ea = 0;
805 c->use_modrm_ea = 1;
806
807 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300808 c->modrm_ptr = decode_register(c->modrm_rm,
809 c->regs, c->d & ByteOp);
810 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200811 return rc;
812 }
813
814 if (c->ad_bytes == 2) {
815 unsigned bx = c->regs[VCPU_REGS_RBX];
816 unsigned bp = c->regs[VCPU_REGS_RBP];
817 unsigned si = c->regs[VCPU_REGS_RSI];
818 unsigned di = c->regs[VCPU_REGS_RDI];
819
820 /* 16-bit ModR/M decode. */
821 switch (c->modrm_mod) {
822 case 0:
823 if (c->modrm_rm == 6)
824 c->modrm_ea += insn_fetch(u16, 2, c->eip);
825 break;
826 case 1:
827 c->modrm_ea += insn_fetch(s8, 1, c->eip);
828 break;
829 case 2:
830 c->modrm_ea += insn_fetch(u16, 2, c->eip);
831 break;
832 }
833 switch (c->modrm_rm) {
834 case 0:
835 c->modrm_ea += bx + si;
836 break;
837 case 1:
838 c->modrm_ea += bx + di;
839 break;
840 case 2:
841 c->modrm_ea += bp + si;
842 break;
843 case 3:
844 c->modrm_ea += bp + di;
845 break;
846 case 4:
847 c->modrm_ea += si;
848 break;
849 case 5:
850 c->modrm_ea += di;
851 break;
852 case 6:
853 if (c->modrm_mod != 0)
854 c->modrm_ea += bp;
855 break;
856 case 7:
857 c->modrm_ea += bx;
858 break;
859 }
860 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
861 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300862 if (!c->has_seg_override)
863 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200864 c->modrm_ea = (u16)c->modrm_ea;
865 } else {
866 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700867 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200868 sib = insn_fetch(u8, 1, c->eip);
869 index_reg |= (sib >> 3) & 7;
870 base_reg |= sib & 7;
871 scale = sib >> 6;
872
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700873 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
874 c->modrm_ea += insn_fetch(s32, 4, c->eip);
875 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700877 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200878 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700879 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
880 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700881 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700882 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200883 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200884 switch (c->modrm_mod) {
885 case 0:
886 if (c->modrm_rm == 5)
887 c->modrm_ea += insn_fetch(s32, 4, c->eip);
888 break;
889 case 1:
890 c->modrm_ea += insn_fetch(s8, 1, c->eip);
891 break;
892 case 2:
893 c->modrm_ea += insn_fetch(s32, 4, c->eip);
894 break;
895 }
896 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897done:
898 return rc;
899}
900
901static int decode_abs(struct x86_emulate_ctxt *ctxt,
902 struct x86_emulate_ops *ops)
903{
904 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900905 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906
907 switch (c->ad_bytes) {
908 case 2:
909 c->modrm_ea = insn_fetch(u16, 2, c->eip);
910 break;
911 case 4:
912 c->modrm_ea = insn_fetch(u32, 4, c->eip);
913 break;
914 case 8:
915 c->modrm_ea = insn_fetch(u64, 8, c->eip);
916 break;
917 }
918done:
919 return rc;
920}
921
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200923x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200925 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900926 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200928 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
Gleb Natapov5cd21912010-03-18 15:20:26 +0200931 /* we cannot decode insn before we complete previous rep insn */
932 WARN_ON(ctxt->restart);
933
934 /* Shadow copy of register state. Committed on successful emulation. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200935 memset(c, 0, sizeof(struct decode_cache));
Gleb Natapov063db062010-03-18 15:20:06 +0200936 c->eip = ctxt->eip;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300937 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800938 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939
940 switch (mode) {
941 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200942 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200944 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 break;
946 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200947 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800949#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200951 def_op_bytes = 4;
952 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 break;
954#endif
955 default:
956 return -1;
957 }
958
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200959 c->op_bytes = def_op_bytes;
960 c->ad_bytes = def_ad_bytes;
961
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200963 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200966 /* switch between 2/4 bytes */
967 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 break;
969 case 0x67: /* address-size override */
970 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200971 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200972 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200974 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200975 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300978 case 0x2e: /* CS override */
979 case 0x36: /* SS override */
980 case 0x3e: /* DS override */
981 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 break;
983 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300985 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200987 case 0x40 ... 0x4f: /* REX */
988 if (mode != X86EMUL_MODE_PROT64)
989 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200990 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200991 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200993 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200995 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100996 c->rep_prefix = REPNE_PREFIX;
997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100999 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 default:
1002 goto done_prefixes;
1003 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001004
1005 /* Any legacy prefix after a REX prefix nullifies its effect. */
1006
Avi Kivity33615aa2007-10-31 11:15:56 +02001007 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 }
1009
1010done_prefixes:
1011
1012 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001013 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001014 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016
1017 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 c->d = opcode_table[c->b];
1019 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001021 if (c->b == 0x0f) {
1022 c->twobyte = 1;
1023 c->b = insn_fetch(u8, 1, c->eip);
1024 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001026 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027
Avi Kivitye09d0822008-01-18 12:38:59 +02001028 if (c->d & Group) {
1029 group = c->d & GroupMask;
1030 c->modrm = insn_fetch(u8, 1, c->eip);
1031 --c->eip;
1032
1033 group = (group << 3) + ((c->modrm >> 3) & 7);
1034 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1035 c->d = group2_table[group];
1036 else
1037 c->d = group_table[group];
1038 }
1039
1040 /* Unrecognised? */
1041 if (c->d == 0) {
1042 DPRINTF("Cannot emulate %02x\n", c->b);
1043 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 }
1045
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001046 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1047 c->op_bytes = 8;
1048
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001050 if (c->d & ModRM)
1051 rc = decode_modrm(ctxt, ops);
1052 else if (c->d & MemAbs)
1053 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001054 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001055 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001057 if (!c->has_seg_override)
1058 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001059
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001060 if (!(!c->twobyte && c->b == 0x8d))
1061 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001062
1063 if (c->ad_bytes != 8)
1064 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001065
1066 if (c->rip_relative)
1067 c->modrm_ea += c->eip;
1068
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 /*
1070 * Decode and fetch the source operand: register, memory
1071 * or immediate.
1072 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001073 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 case SrcNone:
1075 break;
1076 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001077 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 break;
1079 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001080 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 goto srcmem_common;
1082 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001083 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 goto srcmem_common;
1085 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001086 c->src.bytes = (c->d & ByteOp) ? 1 :
1087 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001088 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001089 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001090 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001091 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001092 /*
1093 * For instructions with a ModR/M byte, switch to register
1094 * access if Mod = 3.
1095 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001096 if ((c->d & ModRM) && c->modrm_mod == 3) {
1097 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001098 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001099 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001100 break;
1101 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001102 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001103 c->src.ptr = (unsigned long *)c->modrm_ea;
1104 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 break;
1106 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001107 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001108 c->src.type = OP_IMM;
1109 c->src.ptr = (unsigned long *)c->eip;
1110 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1111 if (c->src.bytes == 8)
1112 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001114 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 break;
1118 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 break;
1121 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001122 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 break;
1124 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001125 if ((c->d & SrcMask) == SrcImmU) {
1126 switch (c->src.bytes) {
1127 case 1:
1128 c->src.val &= 0xff;
1129 break;
1130 case 2:
1131 c->src.val &= 0xffff;
1132 break;
1133 case 4:
1134 c->src.val &= 0xffffffff;
1135 break;
1136 }
1137 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 break;
1139 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001140 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001141 c->src.type = OP_IMM;
1142 c->src.ptr = (unsigned long *)c->eip;
1143 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001144 if ((c->d & SrcMask) == SrcImmByte)
1145 c->src.val = insn_fetch(s8, 1, c->eip);
1146 else
1147 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001149 case SrcOne:
1150 c->src.bytes = 1;
1151 c->src.val = 1;
1152 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001153 case SrcSI:
1154 c->src.type = OP_MEM;
1155 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1156 c->src.ptr = (unsigned long *)
1157 register_address(c, seg_override_base(ctxt, c),
1158 c->regs[VCPU_REGS_RSI]);
1159 c->src.val = 0;
1160 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 }
1162
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001163 /*
1164 * Decode and fetch the second source operand: register, memory
1165 * or immediate.
1166 */
1167 switch (c->d & Src2Mask) {
1168 case Src2None:
1169 break;
1170 case Src2CL:
1171 c->src2.bytes = 1;
1172 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1173 break;
1174 case Src2ImmByte:
1175 c->src2.type = OP_IMM;
1176 c->src2.ptr = (unsigned long *)c->eip;
1177 c->src2.bytes = 1;
1178 c->src2.val = insn_fetch(u8, 1, c->eip);
1179 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001180 case Src2Imm16:
1181 c->src2.type = OP_IMM;
1182 c->src2.ptr = (unsigned long *)c->eip;
1183 c->src2.bytes = 2;
1184 c->src2.val = insn_fetch(u16, 2, c->eip);
1185 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001186 case Src2One:
1187 c->src2.bytes = 1;
1188 c->src2.val = 1;
1189 break;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001190 case Src2Mem16:
Gleb Natapove35b7b92010-02-25 16:36:42 +02001191 c->src2.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001192 c->src2.bytes = 2;
1193 c->src2.ptr = (unsigned long *)(c->modrm_ea + c->src.bytes);
1194 c->src2.val = 0;
Gleb Natapove35b7b92010-02-25 16:36:42 +02001195 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001196 }
1197
Avi Kivity038e51d2007-01-22 20:40:40 -08001198 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001199 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001200 case ImplicitOps:
1201 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001202 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001203 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001204 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001205 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001206 break;
1207 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001208 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001209 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001210 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001211 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001212 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001213 break;
1214 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001215 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001216 c->dst.ptr = (unsigned long *)c->modrm_ea;
1217 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1218 c->dst.val = 0;
1219 if (c->d & BitOp) {
1220 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1221
1222 c->dst.ptr = (void *)c->dst.ptr +
1223 (c->src.val & mask) / 8;
1224 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001225 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001226 case DstAcc:
1227 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001228 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001229 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001230 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001231 case 1:
1232 c->dst.val = *(u8 *)c->dst.ptr;
1233 break;
1234 case 2:
1235 c->dst.val = *(u16 *)c->dst.ptr;
1236 break;
1237 case 4:
1238 c->dst.val = *(u32 *)c->dst.ptr;
1239 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001240 case 8:
1241 c->dst.val = *(u64 *)c->dst.ptr;
1242 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001243 }
1244 c->dst.orig_val = c->dst.val;
1245 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001246 case DstDI:
1247 c->dst.type = OP_MEM;
1248 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1249 c->dst.ptr = (unsigned long *)
1250 register_address(c, es_base(ctxt),
1251 c->regs[VCPU_REGS_RDI]);
1252 c->dst.val = 0;
1253 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001254 }
1255
1256done:
1257 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1258}
1259
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001260static u32 desc_limit_scaled(struct desc_struct *desc)
1261{
1262 u32 limit = get_desc_limit(desc);
1263
1264 return desc->g ? (limit << 12) | 0xfff : limit;
1265}
1266
1267static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1268 struct x86_emulate_ops *ops,
1269 u16 selector, struct desc_ptr *dt)
1270{
1271 if (selector & 1 << 2) {
1272 struct desc_struct desc;
1273 memset (dt, 0, sizeof *dt);
1274 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1275 return;
1276
1277 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1278 dt->address = get_desc_base(&desc);
1279 } else
1280 ops->get_gdt(dt, ctxt->vcpu);
1281}
1282
1283/* allowed just for 8 bytes segments */
1284static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1285 struct x86_emulate_ops *ops,
1286 u16 selector, struct desc_struct *desc)
1287{
1288 struct desc_ptr dt;
1289 u16 index = selector >> 3;
1290 int ret;
1291 u32 err;
1292 ulong addr;
1293
1294 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1295
1296 if (dt.size < index * 8 + 7) {
1297 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1298 return X86EMUL_PROPAGATE_FAULT;
1299 }
1300 addr = dt.address + index * 8;
1301 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1302 if (ret == X86EMUL_PROPAGATE_FAULT)
1303 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1304
1305 return ret;
1306}
1307
1308/* allowed just for 8 bytes segments */
1309static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1310 struct x86_emulate_ops *ops,
1311 u16 selector, struct desc_struct *desc)
1312{
1313 struct desc_ptr dt;
1314 u16 index = selector >> 3;
1315 u32 err;
1316 ulong addr;
1317 int ret;
1318
1319 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1320
1321 if (dt.size < index * 8 + 7) {
1322 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1323 return X86EMUL_PROPAGATE_FAULT;
1324 }
1325
1326 addr = dt.address + index * 8;
1327 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1328 if (ret == X86EMUL_PROPAGATE_FAULT)
1329 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1330
1331 return ret;
1332}
1333
1334static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1335 struct x86_emulate_ops *ops,
1336 u16 selector, int seg)
1337{
1338 struct desc_struct seg_desc;
1339 u8 dpl, rpl, cpl;
1340 unsigned err_vec = GP_VECTOR;
1341 u32 err_code = 0;
1342 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1343 int ret;
1344
1345 memset(&seg_desc, 0, sizeof seg_desc);
1346
1347 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1348 || ctxt->mode == X86EMUL_MODE_REAL) {
1349 /* set real mode segment descriptor */
1350 set_desc_base(&seg_desc, selector << 4);
1351 set_desc_limit(&seg_desc, 0xffff);
1352 seg_desc.type = 3;
1353 seg_desc.p = 1;
1354 seg_desc.s = 1;
1355 goto load;
1356 }
1357
1358 /* NULL selector is not valid for TR, CS and SS */
1359 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1360 && null_selector)
1361 goto exception;
1362
1363 /* TR should be in GDT only */
1364 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1365 goto exception;
1366
1367 if (null_selector) /* for NULL selector skip all following checks */
1368 goto load;
1369
1370 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1371 if (ret != X86EMUL_CONTINUE)
1372 return ret;
1373
1374 err_code = selector & 0xfffc;
1375 err_vec = GP_VECTOR;
1376
1377 /* can't load system descriptor into segment selecor */
1378 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1379 goto exception;
1380
1381 if (!seg_desc.p) {
1382 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1383 goto exception;
1384 }
1385
1386 rpl = selector & 3;
1387 dpl = seg_desc.dpl;
1388 cpl = ops->cpl(ctxt->vcpu);
1389
1390 switch (seg) {
1391 case VCPU_SREG_SS:
1392 /*
1393 * segment is not a writable data segment or segment
1394 * selector's RPL != CPL or segment selector's RPL != CPL
1395 */
1396 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1397 goto exception;
1398 break;
1399 case VCPU_SREG_CS:
1400 if (!(seg_desc.type & 8))
1401 goto exception;
1402
1403 if (seg_desc.type & 4) {
1404 /* conforming */
1405 if (dpl > cpl)
1406 goto exception;
1407 } else {
1408 /* nonconforming */
1409 if (rpl > cpl || dpl != cpl)
1410 goto exception;
1411 }
1412 /* CS(RPL) <- CPL */
1413 selector = (selector & 0xfffc) | cpl;
1414 break;
1415 case VCPU_SREG_TR:
1416 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1417 goto exception;
1418 break;
1419 case VCPU_SREG_LDTR:
1420 if (seg_desc.s || seg_desc.type != 2)
1421 goto exception;
1422 break;
1423 default: /* DS, ES, FS, or GS */
1424 /*
1425 * segment is not a data or readable code segment or
1426 * ((segment is a data or nonconforming code segment)
1427 * and (both RPL and CPL > DPL))
1428 */
1429 if ((seg_desc.type & 0xa) == 0x8 ||
1430 (((seg_desc.type & 0xc) != 0xc) &&
1431 (rpl > dpl && cpl > dpl)))
1432 goto exception;
1433 break;
1434 }
1435
1436 if (seg_desc.s) {
1437 /* mark segment as accessed */
1438 seg_desc.type |= 1;
1439 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1440 if (ret != X86EMUL_CONTINUE)
1441 return ret;
1442 }
1443load:
1444 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1445 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1446 return X86EMUL_CONTINUE;
1447exception:
1448 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1449 return X86EMUL_PROPAGATE_FAULT;
1450}
1451
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001452static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1453{
1454 struct decode_cache *c = &ctxt->decode;
1455
1456 c->dst.type = OP_MEM;
1457 c->dst.bytes = c->op_bytes;
1458 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001459 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001460 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001461 c->regs[VCPU_REGS_RSP]);
1462}
1463
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001464static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001465 struct x86_emulate_ops *ops,
1466 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001467{
1468 struct decode_cache *c = &ctxt->decode;
1469 int rc;
1470
Avi Kivity781d0ed2008-11-27 18:00:28 +02001471 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1472 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001473 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001474 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475 return rc;
1476
Avi Kivity350f69d2009-01-05 11:12:40 +02001477 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001478 return rc;
1479}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001480
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001481static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1482 struct x86_emulate_ops *ops,
1483 void *dest, int len)
1484{
1485 int rc;
1486 unsigned long val, change_mask;
1487 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001488 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001489
1490 rc = emulate_pop(ctxt, ops, &val, len);
1491 if (rc != X86EMUL_CONTINUE)
1492 return rc;
1493
1494 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1495 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1496
1497 switch(ctxt->mode) {
1498 case X86EMUL_MODE_PROT64:
1499 case X86EMUL_MODE_PROT32:
1500 case X86EMUL_MODE_PROT16:
1501 if (cpl == 0)
1502 change_mask |= EFLG_IOPL;
1503 if (cpl <= iopl)
1504 change_mask |= EFLG_IF;
1505 break;
1506 case X86EMUL_MODE_VM86:
1507 if (iopl < 3) {
1508 kvm_inject_gp(ctxt->vcpu, 0);
1509 return X86EMUL_PROPAGATE_FAULT;
1510 }
1511 change_mask |= EFLG_IF;
1512 break;
1513 default: /* real mode */
1514 change_mask |= (EFLG_IOPL | EFLG_IF);
1515 break;
1516 }
1517
1518 *(unsigned long *)dest =
1519 (ctxt->eflags & ~change_mask) | (val & change_mask);
1520
1521 return rc;
1522}
1523
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001524static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1525{
1526 struct decode_cache *c = &ctxt->decode;
1527 struct kvm_segment segment;
1528
1529 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1530
1531 c->src.val = segment.selector;
1532 emulate_push(ctxt);
1533}
1534
1535static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1536 struct x86_emulate_ops *ops, int seg)
1537{
1538 struct decode_cache *c = &ctxt->decode;
1539 unsigned long selector;
1540 int rc;
1541
1542 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001543 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001544 return rc;
1545
Gleb Natapov2e873022010-03-18 15:20:18 +02001546 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001547 return rc;
1548}
1549
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001550static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1551{
1552 struct decode_cache *c = &ctxt->decode;
1553 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1554 int reg = VCPU_REGS_RAX;
1555
1556 while (reg <= VCPU_REGS_RDI) {
1557 (reg == VCPU_REGS_RSP) ?
1558 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1559
1560 emulate_push(ctxt);
1561 ++reg;
1562 }
1563}
1564
1565static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1566 struct x86_emulate_ops *ops)
1567{
1568 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001569 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001570 int reg = VCPU_REGS_RDI;
1571
1572 while (reg >= VCPU_REGS_RAX) {
1573 if (reg == VCPU_REGS_RSP) {
1574 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1575 c->op_bytes);
1576 --reg;
1577 }
1578
1579 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001580 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001581 break;
1582 --reg;
1583 }
1584 return rc;
1585}
1586
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001587static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1588 struct x86_emulate_ops *ops)
1589{
1590 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001591
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001592 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001593}
1594
Laurent Vivier05f086f2007-09-24 11:10:55 +02001595static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001596{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001597 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001598 switch (c->modrm_reg) {
1599 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001600 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001601 break;
1602 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001603 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001604 break;
1605 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001606 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001607 break;
1608 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001609 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001610 break;
1611 case 4: /* sal/shl */
1612 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001613 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614 break;
1615 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001616 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001617 break;
1618 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001619 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001620 break;
1621 }
1622}
1623
1624static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +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 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001631 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001632 break;
1633 case 2: /* not */
1634 c->dst.val = ~c->dst.val;
1635 break;
1636 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001637 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638 break;
1639 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001640 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001642 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001643}
1644
1645static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001646 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647{
1648 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001649
1650 switch (c->modrm_reg) {
1651 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001652 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653 break;
1654 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001655 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001657 case 2: /* call near abs */ {
1658 long int old_eip;
1659 old_eip = c->eip;
1660 c->eip = c->src.val;
1661 c->src.val = old_eip;
1662 emulate_push(ctxt);
1663 break;
1664 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001666 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667 break;
1668 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001669 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001670 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001672 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001673}
1674
1675static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001676 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001677{
1678 struct decode_cache *c = &ctxt->decode;
1679 u64 old, new;
1680 int rc;
1681
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001682 rc = ops->read_emulated(c->modrm_ea, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001683 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684 return rc;
1685
1686 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1687 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1688
1689 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1690 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001691 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692
1693 } else {
1694 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1695 (u32) c->regs[VCPU_REGS_RBX];
1696
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001697 rc = ops->cmpxchg_emulated(c->modrm_ea, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001698 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001700 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001702 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703}
1704
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001705static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1706 struct x86_emulate_ops *ops)
1707{
1708 struct decode_cache *c = &ctxt->decode;
1709 int rc;
1710 unsigned long cs;
1711
1712 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001713 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001714 return rc;
1715 if (c->op_bytes == 4)
1716 c->eip = (u32)c->eip;
1717 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001718 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001719 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001720 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001721 return rc;
1722}
1723
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724static inline int writeback(struct x86_emulate_ctxt *ctxt,
1725 struct x86_emulate_ops *ops)
1726{
1727 int rc;
1728 struct decode_cache *c = &ctxt->decode;
1729
1730 switch (c->dst.type) {
1731 case OP_REG:
1732 /* The 4-byte case *is* correct:
1733 * in 64-bit mode we zero-extend.
1734 */
1735 switch (c->dst.bytes) {
1736 case 1:
1737 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1738 break;
1739 case 2:
1740 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1741 break;
1742 case 4:
1743 *c->dst.ptr = (u32)c->dst.val;
1744 break; /* 64b: zero-ext */
1745 case 8:
1746 *c->dst.ptr = c->dst.val;
1747 break;
1748 }
1749 break;
1750 case OP_MEM:
1751 if (c->lock_prefix)
1752 rc = ops->cmpxchg_emulated(
1753 (unsigned long)c->dst.ptr,
1754 &c->dst.orig_val,
1755 &c->dst.val,
1756 c->dst.bytes,
1757 ctxt->vcpu);
1758 else
1759 rc = ops->write_emulated(
1760 (unsigned long)c->dst.ptr,
1761 &c->dst.val,
1762 c->dst.bytes,
1763 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001764 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001766 break;
1767 case OP_NONE:
1768 /* no writeback */
1769 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 default:
1771 break;
1772 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001773 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774}
1775
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301776static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001777{
1778 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1779 /*
1780 * an sti; sti; sequence only disable interrupts for the first
1781 * instruction. So, if the last instruction, be it emulated or
1782 * not, left the system with the INT_STI flag enabled, it
1783 * means that the last instruction is an sti. We should not
1784 * leave the flag on in this case. The same goes for mov ss
1785 */
1786 if (!(int_shadow & mask))
1787 ctxt->interruptibility = mask;
1788}
1789
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001790static inline void
1791setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1792 struct kvm_segment *cs, struct kvm_segment *ss)
1793{
1794 memset(cs, 0, sizeof(struct kvm_segment));
1795 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1796 memset(ss, 0, sizeof(struct kvm_segment));
1797
1798 cs->l = 0; /* will be adjusted later */
1799 cs->base = 0; /* flat segment */
1800 cs->g = 1; /* 4kb granularity */
1801 cs->limit = 0xffffffff; /* 4GB limit */
1802 cs->type = 0x0b; /* Read, Execute, Accessed */
1803 cs->s = 1;
1804 cs->dpl = 0; /* will be adjusted later */
1805 cs->present = 1;
1806 cs->db = 1;
1807
1808 ss->unusable = 0;
1809 ss->base = 0; /* flat segment */
1810 ss->limit = 0xffffffff; /* 4GB limit */
1811 ss->g = 1; /* 4kb granularity */
1812 ss->s = 1;
1813 ss->type = 0x03; /* Read/Write, Accessed */
1814 ss->db = 1; /* 32bit stack segment */
1815 ss->dpl = 0;
1816 ss->present = 1;
1817}
1818
1819static int
1820emulate_syscall(struct x86_emulate_ctxt *ctxt)
1821{
1822 struct decode_cache *c = &ctxt->decode;
1823 struct kvm_segment cs, ss;
1824 u64 msr_data;
1825
1826 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001827 if (ctxt->mode == X86EMUL_MODE_REAL ||
1828 ctxt->mode == X86EMUL_MODE_VM86) {
1829 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1830 return X86EMUL_PROPAGATE_FAULT;
1831 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001832
1833 setup_syscalls_segments(ctxt, &cs, &ss);
1834
1835 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1836 msr_data >>= 32;
1837 cs.selector = (u16)(msr_data & 0xfffc);
1838 ss.selector = (u16)(msr_data + 8);
1839
1840 if (is_long_mode(ctxt->vcpu)) {
1841 cs.db = 0;
1842 cs.l = 1;
1843 }
1844 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1845 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1846
1847 c->regs[VCPU_REGS_RCX] = c->eip;
1848 if (is_long_mode(ctxt->vcpu)) {
1849#ifdef CONFIG_X86_64
1850 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1851
1852 kvm_x86_ops->get_msr(ctxt->vcpu,
1853 ctxt->mode == X86EMUL_MODE_PROT64 ?
1854 MSR_LSTAR : MSR_CSTAR, &msr_data);
1855 c->eip = msr_data;
1856
1857 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1858 ctxt->eflags &= ~(msr_data | EFLG_RF);
1859#endif
1860 } else {
1861 /* legacy mode */
1862 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1863 c->eip = (u32)msr_data;
1864
1865 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1866 }
1867
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001868 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001869}
1870
Andre Przywara8c604352009-06-18 12:56:01 +02001871static int
1872emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1873{
1874 struct decode_cache *c = &ctxt->decode;
1875 struct kvm_segment cs, ss;
1876 u64 msr_data;
1877
Gleb Natapova0044752010-02-10 14:21:31 +02001878 /* inject #GP if in real mode */
1879 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001880 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001881 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001882 }
1883
1884 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1885 * Therefore, we inject an #UD.
1886 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001887 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1888 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1889 return X86EMUL_PROPAGATE_FAULT;
1890 }
Andre Przywara8c604352009-06-18 12:56:01 +02001891
1892 setup_syscalls_segments(ctxt, &cs, &ss);
1893
1894 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1895 switch (ctxt->mode) {
1896 case X86EMUL_MODE_PROT32:
1897 if ((msr_data & 0xfffc) == 0x0) {
1898 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001899 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001900 }
1901 break;
1902 case X86EMUL_MODE_PROT64:
1903 if (msr_data == 0x0) {
1904 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001905 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001906 }
1907 break;
1908 }
1909
1910 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1911 cs.selector = (u16)msr_data;
1912 cs.selector &= ~SELECTOR_RPL_MASK;
1913 ss.selector = cs.selector + 8;
1914 ss.selector &= ~SELECTOR_RPL_MASK;
1915 if (ctxt->mode == X86EMUL_MODE_PROT64
1916 || is_long_mode(ctxt->vcpu)) {
1917 cs.db = 0;
1918 cs.l = 1;
1919 }
1920
1921 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1922 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1923
1924 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1925 c->eip = msr_data;
1926
1927 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1928 c->regs[VCPU_REGS_RSP] = msr_data;
1929
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001930 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001931}
1932
Andre Przywara4668f052009-06-18 12:56:02 +02001933static int
1934emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1935{
1936 struct decode_cache *c = &ctxt->decode;
1937 struct kvm_segment cs, ss;
1938 u64 msr_data;
1939 int usermode;
1940
Gleb Natapova0044752010-02-10 14:21:31 +02001941 /* inject #GP if in real mode or Virtual 8086 mode */
1942 if (ctxt->mode == X86EMUL_MODE_REAL ||
1943 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001944 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001945 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001946 }
1947
Andre Przywara4668f052009-06-18 12:56:02 +02001948 setup_syscalls_segments(ctxt, &cs, &ss);
1949
1950 if ((c->rex_prefix & 0x8) != 0x0)
1951 usermode = X86EMUL_MODE_PROT64;
1952 else
1953 usermode = X86EMUL_MODE_PROT32;
1954
1955 cs.dpl = 3;
1956 ss.dpl = 3;
1957 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1958 switch (usermode) {
1959 case X86EMUL_MODE_PROT32:
1960 cs.selector = (u16)(msr_data + 16);
1961 if ((msr_data & 0xfffc) == 0x0) {
1962 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001963 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001964 }
1965 ss.selector = (u16)(msr_data + 24);
1966 break;
1967 case X86EMUL_MODE_PROT64:
1968 cs.selector = (u16)(msr_data + 32);
1969 if (msr_data == 0x0) {
1970 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001971 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001972 }
1973 ss.selector = cs.selector + 8;
1974 cs.db = 0;
1975 cs.l = 1;
1976 break;
1977 }
1978 cs.selector |= SELECTOR_RPL_MASK;
1979 ss.selector |= SELECTOR_RPL_MASK;
1980
1981 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1982 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1983
1984 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1985 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1986
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001987 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001988}
1989
Gleb Natapov9c537242010-03-18 15:20:05 +02001990static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1991 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001992{
1993 int iopl;
1994 if (ctxt->mode == X86EMUL_MODE_REAL)
1995 return false;
1996 if (ctxt->mode == X86EMUL_MODE_VM86)
1997 return true;
1998 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001999 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002000}
2001
2002static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2003 struct x86_emulate_ops *ops,
2004 u16 port, u16 len)
2005{
2006 struct kvm_segment tr_seg;
2007 int r;
2008 u16 io_bitmap_ptr;
2009 u8 perm, bit_idx = port & 0x7;
2010 unsigned mask = (1 << len) - 1;
2011
2012 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
2013 if (tr_seg.unusable)
2014 return false;
2015 if (tr_seg.limit < 103)
2016 return false;
2017 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
2018 NULL);
2019 if (r != X86EMUL_CONTINUE)
2020 return false;
2021 if (io_bitmap_ptr + port/8 > tr_seg.limit)
2022 return false;
2023 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
2024 ctxt->vcpu, NULL);
2025 if (r != X86EMUL_CONTINUE)
2026 return false;
2027 if ((perm >> bit_idx) & mask)
2028 return false;
2029 return true;
2030}
2031
2032static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2033 struct x86_emulate_ops *ops,
2034 u16 port, u16 len)
2035{
Gleb Natapov9c537242010-03-18 15:20:05 +02002036 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2038 return false;
2039 return true;
2040}
2041
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002042static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt,
2043 struct x86_emulate_ops *ops,
2044 int seg)
2045{
2046 struct desc_struct desc;
2047 if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu))
2048 return get_desc_base(&desc);
2049 else
2050 return ~0;
2051}
2052
2053static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2054 struct x86_emulate_ops *ops,
2055 struct tss_segment_16 *tss)
2056{
2057 struct decode_cache *c = &ctxt->decode;
2058
2059 tss->ip = c->eip;
2060 tss->flag = ctxt->eflags;
2061 tss->ax = c->regs[VCPU_REGS_RAX];
2062 tss->cx = c->regs[VCPU_REGS_RCX];
2063 tss->dx = c->regs[VCPU_REGS_RDX];
2064 tss->bx = c->regs[VCPU_REGS_RBX];
2065 tss->sp = c->regs[VCPU_REGS_RSP];
2066 tss->bp = c->regs[VCPU_REGS_RBP];
2067 tss->si = c->regs[VCPU_REGS_RSI];
2068 tss->di = c->regs[VCPU_REGS_RDI];
2069
2070 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2071 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2072 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2073 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2074 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2075}
2076
2077static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2078 struct x86_emulate_ops *ops,
2079 struct tss_segment_16 *tss)
2080{
2081 struct decode_cache *c = &ctxt->decode;
2082 int ret;
2083
2084 c->eip = tss->ip;
2085 ctxt->eflags = tss->flag | 2;
2086 c->regs[VCPU_REGS_RAX] = tss->ax;
2087 c->regs[VCPU_REGS_RCX] = tss->cx;
2088 c->regs[VCPU_REGS_RDX] = tss->dx;
2089 c->regs[VCPU_REGS_RBX] = tss->bx;
2090 c->regs[VCPU_REGS_RSP] = tss->sp;
2091 c->regs[VCPU_REGS_RBP] = tss->bp;
2092 c->regs[VCPU_REGS_RSI] = tss->si;
2093 c->regs[VCPU_REGS_RDI] = tss->di;
2094
2095 /*
2096 * SDM says that segment selectors are loaded before segment
2097 * descriptors
2098 */
2099 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2100 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2101 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2102 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2103 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2104
2105 /*
2106 * Now load segment descriptors. If fault happenes at this stage
2107 * it is handled in a context of new task
2108 */
2109 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2110 if (ret != X86EMUL_CONTINUE)
2111 return ret;
2112 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2113 if (ret != X86EMUL_CONTINUE)
2114 return ret;
2115 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2116 if (ret != X86EMUL_CONTINUE)
2117 return ret;
2118 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2119 if (ret != X86EMUL_CONTINUE)
2120 return ret;
2121 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2122 if (ret != X86EMUL_CONTINUE)
2123 return ret;
2124
2125 return X86EMUL_CONTINUE;
2126}
2127
2128static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2129 struct x86_emulate_ops *ops,
2130 u16 tss_selector, u16 old_tss_sel,
2131 ulong old_tss_base, struct desc_struct *new_desc)
2132{
2133 struct tss_segment_16 tss_seg;
2134 int ret;
2135 u32 err, new_tss_base = get_desc_base(new_desc);
2136
2137 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2138 &err);
2139 if (ret == X86EMUL_PROPAGATE_FAULT) {
2140 /* FIXME: need to provide precise fault address */
2141 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2142 return ret;
2143 }
2144
2145 save_state_to_tss16(ctxt, ops, &tss_seg);
2146
2147 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2148 &err);
2149 if (ret == X86EMUL_PROPAGATE_FAULT) {
2150 /* FIXME: need to provide precise fault address */
2151 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2152 return ret;
2153 }
2154
2155 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2156 &err);
2157 if (ret == X86EMUL_PROPAGATE_FAULT) {
2158 /* FIXME: need to provide precise fault address */
2159 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2160 return ret;
2161 }
2162
2163 if (old_tss_sel != 0xffff) {
2164 tss_seg.prev_task_link = old_tss_sel;
2165
2166 ret = ops->write_std(new_tss_base,
2167 &tss_seg.prev_task_link,
2168 sizeof tss_seg.prev_task_link,
2169 ctxt->vcpu, &err);
2170 if (ret == X86EMUL_PROPAGATE_FAULT) {
2171 /* FIXME: need to provide precise fault address */
2172 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2173 return ret;
2174 }
2175 }
2176
2177 return load_state_from_tss16(ctxt, ops, &tss_seg);
2178}
2179
2180static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2181 struct x86_emulate_ops *ops,
2182 struct tss_segment_32 *tss)
2183{
2184 struct decode_cache *c = &ctxt->decode;
2185
2186 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2187 tss->eip = c->eip;
2188 tss->eflags = ctxt->eflags;
2189 tss->eax = c->regs[VCPU_REGS_RAX];
2190 tss->ecx = c->regs[VCPU_REGS_RCX];
2191 tss->edx = c->regs[VCPU_REGS_RDX];
2192 tss->ebx = c->regs[VCPU_REGS_RBX];
2193 tss->esp = c->regs[VCPU_REGS_RSP];
2194 tss->ebp = c->regs[VCPU_REGS_RBP];
2195 tss->esi = c->regs[VCPU_REGS_RSI];
2196 tss->edi = c->regs[VCPU_REGS_RDI];
2197
2198 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2199 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2200 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2201 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2202 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2203 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2204 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2205}
2206
2207static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2208 struct x86_emulate_ops *ops,
2209 struct tss_segment_32 *tss)
2210{
2211 struct decode_cache *c = &ctxt->decode;
2212 int ret;
2213
2214 ops->set_cr(3, tss->cr3, ctxt->vcpu);
2215 c->eip = tss->eip;
2216 ctxt->eflags = tss->eflags | 2;
2217 c->regs[VCPU_REGS_RAX] = tss->eax;
2218 c->regs[VCPU_REGS_RCX] = tss->ecx;
2219 c->regs[VCPU_REGS_RDX] = tss->edx;
2220 c->regs[VCPU_REGS_RBX] = tss->ebx;
2221 c->regs[VCPU_REGS_RSP] = tss->esp;
2222 c->regs[VCPU_REGS_RBP] = tss->ebp;
2223 c->regs[VCPU_REGS_RSI] = tss->esi;
2224 c->regs[VCPU_REGS_RDI] = tss->edi;
2225
2226 /*
2227 * SDM says that segment selectors are loaded before segment
2228 * descriptors
2229 */
2230 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2231 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2232 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2233 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2234 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2235 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2236 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2237
2238 /*
2239 * Now load segment descriptors. If fault happenes at this stage
2240 * it is handled in a context of new task
2241 */
2242 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2243 if (ret != X86EMUL_CONTINUE)
2244 return ret;
2245 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2246 if (ret != X86EMUL_CONTINUE)
2247 return ret;
2248 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2249 if (ret != X86EMUL_CONTINUE)
2250 return ret;
2251 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2252 if (ret != X86EMUL_CONTINUE)
2253 return ret;
2254 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2255 if (ret != X86EMUL_CONTINUE)
2256 return ret;
2257 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2258 if (ret != X86EMUL_CONTINUE)
2259 return ret;
2260 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2261 if (ret != X86EMUL_CONTINUE)
2262 return ret;
2263
2264 return X86EMUL_CONTINUE;
2265}
2266
2267static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2268 struct x86_emulate_ops *ops,
2269 u16 tss_selector, u16 old_tss_sel,
2270 ulong old_tss_base, struct desc_struct *new_desc)
2271{
2272 struct tss_segment_32 tss_seg;
2273 int ret;
2274 u32 err, new_tss_base = get_desc_base(new_desc);
2275
2276 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2277 &err);
2278 if (ret == X86EMUL_PROPAGATE_FAULT) {
2279 /* FIXME: need to provide precise fault address */
2280 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2281 return ret;
2282 }
2283
2284 save_state_to_tss32(ctxt, ops, &tss_seg);
2285
2286 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2287 &err);
2288 if (ret == X86EMUL_PROPAGATE_FAULT) {
2289 /* FIXME: need to provide precise fault address */
2290 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2291 return ret;
2292 }
2293
2294 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2295 &err);
2296 if (ret == X86EMUL_PROPAGATE_FAULT) {
2297 /* FIXME: need to provide precise fault address */
2298 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2299 return ret;
2300 }
2301
2302 if (old_tss_sel != 0xffff) {
2303 tss_seg.prev_task_link = old_tss_sel;
2304
2305 ret = ops->write_std(new_tss_base,
2306 &tss_seg.prev_task_link,
2307 sizeof tss_seg.prev_task_link,
2308 ctxt->vcpu, &err);
2309 if (ret == X86EMUL_PROPAGATE_FAULT) {
2310 /* FIXME: need to provide precise fault address */
2311 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2312 return ret;
2313 }
2314 }
2315
2316 return load_state_from_tss32(ctxt, ops, &tss_seg);
2317}
2318
2319static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2320 struct x86_emulate_ops *ops,
2321 u16 tss_selector, int reason)
2322{
2323 struct desc_struct curr_tss_desc, next_tss_desc;
2324 int ret;
2325 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2326 ulong old_tss_base =
2327 get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002328 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329
2330 /* FIXME: old_tss_base == ~0 ? */
2331
2332 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2333 if (ret != X86EMUL_CONTINUE)
2334 return ret;
2335 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2336 if (ret != X86EMUL_CONTINUE)
2337 return ret;
2338
2339 /* FIXME: check that next_tss_desc is tss */
2340
2341 if (reason != TASK_SWITCH_IRET) {
2342 if ((tss_selector & 3) > next_tss_desc.dpl ||
2343 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2344 kvm_inject_gp(ctxt->vcpu, 0);
2345 return X86EMUL_PROPAGATE_FAULT;
2346 }
2347 }
2348
Gleb Natapovceffb452010-03-18 15:20:19 +02002349 desc_limit = desc_limit_scaled(&next_tss_desc);
2350 if (!next_tss_desc.p ||
2351 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2352 desc_limit < 0x2b)) {
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002353 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2354 tss_selector & 0xfffc);
2355 return X86EMUL_PROPAGATE_FAULT;
2356 }
2357
2358 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2359 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2360 write_segment_descriptor(ctxt, ops, old_tss_sel,
2361 &curr_tss_desc);
2362 }
2363
2364 if (reason == TASK_SWITCH_IRET)
2365 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2366
2367 /* set back link to prev task only if NT bit is set in eflags
2368 note that old_tss_sel is not used afetr this point */
2369 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2370 old_tss_sel = 0xffff;
2371
2372 if (next_tss_desc.type & 8)
2373 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2374 old_tss_base, &next_tss_desc);
2375 else
2376 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2377 old_tss_base, &next_tss_desc);
2378
2379 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2380 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2381
2382 if (reason != TASK_SWITCH_IRET) {
2383 next_tss_desc.type |= (1 << 1); /* set busy flag */
2384 write_segment_descriptor(ctxt, ops, tss_selector,
2385 &next_tss_desc);
2386 }
2387
2388 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2389 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2390 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2391
2392 return ret;
2393}
2394
2395int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2396 struct x86_emulate_ops *ops,
2397 u16 tss_selector, int reason)
2398{
2399 struct decode_cache *c = &ctxt->decode;
2400 int rc;
2401
2402 memset(c, 0, sizeof(struct decode_cache));
2403 c->eip = ctxt->eip;
2404 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
2405
2406 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason);
2407
2408 if (rc == X86EMUL_CONTINUE) {
2409 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
2410 kvm_rip_write(ctxt->vcpu, c->eip);
2411 }
2412
2413 return rc;
2414}
2415
Gleb Natapova682e352010-03-18 15:20:21 +02002416static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002417 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002418{
2419 struct decode_cache *c = &ctxt->decode;
2420 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2421
Gleb Natapovd9271122010-03-18 15:20:22 +02002422 register_address_increment(c, &c->regs[reg], df * op->bytes);
2423 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002424}
2425
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002426int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002427x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002428{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002429 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002430 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002431 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002432 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002433
Glauber Costa310b5d32009-05-12 16:21:06 -04002434 ctxt->interruptibility = 0;
2435
Laurent Vivier34273182007-09-18 11:27:37 +02002436 /* Shadow copy of register state. Committed on successful emulation.
2437 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
2438 * modify them.
2439 */
2440
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002441 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02002442
Gleb Natapov11616242010-02-11 14:43:14 +02002443 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2444 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2445 goto done;
2446 }
2447
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002448 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002449 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002450 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2451 goto done;
2452 }
2453
Gleb Natapove92805a2010-02-10 14:21:35 +02002454 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002455 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002456 kvm_inject_gp(ctxt->vcpu, 0);
2457 goto done;
2458 }
2459
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002460 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002461 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002462 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002463 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002464 string_done:
2465 ctxt->restart = false;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002466 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002467 goto done;
2468 }
2469 /* The second termination condition only applies for REPE
2470 * and REPNE. Test if the repeat string operation prefix is
2471 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2472 * corresponding termination condition according to:
2473 * - if REPE/REPZ and ZF = 0 then done
2474 * - if REPNE/REPNZ and ZF = 1 then done
2475 */
2476 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002477 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002478 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002479 ((ctxt->eflags & EFLG_ZF) == 0))
2480 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002481 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002482 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2483 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002484 }
Gleb Natapov063db062010-03-18 15:20:06 +02002485 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002486 }
2487
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002488 if (c->src.type == OP_MEM) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002489 rc = ops->read_emulated((unsigned long)c->src.ptr,
2490 &c->src.val,
2491 c->src.bytes,
2492 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002493 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002494 goto done;
2495 c->src.orig_val = c->src.val;
2496 }
2497
Gleb Natapove35b7b92010-02-25 16:36:42 +02002498 if (c->src2.type == OP_MEM) {
Gleb Natapove35b7b92010-02-25 16:36:42 +02002499 rc = ops->read_emulated((unsigned long)c->src2.ptr,
2500 &c->src2.val,
2501 c->src2.bytes,
2502 ctxt->vcpu);
2503 if (rc != X86EMUL_CONTINUE)
2504 goto done;
2505 }
2506
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002507 if ((c->d & DstMask) == ImplicitOps)
2508 goto special_insn;
2509
2510
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002511 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2512 /* optimisation - avoid slow emulated read if Mov */
2513 rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val,
2514 c->dst.bytes, ctxt->vcpu);
2515 if (rc != X86EMUL_CONTINUE)
2516 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002517 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002518 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002519
Avi Kivity018a98d2007-11-27 19:30:56 +02002520special_insn:
2521
Laurent Viviere4e03de2007-09-18 11:52:50 +02002522 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 goto twobyte_insn;
2524
Laurent Viviere4e03de2007-09-18 11:52:50 +02002525 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 case 0x00 ... 0x05:
2527 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002528 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002530 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002531 emulate_push_sreg(ctxt, VCPU_SREG_ES);
2532 break;
2533 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002534 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002535 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002536 goto done;
2537 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 case 0x08 ... 0x0d:
2539 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002540 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002542 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002543 emulate_push_sreg(ctxt, VCPU_SREG_CS);
2544 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 case 0x10 ... 0x15:
2546 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002547 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002549 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002550 emulate_push_sreg(ctxt, VCPU_SREG_SS);
2551 break;
2552 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002553 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002554 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002555 goto done;
2556 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 case 0x18 ... 0x1d:
2558 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002559 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002561 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002562 emulate_push_sreg(ctxt, VCPU_SREG_DS);
2563 break;
2564 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002565 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002566 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002567 goto done;
2568 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002569 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002571 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 break;
2573 case 0x28 ... 0x2d:
2574 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002575 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 break;
2577 case 0x30 ... 0x35:
2578 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002579 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 break;
2581 case 0x38 ... 0x3d:
2582 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002583 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002585 case 0x40 ... 0x47: /* inc r16/r32 */
2586 emulate_1op("inc", c->dst, ctxt->eflags);
2587 break;
2588 case 0x48 ... 0x4f: /* dec r16/r32 */
2589 emulate_1op("dec", c->dst, ctxt->eflags);
2590 break;
2591 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002592 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002593 break;
2594 case 0x58 ... 0x5f: /* pop reg */
2595 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002596 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002597 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002598 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002599 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002600 case 0x60: /* pusha */
2601 emulate_pusha(ctxt);
2602 break;
2603 case 0x61: /* popa */
2604 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002605 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002606 goto done;
2607 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002609 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002611 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002613 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002614 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002615 emulate_push(ctxt);
2616 break;
2617 case 0x6c: /* insb */
2618 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002619 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002620 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002621 c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002622 kvm_inject_gp(ctxt->vcpu, 0);
2623 goto done;
2624 }
Gleb Natapov79729952010-03-18 15:20:24 +02002625 if (!ops->pio_in_emulated(c->dst.bytes, c->regs[VCPU_REGS_RDX],
2626 &c->dst.val, 1, ctxt->vcpu))
2627 goto done; /* IO is needed, skip writeback */
2628 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002629 case 0x6e: /* outsb */
2630 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002631 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002632 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002633 c->src.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002634 kvm_inject_gp(ctxt->vcpu, 0);
2635 goto done;
2636 }
Gleb Natapov79729952010-03-18 15:20:24 +02002637 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2638 &c->src.val, 1, ctxt->vcpu);
2639
2640 c->dst.type = OP_NONE; /* nothing to writeback */
2641 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002642 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002643 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002644 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002645 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002647 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 case 0:
2649 goto add;
2650 case 1:
2651 goto or;
2652 case 2:
2653 goto adc;
2654 case 3:
2655 goto sbb;
2656 case 4:
2657 goto and;
2658 case 5:
2659 goto sub;
2660 case 6:
2661 goto xor;
2662 case 7:
2663 goto cmp;
2664 }
2665 break;
2666 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002667 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 break;
2669 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002670 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002672 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002674 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 break;
2676 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002677 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 break;
2679 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002680 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 break; /* 64b reg: zero-extend */
2682 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002683 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 break;
2685 }
2686 /*
2687 * Write back the memory destination with implicit LOCK
2688 * prefix.
2689 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002690 c->dst.val = c->src.val;
2691 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002694 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002695 case 0x8c: { /* mov r/m, sreg */
2696 struct kvm_segment segreg;
2697
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002698 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002699 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2700 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002701 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2702 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002703 }
2704 c->dst.val = segreg.selector;
2705 break;
2706 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002707 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002708 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002709 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002710 case 0x8e: { /* mov seg, r/m16 */
2711 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002712
2713 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002714
Gleb Natapovc6975182010-02-18 12:15:01 +02002715 if (c->modrm_reg == VCPU_SREG_CS ||
2716 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002717 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2718 goto done;
2719 }
2720
Glauber Costa310b5d32009-05-12 16:21:06 -04002721 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002722 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002723
Gleb Natapov2e873022010-03-18 15:20:18 +02002724 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002725
2726 c->dst.type = OP_NONE; /* Disable writeback. */
2727 break;
2728 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002730 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002731 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002734 case 0x90: /* nop / xchg r8,rax */
2735 if (!(c->rex_prefix & 1)) { /* nop */
2736 c->dst.type = OP_NONE;
2737 break;
2738 }
2739 case 0x91 ... 0x97: /* xchg reg,rax */
2740 c->src.type = c->dst.type = OP_REG;
2741 c->src.bytes = c->dst.bytes = c->op_bytes;
2742 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2743 c->src.val = *(c->src.ptr);
2744 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002745 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002746 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002747 emulate_push(ctxt);
2748 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002749 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002750 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002751 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002752 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002753 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2754 if (rc != X86EMUL_CONTINUE)
2755 goto done;
2756 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002757 case 0xa0 ... 0xa1: /* mov */
2758 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2759 c->dst.val = c->src.val;
2760 break;
2761 case 0xa2 ... 0xa3: /* mov */
2762 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2763 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002765 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002767 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002768 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002769 goto cmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002771 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 break;
2773 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002774 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 case 0xae ... 0xaf: /* scas */
2776 DPRINTF("Urk! I don't handle SCAS.\n");
2777 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002778 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002779 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002780 case 0xc0 ... 0xc1:
2781 emulate_grp2(ctxt);
2782 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002783 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002784 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002785 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002786 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002787 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002788 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2789 mov:
2790 c->dst.val = c->src.val;
2791 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002792 case 0xcb: /* ret far */
2793 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002794 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002795 goto done;
2796 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002797 case 0xd0 ... 0xd1: /* Grp2 */
2798 c->src.val = 1;
2799 emulate_grp2(ctxt);
2800 break;
2801 case 0xd2 ... 0xd3: /* Grp2 */
2802 c->src.val = c->regs[VCPU_REGS_RCX];
2803 emulate_grp2(ctxt);
2804 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002805 case 0xe4: /* inb */
2806 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002807 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002808 case 0xe6: /* outb */
2809 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002810 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002811 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002812 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002813 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002814 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002815 emulate_push(ctxt);
2816 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002817 }
2818 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002819 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002820 case 0xea: /* jmp far */
Gleb Natapovea79849d2010-02-25 16:36:43 +02002821 jump_far:
Gleb Natapov2e873022010-03-18 15:20:18 +02002822 if (load_segment_descriptor(ctxt, ops, c->src2.val,
2823 VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002824 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002825
Gleb Natapov782b8772009-04-12 13:36:25 +03002826 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002827 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002828 case 0xeb:
2829 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002830 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002831 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002832 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002833 case 0xec: /* in al,dx */
2834 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002835 c->src.val = c->regs[VCPU_REGS_RDX];
2836 do_io_in:
2837 c->dst.bytes = min(c->dst.bytes, 4u);
2838 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002839 kvm_inject_gp(ctxt->vcpu, 0);
2840 goto done;
2841 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002842 if (!ops->pio_in_emulated(c->dst.bytes, c->src.val,
2843 &c->dst.val, 1, ctxt->vcpu))
2844 goto done; /* IO is needed */
2845 break;
2846 case 0xee: /* out al,dx */
2847 case 0xef: /* out (e/r)ax,dx */
2848 c->src.val = c->regs[VCPU_REGS_RDX];
2849 do_io_out:
2850 c->dst.bytes = min(c->dst.bytes, 4u);
2851 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
2852 kvm_inject_gp(ctxt->vcpu, 0);
2853 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002854 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002855 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2856 ctxt->vcpu);
2857 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002858 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002859 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002860 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002861 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002862 case 0xf5: /* cmc */
2863 /* complement carry flag from eflags reg */
2864 ctxt->eflags ^= EFLG_CF;
2865 c->dst.type = OP_NONE; /* Disable writeback. */
2866 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002867 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002868 if (!emulate_grp3(ctxt, ops))
2869 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002870 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002871 case 0xf8: /* clc */
2872 ctxt->eflags &= ~EFLG_CF;
2873 c->dst.type = OP_NONE; /* Disable writeback. */
2874 break;
2875 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002876 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002877 kvm_inject_gp(ctxt->vcpu, 0);
2878 else {
2879 ctxt->eflags &= ~X86_EFLAGS_IF;
2880 c->dst.type = OP_NONE; /* Disable writeback. */
2881 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002882 break;
2883 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002884 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002885 kvm_inject_gp(ctxt->vcpu, 0);
2886 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002887 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002888 ctxt->eflags |= X86_EFLAGS_IF;
2889 c->dst.type = OP_NONE; /* Disable writeback. */
2890 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002891 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002892 case 0xfc: /* cld */
2893 ctxt->eflags &= ~EFLG_DF;
2894 c->dst.type = OP_NONE; /* Disable writeback. */
2895 break;
2896 case 0xfd: /* std */
2897 ctxt->eflags |= EFLG_DF;
2898 c->dst.type = OP_NONE; /* Disable writeback. */
2899 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002900 case 0xfe: /* Grp4 */
2901 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002902 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002903 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002904 goto done;
2905 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002906 case 0xff: /* Grp5 */
2907 if (c->modrm_reg == 5)
2908 goto jump_far;
2909 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002911
2912writeback:
2913 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002914 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002915 goto done;
2916
Gleb Natapov5cd21912010-03-18 15:20:26 +02002917 /*
2918 * restore dst type in case the decoding will be reused
2919 * (happens for string instruction )
2920 */
2921 c->dst.type = saved_dst_type;
2922
Gleb Natapova682e352010-03-18 15:20:21 +02002923 if ((c->d & SrcMask) == SrcSI)
2924 string_addr_inc(ctxt, seg_override_base(ctxt, c), VCPU_REGS_RSI,
Gleb Natapovd9271122010-03-18 15:20:22 +02002925 &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02002926
2927 if ((c->d & DstMask) == DstDI)
Gleb Natapovd9271122010-03-18 15:20:22 +02002928 string_addr_inc(ctxt, es_base(ctxt), VCPU_REGS_RDI, &c->dst);
2929
Gleb Natapov5cd21912010-03-18 15:20:26 +02002930 if (c->rep_prefix && (c->d & String)) {
Gleb Natapovd9271122010-03-18 15:20:22 +02002931 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov5cd21912010-03-18 15:20:26 +02002932 if (!(c->regs[VCPU_REGS_RCX] & 0x3ff))
2933 ctxt->restart = false;
2934 }
Gleb Natapova682e352010-03-18 15:20:21 +02002935
Avi Kivity018a98d2007-11-27 19:30:56 +02002936 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002937 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002938 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002939
2940done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02002941 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942
2943twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002944 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002946 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 u16 size;
2948 unsigned long address;
2949
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002950 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002951 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002952 goto cannot_emulate;
2953
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002954 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002955 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002956 goto done;
2957
Avi Kivity33e38852008-05-21 15:34:25 +03002958 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002959 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002960 /* Disable writeback. */
2961 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002962 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002964 rc = read_descriptor(ctxt, ops, c->src.ptr,
2965 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002966 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 goto done;
2968 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002969 /* Disable writeback. */
2970 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002972 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002973 if (c->modrm_mod == 3) {
2974 switch (c->modrm_rm) {
2975 case 1:
2976 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002977 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002978 goto done;
2979 break;
2980 default:
2981 goto cannot_emulate;
2982 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002983 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002984 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002985 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002986 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002987 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002988 goto done;
2989 realmode_lidt(ctxt->vcpu, size, address);
2990 }
Avi Kivity16286d02008-04-14 14:40:50 +03002991 /* Disable writeback. */
2992 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 break;
2994 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002995 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02002996 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997 break;
2998 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02002999 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3000 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003001 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003003 case 5: /* not defined */
3004 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3005 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003007 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003008 /* Disable writeback. */
3009 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 break;
3011 default:
3012 goto cannot_emulate;
3013 }
3014 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003015 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003016 rc = emulate_syscall(ctxt);
3017 if (rc != X86EMUL_CONTINUE)
3018 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003019 else
3020 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003021 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003022 case 0x06:
3023 emulate_clts(ctxt->vcpu);
3024 c->dst.type = OP_NONE;
3025 break;
3026 case 0x08: /* invd */
3027 case 0x09: /* wbinvd */
3028 case 0x0d: /* GrpP (prefetch) */
3029 case 0x18: /* Grp16 (prefetch/nop) */
3030 c->dst.type = OP_NONE;
3031 break;
3032 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003033 switch (c->modrm_reg) {
3034 case 1:
3035 case 5 ... 7:
3036 case 9 ... 15:
3037 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3038 goto done;
3039 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003040 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003041 c->dst.type = OP_NONE; /* no writeback */
3042 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003044 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3045 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3046 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3047 goto done;
3048 }
3049 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003050 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003052 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02003053 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003054 c->dst.type = OP_NONE;
3055 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003057 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3058 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3059 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3060 goto done;
3061 }
3062 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003063 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003065 case 0x30:
3066 /* wrmsr */
3067 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3068 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003069 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003070 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003071 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003072 }
3073 rc = X86EMUL_CONTINUE;
3074 c->dst.type = OP_NONE;
3075 break;
3076 case 0x32:
3077 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003078 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003079 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003080 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003081 } else {
3082 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3083 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3084 }
3085 rc = X86EMUL_CONTINUE;
3086 c->dst.type = OP_NONE;
3087 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003088 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003089 rc = emulate_sysenter(ctxt);
3090 if (rc != X86EMUL_CONTINUE)
3091 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003092 else
3093 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003094 break;
3095 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003096 rc = emulate_sysexit(ctxt);
3097 if (rc != X86EMUL_CONTINUE)
3098 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003099 else
3100 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003101 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003103 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003104 if (!test_cc(c->b, ctxt->eflags))
3105 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003107 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003109 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003110 c->dst.type = OP_NONE;
3111 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003112 case 0xa0: /* push fs */
3113 emulate_push_sreg(ctxt, VCPU_SREG_FS);
3114 break;
3115 case 0xa1: /* pop fs */
3116 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003117 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003118 goto done;
3119 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003120 case 0xa3:
3121 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003122 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003123 /* only subword offset */
3124 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003125 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003126 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003127 case 0xa4: /* shld imm8, r, r/m */
3128 case 0xa5: /* shld cl, r, r/m */
3129 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3130 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003131 case 0xa8: /* push gs */
3132 emulate_push_sreg(ctxt, VCPU_SREG_GS);
3133 break;
3134 case 0xa9: /* pop gs */
3135 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003136 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003137 goto done;
3138 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003139 case 0xab:
3140 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003141 /* only subword offset */
3142 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003143 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003144 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003145 case 0xac: /* shrd imm8, r, r/m */
3146 case 0xad: /* shrd cl, r, r/m */
3147 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3148 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003149 case 0xae: /* clflush */
3150 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 case 0xb0 ... 0xb1: /* cmpxchg */
3152 /*
3153 * Save real source value, then compare EAX against
3154 * destination.
3155 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003156 c->src.orig_val = c->src.val;
3157 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003158 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3159 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003161 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 } else {
3163 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003164 c->dst.type = OP_REG;
3165 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 }
3167 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 case 0xb3:
3169 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003170 /* only subword offset */
3171 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003172 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003175 c->dst.bytes = c->op_bytes;
3176 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3177 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003180 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 case 0:
3182 goto bt;
3183 case 1:
3184 goto bts;
3185 case 2:
3186 goto btr;
3187 case 3:
3188 goto btc;
3189 }
3190 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003191 case 0xbb:
3192 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003193 /* only subword offset */
3194 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003195 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003196 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003198 c->dst.bytes = c->op_bytes;
3199 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3200 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003202 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003203 c->dst.bytes = c->op_bytes;
3204 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3205 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003206 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003208 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003209 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003210 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003211 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003212 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 }
3214 goto writeback;
3215
3216cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003217 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 return -1;
3219}