blob: ab3fff5bf7c467637bf53c34eac874d1ba627389 [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 Natapov7b262e92010-03-18 15:20:27 +02001260static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1261 struct x86_emulate_ops *ops,
1262 unsigned int size, unsigned short port,
1263 void *dest)
1264{
1265 struct read_cache *rc = &ctxt->decode.io_read;
1266
1267 if (rc->pos == rc->end) { /* refill pio read ahead */
1268 struct decode_cache *c = &ctxt->decode;
1269 unsigned int in_page, n;
1270 unsigned int count = c->rep_prefix ?
1271 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1272 in_page = (ctxt->eflags & EFLG_DF) ?
1273 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1274 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1275 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1276 count);
1277 if (n == 0)
1278 n = 1;
1279 rc->pos = rc->end = 0;
1280 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1281 return 0;
1282 rc->end = n * size;
1283 }
1284
1285 memcpy(dest, rc->data + rc->pos, size);
1286 rc->pos += size;
1287 return 1;
1288}
1289
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001290static u32 desc_limit_scaled(struct desc_struct *desc)
1291{
1292 u32 limit = get_desc_limit(desc);
1293
1294 return desc->g ? (limit << 12) | 0xfff : limit;
1295}
1296
1297static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1298 struct x86_emulate_ops *ops,
1299 u16 selector, struct desc_ptr *dt)
1300{
1301 if (selector & 1 << 2) {
1302 struct desc_struct desc;
1303 memset (dt, 0, sizeof *dt);
1304 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1305 return;
1306
1307 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1308 dt->address = get_desc_base(&desc);
1309 } else
1310 ops->get_gdt(dt, ctxt->vcpu);
1311}
1312
1313/* allowed just for 8 bytes segments */
1314static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1315 struct x86_emulate_ops *ops,
1316 u16 selector, struct desc_struct *desc)
1317{
1318 struct desc_ptr dt;
1319 u16 index = selector >> 3;
1320 int ret;
1321 u32 err;
1322 ulong addr;
1323
1324 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1325
1326 if (dt.size < index * 8 + 7) {
1327 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1328 return X86EMUL_PROPAGATE_FAULT;
1329 }
1330 addr = dt.address + index * 8;
1331 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1332 if (ret == X86EMUL_PROPAGATE_FAULT)
1333 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1334
1335 return ret;
1336}
1337
1338/* allowed just for 8 bytes segments */
1339static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1340 struct x86_emulate_ops *ops,
1341 u16 selector, struct desc_struct *desc)
1342{
1343 struct desc_ptr dt;
1344 u16 index = selector >> 3;
1345 u32 err;
1346 ulong addr;
1347 int ret;
1348
1349 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1350
1351 if (dt.size < index * 8 + 7) {
1352 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1353 return X86EMUL_PROPAGATE_FAULT;
1354 }
1355
1356 addr = dt.address + index * 8;
1357 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1358 if (ret == X86EMUL_PROPAGATE_FAULT)
1359 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1360
1361 return ret;
1362}
1363
1364static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1365 struct x86_emulate_ops *ops,
1366 u16 selector, int seg)
1367{
1368 struct desc_struct seg_desc;
1369 u8 dpl, rpl, cpl;
1370 unsigned err_vec = GP_VECTOR;
1371 u32 err_code = 0;
1372 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1373 int ret;
1374
1375 memset(&seg_desc, 0, sizeof seg_desc);
1376
1377 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1378 || ctxt->mode == X86EMUL_MODE_REAL) {
1379 /* set real mode segment descriptor */
1380 set_desc_base(&seg_desc, selector << 4);
1381 set_desc_limit(&seg_desc, 0xffff);
1382 seg_desc.type = 3;
1383 seg_desc.p = 1;
1384 seg_desc.s = 1;
1385 goto load;
1386 }
1387
1388 /* NULL selector is not valid for TR, CS and SS */
1389 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1390 && null_selector)
1391 goto exception;
1392
1393 /* TR should be in GDT only */
1394 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1395 goto exception;
1396
1397 if (null_selector) /* for NULL selector skip all following checks */
1398 goto load;
1399
1400 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1401 if (ret != X86EMUL_CONTINUE)
1402 return ret;
1403
1404 err_code = selector & 0xfffc;
1405 err_vec = GP_VECTOR;
1406
1407 /* can't load system descriptor into segment selecor */
1408 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1409 goto exception;
1410
1411 if (!seg_desc.p) {
1412 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1413 goto exception;
1414 }
1415
1416 rpl = selector & 3;
1417 dpl = seg_desc.dpl;
1418 cpl = ops->cpl(ctxt->vcpu);
1419
1420 switch (seg) {
1421 case VCPU_SREG_SS:
1422 /*
1423 * segment is not a writable data segment or segment
1424 * selector's RPL != CPL or segment selector's RPL != CPL
1425 */
1426 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1427 goto exception;
1428 break;
1429 case VCPU_SREG_CS:
1430 if (!(seg_desc.type & 8))
1431 goto exception;
1432
1433 if (seg_desc.type & 4) {
1434 /* conforming */
1435 if (dpl > cpl)
1436 goto exception;
1437 } else {
1438 /* nonconforming */
1439 if (rpl > cpl || dpl != cpl)
1440 goto exception;
1441 }
1442 /* CS(RPL) <- CPL */
1443 selector = (selector & 0xfffc) | cpl;
1444 break;
1445 case VCPU_SREG_TR:
1446 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1447 goto exception;
1448 break;
1449 case VCPU_SREG_LDTR:
1450 if (seg_desc.s || seg_desc.type != 2)
1451 goto exception;
1452 break;
1453 default: /* DS, ES, FS, or GS */
1454 /*
1455 * segment is not a data or readable code segment or
1456 * ((segment is a data or nonconforming code segment)
1457 * and (both RPL and CPL > DPL))
1458 */
1459 if ((seg_desc.type & 0xa) == 0x8 ||
1460 (((seg_desc.type & 0xc) != 0xc) &&
1461 (rpl > dpl && cpl > dpl)))
1462 goto exception;
1463 break;
1464 }
1465
1466 if (seg_desc.s) {
1467 /* mark segment as accessed */
1468 seg_desc.type |= 1;
1469 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1470 if (ret != X86EMUL_CONTINUE)
1471 return ret;
1472 }
1473load:
1474 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1475 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1476 return X86EMUL_CONTINUE;
1477exception:
1478 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1479 return X86EMUL_PROPAGATE_FAULT;
1480}
1481
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001482static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1483{
1484 struct decode_cache *c = &ctxt->decode;
1485
1486 c->dst.type = OP_MEM;
1487 c->dst.bytes = c->op_bytes;
1488 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001489 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001490 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001491 c->regs[VCPU_REGS_RSP]);
1492}
1493
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001494static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001495 struct x86_emulate_ops *ops,
1496 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001497{
1498 struct decode_cache *c = &ctxt->decode;
1499 int rc;
1500
Avi Kivity781d0ed2008-11-27 18:00:28 +02001501 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1502 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001503 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001504 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505 return rc;
1506
Avi Kivity350f69d2009-01-05 11:12:40 +02001507 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001508 return rc;
1509}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001510
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001511static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1512 struct x86_emulate_ops *ops,
1513 void *dest, int len)
1514{
1515 int rc;
1516 unsigned long val, change_mask;
1517 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001518 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001519
1520 rc = emulate_pop(ctxt, ops, &val, len);
1521 if (rc != X86EMUL_CONTINUE)
1522 return rc;
1523
1524 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1525 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1526
1527 switch(ctxt->mode) {
1528 case X86EMUL_MODE_PROT64:
1529 case X86EMUL_MODE_PROT32:
1530 case X86EMUL_MODE_PROT16:
1531 if (cpl == 0)
1532 change_mask |= EFLG_IOPL;
1533 if (cpl <= iopl)
1534 change_mask |= EFLG_IF;
1535 break;
1536 case X86EMUL_MODE_VM86:
1537 if (iopl < 3) {
1538 kvm_inject_gp(ctxt->vcpu, 0);
1539 return X86EMUL_PROPAGATE_FAULT;
1540 }
1541 change_mask |= EFLG_IF;
1542 break;
1543 default: /* real mode */
1544 change_mask |= (EFLG_IOPL | EFLG_IF);
1545 break;
1546 }
1547
1548 *(unsigned long *)dest =
1549 (ctxt->eflags & ~change_mask) | (val & change_mask);
1550
1551 return rc;
1552}
1553
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001554static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1555{
1556 struct decode_cache *c = &ctxt->decode;
1557 struct kvm_segment segment;
1558
1559 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1560
1561 c->src.val = segment.selector;
1562 emulate_push(ctxt);
1563}
1564
1565static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1566 struct x86_emulate_ops *ops, int seg)
1567{
1568 struct decode_cache *c = &ctxt->decode;
1569 unsigned long selector;
1570 int rc;
1571
1572 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001573 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001574 return rc;
1575
Gleb Natapov2e873022010-03-18 15:20:18 +02001576 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001577 return rc;
1578}
1579
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001580static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1581{
1582 struct decode_cache *c = &ctxt->decode;
1583 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1584 int reg = VCPU_REGS_RAX;
1585
1586 while (reg <= VCPU_REGS_RDI) {
1587 (reg == VCPU_REGS_RSP) ?
1588 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1589
1590 emulate_push(ctxt);
1591 ++reg;
1592 }
1593}
1594
1595static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1596 struct x86_emulate_ops *ops)
1597{
1598 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001599 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001600 int reg = VCPU_REGS_RDI;
1601
1602 while (reg >= VCPU_REGS_RAX) {
1603 if (reg == VCPU_REGS_RSP) {
1604 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1605 c->op_bytes);
1606 --reg;
1607 }
1608
1609 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001610 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001611 break;
1612 --reg;
1613 }
1614 return rc;
1615}
1616
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001617static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1618 struct x86_emulate_ops *ops)
1619{
1620 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001621
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001622 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001623}
1624
Laurent Vivier05f086f2007-09-24 11:10:55 +02001625static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001626{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001627 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628 switch (c->modrm_reg) {
1629 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001630 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001631 break;
1632 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001633 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001634 break;
1635 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001636 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637 break;
1638 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001639 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640 break;
1641 case 4: /* sal/shl */
1642 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001643 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001644 break;
1645 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001646 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647 break;
1648 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001649 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 break;
1651 }
1652}
1653
1654static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001655 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656{
1657 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001658
1659 switch (c->modrm_reg) {
1660 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001661 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001662 break;
1663 case 2: /* not */
1664 c->dst.val = ~c->dst.val;
1665 break;
1666 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001667 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001668 break;
1669 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001670 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001672 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001673}
1674
1675static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001676 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001677{
1678 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001679
1680 switch (c->modrm_reg) {
1681 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001682 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001685 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001687 case 2: /* call near abs */ {
1688 long int old_eip;
1689 old_eip = c->eip;
1690 c->eip = c->src.val;
1691 c->src.val = old_eip;
1692 emulate_push(ctxt);
1693 break;
1694 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001695 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001696 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697 break;
1698 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001699 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700 break;
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
1705static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001706 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707{
1708 struct decode_cache *c = &ctxt->decode;
1709 u64 old, new;
1710 int rc;
1711
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001712 rc = ops->read_emulated(c->modrm_ea, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001713 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 return rc;
1715
1716 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1717 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1718
1719 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1720 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001721 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722
1723 } else {
1724 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1725 (u32) c->regs[VCPU_REGS_RBX];
1726
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001727 rc = ops->cmpxchg_emulated(c->modrm_ea, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001728 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001729 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001730 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001731 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001732 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001733}
1734
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001735static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1736 struct x86_emulate_ops *ops)
1737{
1738 struct decode_cache *c = &ctxt->decode;
1739 int rc;
1740 unsigned long cs;
1741
1742 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001743 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001744 return rc;
1745 if (c->op_bytes == 4)
1746 c->eip = (u32)c->eip;
1747 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001748 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001749 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001750 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001751 return rc;
1752}
1753
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754static inline int writeback(struct x86_emulate_ctxt *ctxt,
1755 struct x86_emulate_ops *ops)
1756{
1757 int rc;
1758 struct decode_cache *c = &ctxt->decode;
1759
1760 switch (c->dst.type) {
1761 case OP_REG:
1762 /* The 4-byte case *is* correct:
1763 * in 64-bit mode we zero-extend.
1764 */
1765 switch (c->dst.bytes) {
1766 case 1:
1767 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1768 break;
1769 case 2:
1770 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1771 break;
1772 case 4:
1773 *c->dst.ptr = (u32)c->dst.val;
1774 break; /* 64b: zero-ext */
1775 case 8:
1776 *c->dst.ptr = c->dst.val;
1777 break;
1778 }
1779 break;
1780 case OP_MEM:
1781 if (c->lock_prefix)
1782 rc = ops->cmpxchg_emulated(
1783 (unsigned long)c->dst.ptr,
1784 &c->dst.orig_val,
1785 &c->dst.val,
1786 c->dst.bytes,
1787 ctxt->vcpu);
1788 else
1789 rc = ops->write_emulated(
1790 (unsigned long)c->dst.ptr,
1791 &c->dst.val,
1792 c->dst.bytes,
1793 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001794 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001796 break;
1797 case OP_NONE:
1798 /* no writeback */
1799 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800 default:
1801 break;
1802 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001803 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804}
1805
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301806static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001807{
1808 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1809 /*
1810 * an sti; sti; sequence only disable interrupts for the first
1811 * instruction. So, if the last instruction, be it emulated or
1812 * not, left the system with the INT_STI flag enabled, it
1813 * means that the last instruction is an sti. We should not
1814 * leave the flag on in this case. The same goes for mov ss
1815 */
1816 if (!(int_shadow & mask))
1817 ctxt->interruptibility = mask;
1818}
1819
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001820static inline void
1821setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1822 struct kvm_segment *cs, struct kvm_segment *ss)
1823{
1824 memset(cs, 0, sizeof(struct kvm_segment));
1825 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1826 memset(ss, 0, sizeof(struct kvm_segment));
1827
1828 cs->l = 0; /* will be adjusted later */
1829 cs->base = 0; /* flat segment */
1830 cs->g = 1; /* 4kb granularity */
1831 cs->limit = 0xffffffff; /* 4GB limit */
1832 cs->type = 0x0b; /* Read, Execute, Accessed */
1833 cs->s = 1;
1834 cs->dpl = 0; /* will be adjusted later */
1835 cs->present = 1;
1836 cs->db = 1;
1837
1838 ss->unusable = 0;
1839 ss->base = 0; /* flat segment */
1840 ss->limit = 0xffffffff; /* 4GB limit */
1841 ss->g = 1; /* 4kb granularity */
1842 ss->s = 1;
1843 ss->type = 0x03; /* Read/Write, Accessed */
1844 ss->db = 1; /* 32bit stack segment */
1845 ss->dpl = 0;
1846 ss->present = 1;
1847}
1848
1849static int
1850emulate_syscall(struct x86_emulate_ctxt *ctxt)
1851{
1852 struct decode_cache *c = &ctxt->decode;
1853 struct kvm_segment cs, ss;
1854 u64 msr_data;
1855
1856 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001857 if (ctxt->mode == X86EMUL_MODE_REAL ||
1858 ctxt->mode == X86EMUL_MODE_VM86) {
1859 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1860 return X86EMUL_PROPAGATE_FAULT;
1861 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001862
1863 setup_syscalls_segments(ctxt, &cs, &ss);
1864
1865 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1866 msr_data >>= 32;
1867 cs.selector = (u16)(msr_data & 0xfffc);
1868 ss.selector = (u16)(msr_data + 8);
1869
1870 if (is_long_mode(ctxt->vcpu)) {
1871 cs.db = 0;
1872 cs.l = 1;
1873 }
1874 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1875 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1876
1877 c->regs[VCPU_REGS_RCX] = c->eip;
1878 if (is_long_mode(ctxt->vcpu)) {
1879#ifdef CONFIG_X86_64
1880 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1881
1882 kvm_x86_ops->get_msr(ctxt->vcpu,
1883 ctxt->mode == X86EMUL_MODE_PROT64 ?
1884 MSR_LSTAR : MSR_CSTAR, &msr_data);
1885 c->eip = msr_data;
1886
1887 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1888 ctxt->eflags &= ~(msr_data | EFLG_RF);
1889#endif
1890 } else {
1891 /* legacy mode */
1892 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1893 c->eip = (u32)msr_data;
1894
1895 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1896 }
1897
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001898 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899}
1900
Andre Przywara8c604352009-06-18 12:56:01 +02001901static int
1902emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1903{
1904 struct decode_cache *c = &ctxt->decode;
1905 struct kvm_segment cs, ss;
1906 u64 msr_data;
1907
Gleb Natapova0044752010-02-10 14:21:31 +02001908 /* inject #GP if in real mode */
1909 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001910 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001911 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001912 }
1913
1914 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1915 * Therefore, we inject an #UD.
1916 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001917 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1918 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1919 return X86EMUL_PROPAGATE_FAULT;
1920 }
Andre Przywara8c604352009-06-18 12:56:01 +02001921
1922 setup_syscalls_segments(ctxt, &cs, &ss);
1923
1924 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1925 switch (ctxt->mode) {
1926 case X86EMUL_MODE_PROT32:
1927 if ((msr_data & 0xfffc) == 0x0) {
1928 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001929 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001930 }
1931 break;
1932 case X86EMUL_MODE_PROT64:
1933 if (msr_data == 0x0) {
1934 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001935 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001936 }
1937 break;
1938 }
1939
1940 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1941 cs.selector = (u16)msr_data;
1942 cs.selector &= ~SELECTOR_RPL_MASK;
1943 ss.selector = cs.selector + 8;
1944 ss.selector &= ~SELECTOR_RPL_MASK;
1945 if (ctxt->mode == X86EMUL_MODE_PROT64
1946 || is_long_mode(ctxt->vcpu)) {
1947 cs.db = 0;
1948 cs.l = 1;
1949 }
1950
1951 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1952 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1953
1954 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1955 c->eip = msr_data;
1956
1957 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1958 c->regs[VCPU_REGS_RSP] = msr_data;
1959
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001960 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001961}
1962
Andre Przywara4668f052009-06-18 12:56:02 +02001963static int
1964emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1965{
1966 struct decode_cache *c = &ctxt->decode;
1967 struct kvm_segment cs, ss;
1968 u64 msr_data;
1969 int usermode;
1970
Gleb Natapova0044752010-02-10 14:21:31 +02001971 /* inject #GP if in real mode or Virtual 8086 mode */
1972 if (ctxt->mode == X86EMUL_MODE_REAL ||
1973 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001974 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001975 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001976 }
1977
Andre Przywara4668f052009-06-18 12:56:02 +02001978 setup_syscalls_segments(ctxt, &cs, &ss);
1979
1980 if ((c->rex_prefix & 0x8) != 0x0)
1981 usermode = X86EMUL_MODE_PROT64;
1982 else
1983 usermode = X86EMUL_MODE_PROT32;
1984
1985 cs.dpl = 3;
1986 ss.dpl = 3;
1987 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1988 switch (usermode) {
1989 case X86EMUL_MODE_PROT32:
1990 cs.selector = (u16)(msr_data + 16);
1991 if ((msr_data & 0xfffc) == 0x0) {
1992 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001993 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001994 }
1995 ss.selector = (u16)(msr_data + 24);
1996 break;
1997 case X86EMUL_MODE_PROT64:
1998 cs.selector = (u16)(msr_data + 32);
1999 if (msr_data == 0x0) {
2000 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002001 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002002 }
2003 ss.selector = cs.selector + 8;
2004 cs.db = 0;
2005 cs.l = 1;
2006 break;
2007 }
2008 cs.selector |= SELECTOR_RPL_MASK;
2009 ss.selector |= SELECTOR_RPL_MASK;
2010
2011 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
2012 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
2013
2014 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
2015 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
2016
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002017 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002018}
2019
Gleb Natapov9c537242010-03-18 15:20:05 +02002020static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2021 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002022{
2023 int iopl;
2024 if (ctxt->mode == X86EMUL_MODE_REAL)
2025 return false;
2026 if (ctxt->mode == X86EMUL_MODE_VM86)
2027 return true;
2028 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002029 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002030}
2031
2032static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2033 struct x86_emulate_ops *ops,
2034 u16 port, u16 len)
2035{
2036 struct kvm_segment tr_seg;
2037 int r;
2038 u16 io_bitmap_ptr;
2039 u8 perm, bit_idx = port & 0x7;
2040 unsigned mask = (1 << len) - 1;
2041
2042 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
2043 if (tr_seg.unusable)
2044 return false;
2045 if (tr_seg.limit < 103)
2046 return false;
2047 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
2048 NULL);
2049 if (r != X86EMUL_CONTINUE)
2050 return false;
2051 if (io_bitmap_ptr + port/8 > tr_seg.limit)
2052 return false;
2053 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
2054 ctxt->vcpu, NULL);
2055 if (r != X86EMUL_CONTINUE)
2056 return false;
2057 if ((perm >> bit_idx) & mask)
2058 return false;
2059 return true;
2060}
2061
2062static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2063 struct x86_emulate_ops *ops,
2064 u16 port, u16 len)
2065{
Gleb Natapov9c537242010-03-18 15:20:05 +02002066 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002067 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2068 return false;
2069 return true;
2070}
2071
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002072static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt,
2073 struct x86_emulate_ops *ops,
2074 int seg)
2075{
2076 struct desc_struct desc;
2077 if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu))
2078 return get_desc_base(&desc);
2079 else
2080 return ~0;
2081}
2082
2083static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2084 struct x86_emulate_ops *ops,
2085 struct tss_segment_16 *tss)
2086{
2087 struct decode_cache *c = &ctxt->decode;
2088
2089 tss->ip = c->eip;
2090 tss->flag = ctxt->eflags;
2091 tss->ax = c->regs[VCPU_REGS_RAX];
2092 tss->cx = c->regs[VCPU_REGS_RCX];
2093 tss->dx = c->regs[VCPU_REGS_RDX];
2094 tss->bx = c->regs[VCPU_REGS_RBX];
2095 tss->sp = c->regs[VCPU_REGS_RSP];
2096 tss->bp = c->regs[VCPU_REGS_RBP];
2097 tss->si = c->regs[VCPU_REGS_RSI];
2098 tss->di = c->regs[VCPU_REGS_RDI];
2099
2100 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2101 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2102 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2103 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2104 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2105}
2106
2107static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2108 struct x86_emulate_ops *ops,
2109 struct tss_segment_16 *tss)
2110{
2111 struct decode_cache *c = &ctxt->decode;
2112 int ret;
2113
2114 c->eip = tss->ip;
2115 ctxt->eflags = tss->flag | 2;
2116 c->regs[VCPU_REGS_RAX] = tss->ax;
2117 c->regs[VCPU_REGS_RCX] = tss->cx;
2118 c->regs[VCPU_REGS_RDX] = tss->dx;
2119 c->regs[VCPU_REGS_RBX] = tss->bx;
2120 c->regs[VCPU_REGS_RSP] = tss->sp;
2121 c->regs[VCPU_REGS_RBP] = tss->bp;
2122 c->regs[VCPU_REGS_RSI] = tss->si;
2123 c->regs[VCPU_REGS_RDI] = tss->di;
2124
2125 /*
2126 * SDM says that segment selectors are loaded before segment
2127 * descriptors
2128 */
2129 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2130 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2131 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2132 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2133 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2134
2135 /*
2136 * Now load segment descriptors. If fault happenes at this stage
2137 * it is handled in a context of new task
2138 */
2139 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2140 if (ret != X86EMUL_CONTINUE)
2141 return ret;
2142 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2143 if (ret != X86EMUL_CONTINUE)
2144 return ret;
2145 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2146 if (ret != X86EMUL_CONTINUE)
2147 return ret;
2148 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2149 if (ret != X86EMUL_CONTINUE)
2150 return ret;
2151 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2152 if (ret != X86EMUL_CONTINUE)
2153 return ret;
2154
2155 return X86EMUL_CONTINUE;
2156}
2157
2158static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2159 struct x86_emulate_ops *ops,
2160 u16 tss_selector, u16 old_tss_sel,
2161 ulong old_tss_base, struct desc_struct *new_desc)
2162{
2163 struct tss_segment_16 tss_seg;
2164 int ret;
2165 u32 err, new_tss_base = get_desc_base(new_desc);
2166
2167 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2168 &err);
2169 if (ret == X86EMUL_PROPAGATE_FAULT) {
2170 /* FIXME: need to provide precise fault address */
2171 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2172 return ret;
2173 }
2174
2175 save_state_to_tss16(ctxt, ops, &tss_seg);
2176
2177 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2178 &err);
2179 if (ret == X86EMUL_PROPAGATE_FAULT) {
2180 /* FIXME: need to provide precise fault address */
2181 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2182 return ret;
2183 }
2184
2185 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2186 &err);
2187 if (ret == X86EMUL_PROPAGATE_FAULT) {
2188 /* FIXME: need to provide precise fault address */
2189 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2190 return ret;
2191 }
2192
2193 if (old_tss_sel != 0xffff) {
2194 tss_seg.prev_task_link = old_tss_sel;
2195
2196 ret = ops->write_std(new_tss_base,
2197 &tss_seg.prev_task_link,
2198 sizeof tss_seg.prev_task_link,
2199 ctxt->vcpu, &err);
2200 if (ret == X86EMUL_PROPAGATE_FAULT) {
2201 /* FIXME: need to provide precise fault address */
2202 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2203 return ret;
2204 }
2205 }
2206
2207 return load_state_from_tss16(ctxt, ops, &tss_seg);
2208}
2209
2210static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2211 struct x86_emulate_ops *ops,
2212 struct tss_segment_32 *tss)
2213{
2214 struct decode_cache *c = &ctxt->decode;
2215
2216 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2217 tss->eip = c->eip;
2218 tss->eflags = ctxt->eflags;
2219 tss->eax = c->regs[VCPU_REGS_RAX];
2220 tss->ecx = c->regs[VCPU_REGS_RCX];
2221 tss->edx = c->regs[VCPU_REGS_RDX];
2222 tss->ebx = c->regs[VCPU_REGS_RBX];
2223 tss->esp = c->regs[VCPU_REGS_RSP];
2224 tss->ebp = c->regs[VCPU_REGS_RBP];
2225 tss->esi = c->regs[VCPU_REGS_RSI];
2226 tss->edi = c->regs[VCPU_REGS_RDI];
2227
2228 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2229 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2230 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2231 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2232 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2233 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2234 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2235}
2236
2237static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2238 struct x86_emulate_ops *ops,
2239 struct tss_segment_32 *tss)
2240{
2241 struct decode_cache *c = &ctxt->decode;
2242 int ret;
2243
2244 ops->set_cr(3, tss->cr3, ctxt->vcpu);
2245 c->eip = tss->eip;
2246 ctxt->eflags = tss->eflags | 2;
2247 c->regs[VCPU_REGS_RAX] = tss->eax;
2248 c->regs[VCPU_REGS_RCX] = tss->ecx;
2249 c->regs[VCPU_REGS_RDX] = tss->edx;
2250 c->regs[VCPU_REGS_RBX] = tss->ebx;
2251 c->regs[VCPU_REGS_RSP] = tss->esp;
2252 c->regs[VCPU_REGS_RBP] = tss->ebp;
2253 c->regs[VCPU_REGS_RSI] = tss->esi;
2254 c->regs[VCPU_REGS_RDI] = tss->edi;
2255
2256 /*
2257 * SDM says that segment selectors are loaded before segment
2258 * descriptors
2259 */
2260 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2261 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2262 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2263 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2264 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2265 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2266 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2267
2268 /*
2269 * Now load segment descriptors. If fault happenes at this stage
2270 * it is handled in a context of new task
2271 */
2272 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2273 if (ret != X86EMUL_CONTINUE)
2274 return ret;
2275 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2276 if (ret != X86EMUL_CONTINUE)
2277 return ret;
2278 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2279 if (ret != X86EMUL_CONTINUE)
2280 return ret;
2281 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2282 if (ret != X86EMUL_CONTINUE)
2283 return ret;
2284 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2285 if (ret != X86EMUL_CONTINUE)
2286 return ret;
2287 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2288 if (ret != X86EMUL_CONTINUE)
2289 return ret;
2290 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2291 if (ret != X86EMUL_CONTINUE)
2292 return ret;
2293
2294 return X86EMUL_CONTINUE;
2295}
2296
2297static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2298 struct x86_emulate_ops *ops,
2299 u16 tss_selector, u16 old_tss_sel,
2300 ulong old_tss_base, struct desc_struct *new_desc)
2301{
2302 struct tss_segment_32 tss_seg;
2303 int ret;
2304 u32 err, new_tss_base = get_desc_base(new_desc);
2305
2306 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2307 &err);
2308 if (ret == X86EMUL_PROPAGATE_FAULT) {
2309 /* FIXME: need to provide precise fault address */
2310 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2311 return ret;
2312 }
2313
2314 save_state_to_tss32(ctxt, ops, &tss_seg);
2315
2316 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2317 &err);
2318 if (ret == X86EMUL_PROPAGATE_FAULT) {
2319 /* FIXME: need to provide precise fault address */
2320 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2321 return ret;
2322 }
2323
2324 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2325 &err);
2326 if (ret == X86EMUL_PROPAGATE_FAULT) {
2327 /* FIXME: need to provide precise fault address */
2328 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2329 return ret;
2330 }
2331
2332 if (old_tss_sel != 0xffff) {
2333 tss_seg.prev_task_link = old_tss_sel;
2334
2335 ret = ops->write_std(new_tss_base,
2336 &tss_seg.prev_task_link,
2337 sizeof tss_seg.prev_task_link,
2338 ctxt->vcpu, &err);
2339 if (ret == X86EMUL_PROPAGATE_FAULT) {
2340 /* FIXME: need to provide precise fault address */
2341 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2342 return ret;
2343 }
2344 }
2345
2346 return load_state_from_tss32(ctxt, ops, &tss_seg);
2347}
2348
2349static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2350 struct x86_emulate_ops *ops,
2351 u16 tss_selector, int reason)
2352{
2353 struct desc_struct curr_tss_desc, next_tss_desc;
2354 int ret;
2355 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2356 ulong old_tss_base =
2357 get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002358 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002359
2360 /* FIXME: old_tss_base == ~0 ? */
2361
2362 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2363 if (ret != X86EMUL_CONTINUE)
2364 return ret;
2365 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2366 if (ret != X86EMUL_CONTINUE)
2367 return ret;
2368
2369 /* FIXME: check that next_tss_desc is tss */
2370
2371 if (reason != TASK_SWITCH_IRET) {
2372 if ((tss_selector & 3) > next_tss_desc.dpl ||
2373 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2374 kvm_inject_gp(ctxt->vcpu, 0);
2375 return X86EMUL_PROPAGATE_FAULT;
2376 }
2377 }
2378
Gleb Natapovceffb452010-03-18 15:20:19 +02002379 desc_limit = desc_limit_scaled(&next_tss_desc);
2380 if (!next_tss_desc.p ||
2381 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2382 desc_limit < 0x2b)) {
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002383 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2384 tss_selector & 0xfffc);
2385 return X86EMUL_PROPAGATE_FAULT;
2386 }
2387
2388 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2389 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2390 write_segment_descriptor(ctxt, ops, old_tss_sel,
2391 &curr_tss_desc);
2392 }
2393
2394 if (reason == TASK_SWITCH_IRET)
2395 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2396
2397 /* set back link to prev task only if NT bit is set in eflags
2398 note that old_tss_sel is not used afetr this point */
2399 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2400 old_tss_sel = 0xffff;
2401
2402 if (next_tss_desc.type & 8)
2403 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2404 old_tss_base, &next_tss_desc);
2405 else
2406 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2407 old_tss_base, &next_tss_desc);
2408
2409 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2410 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2411
2412 if (reason != TASK_SWITCH_IRET) {
2413 next_tss_desc.type |= (1 << 1); /* set busy flag */
2414 write_segment_descriptor(ctxt, ops, tss_selector,
2415 &next_tss_desc);
2416 }
2417
2418 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2419 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2420 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2421
2422 return ret;
2423}
2424
2425int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2426 struct x86_emulate_ops *ops,
2427 u16 tss_selector, int reason)
2428{
2429 struct decode_cache *c = &ctxt->decode;
2430 int rc;
2431
2432 memset(c, 0, sizeof(struct decode_cache));
2433 c->eip = ctxt->eip;
2434 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
2435
2436 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason);
2437
2438 if (rc == X86EMUL_CONTINUE) {
2439 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
2440 kvm_rip_write(ctxt->vcpu, c->eip);
2441 }
2442
2443 return rc;
2444}
2445
Gleb Natapova682e352010-03-18 15:20:21 +02002446static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002447 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002448{
2449 struct decode_cache *c = &ctxt->decode;
2450 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2451
Gleb Natapovd9271122010-03-18 15:20:22 +02002452 register_address_increment(c, &c->regs[reg], df * op->bytes);
2453 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002454}
2455
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002456int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002457x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002458{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002459 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002460 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002461 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002462 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002463
Glauber Costa310b5d32009-05-12 16:21:06 -04002464 ctxt->interruptibility = 0;
2465
Laurent Vivier34273182007-09-18 11:27:37 +02002466 /* Shadow copy of register state. Committed on successful emulation.
2467 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
2468 * modify them.
2469 */
2470
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002471 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02002472
Gleb Natapov11616242010-02-11 14:43:14 +02002473 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2474 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2475 goto done;
2476 }
2477
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002478 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002479 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002480 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2481 goto done;
2482 }
2483
Gleb Natapove92805a2010-02-10 14:21:35 +02002484 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002485 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002486 kvm_inject_gp(ctxt->vcpu, 0);
2487 goto done;
2488 }
2489
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002490 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002491 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002492 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002493 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002494 string_done:
2495 ctxt->restart = false;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002496 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002497 goto done;
2498 }
2499 /* The second termination condition only applies for REPE
2500 * and REPNE. Test if the repeat string operation prefix is
2501 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2502 * corresponding termination condition according to:
2503 * - if REPE/REPZ and ZF = 0 then done
2504 * - if REPNE/REPNZ and ZF = 1 then done
2505 */
2506 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002507 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002508 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002509 ((ctxt->eflags & EFLG_ZF) == 0))
2510 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002511 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002512 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2513 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002514 }
Gleb Natapov063db062010-03-18 15:20:06 +02002515 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002516 }
2517
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002518 if (c->src.type == OP_MEM) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002519 rc = ops->read_emulated((unsigned long)c->src.ptr,
2520 &c->src.val,
2521 c->src.bytes,
2522 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002523 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002524 goto done;
2525 c->src.orig_val = c->src.val;
2526 }
2527
Gleb Natapove35b7b92010-02-25 16:36:42 +02002528 if (c->src2.type == OP_MEM) {
Gleb Natapove35b7b92010-02-25 16:36:42 +02002529 rc = ops->read_emulated((unsigned long)c->src2.ptr,
2530 &c->src2.val,
2531 c->src2.bytes,
2532 ctxt->vcpu);
2533 if (rc != X86EMUL_CONTINUE)
2534 goto done;
2535 }
2536
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002537 if ((c->d & DstMask) == ImplicitOps)
2538 goto special_insn;
2539
2540
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002541 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2542 /* optimisation - avoid slow emulated read if Mov */
2543 rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val,
2544 c->dst.bytes, ctxt->vcpu);
2545 if (rc != X86EMUL_CONTINUE)
2546 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002547 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002548 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002549
Avi Kivity018a98d2007-11-27 19:30:56 +02002550special_insn:
2551
Laurent Viviere4e03de2007-09-18 11:52:50 +02002552 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 goto twobyte_insn;
2554
Laurent Viviere4e03de2007-09-18 11:52:50 +02002555 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 case 0x00 ... 0x05:
2557 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002558 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002560 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002561 emulate_push_sreg(ctxt, VCPU_SREG_ES);
2562 break;
2563 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002564 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002565 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002566 goto done;
2567 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 case 0x08 ... 0x0d:
2569 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002570 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002572 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002573 emulate_push_sreg(ctxt, VCPU_SREG_CS);
2574 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 case 0x10 ... 0x15:
2576 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002577 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002579 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002580 emulate_push_sreg(ctxt, VCPU_SREG_SS);
2581 break;
2582 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002583 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002584 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002585 goto done;
2586 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 case 0x18 ... 0x1d:
2588 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002589 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002591 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002592 emulate_push_sreg(ctxt, VCPU_SREG_DS);
2593 break;
2594 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002595 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002596 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002597 goto done;
2598 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002599 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002601 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 break;
2603 case 0x28 ... 0x2d:
2604 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002605 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 break;
2607 case 0x30 ... 0x35:
2608 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002609 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 break;
2611 case 0x38 ... 0x3d:
2612 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002613 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002615 case 0x40 ... 0x47: /* inc r16/r32 */
2616 emulate_1op("inc", c->dst, ctxt->eflags);
2617 break;
2618 case 0x48 ... 0x4f: /* dec r16/r32 */
2619 emulate_1op("dec", c->dst, ctxt->eflags);
2620 break;
2621 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02002622 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02002623 break;
2624 case 0x58 ... 0x5f: /* pop reg */
2625 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002626 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002627 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002628 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002629 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002630 case 0x60: /* pusha */
2631 emulate_pusha(ctxt);
2632 break;
2633 case 0x61: /* popa */
2634 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002635 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002636 goto done;
2637 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002639 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002641 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002643 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002644 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002645 emulate_push(ctxt);
2646 break;
2647 case 0x6c: /* insb */
2648 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002649 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002650 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002651 c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002652 kvm_inject_gp(ctxt->vcpu, 0);
2653 goto done;
2654 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002655 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2656 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002657 goto done; /* IO is needed, skip writeback */
2658 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002659 case 0x6e: /* outsb */
2660 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002661 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002662 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002663 c->src.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002664 kvm_inject_gp(ctxt->vcpu, 0);
2665 goto done;
2666 }
Gleb Natapov79729952010-03-18 15:20:24 +02002667 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2668 &c->src.val, 1, ctxt->vcpu);
2669
2670 c->dst.type = OP_NONE; /* nothing to writeback */
2671 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002672 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002673 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002674 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002675 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002677 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 case 0:
2679 goto add;
2680 case 1:
2681 goto or;
2682 case 2:
2683 goto adc;
2684 case 3:
2685 goto sbb;
2686 case 4:
2687 goto and;
2688 case 5:
2689 goto sub;
2690 case 6:
2691 goto xor;
2692 case 7:
2693 goto cmp;
2694 }
2695 break;
2696 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002697 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 break;
2699 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002700 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002702 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002704 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705 break;
2706 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002707 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 break;
2709 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002710 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 break; /* 64b reg: zero-extend */
2712 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002713 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 break;
2715 }
2716 /*
2717 * Write back the memory destination with implicit LOCK
2718 * prefix.
2719 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002720 c->dst.val = c->src.val;
2721 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002724 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002725 case 0x8c: { /* mov r/m, sreg */
2726 struct kvm_segment segreg;
2727
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002728 if (c->modrm_reg <= VCPU_SREG_GS)
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002729 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2730 else {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002731 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2732 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002733 }
2734 c->dst.val = segreg.selector;
2735 break;
2736 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002737 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002738 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002739 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002740 case 0x8e: { /* mov seg, r/m16 */
2741 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002742
2743 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002744
Gleb Natapovc6975182010-02-18 12:15:01 +02002745 if (c->modrm_reg == VCPU_SREG_CS ||
2746 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002747 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2748 goto done;
2749 }
2750
Glauber Costa310b5d32009-05-12 16:21:06 -04002751 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002752 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002753
Gleb Natapov2e873022010-03-18 15:20:18 +02002754 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002755
2756 c->dst.type = OP_NONE; /* Disable writeback. */
2757 break;
2758 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002760 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002761 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002764 case 0x90: /* nop / xchg r8,rax */
2765 if (!(c->rex_prefix & 1)) { /* nop */
2766 c->dst.type = OP_NONE;
2767 break;
2768 }
2769 case 0x91 ... 0x97: /* xchg reg,rax */
2770 c->src.type = c->dst.type = OP_REG;
2771 c->src.bytes = c->dst.bytes = c->op_bytes;
2772 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2773 c->src.val = *(c->src.ptr);
2774 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002775 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002776 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002777 emulate_push(ctxt);
2778 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002779 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002780 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002781 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002782 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002783 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2784 if (rc != X86EMUL_CONTINUE)
2785 goto done;
2786 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002787 case 0xa0 ... 0xa1: /* mov */
2788 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2789 c->dst.val = c->src.val;
2790 break;
2791 case 0xa2 ... 0xa3: /* mov */
2792 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002795 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002797 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002798 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002799 goto cmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002801 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 break;
2803 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002804 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 case 0xae ... 0xaf: /* scas */
2806 DPRINTF("Urk! I don't handle SCAS.\n");
2807 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002808 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002809 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002810 case 0xc0 ... 0xc1:
2811 emulate_grp2(ctxt);
2812 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002813 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002814 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002815 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002816 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002817 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002818 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2819 mov:
2820 c->dst.val = c->src.val;
2821 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002822 case 0xcb: /* ret far */
2823 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002824 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002825 goto done;
2826 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002827 case 0xd0 ... 0xd1: /* Grp2 */
2828 c->src.val = 1;
2829 emulate_grp2(ctxt);
2830 break;
2831 case 0xd2 ... 0xd3: /* Grp2 */
2832 c->src.val = c->regs[VCPU_REGS_RCX];
2833 emulate_grp2(ctxt);
2834 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002835 case 0xe4: /* inb */
2836 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002837 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002838 case 0xe6: /* outb */
2839 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002840 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002841 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002842 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002843 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002844 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002845 emulate_push(ctxt);
2846 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002847 }
2848 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002849 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002850 case 0xea: /* jmp far */
Gleb Natapovea79849d2010-02-25 16:36:43 +02002851 jump_far:
Gleb Natapov2e873022010-03-18 15:20:18 +02002852 if (load_segment_descriptor(ctxt, ops, c->src2.val,
2853 VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002854 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002855
Gleb Natapov782b8772009-04-12 13:36:25 +03002856 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002857 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002858 case 0xeb:
2859 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002860 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002861 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002862 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002863 case 0xec: /* in al,dx */
2864 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002865 c->src.val = c->regs[VCPU_REGS_RDX];
2866 do_io_in:
2867 c->dst.bytes = min(c->dst.bytes, 4u);
2868 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002869 kvm_inject_gp(ctxt->vcpu, 0);
2870 goto done;
2871 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002872 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2873 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002874 goto done; /* IO is needed */
2875 break;
2876 case 0xee: /* out al,dx */
2877 case 0xef: /* out (e/r)ax,dx */
2878 c->src.val = c->regs[VCPU_REGS_RDX];
2879 do_io_out:
2880 c->dst.bytes = min(c->dst.bytes, 4u);
2881 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
2882 kvm_inject_gp(ctxt->vcpu, 0);
2883 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002884 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002885 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2886 ctxt->vcpu);
2887 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002888 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002889 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002890 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002891 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002892 case 0xf5: /* cmc */
2893 /* complement carry flag from eflags reg */
2894 ctxt->eflags ^= EFLG_CF;
2895 c->dst.type = OP_NONE; /* Disable writeback. */
2896 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002897 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002898 if (!emulate_grp3(ctxt, ops))
2899 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002900 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002901 case 0xf8: /* clc */
2902 ctxt->eflags &= ~EFLG_CF;
2903 c->dst.type = OP_NONE; /* Disable writeback. */
2904 break;
2905 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002906 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002907 kvm_inject_gp(ctxt->vcpu, 0);
2908 else {
2909 ctxt->eflags &= ~X86_EFLAGS_IF;
2910 c->dst.type = OP_NONE; /* Disable writeback. */
2911 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002912 break;
2913 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002914 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002915 kvm_inject_gp(ctxt->vcpu, 0);
2916 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002917 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002918 ctxt->eflags |= X86_EFLAGS_IF;
2919 c->dst.type = OP_NONE; /* Disable writeback. */
2920 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002921 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002922 case 0xfc: /* cld */
2923 ctxt->eflags &= ~EFLG_DF;
2924 c->dst.type = OP_NONE; /* Disable writeback. */
2925 break;
2926 case 0xfd: /* std */
2927 ctxt->eflags |= EFLG_DF;
2928 c->dst.type = OP_NONE; /* Disable writeback. */
2929 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002930 case 0xfe: /* Grp4 */
2931 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002932 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002933 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002934 goto done;
2935 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002936 case 0xff: /* Grp5 */
2937 if (c->modrm_reg == 5)
2938 goto jump_far;
2939 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002941
2942writeback:
2943 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002944 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002945 goto done;
2946
Gleb Natapov5cd21912010-03-18 15:20:26 +02002947 /*
2948 * restore dst type in case the decoding will be reused
2949 * (happens for string instruction )
2950 */
2951 c->dst.type = saved_dst_type;
2952
Gleb Natapova682e352010-03-18 15:20:21 +02002953 if ((c->d & SrcMask) == SrcSI)
2954 string_addr_inc(ctxt, seg_override_base(ctxt, c), VCPU_REGS_RSI,
Gleb Natapovd9271122010-03-18 15:20:22 +02002955 &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02002956
2957 if ((c->d & DstMask) == DstDI)
Gleb Natapovd9271122010-03-18 15:20:22 +02002958 string_addr_inc(ctxt, es_base(ctxt), VCPU_REGS_RDI, &c->dst);
2959
Gleb Natapov5cd21912010-03-18 15:20:26 +02002960 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02002961 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02002962 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02002963 /*
2964 * Re-enter guest when pio read ahead buffer is empty or,
2965 * if it is not used, after each 1024 iteration.
2966 */
2967 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
2968 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02002969 ctxt->restart = false;
2970 }
Gleb Natapova682e352010-03-18 15:20:21 +02002971
Avi Kivity018a98d2007-11-27 19:30:56 +02002972 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002973 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002974 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002975
2976done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02002977 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978
2979twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002980 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002982 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 u16 size;
2984 unsigned long address;
2985
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002986 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002987 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002988 goto cannot_emulate;
2989
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002990 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002991 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002992 goto done;
2993
Avi Kivity33e38852008-05-21 15:34:25 +03002994 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02002995 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03002996 /* Disable writeback. */
2997 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003000 rc = read_descriptor(ctxt, ops, c->src.ptr,
3001 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003002 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 goto done;
3004 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003005 /* Disable writeback. */
3006 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003008 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003009 if (c->modrm_mod == 3) {
3010 switch (c->modrm_rm) {
3011 case 1:
3012 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003013 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003014 goto done;
3015 break;
3016 default:
3017 goto cannot_emulate;
3018 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003019 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003020 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003021 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003022 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003023 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003024 goto done;
3025 realmode_lidt(ctxt->vcpu, size, address);
3026 }
Avi Kivity16286d02008-04-14 14:40:50 +03003027 /* Disable writeback. */
3028 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 break;
3030 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003031 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003032 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 break;
3034 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003035 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3036 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003037 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003039 case 5: /* not defined */
3040 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3041 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003043 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003044 /* Disable writeback. */
3045 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 break;
3047 default:
3048 goto cannot_emulate;
3049 }
3050 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003051 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003052 rc = emulate_syscall(ctxt);
3053 if (rc != X86EMUL_CONTINUE)
3054 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003055 else
3056 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003057 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003058 case 0x06:
3059 emulate_clts(ctxt->vcpu);
3060 c->dst.type = OP_NONE;
3061 break;
3062 case 0x08: /* invd */
3063 case 0x09: /* wbinvd */
3064 case 0x0d: /* GrpP (prefetch) */
3065 case 0x18: /* Grp16 (prefetch/nop) */
3066 c->dst.type = OP_NONE;
3067 break;
3068 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003069 switch (c->modrm_reg) {
3070 case 1:
3071 case 5 ... 7:
3072 case 9 ... 15:
3073 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3074 goto done;
3075 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003076 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003077 c->dst.type = OP_NONE; /* no writeback */
3078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003080 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3081 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3082 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3083 goto done;
3084 }
3085 emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003086 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003088 case 0x22: /* mov reg, cr */
Gleb Natapov52a46612010-03-18 15:20:03 +02003089 ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003090 c->dst.type = OP_NONE;
3091 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003093 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3094 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3095 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3096 goto done;
3097 }
3098 emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003099 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003101 case 0x30:
3102 /* wrmsr */
3103 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3104 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003105 if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003106 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003107 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 }
3109 rc = X86EMUL_CONTINUE;
3110 c->dst.type = OP_NONE;
3111 break;
3112 case 0x32:
3113 /* rdmsr */
Takuya Yoshikawa0e4176a2010-02-12 16:00:55 +09003114 if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003115 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003116 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003117 } else {
3118 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3119 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3120 }
3121 rc = X86EMUL_CONTINUE;
3122 c->dst.type = OP_NONE;
3123 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003124 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003125 rc = emulate_sysenter(ctxt);
3126 if (rc != X86EMUL_CONTINUE)
3127 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003128 else
3129 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003130 break;
3131 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003132 rc = emulate_sysexit(ctxt);
3133 if (rc != X86EMUL_CONTINUE)
3134 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003135 else
3136 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003137 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003139 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003140 if (!test_cc(c->b, ctxt->eflags))
3141 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003143 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003144 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003145 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003146 c->dst.type = OP_NONE;
3147 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003148 case 0xa0: /* push fs */
3149 emulate_push_sreg(ctxt, VCPU_SREG_FS);
3150 break;
3151 case 0xa1: /* pop fs */
3152 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003153 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003154 goto done;
3155 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003156 case 0xa3:
3157 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003158 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003159 /* only subword offset */
3160 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003161 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003162 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003163 case 0xa4: /* shld imm8, r, r/m */
3164 case 0xa5: /* shld cl, r, r/m */
3165 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3166 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003167 case 0xa8: /* push gs */
3168 emulate_push_sreg(ctxt, VCPU_SREG_GS);
3169 break;
3170 case 0xa9: /* pop gs */
3171 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003172 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003173 goto done;
3174 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003175 case 0xab:
3176 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003177 /* only subword offset */
3178 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003179 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003180 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003181 case 0xac: /* shrd imm8, r, r/m */
3182 case 0xad: /* shrd cl, r, r/m */
3183 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3184 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003185 case 0xae: /* clflush */
3186 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 case 0xb0 ... 0xb1: /* cmpxchg */
3188 /*
3189 * Save real source value, then compare EAX against
3190 * destination.
3191 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003192 c->src.orig_val = c->src.val;
3193 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003194 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3195 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003197 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198 } else {
3199 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003200 c->dst.type = OP_REG;
3201 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 }
3203 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 case 0xb3:
3205 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003206 /* only subword offset */
3207 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003208 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003211 c->dst.bytes = c->op_bytes;
3212 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3213 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003216 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 case 0:
3218 goto bt;
3219 case 1:
3220 goto bts;
3221 case 2:
3222 goto btr;
3223 case 3:
3224 goto btc;
3225 }
3226 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003227 case 0xbb:
3228 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003229 /* only subword offset */
3230 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003231 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003234 c->dst.bytes = c->op_bytes;
3235 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3236 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003238 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003239 c->dst.bytes = c->op_bytes;
3240 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3241 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003242 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003244 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003245 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003246 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003247 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003248 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 }
3250 goto writeback;
3251
3252cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003253 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 return -1;
3255}