blob: 286572a5675b03f612bc00202f4140c96cda7177 [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
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
49#define ByteOp (1<<0) /* 8-bit operands. */
50/* Destination operand type. */
51#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020054#define DstAcc (4<<1) /* Destination Accumulator */
Gleb Natapova682e352010-03-18 15:20:21 +020055#define DstDI (5<<1) /* Destination is in ES:(E)DI */
Gleb Natapov6550e1f2010-03-21 13:08:21 +020056#define DstMem64 (6<<1) /* 64bit memory operand */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020057#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Gleb Natapov341de7e2009-04-12 13:36:41 +030073#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080074/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030075#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030077#define Mov (1<<9)
78#define BitOp (1<<10)
79#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020080#define String (1<<12) /* String instruction (rep capable) */
81#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020082#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
83#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
84#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020086#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020087#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030088#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010089/* Source 2 operand type */
90#define Src2None (0<<29)
91#define Src2CL (1<<29)
92#define Src2ImmByte (2<<29)
93#define Src2One (3<<29)
94#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,
Wei Yongjune97e8832010-07-06 16:51:09 +0800126 ByteOp | 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,
Mohammed Gamalabc19082010-05-12 01:39:21 +0300130 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 /* 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,
Mohammed Gamal222b7c52010-05-12 01:39:22 +0300134 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135 /* 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,
Wei Yongjunb16b2b72010-07-06 16:52:53 +0800172 DstMem | SrcNone | ModRM | Mov, ModRM | DstReg,
Wei Yongjuna5046e62010-07-06 16:49:05 +0800173 ImplicitOps | SrcMem16 | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300174 /* 0x90 - 0x97 */
175 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
176 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300177 0, 0, SrcImmFAddr | 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 */
Mohammed Gamaldfb507c2010-05-11 22:22:40 +0300185 DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
Gleb Natapova682e352010-03-18 15:20:21 +0200186 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,
Gleb Natapov414e6272010-04-28 19:15:26 +0300217 SrcImmFAddr | 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] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800339 ByteOp | SrcImm | DstMem | ModRM, ByteOp | SrcImm | DstMem | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200340 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
341 0, 0, 0, 0,
342 [Group3*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800343 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
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 Natapov414e6272010-04-28 19:15:26 +0300352 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200353 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 Natapov6550e1f2010-03-21 13:08:21 +0200363 0, DstMem64 | 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
Gleb Natapov414e6272010-04-28 19:15:26 +0300578#define insn_fetch_arr(_arr, _size, _eip) \
579({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
580 if (rc != X86EMUL_CONTINUE) \
581 goto done; \
582 (_eip) += (_size); \
583})
584
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800585static inline unsigned long ad_mask(struct decode_cache *c)
586{
587 return (1UL << (c->ad_bytes << 3)) - 1;
588}
589
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800591static inline unsigned long
592address_mask(struct decode_cache *c, unsigned long reg)
593{
594 if (c->ad_bytes == sizeof(unsigned long))
595 return reg;
596 else
597 return reg & ad_mask(c);
598}
599
600static inline unsigned long
601register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
602{
603 return base + address_mask(c, reg);
604}
605
Harvey Harrison7a9572752008-02-19 07:40:41 -0800606static inline void
607register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
608{
609 if (c->ad_bytes == sizeof(unsigned long))
610 *reg += inc;
611 else
612 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
613}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614
Harvey Harrison7a9572752008-02-19 07:40:41 -0800615static inline void jmp_rel(struct decode_cache *c, int rel)
616{
617 register_address_increment(c, &c->eip, rel);
618}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300619
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300620static void set_seg_override(struct decode_cache *c, int seg)
621{
622 c->has_seg_override = true;
623 c->seg_override = seg;
624}
625
Gleb Natapov79168fd2010-04-28 19:15:30 +0300626static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
627 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300628{
629 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
630 return 0;
631
Gleb Natapov79168fd2010-04-28 19:15:30 +0300632 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300633}
634
635static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300636 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300637 struct decode_cache *c)
638{
639 if (!c->has_seg_override)
640 return 0;
641
Gleb Natapov79168fd2010-04-28 19:15:30 +0300642 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300643}
644
Gleb Natapov79168fd2010-04-28 19:15:30 +0300645static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
646 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300647{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300648 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300649}
650
Gleb Natapov79168fd2010-04-28 19:15:30 +0300651static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
652 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300653{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300654 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300655}
656
Gleb Natapov54b84862010-04-28 19:15:44 +0300657static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
658 u32 error, bool valid)
659{
660 ctxt->exception = vec;
661 ctxt->error_code = error;
662 ctxt->error_code_valid = valid;
663 ctxt->restart = false;
664}
665
666static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
667{
668 emulate_exception(ctxt, GP_VECTOR, err, true);
669}
670
671static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
672 int err)
673{
674 ctxt->cr2 = addr;
675 emulate_exception(ctxt, PF_VECTOR, err, true);
676}
677
678static void emulate_ud(struct x86_emulate_ctxt *ctxt)
679{
680 emulate_exception(ctxt, UD_VECTOR, 0, false);
681}
682
683static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
684{
685 emulate_exception(ctxt, TS_VECTOR, err, true);
686}
687
Avi Kivity62266862007-11-20 13:15:52 +0200688static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
689 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300690 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200691{
692 struct fetch_cache *fc = &ctxt->decode.fetch;
693 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300694 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200695
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300696 if (eip == fc->end) {
697 cur_size = fc->end - fc->start;
698 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
699 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
700 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900701 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200702 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300703 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200704 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300705 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900706 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200707}
708
709static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
710 struct x86_emulate_ops *ops,
711 unsigned long eip, void *dest, unsigned size)
712{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900713 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200714
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200715 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200716 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200717 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200718 while (size--) {
719 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900720 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200721 return rc;
722 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900723 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200724}
725
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000726/*
727 * Given the 'reg' portion of a ModRM byte, and a register block, return a
728 * pointer into the block that addresses the relevant register.
729 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
730 */
731static void *decode_register(u8 modrm_reg, unsigned long *regs,
732 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733{
734 void *p;
735
736 p = &regs[modrm_reg];
737 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
738 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
739 return p;
740}
741
742static int read_descriptor(struct x86_emulate_ctxt *ctxt,
743 struct x86_emulate_ops *ops,
744 void *ptr,
745 u16 *size, unsigned long *address, int op_bytes)
746{
747 int rc;
748
749 if (op_bytes == 2)
750 op_bytes = 3;
751 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300752 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200753 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900754 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300756 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200757 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 return rc;
759}
760
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300761static int test_cc(unsigned int condition, unsigned int flags)
762{
763 int rc = 0;
764
765 switch ((condition & 15) >> 1) {
766 case 0: /* o */
767 rc |= (flags & EFLG_OF);
768 break;
769 case 1: /* b/c/nae */
770 rc |= (flags & EFLG_CF);
771 break;
772 case 2: /* z/e */
773 rc |= (flags & EFLG_ZF);
774 break;
775 case 3: /* be/na */
776 rc |= (flags & (EFLG_CF|EFLG_ZF));
777 break;
778 case 4: /* s */
779 rc |= (flags & EFLG_SF);
780 break;
781 case 5: /* p/pe */
782 rc |= (flags & EFLG_PF);
783 break;
784 case 7: /* le/ng */
785 rc |= (flags & EFLG_ZF);
786 /* fall through */
787 case 6: /* l/nge */
788 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
789 break;
790 }
791
792 /* Odd condition identifiers (lsb == 1) have inverted sense. */
793 return (!!rc ^ (condition & 1));
794}
795
Avi Kivity3c118e22007-10-31 10:27:04 +0200796static void decode_register_operand(struct operand *op,
797 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200798 int inhibit_bytereg)
799{
Avi Kivity33615aa2007-10-31 11:15:56 +0200800 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200801 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200802
803 if (!(c->d & ModRM))
804 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200805 op->type = OP_REG;
806 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200807 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200808 op->val = *(u8 *)op->ptr;
809 op->bytes = 1;
810 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200811 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200812 op->bytes = c->op_bytes;
813 switch (op->bytes) {
814 case 2:
815 op->val = *(u16 *)op->ptr;
816 break;
817 case 4:
818 op->val = *(u32 *)op->ptr;
819 break;
820 case 8:
821 op->val = *(u64 *) op->ptr;
822 break;
823 }
824 }
825 op->orig_val = op->val;
826}
827
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200828static int decode_modrm(struct x86_emulate_ctxt *ctxt,
829 struct x86_emulate_ops *ops)
830{
831 struct decode_cache *c = &ctxt->decode;
832 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700833 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900834 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200835
836 if (c->rex_prefix) {
837 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
838 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
839 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
840 }
841
842 c->modrm = insn_fetch(u8, 1, c->eip);
843 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
844 c->modrm_reg |= (c->modrm & 0x38) >> 3;
845 c->modrm_rm |= (c->modrm & 0x07);
846 c->modrm_ea = 0;
847 c->use_modrm_ea = 1;
848
849 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300850 c->modrm_ptr = decode_register(c->modrm_rm,
851 c->regs, c->d & ByteOp);
852 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200853 return rc;
854 }
855
856 if (c->ad_bytes == 2) {
857 unsigned bx = c->regs[VCPU_REGS_RBX];
858 unsigned bp = c->regs[VCPU_REGS_RBP];
859 unsigned si = c->regs[VCPU_REGS_RSI];
860 unsigned di = c->regs[VCPU_REGS_RDI];
861
862 /* 16-bit ModR/M decode. */
863 switch (c->modrm_mod) {
864 case 0:
865 if (c->modrm_rm == 6)
866 c->modrm_ea += insn_fetch(u16, 2, c->eip);
867 break;
868 case 1:
869 c->modrm_ea += insn_fetch(s8, 1, c->eip);
870 break;
871 case 2:
872 c->modrm_ea += insn_fetch(u16, 2, c->eip);
873 break;
874 }
875 switch (c->modrm_rm) {
876 case 0:
877 c->modrm_ea += bx + si;
878 break;
879 case 1:
880 c->modrm_ea += bx + di;
881 break;
882 case 2:
883 c->modrm_ea += bp + si;
884 break;
885 case 3:
886 c->modrm_ea += bp + di;
887 break;
888 case 4:
889 c->modrm_ea += si;
890 break;
891 case 5:
892 c->modrm_ea += di;
893 break;
894 case 6:
895 if (c->modrm_mod != 0)
896 c->modrm_ea += bp;
897 break;
898 case 7:
899 c->modrm_ea += bx;
900 break;
901 }
902 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
903 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300904 if (!c->has_seg_override)
905 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906 c->modrm_ea = (u16)c->modrm_ea;
907 } else {
908 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700909 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200910 sib = insn_fetch(u8, 1, c->eip);
911 index_reg |= (sib >> 3) & 7;
912 base_reg |= sib & 7;
913 scale = sib >> 6;
914
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700915 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
916 c->modrm_ea += insn_fetch(s32, 4, c->eip);
917 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200918 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700919 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200920 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700921 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
922 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700923 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700924 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200925 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926 switch (c->modrm_mod) {
927 case 0:
928 if (c->modrm_rm == 5)
929 c->modrm_ea += insn_fetch(s32, 4, c->eip);
930 break;
931 case 1:
932 c->modrm_ea += insn_fetch(s8, 1, c->eip);
933 break;
934 case 2:
935 c->modrm_ea += insn_fetch(s32, 4, c->eip);
936 break;
937 }
938 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939done:
940 return rc;
941}
942
943static int decode_abs(struct x86_emulate_ctxt *ctxt,
944 struct x86_emulate_ops *ops)
945{
946 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900947 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948
949 switch (c->ad_bytes) {
950 case 2:
951 c->modrm_ea = insn_fetch(u16, 2, c->eip);
952 break;
953 case 4:
954 c->modrm_ea = insn_fetch(u32, 4, c->eip);
955 break;
956 case 8:
957 c->modrm_ea = insn_fetch(u64, 8, c->eip);
958 break;
959 }
960done:
961 return rc;
962}
963
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200965x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900968 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200970 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
Gleb Natapov5cd21912010-03-18 15:20:26 +0200973 /* we cannot decode insn before we complete previous rep insn */
974 WARN_ON(ctxt->restart);
975
Gleb Natapov063db062010-03-18 15:20:06 +0200976 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300977 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300978 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979
980 switch (mode) {
981 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200982 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200984 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 break;
986 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200987 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800989#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200991 def_op_bytes = 4;
992 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 break;
994#endif
995 default:
996 return -1;
997 }
998
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200999 c->op_bytes = def_op_bytes;
1000 c->ad_bytes = def_ad_bytes;
1001
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001003 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001004 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001006 /* switch between 2/4 bytes */
1007 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 break;
1009 case 0x67: /* address-size override */
1010 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001012 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001015 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001018 case 0x2e: /* CS override */
1019 case 0x36: /* SS override */
1020 case 0x3e: /* DS override */
1021 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 break;
1023 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001025 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001027 case 0x40 ... 0x4f: /* REX */
1028 if (mode != X86EMUL_MODE_PROT64)
1029 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001030 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001031 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001033 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001035 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001036 c->rep_prefix = REPNE_PREFIX;
1037 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001039 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 default:
1042 goto done_prefixes;
1043 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001044
1045 /* Any legacy prefix after a REX prefix nullifies its effect. */
1046
Avi Kivity33615aa2007-10-31 11:15:56 +02001047 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 }
1049
1050done_prefixes:
1051
1052 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001053 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001054 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001055 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
1057 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001058 c->d = opcode_table[c->b];
1059 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001061 if (c->b == 0x0f) {
1062 c->twobyte = 1;
1063 c->b = insn_fetch(u8, 1, c->eip);
1064 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001066 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067
Avi Kivitye09d0822008-01-18 12:38:59 +02001068 if (c->d & Group) {
1069 group = c->d & GroupMask;
1070 c->modrm = insn_fetch(u8, 1, c->eip);
1071 --c->eip;
1072
1073 group = (group << 3) + ((c->modrm >> 3) & 7);
1074 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1075 c->d = group2_table[group];
1076 else
1077 c->d = group_table[group];
1078 }
1079
1080 /* Unrecognised? */
1081 if (c->d == 0) {
1082 DPRINTF("Cannot emulate %02x\n", c->b);
1083 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 }
1085
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001086 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1087 c->op_bytes = 8;
1088
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001090 if (c->d & ModRM)
1091 rc = decode_modrm(ctxt, ops);
1092 else if (c->d & MemAbs)
1093 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001094 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001095 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001097 if (!c->has_seg_override)
1098 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001099
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001100 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001101 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001102
1103 if (c->ad_bytes != 8)
1104 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001105
1106 if (c->rip_relative)
1107 c->modrm_ea += c->eip;
1108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 /*
1110 * Decode and fetch the source operand: register, memory
1111 * or immediate.
1112 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 case SrcNone:
1115 break;
1116 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001117 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 break;
1119 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001120 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 goto srcmem_common;
1122 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001123 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 goto srcmem_common;
1125 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001126 c->src.bytes = (c->d & ByteOp) ? 1 :
1127 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001128 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001129 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001130 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001131 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001132 /*
1133 * For instructions with a ModR/M byte, switch to register
1134 * access if Mod = 3.
1135 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 if ((c->d & ModRM) && c->modrm_mod == 3) {
1137 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001138 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001139 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001140 break;
1141 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001142 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001143 c->src.ptr = (unsigned long *)c->modrm_ea;
1144 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 break;
1146 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001147 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001148 c->src.type = OP_IMM;
1149 c->src.ptr = (unsigned long *)c->eip;
1150 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1151 if (c->src.bytes == 8)
1152 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001154 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001156 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 break;
1158 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001159 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 break;
1161 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001162 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 break;
1164 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001165 if ((c->d & SrcMask) == SrcImmU) {
1166 switch (c->src.bytes) {
1167 case 1:
1168 c->src.val &= 0xff;
1169 break;
1170 case 2:
1171 c->src.val &= 0xffff;
1172 break;
1173 case 4:
1174 c->src.val &= 0xffffffff;
1175 break;
1176 }
1177 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 break;
1179 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001180 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001181 c->src.type = OP_IMM;
1182 c->src.ptr = (unsigned long *)c->eip;
1183 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001184 if ((c->d & SrcMask) == SrcImmByte)
1185 c->src.val = insn_fetch(s8, 1, c->eip);
1186 else
1187 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001189 case SrcOne:
1190 c->src.bytes = 1;
1191 c->src.val = 1;
1192 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001193 case SrcSI:
1194 c->src.type = OP_MEM;
1195 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1196 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001197 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001198 c->regs[VCPU_REGS_RSI]);
1199 c->src.val = 0;
1200 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001201 case SrcImmFAddr:
1202 c->src.type = OP_IMM;
1203 c->src.ptr = (unsigned long *)c->eip;
1204 c->src.bytes = c->op_bytes + 2;
1205 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1206 break;
1207 case SrcMemFAddr:
1208 c->src.type = OP_MEM;
1209 c->src.ptr = (unsigned long *)c->modrm_ea;
1210 c->src.bytes = c->op_bytes + 2;
1211 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 }
1213
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001214 /*
1215 * Decode and fetch the second source operand: register, memory
1216 * or immediate.
1217 */
1218 switch (c->d & Src2Mask) {
1219 case Src2None:
1220 break;
1221 case Src2CL:
1222 c->src2.bytes = 1;
1223 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1224 break;
1225 case Src2ImmByte:
1226 c->src2.type = OP_IMM;
1227 c->src2.ptr = (unsigned long *)c->eip;
1228 c->src2.bytes = 1;
1229 c->src2.val = insn_fetch(u8, 1, c->eip);
1230 break;
1231 case Src2One:
1232 c->src2.bytes = 1;
1233 c->src2.val = 1;
1234 break;
1235 }
1236
Avi Kivity038e51d2007-01-22 20:40:40 -08001237 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001238 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001239 case ImplicitOps:
1240 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001241 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001242 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001243 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001244 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001245 break;
1246 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001247 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001248 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001249 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001250 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001251 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001252 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001253 break;
1254 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001255 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001256 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001257 if ((c->d & DstMask) == DstMem64)
1258 c->dst.bytes = 8;
1259 else
1260 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001261 c->dst.val = 0;
1262 if (c->d & BitOp) {
1263 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1264
1265 c->dst.ptr = (void *)c->dst.ptr +
1266 (c->src.val & mask) / 8;
1267 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001268 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001269 case DstAcc:
1270 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001271 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001272 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001273 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001274 case 1:
1275 c->dst.val = *(u8 *)c->dst.ptr;
1276 break;
1277 case 2:
1278 c->dst.val = *(u16 *)c->dst.ptr;
1279 break;
1280 case 4:
1281 c->dst.val = *(u32 *)c->dst.ptr;
1282 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001283 case 8:
1284 c->dst.val = *(u64 *)c->dst.ptr;
1285 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001286 }
1287 c->dst.orig_val = c->dst.val;
1288 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001289 case DstDI:
1290 c->dst.type = OP_MEM;
1291 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1292 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001293 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001294 c->regs[VCPU_REGS_RDI]);
1295 c->dst.val = 0;
1296 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001297 }
1298
1299done:
1300 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1301}
1302
Gleb Natapov9de41572010-04-28 19:15:22 +03001303static int read_emulated(struct x86_emulate_ctxt *ctxt,
1304 struct x86_emulate_ops *ops,
1305 unsigned long addr, void *dest, unsigned size)
1306{
1307 int rc;
1308 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001309 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001310
1311 while (size) {
1312 int n = min(size, 8u);
1313 size -= n;
1314 if (mc->pos < mc->end)
1315 goto read_cached;
1316
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001317 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1318 ctxt->vcpu);
1319 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001320 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001321 if (rc != X86EMUL_CONTINUE)
1322 return rc;
1323 mc->end += n;
1324
1325 read_cached:
1326 memcpy(dest, mc->data + mc->pos, n);
1327 mc->pos += n;
1328 dest += n;
1329 addr += n;
1330 }
1331 return X86EMUL_CONTINUE;
1332}
1333
Gleb Natapov7b262e92010-03-18 15:20:27 +02001334static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1335 struct x86_emulate_ops *ops,
1336 unsigned int size, unsigned short port,
1337 void *dest)
1338{
1339 struct read_cache *rc = &ctxt->decode.io_read;
1340
1341 if (rc->pos == rc->end) { /* refill pio read ahead */
1342 struct decode_cache *c = &ctxt->decode;
1343 unsigned int in_page, n;
1344 unsigned int count = c->rep_prefix ?
1345 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1346 in_page = (ctxt->eflags & EFLG_DF) ?
1347 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1348 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1349 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1350 count);
1351 if (n == 0)
1352 n = 1;
1353 rc->pos = rc->end = 0;
1354 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1355 return 0;
1356 rc->end = n * size;
1357 }
1358
1359 memcpy(dest, rc->data + rc->pos, size);
1360 rc->pos += size;
1361 return 1;
1362}
1363
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001364static u32 desc_limit_scaled(struct desc_struct *desc)
1365{
1366 u32 limit = get_desc_limit(desc);
1367
1368 return desc->g ? (limit << 12) | 0xfff : limit;
1369}
1370
1371static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1372 struct x86_emulate_ops *ops,
1373 u16 selector, struct desc_ptr *dt)
1374{
1375 if (selector & 1 << 2) {
1376 struct desc_struct desc;
1377 memset (dt, 0, sizeof *dt);
1378 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1379 return;
1380
1381 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1382 dt->address = get_desc_base(&desc);
1383 } else
1384 ops->get_gdt(dt, ctxt->vcpu);
1385}
1386
1387/* allowed just for 8 bytes segments */
1388static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1389 struct x86_emulate_ops *ops,
1390 u16 selector, struct desc_struct *desc)
1391{
1392 struct desc_ptr dt;
1393 u16 index = selector >> 3;
1394 int ret;
1395 u32 err;
1396 ulong addr;
1397
1398 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1399
1400 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001401 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001402 return X86EMUL_PROPAGATE_FAULT;
1403 }
1404 addr = dt.address + index * 8;
1405 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1406 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001407 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408
1409 return ret;
1410}
1411
1412/* allowed just for 8 bytes segments */
1413static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1414 struct x86_emulate_ops *ops,
1415 u16 selector, struct desc_struct *desc)
1416{
1417 struct desc_ptr dt;
1418 u16 index = selector >> 3;
1419 u32 err;
1420 ulong addr;
1421 int ret;
1422
1423 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1424
1425 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001426 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 return X86EMUL_PROPAGATE_FAULT;
1428 }
1429
1430 addr = dt.address + index * 8;
1431 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1432 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001433 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434
1435 return ret;
1436}
1437
1438static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1439 struct x86_emulate_ops *ops,
1440 u16 selector, int seg)
1441{
1442 struct desc_struct seg_desc;
1443 u8 dpl, rpl, cpl;
1444 unsigned err_vec = GP_VECTOR;
1445 u32 err_code = 0;
1446 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1447 int ret;
1448
1449 memset(&seg_desc, 0, sizeof seg_desc);
1450
1451 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1452 || ctxt->mode == X86EMUL_MODE_REAL) {
1453 /* set real mode segment descriptor */
1454 set_desc_base(&seg_desc, selector << 4);
1455 set_desc_limit(&seg_desc, 0xffff);
1456 seg_desc.type = 3;
1457 seg_desc.p = 1;
1458 seg_desc.s = 1;
1459 goto load;
1460 }
1461
1462 /* NULL selector is not valid for TR, CS and SS */
1463 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1464 && null_selector)
1465 goto exception;
1466
1467 /* TR should be in GDT only */
1468 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1469 goto exception;
1470
1471 if (null_selector) /* for NULL selector skip all following checks */
1472 goto load;
1473
1474 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1475 if (ret != X86EMUL_CONTINUE)
1476 return ret;
1477
1478 err_code = selector & 0xfffc;
1479 err_vec = GP_VECTOR;
1480
1481 /* can't load system descriptor into segment selecor */
1482 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1483 goto exception;
1484
1485 if (!seg_desc.p) {
1486 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1487 goto exception;
1488 }
1489
1490 rpl = selector & 3;
1491 dpl = seg_desc.dpl;
1492 cpl = ops->cpl(ctxt->vcpu);
1493
1494 switch (seg) {
1495 case VCPU_SREG_SS:
1496 /*
1497 * segment is not a writable data segment or segment
1498 * selector's RPL != CPL or segment selector's RPL != CPL
1499 */
1500 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1501 goto exception;
1502 break;
1503 case VCPU_SREG_CS:
1504 if (!(seg_desc.type & 8))
1505 goto exception;
1506
1507 if (seg_desc.type & 4) {
1508 /* conforming */
1509 if (dpl > cpl)
1510 goto exception;
1511 } else {
1512 /* nonconforming */
1513 if (rpl > cpl || dpl != cpl)
1514 goto exception;
1515 }
1516 /* CS(RPL) <- CPL */
1517 selector = (selector & 0xfffc) | cpl;
1518 break;
1519 case VCPU_SREG_TR:
1520 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1521 goto exception;
1522 break;
1523 case VCPU_SREG_LDTR:
1524 if (seg_desc.s || seg_desc.type != 2)
1525 goto exception;
1526 break;
1527 default: /* DS, ES, FS, or GS */
1528 /*
1529 * segment is not a data or readable code segment or
1530 * ((segment is a data or nonconforming code segment)
1531 * and (both RPL and CPL > DPL))
1532 */
1533 if ((seg_desc.type & 0xa) == 0x8 ||
1534 (((seg_desc.type & 0xc) != 0xc) &&
1535 (rpl > dpl && cpl > dpl)))
1536 goto exception;
1537 break;
1538 }
1539
1540 if (seg_desc.s) {
1541 /* mark segment as accessed */
1542 seg_desc.type |= 1;
1543 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1544 if (ret != X86EMUL_CONTINUE)
1545 return ret;
1546 }
1547load:
1548 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1549 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1550 return X86EMUL_CONTINUE;
1551exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001552 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 return X86EMUL_PROPAGATE_FAULT;
1554}
1555
Wei Yongjunc37eda12010-06-15 09:03:33 +08001556static inline int writeback(struct x86_emulate_ctxt *ctxt,
1557 struct x86_emulate_ops *ops)
1558{
1559 int rc;
1560 struct decode_cache *c = &ctxt->decode;
1561 u32 err;
1562
1563 switch (c->dst.type) {
1564 case OP_REG:
1565 /* The 4-byte case *is* correct:
1566 * in 64-bit mode we zero-extend.
1567 */
1568 switch (c->dst.bytes) {
1569 case 1:
1570 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1571 break;
1572 case 2:
1573 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1574 break;
1575 case 4:
1576 *c->dst.ptr = (u32)c->dst.val;
1577 break; /* 64b: zero-ext */
1578 case 8:
1579 *c->dst.ptr = c->dst.val;
1580 break;
1581 }
1582 break;
1583 case OP_MEM:
1584 if (c->lock_prefix)
1585 rc = ops->cmpxchg_emulated(
1586 (unsigned long)c->dst.ptr,
1587 &c->dst.orig_val,
1588 &c->dst.val,
1589 c->dst.bytes,
1590 &err,
1591 ctxt->vcpu);
1592 else
1593 rc = ops->write_emulated(
1594 (unsigned long)c->dst.ptr,
1595 &c->dst.val,
1596 c->dst.bytes,
1597 &err,
1598 ctxt->vcpu);
1599 if (rc == X86EMUL_PROPAGATE_FAULT)
1600 emulate_pf(ctxt,
1601 (unsigned long)c->dst.ptr, err);
1602 if (rc != X86EMUL_CONTINUE)
1603 return rc;
1604 break;
1605 case OP_NONE:
1606 /* no writeback */
1607 break;
1608 default:
1609 break;
1610 }
1611 return X86EMUL_CONTINUE;
1612}
1613
Gleb Natapov79168fd2010-04-28 19:15:30 +03001614static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1615 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001616{
1617 struct decode_cache *c = &ctxt->decode;
1618
1619 c->dst.type = OP_MEM;
1620 c->dst.bytes = c->op_bytes;
1621 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001622 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001623 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001624 c->regs[VCPU_REGS_RSP]);
1625}
1626
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001627static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001628 struct x86_emulate_ops *ops,
1629 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001630{
1631 struct decode_cache *c = &ctxt->decode;
1632 int rc;
1633
Gleb Natapov79168fd2010-04-28 19:15:30 +03001634 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001635 c->regs[VCPU_REGS_RSP]),
1636 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001637 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638 return rc;
1639
Avi Kivity350f69d2009-01-05 11:12:40 +02001640 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001641 return rc;
1642}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001643
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001644static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1645 struct x86_emulate_ops *ops,
1646 void *dest, int len)
1647{
1648 int rc;
1649 unsigned long val, change_mask;
1650 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001651 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001652
1653 rc = emulate_pop(ctxt, ops, &val, len);
1654 if (rc != X86EMUL_CONTINUE)
1655 return rc;
1656
1657 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1658 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1659
1660 switch(ctxt->mode) {
1661 case X86EMUL_MODE_PROT64:
1662 case X86EMUL_MODE_PROT32:
1663 case X86EMUL_MODE_PROT16:
1664 if (cpl == 0)
1665 change_mask |= EFLG_IOPL;
1666 if (cpl <= iopl)
1667 change_mask |= EFLG_IF;
1668 break;
1669 case X86EMUL_MODE_VM86:
1670 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001671 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001672 return X86EMUL_PROPAGATE_FAULT;
1673 }
1674 change_mask |= EFLG_IF;
1675 break;
1676 default: /* real mode */
1677 change_mask |= (EFLG_IOPL | EFLG_IF);
1678 break;
1679 }
1680
1681 *(unsigned long *)dest =
1682 (ctxt->eflags & ~change_mask) | (val & change_mask);
1683
1684 return rc;
1685}
1686
Gleb Natapov79168fd2010-04-28 19:15:30 +03001687static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1688 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001689{
1690 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001691
Gleb Natapov79168fd2010-04-28 19:15:30 +03001692 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001693
Gleb Natapov79168fd2010-04-28 19:15:30 +03001694 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001695}
1696
1697static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1698 struct x86_emulate_ops *ops, int seg)
1699{
1700 struct decode_cache *c = &ctxt->decode;
1701 unsigned long selector;
1702 int rc;
1703
1704 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001705 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001706 return rc;
1707
Gleb Natapov2e873022010-03-18 15:20:18 +02001708 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001709 return rc;
1710}
1711
Wei Yongjunc37eda12010-06-15 09:03:33 +08001712static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001713 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001714{
1715 struct decode_cache *c = &ctxt->decode;
1716 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001717 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001718 int reg = VCPU_REGS_RAX;
1719
1720 while (reg <= VCPU_REGS_RDI) {
1721 (reg == VCPU_REGS_RSP) ?
1722 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1723
Gleb Natapov79168fd2010-04-28 19:15:30 +03001724 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001725
1726 rc = writeback(ctxt, ops);
1727 if (rc != X86EMUL_CONTINUE)
1728 return rc;
1729
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001730 ++reg;
1731 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001732
1733 /* Disable writeback. */
1734 c->dst.type = OP_NONE;
1735
1736 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001737}
1738
1739static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1740 struct x86_emulate_ops *ops)
1741{
1742 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001743 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001744 int reg = VCPU_REGS_RDI;
1745
1746 while (reg >= VCPU_REGS_RAX) {
1747 if (reg == VCPU_REGS_RSP) {
1748 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1749 c->op_bytes);
1750 --reg;
1751 }
1752
1753 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001754 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001755 break;
1756 --reg;
1757 }
1758 return rc;
1759}
1760
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001761static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1762 struct x86_emulate_ops *ops)
1763{
1764 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001765
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001766 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767}
1768
Laurent Vivier05f086f2007-09-24 11:10:55 +02001769static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001771 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 switch (c->modrm_reg) {
1773 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001774 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 break;
1776 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001777 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778 break;
1779 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001780 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 break;
1782 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001783 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784 break;
1785 case 4: /* sal/shl */
1786 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001787 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 break;
1789 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001790 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 break;
1792 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001793 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794 break;
1795 }
1796}
1797
1798static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001799 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800{
1801 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802
1803 switch (c->modrm_reg) {
1804 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001805 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806 break;
1807 case 2: /* not */
1808 c->dst.val = ~c->dst.val;
1809 break;
1810 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001811 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001812 break;
1813 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001814 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001815 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001816 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001817}
1818
1819static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001820 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821{
1822 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823
1824 switch (c->modrm_reg) {
1825 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001826 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 break;
1828 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001829 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001831 case 2: /* call near abs */ {
1832 long int old_eip;
1833 old_eip = c->eip;
1834 c->eip = c->src.val;
1835 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001836 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001837 break;
1838 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001839 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001840 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001841 break;
1842 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001845 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001846 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001847}
1848
1849static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001850 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001851{
1852 struct decode_cache *c = &ctxt->decode;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001853 u64 old = c->dst.orig_val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001854
1855 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1856 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1857
1858 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1859 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001860 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861 } else {
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001862 c->dst.val = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001863 (u32) c->regs[VCPU_REGS_RBX];
1864
Laurent Vivier05f086f2007-09-24 11:10:55 +02001865 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001867 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001868}
1869
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001870static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1871 struct x86_emulate_ops *ops)
1872{
1873 struct decode_cache *c = &ctxt->decode;
1874 int rc;
1875 unsigned long cs;
1876
1877 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001878 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001879 return rc;
1880 if (c->op_bytes == 4)
1881 c->eip = (u32)c->eip;
1882 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001883 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001884 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001885 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001886 return rc;
1887}
1888
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889static inline void
1890setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001891 struct x86_emulate_ops *ops, struct desc_struct *cs,
1892 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001893{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001894 memset(cs, 0, sizeof(struct desc_struct));
1895 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1896 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897
1898 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001900 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001901 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001902 cs->type = 0x0b; /* Read, Execute, Accessed */
1903 cs->s = 1;
1904 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001905 cs->p = 1;
1906 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907
Gleb Natapov79168fd2010-04-28 19:15:30 +03001908 set_desc_base(ss, 0); /* flat segment */
1909 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001910 ss->g = 1; /* 4kb granularity */
1911 ss->s = 1;
1912 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001913 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001915 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001916}
1917
1918static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001919emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001920{
1921 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001922 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001923 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001924 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001925
1926 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001927 if (ctxt->mode == X86EMUL_MODE_REAL ||
1928 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001929 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001930 return X86EMUL_PROPAGATE_FAULT;
1931 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001932
Gleb Natapov79168fd2010-04-28 19:15:30 +03001933 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001934
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001935 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001936 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001937 cs_sel = (u16)(msr_data & 0xfffc);
1938 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001939
1940 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001941 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001942 cs.l = 1;
1943 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001944 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1945 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1946 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1947 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001948
1949 c->regs[VCPU_REGS_RCX] = c->eip;
1950 if (is_long_mode(ctxt->vcpu)) {
1951#ifdef CONFIG_X86_64
1952 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1953
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001954 ops->get_msr(ctxt->vcpu,
1955 ctxt->mode == X86EMUL_MODE_PROT64 ?
1956 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001957 c->eip = msr_data;
1958
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001959 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001960 ctxt->eflags &= ~(msr_data | EFLG_RF);
1961#endif
1962 } else {
1963 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001964 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001965 c->eip = (u32)msr_data;
1966
1967 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1968 }
1969
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001970 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001971}
1972
Andre Przywara8c604352009-06-18 12:56:01 +02001973static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001974emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001975{
1976 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001977 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001978 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001979 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001980
Gleb Natapova0044752010-02-10 14:21:31 +02001981 /* inject #GP if in real mode */
1982 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001983 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001984 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001985 }
1986
1987 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1988 * Therefore, we inject an #UD.
1989 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001990 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001991 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001992 return X86EMUL_PROPAGATE_FAULT;
1993 }
Andre Przywara8c604352009-06-18 12:56:01 +02001994
Gleb Natapov79168fd2010-04-28 19:15:30 +03001995 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001996
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001997 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001998 switch (ctxt->mode) {
1999 case X86EMUL_MODE_PROT32:
2000 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002001 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002002 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002003 }
2004 break;
2005 case X86EMUL_MODE_PROT64:
2006 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002007 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002008 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002009 }
2010 break;
2011 }
2012
2013 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002014 cs_sel = (u16)msr_data;
2015 cs_sel &= ~SELECTOR_RPL_MASK;
2016 ss_sel = cs_sel + 8;
2017 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002018 if (ctxt->mode == X86EMUL_MODE_PROT64
2019 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002020 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002021 cs.l = 1;
2022 }
2023
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2025 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2026 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2027 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002028
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002029 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002030 c->eip = msr_data;
2031
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002032 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002033 c->regs[VCPU_REGS_RSP] = msr_data;
2034
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002035 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002036}
2037
Andre Przywara4668f052009-06-18 12:56:02 +02002038static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002039emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002040{
2041 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002042 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002043 u64 msr_data;
2044 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002046
Gleb Natapova0044752010-02-10 14:21:31 +02002047 /* inject #GP if in real mode or Virtual 8086 mode */
2048 if (ctxt->mode == X86EMUL_MODE_REAL ||
2049 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002050 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002051 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002052 }
2053
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002055
2056 if ((c->rex_prefix & 0x8) != 0x0)
2057 usermode = X86EMUL_MODE_PROT64;
2058 else
2059 usermode = X86EMUL_MODE_PROT32;
2060
2061 cs.dpl = 3;
2062 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002063 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002064 switch (usermode) {
2065 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002066 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002067 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002068 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002069 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002070 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002071 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002072 break;
2073 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002074 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002075 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002076 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002077 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002078 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002079 ss_sel = cs_sel + 8;
2080 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002081 cs.l = 1;
2082 break;
2083 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002084 cs_sel |= SELECTOR_RPL_MASK;
2085 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002086
Gleb Natapov79168fd2010-04-28 19:15:30 +03002087 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2088 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2089 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2090 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002091
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002092 c->eip = c->regs[VCPU_REGS_RDX];
2093 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002094
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002095 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002096}
2097
Gleb Natapov9c537242010-03-18 15:20:05 +02002098static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2099 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002100{
2101 int iopl;
2102 if (ctxt->mode == X86EMUL_MODE_REAL)
2103 return false;
2104 if (ctxt->mode == X86EMUL_MODE_VM86)
2105 return true;
2106 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002107 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002108}
2109
2110static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2111 struct x86_emulate_ops *ops,
2112 u16 port, u16 len)
2113{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002114 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002115 int r;
2116 u16 io_bitmap_ptr;
2117 u8 perm, bit_idx = port & 0x7;
2118 unsigned mask = (1 << len) - 1;
2119
Gleb Natapov79168fd2010-04-28 19:15:30 +03002120 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2121 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002122 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002123 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002124 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002125 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2126 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002127 if (r != X86EMUL_CONTINUE)
2128 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002129 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002130 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002131 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2132 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002133 if (r != X86EMUL_CONTINUE)
2134 return false;
2135 if ((perm >> bit_idx) & mask)
2136 return false;
2137 return true;
2138}
2139
2140static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2141 struct x86_emulate_ops *ops,
2142 u16 port, u16 len)
2143{
Gleb Natapov9c537242010-03-18 15:20:05 +02002144 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002145 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2146 return false;
2147 return true;
2148}
2149
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2151 struct x86_emulate_ops *ops,
2152 struct tss_segment_16 *tss)
2153{
2154 struct decode_cache *c = &ctxt->decode;
2155
2156 tss->ip = c->eip;
2157 tss->flag = ctxt->eflags;
2158 tss->ax = c->regs[VCPU_REGS_RAX];
2159 tss->cx = c->regs[VCPU_REGS_RCX];
2160 tss->dx = c->regs[VCPU_REGS_RDX];
2161 tss->bx = c->regs[VCPU_REGS_RBX];
2162 tss->sp = c->regs[VCPU_REGS_RSP];
2163 tss->bp = c->regs[VCPU_REGS_RBP];
2164 tss->si = c->regs[VCPU_REGS_RSI];
2165 tss->di = c->regs[VCPU_REGS_RDI];
2166
2167 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2168 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2169 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2170 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2171 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2172}
2173
2174static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2175 struct x86_emulate_ops *ops,
2176 struct tss_segment_16 *tss)
2177{
2178 struct decode_cache *c = &ctxt->decode;
2179 int ret;
2180
2181 c->eip = tss->ip;
2182 ctxt->eflags = tss->flag | 2;
2183 c->regs[VCPU_REGS_RAX] = tss->ax;
2184 c->regs[VCPU_REGS_RCX] = tss->cx;
2185 c->regs[VCPU_REGS_RDX] = tss->dx;
2186 c->regs[VCPU_REGS_RBX] = tss->bx;
2187 c->regs[VCPU_REGS_RSP] = tss->sp;
2188 c->regs[VCPU_REGS_RBP] = tss->bp;
2189 c->regs[VCPU_REGS_RSI] = tss->si;
2190 c->regs[VCPU_REGS_RDI] = tss->di;
2191
2192 /*
2193 * SDM says that segment selectors are loaded before segment
2194 * descriptors
2195 */
2196 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2197 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2198 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2199 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2200 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2201
2202 /*
2203 * Now load segment descriptors. If fault happenes at this stage
2204 * it is handled in a context of new task
2205 */
2206 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2207 if (ret != X86EMUL_CONTINUE)
2208 return ret;
2209 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2210 if (ret != X86EMUL_CONTINUE)
2211 return ret;
2212 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2213 if (ret != X86EMUL_CONTINUE)
2214 return ret;
2215 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2216 if (ret != X86EMUL_CONTINUE)
2217 return ret;
2218 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2219 if (ret != X86EMUL_CONTINUE)
2220 return ret;
2221
2222 return X86EMUL_CONTINUE;
2223}
2224
2225static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2226 struct x86_emulate_ops *ops,
2227 u16 tss_selector, u16 old_tss_sel,
2228 ulong old_tss_base, struct desc_struct *new_desc)
2229{
2230 struct tss_segment_16 tss_seg;
2231 int ret;
2232 u32 err, new_tss_base = get_desc_base(new_desc);
2233
2234 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2235 &err);
2236 if (ret == X86EMUL_PROPAGATE_FAULT) {
2237 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002238 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002239 return ret;
2240 }
2241
2242 save_state_to_tss16(ctxt, ops, &tss_seg);
2243
2244 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2245 &err);
2246 if (ret == X86EMUL_PROPAGATE_FAULT) {
2247 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002248 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249 return ret;
2250 }
2251
2252 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2253 &err);
2254 if (ret == X86EMUL_PROPAGATE_FAULT) {
2255 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002256 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002257 return ret;
2258 }
2259
2260 if (old_tss_sel != 0xffff) {
2261 tss_seg.prev_task_link = old_tss_sel;
2262
2263 ret = ops->write_std(new_tss_base,
2264 &tss_seg.prev_task_link,
2265 sizeof tss_seg.prev_task_link,
2266 ctxt->vcpu, &err);
2267 if (ret == X86EMUL_PROPAGATE_FAULT) {
2268 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002269 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270 return ret;
2271 }
2272 }
2273
2274 return load_state_from_tss16(ctxt, ops, &tss_seg);
2275}
2276
2277static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2278 struct x86_emulate_ops *ops,
2279 struct tss_segment_32 *tss)
2280{
2281 struct decode_cache *c = &ctxt->decode;
2282
2283 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2284 tss->eip = c->eip;
2285 tss->eflags = ctxt->eflags;
2286 tss->eax = c->regs[VCPU_REGS_RAX];
2287 tss->ecx = c->regs[VCPU_REGS_RCX];
2288 tss->edx = c->regs[VCPU_REGS_RDX];
2289 tss->ebx = c->regs[VCPU_REGS_RBX];
2290 tss->esp = c->regs[VCPU_REGS_RSP];
2291 tss->ebp = c->regs[VCPU_REGS_RBP];
2292 tss->esi = c->regs[VCPU_REGS_RSI];
2293 tss->edi = c->regs[VCPU_REGS_RDI];
2294
2295 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2296 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2297 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2298 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2299 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2300 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2301 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2302}
2303
2304static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2305 struct x86_emulate_ops *ops,
2306 struct tss_segment_32 *tss)
2307{
2308 struct decode_cache *c = &ctxt->decode;
2309 int ret;
2310
Gleb Natapov0f122442010-04-28 19:15:31 +03002311 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002312 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002313 return X86EMUL_PROPAGATE_FAULT;
2314 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315 c->eip = tss->eip;
2316 ctxt->eflags = tss->eflags | 2;
2317 c->regs[VCPU_REGS_RAX] = tss->eax;
2318 c->regs[VCPU_REGS_RCX] = tss->ecx;
2319 c->regs[VCPU_REGS_RDX] = tss->edx;
2320 c->regs[VCPU_REGS_RBX] = tss->ebx;
2321 c->regs[VCPU_REGS_RSP] = tss->esp;
2322 c->regs[VCPU_REGS_RBP] = tss->ebp;
2323 c->regs[VCPU_REGS_RSI] = tss->esi;
2324 c->regs[VCPU_REGS_RDI] = tss->edi;
2325
2326 /*
2327 * SDM says that segment selectors are loaded before segment
2328 * descriptors
2329 */
2330 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2331 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2332 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2333 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2334 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2335 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2336 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2337
2338 /*
2339 * Now load segment descriptors. If fault happenes at this stage
2340 * it is handled in a context of new task
2341 */
2342 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2343 if (ret != X86EMUL_CONTINUE)
2344 return ret;
2345 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2346 if (ret != X86EMUL_CONTINUE)
2347 return ret;
2348 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2349 if (ret != X86EMUL_CONTINUE)
2350 return ret;
2351 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2352 if (ret != X86EMUL_CONTINUE)
2353 return ret;
2354 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2355 if (ret != X86EMUL_CONTINUE)
2356 return ret;
2357 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2358 if (ret != X86EMUL_CONTINUE)
2359 return ret;
2360 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2361 if (ret != X86EMUL_CONTINUE)
2362 return ret;
2363
2364 return X86EMUL_CONTINUE;
2365}
2366
2367static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2368 struct x86_emulate_ops *ops,
2369 u16 tss_selector, u16 old_tss_sel,
2370 ulong old_tss_base, struct desc_struct *new_desc)
2371{
2372 struct tss_segment_32 tss_seg;
2373 int ret;
2374 u32 err, new_tss_base = get_desc_base(new_desc);
2375
2376 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2377 &err);
2378 if (ret == X86EMUL_PROPAGATE_FAULT) {
2379 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002380 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002381 return ret;
2382 }
2383
2384 save_state_to_tss32(ctxt, ops, &tss_seg);
2385
2386 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2387 &err);
2388 if (ret == X86EMUL_PROPAGATE_FAULT) {
2389 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002390 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391 return ret;
2392 }
2393
2394 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2395 &err);
2396 if (ret == X86EMUL_PROPAGATE_FAULT) {
2397 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002398 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 return ret;
2400 }
2401
2402 if (old_tss_sel != 0xffff) {
2403 tss_seg.prev_task_link = old_tss_sel;
2404
2405 ret = ops->write_std(new_tss_base,
2406 &tss_seg.prev_task_link,
2407 sizeof tss_seg.prev_task_link,
2408 ctxt->vcpu, &err);
2409 if (ret == X86EMUL_PROPAGATE_FAULT) {
2410 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002411 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 return ret;
2413 }
2414 }
2415
2416 return load_state_from_tss32(ctxt, ops, &tss_seg);
2417}
2418
2419static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002420 struct x86_emulate_ops *ops,
2421 u16 tss_selector, int reason,
2422 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423{
2424 struct desc_struct curr_tss_desc, next_tss_desc;
2425 int ret;
2426 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2427 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002428 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002429 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430
2431 /* FIXME: old_tss_base == ~0 ? */
2432
2433 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2434 if (ret != X86EMUL_CONTINUE)
2435 return ret;
2436 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2437 if (ret != X86EMUL_CONTINUE)
2438 return ret;
2439
2440 /* FIXME: check that next_tss_desc is tss */
2441
2442 if (reason != TASK_SWITCH_IRET) {
2443 if ((tss_selector & 3) > next_tss_desc.dpl ||
2444 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002445 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446 return X86EMUL_PROPAGATE_FAULT;
2447 }
2448 }
2449
Gleb Natapovceffb452010-03-18 15:20:19 +02002450 desc_limit = desc_limit_scaled(&next_tss_desc);
2451 if (!next_tss_desc.p ||
2452 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2453 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002454 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002455 return X86EMUL_PROPAGATE_FAULT;
2456 }
2457
2458 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2459 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2460 write_segment_descriptor(ctxt, ops, old_tss_sel,
2461 &curr_tss_desc);
2462 }
2463
2464 if (reason == TASK_SWITCH_IRET)
2465 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2466
2467 /* set back link to prev task only if NT bit is set in eflags
2468 note that old_tss_sel is not used afetr this point */
2469 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2470 old_tss_sel = 0xffff;
2471
2472 if (next_tss_desc.type & 8)
2473 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2474 old_tss_base, &next_tss_desc);
2475 else
2476 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2477 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002478 if (ret != X86EMUL_CONTINUE)
2479 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480
2481 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2482 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2483
2484 if (reason != TASK_SWITCH_IRET) {
2485 next_tss_desc.type |= (1 << 1); /* set busy flag */
2486 write_segment_descriptor(ctxt, ops, tss_selector,
2487 &next_tss_desc);
2488 }
2489
2490 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2491 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2492 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2493
Jan Kiszkae269fb22010-04-14 15:51:09 +02002494 if (has_error_code) {
2495 struct decode_cache *c = &ctxt->decode;
2496
2497 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2498 c->lock_prefix = 0;
2499 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002500 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002501 }
2502
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 return ret;
2504}
2505
2506int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2507 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002508 u16 tss_selector, int reason,
2509 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510{
2511 struct decode_cache *c = &ctxt->decode;
2512 int rc;
2513
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002515 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516
Jan Kiszkae269fb22010-04-14 15:51:09 +02002517 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2518 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519
2520 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002521 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002522 if (rc == X86EMUL_CONTINUE)
2523 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 }
2525
Gleb Natapov19d04432010-04-15 12:29:50 +03002526 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527}
2528
Gleb Natapova682e352010-03-18 15:20:21 +02002529static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002530 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002531{
2532 struct decode_cache *c = &ctxt->decode;
2533 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2534
Gleb Natapovd9271122010-03-18 15:20:22 +02002535 register_address_increment(c, &c->regs[reg], df * op->bytes);
2536 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002537}
2538
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002539int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002540x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002541{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002542 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002543 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002544 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002545 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002546
Gleb Natapov9de41572010-04-28 19:15:22 +03002547 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002548
Gleb Natapov11616242010-02-11 14:43:14 +02002549 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002550 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002551 goto done;
2552 }
2553
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002554 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002555 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002556 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002557 goto done;
2558 }
2559
Gleb Natapove92805a2010-02-10 14:21:35 +02002560 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002561 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002562 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002563 goto done;
2564 }
2565
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002566 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002567 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002568 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002569 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002570 string_done:
2571 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002572 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002573 goto done;
2574 }
2575 /* The second termination condition only applies for REPE
2576 * and REPNE. Test if the repeat string operation prefix is
2577 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2578 * corresponding termination condition according to:
2579 * - if REPE/REPZ and ZF = 0 then done
2580 * - if REPNE/REPNZ and ZF = 1 then done
2581 */
2582 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002583 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002584 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002585 ((ctxt->eflags & EFLG_ZF) == 0))
2586 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002587 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002588 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2589 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002590 }
Gleb Natapov063db062010-03-18 15:20:06 +02002591 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002592 }
2593
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002594 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002595 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002596 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002597 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002598 goto done;
2599 c->src.orig_val = c->src.val;
2600 }
2601
Gleb Natapove35b7b92010-02-25 16:36:42 +02002602 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002603 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2604 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002605 if (rc != X86EMUL_CONTINUE)
2606 goto done;
2607 }
2608
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002609 if ((c->d & DstMask) == ImplicitOps)
2610 goto special_insn;
2611
2612
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002613 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2614 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002615 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2616 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002617 if (rc != X86EMUL_CONTINUE)
2618 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002619 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002620 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002621
Avi Kivity018a98d2007-11-27 19:30:56 +02002622special_insn:
2623
Laurent Viviere4e03de2007-09-18 11:52:50 +02002624 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 goto twobyte_insn;
2626
Laurent Viviere4e03de2007-09-18 11:52:50 +02002627 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 case 0x00 ... 0x05:
2629 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002630 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002632 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002633 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002634 break;
2635 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002636 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002637 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002638 goto done;
2639 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 case 0x08 ... 0x0d:
2641 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002642 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002644 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002645 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002646 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 case 0x10 ... 0x15:
2648 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002649 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002651 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002652 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002653 break;
2654 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002655 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002656 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002657 goto done;
2658 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 case 0x18 ... 0x1d:
2660 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002661 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002663 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002664 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002665 break;
2666 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002667 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002668 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002669 goto done;
2670 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002671 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002673 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 break;
2675 case 0x28 ... 0x2d:
2676 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002677 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 break;
2679 case 0x30 ... 0x35:
2680 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002681 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 break;
2683 case 0x38 ... 0x3d:
2684 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002685 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002687 case 0x40 ... 0x47: /* inc r16/r32 */
2688 emulate_1op("inc", c->dst, ctxt->eflags);
2689 break;
2690 case 0x48 ... 0x4f: /* dec r16/r32 */
2691 emulate_1op("dec", c->dst, ctxt->eflags);
2692 break;
2693 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002694 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002695 break;
2696 case 0x58 ... 0x5f: /* pop reg */
2697 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002698 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002699 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002700 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002701 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002702 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002703 rc = emulate_pusha(ctxt, ops);
2704 if (rc != X86EMUL_CONTINUE)
2705 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002706 break;
2707 case 0x61: /* popa */
2708 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002709 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002710 goto done;
2711 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002713 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002715 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002717 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002718 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002719 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002720 break;
2721 case 0x6c: /* insb */
2722 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002723 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002724 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002725 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002726 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002727 goto done;
2728 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002729 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2730 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002731 goto done; /* IO is needed, skip writeback */
2732 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002733 case 0x6e: /* outsb */
2734 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002735 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002736 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002737 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002738 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002739 goto done;
2740 }
Gleb Natapov79729952010-03-18 15:20:24 +02002741 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2742 &c->src.val, 1, ctxt->vcpu);
2743
2744 c->dst.type = OP_NONE; /* nothing to writeback */
2745 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002746 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002747 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002748 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002751 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 case 0:
2753 goto add;
2754 case 1:
2755 goto or;
2756 case 2:
2757 goto adc;
2758 case 3:
2759 goto sbb;
2760 case 4:
2761 goto and;
2762 case 5:
2763 goto sub;
2764 case 6:
2765 goto xor;
2766 case 7:
2767 goto cmp;
2768 }
2769 break;
2770 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002771 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002772 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 break;
2774 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002775 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002777 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002779 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 break;
2781 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002782 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 break;
2784 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002785 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 break; /* 64b reg: zero-extend */
2787 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002788 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 break;
2790 }
2791 /*
2792 * Write back the memory destination with implicit LOCK
2793 * prefix.
2794 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002795 c->dst.val = c->src.val;
2796 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002799 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002800 case 0x8c: /* mov r/m, sreg */
2801 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002802 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002803 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002804 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002805 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002806 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002807 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002808 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002809 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002810 case 0x8e: { /* mov seg, r/m16 */
2811 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002812
2813 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002814
Gleb Natapovc6975182010-02-18 12:15:01 +02002815 if (c->modrm_reg == VCPU_SREG_CS ||
2816 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002817 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002818 goto done;
2819 }
2820
Glauber Costa310b5d32009-05-12 16:21:06 -04002821 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002822 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002823
Gleb Natapov2e873022010-03-18 15:20:18 +02002824 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002825
2826 c->dst.type = OP_NONE; /* Disable writeback. */
2827 break;
2828 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002830 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002831 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002834 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002835 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2836 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002837 break;
2838 }
2839 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002840 c->src.type = OP_REG;
2841 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002842 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2843 c->src.val = *(c->src.ptr);
2844 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002845 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002846 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002847 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002848 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002849 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002850 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002851 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002852 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002853 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2854 if (rc != X86EMUL_CONTINUE)
2855 goto done;
2856 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002857 case 0xa0 ... 0xa1: /* mov */
2858 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2859 c->dst.val = c->src.val;
2860 break;
2861 case 0xa2 ... 0xa3: /* mov */
2862 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2863 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002865 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002867 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002868 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002869 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002870 case 0xa8 ... 0xa9: /* test ax, imm */
2871 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002873 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break;
2875 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002876 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 case 0xae ... 0xaf: /* scas */
2878 DPRINTF("Urk! I don't handle SCAS.\n");
2879 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002880 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002881 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002882 case 0xc0 ... 0xc1:
2883 emulate_grp2(ctxt);
2884 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002885 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002886 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002887 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002888 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002889 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002890 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2891 mov:
2892 c->dst.val = c->src.val;
2893 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002894 case 0xcb: /* ret far */
2895 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002896 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002897 goto done;
2898 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002899 case 0xd0 ... 0xd1: /* Grp2 */
2900 c->src.val = 1;
2901 emulate_grp2(ctxt);
2902 break;
2903 case 0xd2 ... 0xd3: /* Grp2 */
2904 c->src.val = c->regs[VCPU_REGS_RCX];
2905 emulate_grp2(ctxt);
2906 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002907 case 0xe4: /* inb */
2908 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002909 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002910 case 0xe6: /* outb */
2911 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002912 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002913 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002914 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002915 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002916 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002917 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002918 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002919 }
2920 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002921 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002922 case 0xea: { /* jmp far */
2923 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002924 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002925 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2926
2927 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002928 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002929
Gleb Natapov414e6272010-04-28 19:15:26 +03002930 c->eip = 0;
2931 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002932 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002933 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002934 case 0xeb:
2935 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002936 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002937 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002938 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002939 case 0xec: /* in al,dx */
2940 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002941 c->src.val = c->regs[VCPU_REGS_RDX];
2942 do_io_in:
2943 c->dst.bytes = min(c->dst.bytes, 4u);
2944 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002945 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002946 goto done;
2947 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002948 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2949 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002950 goto done; /* IO is needed */
2951 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08002952 case 0xee: /* out dx,al */
2953 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002954 c->src.val = c->regs[VCPU_REGS_RDX];
2955 do_io_out:
2956 c->dst.bytes = min(c->dst.bytes, 4u);
2957 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002958 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002959 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002960 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002961 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2962 ctxt->vcpu);
2963 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002964 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002965 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002966 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002967 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002968 case 0xf5: /* cmc */
2969 /* complement carry flag from eflags reg */
2970 ctxt->eflags ^= EFLG_CF;
2971 c->dst.type = OP_NONE; /* Disable writeback. */
2972 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002973 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002974 if (!emulate_grp3(ctxt, ops))
2975 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002976 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002977 case 0xf8: /* clc */
2978 ctxt->eflags &= ~EFLG_CF;
2979 c->dst.type = OP_NONE; /* Disable writeback. */
2980 break;
2981 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002982 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002983 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002984 goto done;
2985 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002986 ctxt->eflags &= ~X86_EFLAGS_IF;
2987 c->dst.type = OP_NONE; /* Disable writeback. */
2988 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002989 break;
2990 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002991 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002992 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002993 goto done;
2994 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03002995 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002996 ctxt->eflags |= X86_EFLAGS_IF;
2997 c->dst.type = OP_NONE; /* Disable writeback. */
2998 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002999 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003000 case 0xfc: /* cld */
3001 ctxt->eflags &= ~EFLG_DF;
3002 c->dst.type = OP_NONE; /* Disable writeback. */
3003 break;
3004 case 0xfd: /* std */
3005 ctxt->eflags |= EFLG_DF;
3006 c->dst.type = OP_NONE; /* Disable writeback. */
3007 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003008 case 0xfe: /* Grp4 */
3009 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003010 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003011 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003012 goto done;
3013 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003014 case 0xff: /* Grp5 */
3015 if (c->modrm_reg == 5)
3016 goto jump_far;
3017 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003019
3020writeback:
3021 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003022 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003023 goto done;
3024
Gleb Natapov5cd21912010-03-18 15:20:26 +02003025 /*
3026 * restore dst type in case the decoding will be reused
3027 * (happens for string instruction )
3028 */
3029 c->dst.type = saved_dst_type;
3030
Gleb Natapova682e352010-03-18 15:20:21 +02003031 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003032 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3033 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003034
3035 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003036 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3037 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003038
Gleb Natapov5cd21912010-03-18 15:20:26 +02003039 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003040 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003041 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003042 /*
3043 * Re-enter guest when pio read ahead buffer is empty or,
3044 * if it is not used, after each 1024 iteration.
3045 */
3046 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3047 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003048 ctxt->restart = false;
3049 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003050 /*
3051 * reset read cache here in case string instruction is restared
3052 * without decoding
3053 */
3054 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003055 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003056
3057done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003058 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059
3060twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003061 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003063 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 u16 size;
3065 unsigned long address;
3066
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003067 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003068 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003069 goto cannot_emulate;
3070
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003071 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003072 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003073 goto done;
3074
Avi Kivity33e38852008-05-21 15:34:25 +03003075 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003076 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003077 /* Disable writeback. */
3078 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003079 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003081 rc = read_descriptor(ctxt, ops, c->src.ptr,
3082 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003083 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 goto done;
3085 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003086 /* Disable writeback. */
3087 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003089 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003090 if (c->modrm_mod == 3) {
3091 switch (c->modrm_rm) {
3092 case 1:
3093 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003094 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003095 goto done;
3096 break;
3097 default:
3098 goto cannot_emulate;
3099 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003100 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003101 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003102 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003103 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003104 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003105 goto done;
3106 realmode_lidt(ctxt->vcpu, size, address);
3107 }
Avi Kivity16286d02008-04-14 14:40:50 +03003108 /* Disable writeback. */
3109 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 break;
3111 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003112 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003113 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 break;
3115 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003116 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3117 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003118 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003120 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003121 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003122 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003124 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003125 /* Disable writeback. */
3126 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 break;
3128 default:
3129 goto cannot_emulate;
3130 }
3131 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003132 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003133 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003134 if (rc != X86EMUL_CONTINUE)
3135 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003136 else
3137 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003138 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003139 case 0x06:
3140 emulate_clts(ctxt->vcpu);
3141 c->dst.type = OP_NONE;
3142 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003143 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003144 kvm_emulate_wbinvd(ctxt->vcpu);
3145 c->dst.type = OP_NONE;
3146 break;
3147 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003148 case 0x0d: /* GrpP (prefetch) */
3149 case 0x18: /* Grp16 (prefetch/nop) */
3150 c->dst.type = OP_NONE;
3151 break;
3152 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003153 switch (c->modrm_reg) {
3154 case 1:
3155 case 5 ... 7:
3156 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003157 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003158 goto done;
3159 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003160 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003161 c->dst.type = OP_NONE; /* no writeback */
3162 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003164 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3165 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003166 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003167 goto done;
3168 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003169 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003170 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003172 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003173 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003174 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003175 goto done;
3176 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003177 c->dst.type = OP_NONE;
3178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003180 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3181 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003182 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003183 goto done;
3184 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003185
Gleb Natapov338dbc92010-04-28 19:15:32 +03003186 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3187 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3188 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3189 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003190 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003191 goto done;
3192 }
3193
Laurent Viviera01af5e2007-09-24 11:10:56 +02003194 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003196 case 0x30:
3197 /* wrmsr */
3198 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3199 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003200 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003201 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003202 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003203 }
3204 rc = X86EMUL_CONTINUE;
3205 c->dst.type = OP_NONE;
3206 break;
3207 case 0x32:
3208 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003209 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003210 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003211 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003212 } else {
3213 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3214 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3215 }
3216 rc = X86EMUL_CONTINUE;
3217 c->dst.type = OP_NONE;
3218 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003219 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003220 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003221 if (rc != X86EMUL_CONTINUE)
3222 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003223 else
3224 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003225 break;
3226 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003227 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003228 if (rc != X86EMUL_CONTINUE)
3229 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003230 else
3231 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003234 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003235 if (!test_cc(c->b, ctxt->eflags))
3236 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003238 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003239 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003240 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003241 c->dst.type = OP_NONE;
3242 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003243 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003244 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003245 break;
3246 case 0xa1: /* pop fs */
3247 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003248 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003249 goto done;
3250 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003251 case 0xa3:
3252 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003253 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003254 /* only subword offset */
3255 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003256 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003257 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003258 case 0xa4: /* shld imm8, r, r/m */
3259 case 0xa5: /* shld cl, r, r/m */
3260 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3261 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003262 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003263 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003264 break;
3265 case 0xa9: /* pop gs */
3266 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003267 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003268 goto done;
3269 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003270 case 0xab:
3271 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003272 /* only subword offset */
3273 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003274 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003275 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003276 case 0xac: /* shrd imm8, r, r/m */
3277 case 0xad: /* shrd cl, r, r/m */
3278 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3279 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003280 case 0xae: /* clflush */
3281 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282 case 0xb0 ... 0xb1: /* cmpxchg */
3283 /*
3284 * Save real source value, then compare EAX against
3285 * destination.
3286 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003287 c->src.orig_val = c->src.val;
3288 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003289 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3290 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003292 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 } else {
3294 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003295 c->dst.type = OP_REG;
3296 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 }
3298 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 case 0xb3:
3300 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003301 /* only subword offset */
3302 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003303 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003306 c->dst.bytes = c->op_bytes;
3307 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3308 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003311 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 case 0:
3313 goto bt;
3314 case 1:
3315 goto bts;
3316 case 2:
3317 goto btr;
3318 case 3:
3319 goto btc;
3320 }
3321 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003322 case 0xbb:
3323 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003324 /* only subword offset */
3325 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003326 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003327 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003329 c->dst.bytes = c->op_bytes;
3330 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3331 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003333 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003334 c->dst.bytes = c->op_bytes;
3335 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3336 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003337 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003339 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003340 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003341 goto done;
3342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343 }
3344 goto writeback;
3345
3346cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003347 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 return -1;
3349}