blob: c5c42e041e482b085a555663aa78a60d12145727 [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 */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
85#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020087#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020088#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030089#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010090/* Source 2 operand type */
91#define Src2None (0<<29)
92#define Src2CL (1<<29)
93#define Src2ImmByte (2<<29)
94#define Src2One (3<<29)
95#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivity83babbc2010-07-26 14:37:39 +030097#define X2(x) (x), (x)
98#define X3(x) X2(x), (x)
99#define X4(x) X2(x), X2(x)
100#define X5(x) X4(x), (x)
101#define X6(x) X4(x), X2(x)
102#define X7(x) X4(x), X3(x)
103#define X8(x) X4(x), X4(x)
104#define X16(x) X8(x), X8(x)
105
Avi Kivity43bb19c2008-01-18 12:46:50 +0200106enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200107 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +0200108 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200109 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200110};
111
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100112static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 /* 0x00 - 0x07 */
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 /* 0x08 - 0x0F */
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 Gamal94677e62009-08-28 16:41:44 +0200121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
122 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 /* 0x10 - 0x17 */
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,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300126 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200129 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300131 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300132 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200134 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Wei Yongjune97e8832010-07-06 16:51:09 +0800136 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200138 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamalabc19082010-05-12 01:39:21 +0300140 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200142 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal222b7c52010-05-12 01:39:22 +0300144 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145 /* 0x38 - 0x3F */
146 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
147 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200148 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
149 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700150 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200151 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700152 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200153 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300154 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200155 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
156 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300157 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200158 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
159 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700160 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200161 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
162 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700163 0, 0, 0, 0,
164 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300165 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200166 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
167 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300168 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300169 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
170 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300171 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300172 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
173 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200175 Group | Group1_80, Group | Group1_81,
176 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200178 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 /* 0x88 - 0x8F */
180 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
181 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Wei Yongjunb16b2b72010-07-06 16:52:53 +0800182 DstMem | SrcNone | ModRM | Mov, ModRM | DstReg,
Wei Yongjuna5046e62010-07-06 16:49:05 +0800183 ImplicitOps | SrcMem16 | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300184 /* 0x90 - 0x97 */
185 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
186 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300187 0, 0, SrcImmFAddr | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300188 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0xA0 - 0xA7 */
Wei Yongjun5d55f292010-07-07 17:43:35 +0800190 ByteOp | DstAcc | SrcMem | Mov | MemAbs, DstAcc | SrcMem | Mov | MemAbs,
191 ByteOp | DstMem | SrcAcc | Mov | MemAbs, DstMem | SrcAcc | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200192 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
193 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194 /* 0xA8 - 0xAF */
Mohammed Gamaldfb507c2010-05-11 22:22:40 +0300195 DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
Gleb Natapova682e352010-03-18 15:20:21 +0200196 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
197 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300198 /* 0xB0 - 0xB7 */
199 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
200 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
201 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
202 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
203 /* 0xB8 - 0xBF */
204 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
205 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
206 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
207 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300209 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200210 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300211 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300213 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300214 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 /* 0xD0 - 0xD7 */
216 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
217 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
218 0, 0, 0, 0,
219 /* 0xD8 - 0xDF */
220 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300221 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300222 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200223 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
224 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300225 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300226 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov414e6272010-04-28 19:15:26 +0300227 SrcImmFAddr | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200228 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
229 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 /* 0xF0 - 0xF7 */
231 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200232 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700234 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300235 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236};
237
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100238static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200240 0, Group | GroupDual | Group7, 0, 0,
241 0, ImplicitOps, ImplicitOps | Priv, 0,
242 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
243 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0x10 - 0x1F */
245 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
246 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200247 ModRM | ImplicitOps | Priv, ModRM | Priv,
248 ModRM | ImplicitOps | Priv, ModRM | Priv,
249 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200252 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
253 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200254 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 /* 0x40 - 0x47 */
256 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
257 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
258 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
259 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
260 /* 0x48 - 0x4F */
261 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
262 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
263 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
264 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
265 /* 0x50 - 0x5F */
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267 /* 0x60 - 0x6F */
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
269 /* 0x70 - 0x7F */
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
271 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300272 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
273 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 /* 0x90 - 0x9F */
275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300277 ImplicitOps | Stack, ImplicitOps | Stack,
278 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100279 DstMem | SrcReg | Src2ImmByte | ModRM,
280 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300282 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200283 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100284 DstMem | SrcReg | Src2ImmByte | ModRM,
285 DstMem | SrcReg | Src2CL | ModRM,
286 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200288 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
289 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
291 DstReg | SrcMem16 | ModRM | Mov,
292 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200293 0, 0,
294 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
296 DstReg | SrcMem16 | ModRM | Mov,
297 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200298 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
299 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800300 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 /* 0xD0 - 0xDF */
302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
303 /* 0xE0 - 0xEF */
304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
305 /* 0xF0 - 0xFF */
306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
307};
308
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100309static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200310 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200311 ByteOp | DstMem | SrcImm | ModRM | Lock,
312 ByteOp | DstMem | SrcImm | ModRM | Lock,
313 ByteOp | DstMem | SrcImm | ModRM | Lock,
314 ByteOp | DstMem | SrcImm | ModRM | Lock,
315 ByteOp | DstMem | SrcImm | ModRM | Lock,
316 ByteOp | DstMem | SrcImm | ModRM | Lock,
317 ByteOp | DstMem | SrcImm | ModRM | Lock,
318 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200319 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200320 DstMem | SrcImm | ModRM | Lock,
321 DstMem | SrcImm | ModRM | Lock,
322 DstMem | SrcImm | ModRM | Lock,
323 DstMem | SrcImm | ModRM | Lock,
324 DstMem | SrcImm | ModRM | Lock,
325 DstMem | SrcImm | ModRM | Lock,
326 DstMem | SrcImm | ModRM | Lock,
327 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200328 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200329 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
330 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
331 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
332 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
333 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
334 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
335 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
336 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200337 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200338 DstMem | SrcImmByte | ModRM | Lock,
339 DstMem | SrcImmByte | ModRM | Lock,
340 DstMem | SrcImmByte | ModRM | Lock,
341 DstMem | SrcImmByte | ModRM | Lock,
342 DstMem | SrcImmByte | ModRM | Lock,
343 DstMem | SrcImmByte | ModRM | Lock,
344 DstMem | SrcImmByte | ModRM | Lock,
345 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200346 [Group1A*8] =
347 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200348 [Group3_Byte*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800349 ByteOp | SrcImm | DstMem | ModRM, ByteOp | SrcImm | DstMem | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200350 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
351 0, 0, 0, 0,
352 [Group3*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800353 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300354 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200355 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200356 [Group4*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300357 ByteOp | DstMem | SrcNone | ModRM | Lock, ByteOp | DstMem | SrcNone | ModRM | Lock,
Avi Kivityfd607542008-01-18 13:12:26 +0200358 0, 0, 0, 0, 0, 0,
359 [Group5*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300360 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Mohammed Gamald19292e2008-09-08 21:47:19 +0300361 SrcMem | ModRM | Stack, 0,
Gleb Natapov414e6272010-04-28 19:15:26 +0300362 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200363 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200364 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200365 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300366 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200367 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200368 [Group8*8] =
369 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200370 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
371 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200372 [Group9*8] =
Gleb Natapov6550e1f2010-03-21 13:08:21 +0200373 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200374};
375
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100376static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200377 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200378 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300379 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200380 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200381 [Group9*8] =
382 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200383};
384
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200386#define EFLG_ID (1<<21)
387#define EFLG_VIP (1<<20)
388#define EFLG_VIF (1<<19)
389#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200390#define EFLG_VM (1<<17)
391#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200392#define EFLG_IOPL (3<<12)
393#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394#define EFLG_OF (1<<11)
395#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200396#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200397#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398#define EFLG_SF (1<<7)
399#define EFLG_ZF (1<<6)
400#define EFLG_AF (1<<4)
401#define EFLG_PF (1<<2)
402#define EFLG_CF (1<<0)
403
404/*
405 * Instruction emulation:
406 * Most instructions are emulated directly via a fragment of inline assembly
407 * code. This allows us to save/restore EFLAGS and thus very easily pick up
408 * any modified flags.
409 */
410
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800411#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412#define _LO32 "k" /* force 32-bit operand */
413#define _STK "%%rsp" /* stack pointer */
414#elif defined(__i386__)
415#define _LO32 "" /* force 32-bit operand */
416#define _STK "%%esp" /* stack pointer */
417#endif
418
419/*
420 * These EFLAGS bits are restored from saved value during emulation, and
421 * any changes are written back to the saved value after emulation.
422 */
423#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
424
425/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200426#define _PRE_EFLAGS(_sav, _msk, _tmp) \
427 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
428 "movl %"_sav",%"_LO32 _tmp"; " \
429 "push %"_tmp"; " \
430 "push %"_tmp"; " \
431 "movl %"_msk",%"_LO32 _tmp"; " \
432 "andl %"_LO32 _tmp",("_STK"); " \
433 "pushf; " \
434 "notl %"_LO32 _tmp"; " \
435 "andl %"_LO32 _tmp",("_STK"); " \
436 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
437 "pop %"_tmp"; " \
438 "orl %"_LO32 _tmp",("_STK"); " \
439 "popf; " \
440 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
442/* After executing instruction: write-back necessary bits in EFLAGS. */
443#define _POST_EFLAGS(_sav, _msk, _tmp) \
444 /* _sav |= EFLAGS & _msk; */ \
445 "pushf; " \
446 "pop %"_tmp"; " \
447 "andl %"_msk",%"_LO32 _tmp"; " \
448 "orl %"_LO32 _tmp",%"_sav"; "
449
Avi Kivitydda96d82008-11-26 15:14:10 +0200450#ifdef CONFIG_X86_64
451#define ON64(x) x
452#else
453#define ON64(x)
454#endif
455
Avi Kivity6b7ad612008-11-26 15:30:45 +0200456#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
457 do { \
458 __asm__ __volatile__ ( \
459 _PRE_EFLAGS("0", "4", "2") \
460 _op _suffix " %"_x"3,%1; " \
461 _POST_EFLAGS("0", "4", "2") \
462 : "=m" (_eflags), "=m" ((_dst).val), \
463 "=&r" (_tmp) \
464 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200465 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200466
467
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468/* Raw emulation: instruction has two explicit operands. */
469#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200470 do { \
471 unsigned long _tmp; \
472 \
473 switch ((_dst).bytes) { \
474 case 2: \
475 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
476 break; \
477 case 4: \
478 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
479 break; \
480 case 8: \
481 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
482 break; \
483 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 } while (0)
485
486#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
487 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200488 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400489 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200491 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492 break; \
493 default: \
494 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
495 _wx, _wy, _lx, _ly, _qx, _qy); \
496 break; \
497 } \
498 } while (0)
499
500/* Source operand is byte-sized and may be restricted to just %cl. */
501#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
502 __emulate_2op(_op, _src, _dst, _eflags, \
503 "b", "c", "b", "c", "b", "c", "b", "c")
504
505/* Source operand is byte, word, long or quad sized. */
506#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
507 __emulate_2op(_op, _src, _dst, _eflags, \
508 "b", "q", "w", "r", _LO32, "r", "", "r")
509
510/* Source operand is word, long or quad sized. */
511#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
512 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
513 "w", "r", _LO32, "r", "", "r")
514
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100515/* Instruction has three operands and one operand is stored in ECX register */
516#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
517 do { \
518 unsigned long _tmp; \
519 _type _clv = (_cl).val; \
520 _type _srcv = (_src).val; \
521 _type _dstv = (_dst).val; \
522 \
523 __asm__ __volatile__ ( \
524 _PRE_EFLAGS("0", "5", "2") \
525 _op _suffix " %4,%1 \n" \
526 _POST_EFLAGS("0", "5", "2") \
527 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
528 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
529 ); \
530 \
531 (_cl).val = (unsigned long) _clv; \
532 (_src).val = (unsigned long) _srcv; \
533 (_dst).val = (unsigned long) _dstv; \
534 } while (0)
535
536#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
537 do { \
538 switch ((_dst).bytes) { \
539 case 2: \
540 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
541 "w", unsigned short); \
542 break; \
543 case 4: \
544 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
545 "l", unsigned int); \
546 break; \
547 case 8: \
548 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
549 "q", unsigned long)); \
550 break; \
551 } \
552 } while (0)
553
Avi Kivitydda96d82008-11-26 15:14:10 +0200554#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 do { \
556 unsigned long _tmp; \
557 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200558 __asm__ __volatile__ ( \
559 _PRE_EFLAGS("0", "3", "2") \
560 _op _suffix " %1; " \
561 _POST_EFLAGS("0", "3", "2") \
562 : "=m" (_eflags), "+m" ((_dst).val), \
563 "=&r" (_tmp) \
564 : "i" (EFLAGS_MASK)); \
565 } while (0)
566
567/* Instruction has only one explicit operand (no source operand). */
568#define emulate_1op(_op, _dst, _eflags) \
569 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400570 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200571 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
572 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
573 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
574 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 } \
576 } while (0)
577
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578/* Fetch next part of the instruction being emulated. */
579#define insn_fetch(_type, _size, _eip) \
580({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200581 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200582 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 goto done; \
584 (_eip) += (_size); \
585 (_type)_x; \
586})
587
Gleb Natapov414e6272010-04-28 19:15:26 +0300588#define insn_fetch_arr(_arr, _size, _eip) \
589({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
590 if (rc != X86EMUL_CONTINUE) \
591 goto done; \
592 (_eip) += (_size); \
593})
594
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800595static inline unsigned long ad_mask(struct decode_cache *c)
596{
597 return (1UL << (c->ad_bytes << 3)) - 1;
598}
599
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800601static inline unsigned long
602address_mask(struct decode_cache *c, unsigned long reg)
603{
604 if (c->ad_bytes == sizeof(unsigned long))
605 return reg;
606 else
607 return reg & ad_mask(c);
608}
609
610static inline unsigned long
611register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
612{
613 return base + address_mask(c, reg);
614}
615
Harvey Harrison7a9572752008-02-19 07:40:41 -0800616static inline void
617register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
618{
619 if (c->ad_bytes == sizeof(unsigned long))
620 *reg += inc;
621 else
622 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
623}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
Harvey Harrison7a9572752008-02-19 07:40:41 -0800625static inline void jmp_rel(struct decode_cache *c, int rel)
626{
627 register_address_increment(c, &c->eip, rel);
628}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300629
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300630static void set_seg_override(struct decode_cache *c, int seg)
631{
632 c->has_seg_override = true;
633 c->seg_override = seg;
634}
635
Gleb Natapov79168fd2010-04-28 19:15:30 +0300636static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
637 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300638{
639 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
640 return 0;
641
Gleb Natapov79168fd2010-04-28 19:15:30 +0300642 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300643}
644
645static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300646 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300647 struct decode_cache *c)
648{
649 if (!c->has_seg_override)
650 return 0;
651
Gleb Natapov79168fd2010-04-28 19:15:30 +0300652 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300653}
654
Gleb Natapov79168fd2010-04-28 19:15:30 +0300655static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
656 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300657{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300658 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300659}
660
Gleb Natapov79168fd2010-04-28 19:15:30 +0300661static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
662 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300663{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300664 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300665}
666
Gleb Natapov54b84862010-04-28 19:15:44 +0300667static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
668 u32 error, bool valid)
669{
670 ctxt->exception = vec;
671 ctxt->error_code = error;
672 ctxt->error_code_valid = valid;
673 ctxt->restart = false;
674}
675
676static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
677{
678 emulate_exception(ctxt, GP_VECTOR, err, true);
679}
680
681static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
682 int err)
683{
684 ctxt->cr2 = addr;
685 emulate_exception(ctxt, PF_VECTOR, err, true);
686}
687
688static void emulate_ud(struct x86_emulate_ctxt *ctxt)
689{
690 emulate_exception(ctxt, UD_VECTOR, 0, false);
691}
692
693static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
694{
695 emulate_exception(ctxt, TS_VECTOR, err, true);
696}
697
Avi Kivity62266862007-11-20 13:15:52 +0200698static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
699 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300700 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200701{
702 struct fetch_cache *fc = &ctxt->decode.fetch;
703 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300704 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200705
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300706 if (eip == fc->end) {
707 cur_size = fc->end - fc->start;
708 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
709 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
710 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900711 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200712 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300713 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200714 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300715 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900716 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200717}
718
719static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
720 struct x86_emulate_ops *ops,
721 unsigned long eip, void *dest, unsigned size)
722{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900723 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200724
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200725 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200726 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200727 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200728 while (size--) {
729 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900730 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200731 return rc;
732 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900733 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200734}
735
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000736/*
737 * Given the 'reg' portion of a ModRM byte, and a register block, return a
738 * pointer into the block that addresses the relevant register.
739 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
740 */
741static void *decode_register(u8 modrm_reg, unsigned long *regs,
742 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743{
744 void *p;
745
746 p = &regs[modrm_reg];
747 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
748 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
749 return p;
750}
751
752static int read_descriptor(struct x86_emulate_ctxt *ctxt,
753 struct x86_emulate_ops *ops,
754 void *ptr,
755 u16 *size, unsigned long *address, int op_bytes)
756{
757 int rc;
758
759 if (op_bytes == 2)
760 op_bytes = 3;
761 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300762 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200763 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900764 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300766 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200767 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 return rc;
769}
770
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300771static int test_cc(unsigned int condition, unsigned int flags)
772{
773 int rc = 0;
774
775 switch ((condition & 15) >> 1) {
776 case 0: /* o */
777 rc |= (flags & EFLG_OF);
778 break;
779 case 1: /* b/c/nae */
780 rc |= (flags & EFLG_CF);
781 break;
782 case 2: /* z/e */
783 rc |= (flags & EFLG_ZF);
784 break;
785 case 3: /* be/na */
786 rc |= (flags & (EFLG_CF|EFLG_ZF));
787 break;
788 case 4: /* s */
789 rc |= (flags & EFLG_SF);
790 break;
791 case 5: /* p/pe */
792 rc |= (flags & EFLG_PF);
793 break;
794 case 7: /* le/ng */
795 rc |= (flags & EFLG_ZF);
796 /* fall through */
797 case 6: /* l/nge */
798 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
799 break;
800 }
801
802 /* Odd condition identifiers (lsb == 1) have inverted sense. */
803 return (!!rc ^ (condition & 1));
804}
805
Avi Kivity3c118e22007-10-31 10:27:04 +0200806static void decode_register_operand(struct operand *op,
807 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200808 int inhibit_bytereg)
809{
Avi Kivity33615aa2007-10-31 11:15:56 +0200810 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200811 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200812
813 if (!(c->d & ModRM))
814 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200815 op->type = OP_REG;
816 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200817 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200818 op->val = *(u8 *)op->ptr;
819 op->bytes = 1;
820 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200821 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200822 op->bytes = c->op_bytes;
823 switch (op->bytes) {
824 case 2:
825 op->val = *(u16 *)op->ptr;
826 break;
827 case 4:
828 op->val = *(u32 *)op->ptr;
829 break;
830 case 8:
831 op->val = *(u64 *) op->ptr;
832 break;
833 }
834 }
835 op->orig_val = op->val;
836}
837
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200838static int decode_modrm(struct x86_emulate_ctxt *ctxt,
839 struct x86_emulate_ops *ops)
840{
841 struct decode_cache *c = &ctxt->decode;
842 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700843 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900844 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200845
846 if (c->rex_prefix) {
847 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
848 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
849 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
850 }
851
852 c->modrm = insn_fetch(u8, 1, c->eip);
853 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
854 c->modrm_reg |= (c->modrm & 0x38) >> 3;
855 c->modrm_rm |= (c->modrm & 0x07);
856 c->modrm_ea = 0;
857 c->use_modrm_ea = 1;
858
859 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300860 c->modrm_ptr = decode_register(c->modrm_rm,
861 c->regs, c->d & ByteOp);
862 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200863 return rc;
864 }
865
866 if (c->ad_bytes == 2) {
867 unsigned bx = c->regs[VCPU_REGS_RBX];
868 unsigned bp = c->regs[VCPU_REGS_RBP];
869 unsigned si = c->regs[VCPU_REGS_RSI];
870 unsigned di = c->regs[VCPU_REGS_RDI];
871
872 /* 16-bit ModR/M decode. */
873 switch (c->modrm_mod) {
874 case 0:
875 if (c->modrm_rm == 6)
876 c->modrm_ea += insn_fetch(u16, 2, c->eip);
877 break;
878 case 1:
879 c->modrm_ea += insn_fetch(s8, 1, c->eip);
880 break;
881 case 2:
882 c->modrm_ea += insn_fetch(u16, 2, c->eip);
883 break;
884 }
885 switch (c->modrm_rm) {
886 case 0:
887 c->modrm_ea += bx + si;
888 break;
889 case 1:
890 c->modrm_ea += bx + di;
891 break;
892 case 2:
893 c->modrm_ea += bp + si;
894 break;
895 case 3:
896 c->modrm_ea += bp + di;
897 break;
898 case 4:
899 c->modrm_ea += si;
900 break;
901 case 5:
902 c->modrm_ea += di;
903 break;
904 case 6:
905 if (c->modrm_mod != 0)
906 c->modrm_ea += bp;
907 break;
908 case 7:
909 c->modrm_ea += bx;
910 break;
911 }
912 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
913 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300914 if (!c->has_seg_override)
915 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200916 c->modrm_ea = (u16)c->modrm_ea;
917 } else {
918 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700919 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200920 sib = insn_fetch(u8, 1, c->eip);
921 index_reg |= (sib >> 3) & 7;
922 base_reg |= sib & 7;
923 scale = sib >> 6;
924
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700925 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
926 c->modrm_ea += insn_fetch(s32, 4, c->eip);
927 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200928 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700929 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200930 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700931 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
932 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700933 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700934 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200936 switch (c->modrm_mod) {
937 case 0:
938 if (c->modrm_rm == 5)
939 c->modrm_ea += insn_fetch(s32, 4, c->eip);
940 break;
941 case 1:
942 c->modrm_ea += insn_fetch(s8, 1, c->eip);
943 break;
944 case 2:
945 c->modrm_ea += insn_fetch(s32, 4, c->eip);
946 break;
947 }
948 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949done:
950 return rc;
951}
952
953static int decode_abs(struct x86_emulate_ctxt *ctxt,
954 struct x86_emulate_ops *ops)
955{
956 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900957 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200958
959 switch (c->ad_bytes) {
960 case 2:
961 c->modrm_ea = insn_fetch(u16, 2, c->eip);
962 break;
963 case 4:
964 c->modrm_ea = insn_fetch(u32, 4, c->eip);
965 break;
966 case 8:
967 c->modrm_ea = insn_fetch(u64, 8, c->eip);
968 break;
969 }
970done:
971 return rc;
972}
973
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200975x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200977 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900978 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200980 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Gleb Natapov5cd21912010-03-18 15:20:26 +0200983 /* we cannot decode insn before we complete previous rep insn */
984 WARN_ON(ctxt->restart);
985
Gleb Natapov063db062010-03-18 15:20:06 +0200986 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300987 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300988 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989
990 switch (mode) {
991 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200992 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200994 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
996 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200997 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800999#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001001 def_op_bytes = 4;
1002 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 break;
1004#endif
1005 default:
1006 return -1;
1007 }
1008
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001009 c->op_bytes = def_op_bytes;
1010 c->ad_bytes = def_ad_bytes;
1011
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001013 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001016 /* switch between 2/4 bytes */
1017 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 break;
1019 case 0x67: /* address-size override */
1020 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001021 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001022 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001024 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001025 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001028 case 0x2e: /* CS override */
1029 case 0x36: /* SS override */
1030 case 0x3e: /* DS override */
1031 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001035 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001037 case 0x40 ... 0x4f: /* REX */
1038 if (mode != X86EMUL_MODE_PROT64)
1039 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001040 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001041 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001045 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001046 c->rep_prefix = REPNE_PREFIX;
1047 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001049 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 default:
1052 goto done_prefixes;
1053 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001054
1055 /* Any legacy prefix after a REX prefix nullifies its effect. */
1056
Avi Kivity33615aa2007-10-31 11:15:56 +02001057 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 }
1059
1060done_prefixes:
1061
1062 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001064 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001065 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
1067 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001068 c->d = opcode_table[c->b];
1069 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001071 if (c->b == 0x0f) {
1072 c->twobyte = 1;
1073 c->b = insn_fetch(u8, 1, c->eip);
1074 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001076 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077
Avi Kivitye09d0822008-01-18 12:38:59 +02001078 if (c->d & Group) {
1079 group = c->d & GroupMask;
1080 c->modrm = insn_fetch(u8, 1, c->eip);
1081 --c->eip;
1082
1083 group = (group << 3) + ((c->modrm >> 3) & 7);
1084 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1085 c->d = group2_table[group];
1086 else
1087 c->d = group_table[group];
1088 }
1089
1090 /* Unrecognised? */
1091 if (c->d == 0) {
1092 DPRINTF("Cannot emulate %02x\n", c->b);
1093 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 }
1095
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001096 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1097 c->op_bytes = 8;
1098
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100 if (c->d & ModRM)
1101 rc = decode_modrm(ctxt, ops);
1102 else if (c->d & MemAbs)
1103 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001104 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001107 if (!c->has_seg_override)
1108 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001109
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001110 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001111 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001112
1113 if (c->ad_bytes != 8)
1114 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001115
1116 if (c->rip_relative)
1117 c->modrm_ea += c->eip;
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 /*
1120 * Decode and fetch the source operand: register, memory
1121 * or immediate.
1122 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001123 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 case SrcNone:
1125 break;
1126 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001127 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 break;
1129 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 goto srcmem_common;
1132 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001133 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 goto srcmem_common;
1135 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 c->src.bytes = (c->d & ByteOp) ? 1 :
1137 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001138 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001139 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001140 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001141 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001142 /*
1143 * For instructions with a ModR/M byte, switch to register
1144 * access if Mod = 3.
1145 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001146 if ((c->d & ModRM) && c->modrm_mod == 3) {
1147 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001148 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001149 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001150 break;
1151 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001152 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001153 c->src.ptr = (unsigned long *)c->modrm_ea;
1154 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 break;
1156 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001157 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001158 c->src.type = OP_IMM;
1159 c->src.ptr = (unsigned long *)c->eip;
1160 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1161 if (c->src.bytes == 8)
1162 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001164 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001166 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 break;
1168 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001169 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 break;
1171 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001172 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 break;
1174 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001175 if ((c->d & SrcMask) == SrcImmU) {
1176 switch (c->src.bytes) {
1177 case 1:
1178 c->src.val &= 0xff;
1179 break;
1180 case 2:
1181 c->src.val &= 0xffff;
1182 break;
1183 case 4:
1184 c->src.val &= 0xffffffff;
1185 break;
1186 }
1187 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 break;
1189 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001190 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001191 c->src.type = OP_IMM;
1192 c->src.ptr = (unsigned long *)c->eip;
1193 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001194 if ((c->d & SrcMask) == SrcImmByte)
1195 c->src.val = insn_fetch(s8, 1, c->eip);
1196 else
1197 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001199 case SrcAcc:
1200 c->src.type = OP_REG;
1201 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1202 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1203 switch (c->src.bytes) {
1204 case 1:
1205 c->src.val = *(u8 *)c->src.ptr;
1206 break;
1207 case 2:
1208 c->src.val = *(u16 *)c->src.ptr;
1209 break;
1210 case 4:
1211 c->src.val = *(u32 *)c->src.ptr;
1212 break;
1213 case 8:
1214 c->src.val = *(u64 *)c->src.ptr;
1215 break;
1216 }
1217 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001218 case SrcOne:
1219 c->src.bytes = 1;
1220 c->src.val = 1;
1221 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001222 case SrcSI:
1223 c->src.type = OP_MEM;
1224 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1225 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001226 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001227 c->regs[VCPU_REGS_RSI]);
1228 c->src.val = 0;
1229 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001230 case SrcImmFAddr:
1231 c->src.type = OP_IMM;
1232 c->src.ptr = (unsigned long *)c->eip;
1233 c->src.bytes = c->op_bytes + 2;
1234 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1235 break;
1236 case SrcMemFAddr:
1237 c->src.type = OP_MEM;
1238 c->src.ptr = (unsigned long *)c->modrm_ea;
1239 c->src.bytes = c->op_bytes + 2;
1240 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 }
1242
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001243 /*
1244 * Decode and fetch the second source operand: register, memory
1245 * or immediate.
1246 */
1247 switch (c->d & Src2Mask) {
1248 case Src2None:
1249 break;
1250 case Src2CL:
1251 c->src2.bytes = 1;
1252 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1253 break;
1254 case Src2ImmByte:
1255 c->src2.type = OP_IMM;
1256 c->src2.ptr = (unsigned long *)c->eip;
1257 c->src2.bytes = 1;
1258 c->src2.val = insn_fetch(u8, 1, c->eip);
1259 break;
1260 case Src2One:
1261 c->src2.bytes = 1;
1262 c->src2.val = 1;
1263 break;
1264 }
1265
Avi Kivity038e51d2007-01-22 20:40:40 -08001266 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001267 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001268 case ImplicitOps:
1269 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001270 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001271 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001272 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001273 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001274 break;
1275 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001276 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001277 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001278 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001279 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001280 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001281 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001282 break;
1283 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001284 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001285 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001286 if ((c->d & DstMask) == DstMem64)
1287 c->dst.bytes = 8;
1288 else
1289 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001290 c->dst.val = 0;
1291 if (c->d & BitOp) {
1292 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1293
1294 c->dst.ptr = (void *)c->dst.ptr +
1295 (c->src.val & mask) / 8;
1296 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001297 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001298 case DstAcc:
1299 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001300 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001301 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001302 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001303 case 1:
1304 c->dst.val = *(u8 *)c->dst.ptr;
1305 break;
1306 case 2:
1307 c->dst.val = *(u16 *)c->dst.ptr;
1308 break;
1309 case 4:
1310 c->dst.val = *(u32 *)c->dst.ptr;
1311 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001312 case 8:
1313 c->dst.val = *(u64 *)c->dst.ptr;
1314 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001315 }
1316 c->dst.orig_val = c->dst.val;
1317 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001318 case DstDI:
1319 c->dst.type = OP_MEM;
1320 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1321 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001322 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001323 c->regs[VCPU_REGS_RDI]);
1324 c->dst.val = 0;
1325 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001326 }
1327
1328done:
1329 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1330}
1331
Gleb Natapov9de41572010-04-28 19:15:22 +03001332static int read_emulated(struct x86_emulate_ctxt *ctxt,
1333 struct x86_emulate_ops *ops,
1334 unsigned long addr, void *dest, unsigned size)
1335{
1336 int rc;
1337 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001338 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001339
1340 while (size) {
1341 int n = min(size, 8u);
1342 size -= n;
1343 if (mc->pos < mc->end)
1344 goto read_cached;
1345
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001346 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1347 ctxt->vcpu);
1348 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001349 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001350 if (rc != X86EMUL_CONTINUE)
1351 return rc;
1352 mc->end += n;
1353
1354 read_cached:
1355 memcpy(dest, mc->data + mc->pos, n);
1356 mc->pos += n;
1357 dest += n;
1358 addr += n;
1359 }
1360 return X86EMUL_CONTINUE;
1361}
1362
Gleb Natapov7b262e92010-03-18 15:20:27 +02001363static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1364 struct x86_emulate_ops *ops,
1365 unsigned int size, unsigned short port,
1366 void *dest)
1367{
1368 struct read_cache *rc = &ctxt->decode.io_read;
1369
1370 if (rc->pos == rc->end) { /* refill pio read ahead */
1371 struct decode_cache *c = &ctxt->decode;
1372 unsigned int in_page, n;
1373 unsigned int count = c->rep_prefix ?
1374 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1375 in_page = (ctxt->eflags & EFLG_DF) ?
1376 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1377 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1378 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1379 count);
1380 if (n == 0)
1381 n = 1;
1382 rc->pos = rc->end = 0;
1383 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1384 return 0;
1385 rc->end = n * size;
1386 }
1387
1388 memcpy(dest, rc->data + rc->pos, size);
1389 rc->pos += size;
1390 return 1;
1391}
1392
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393static u32 desc_limit_scaled(struct desc_struct *desc)
1394{
1395 u32 limit = get_desc_limit(desc);
1396
1397 return desc->g ? (limit << 12) | 0xfff : limit;
1398}
1399
1400static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1401 struct x86_emulate_ops *ops,
1402 u16 selector, struct desc_ptr *dt)
1403{
1404 if (selector & 1 << 2) {
1405 struct desc_struct desc;
1406 memset (dt, 0, sizeof *dt);
1407 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1408 return;
1409
1410 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1411 dt->address = get_desc_base(&desc);
1412 } else
1413 ops->get_gdt(dt, ctxt->vcpu);
1414}
1415
1416/* allowed just for 8 bytes segments */
1417static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1418 struct x86_emulate_ops *ops,
1419 u16 selector, struct desc_struct *desc)
1420{
1421 struct desc_ptr dt;
1422 u16 index = selector >> 3;
1423 int ret;
1424 u32 err;
1425 ulong addr;
1426
1427 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1428
1429 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001430 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431 return X86EMUL_PROPAGATE_FAULT;
1432 }
1433 addr = dt.address + index * 8;
1434 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1435 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001436 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437
1438 return ret;
1439}
1440
1441/* allowed just for 8 bytes segments */
1442static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1443 struct x86_emulate_ops *ops,
1444 u16 selector, struct desc_struct *desc)
1445{
1446 struct desc_ptr dt;
1447 u16 index = selector >> 3;
1448 u32 err;
1449 ulong addr;
1450 int ret;
1451
1452 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1453
1454 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001455 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456 return X86EMUL_PROPAGATE_FAULT;
1457 }
1458
1459 addr = dt.address + index * 8;
1460 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1461 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001462 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463
1464 return ret;
1465}
1466
1467static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1468 struct x86_emulate_ops *ops,
1469 u16 selector, int seg)
1470{
1471 struct desc_struct seg_desc;
1472 u8 dpl, rpl, cpl;
1473 unsigned err_vec = GP_VECTOR;
1474 u32 err_code = 0;
1475 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1476 int ret;
1477
1478 memset(&seg_desc, 0, sizeof seg_desc);
1479
1480 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1481 || ctxt->mode == X86EMUL_MODE_REAL) {
1482 /* set real mode segment descriptor */
1483 set_desc_base(&seg_desc, selector << 4);
1484 set_desc_limit(&seg_desc, 0xffff);
1485 seg_desc.type = 3;
1486 seg_desc.p = 1;
1487 seg_desc.s = 1;
1488 goto load;
1489 }
1490
1491 /* NULL selector is not valid for TR, CS and SS */
1492 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1493 && null_selector)
1494 goto exception;
1495
1496 /* TR should be in GDT only */
1497 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1498 goto exception;
1499
1500 if (null_selector) /* for NULL selector skip all following checks */
1501 goto load;
1502
1503 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1504 if (ret != X86EMUL_CONTINUE)
1505 return ret;
1506
1507 err_code = selector & 0xfffc;
1508 err_vec = GP_VECTOR;
1509
1510 /* can't load system descriptor into segment selecor */
1511 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1512 goto exception;
1513
1514 if (!seg_desc.p) {
1515 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1516 goto exception;
1517 }
1518
1519 rpl = selector & 3;
1520 dpl = seg_desc.dpl;
1521 cpl = ops->cpl(ctxt->vcpu);
1522
1523 switch (seg) {
1524 case VCPU_SREG_SS:
1525 /*
1526 * segment is not a writable data segment or segment
1527 * selector's RPL != CPL or segment selector's RPL != CPL
1528 */
1529 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1530 goto exception;
1531 break;
1532 case VCPU_SREG_CS:
1533 if (!(seg_desc.type & 8))
1534 goto exception;
1535
1536 if (seg_desc.type & 4) {
1537 /* conforming */
1538 if (dpl > cpl)
1539 goto exception;
1540 } else {
1541 /* nonconforming */
1542 if (rpl > cpl || dpl != cpl)
1543 goto exception;
1544 }
1545 /* CS(RPL) <- CPL */
1546 selector = (selector & 0xfffc) | cpl;
1547 break;
1548 case VCPU_SREG_TR:
1549 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1550 goto exception;
1551 break;
1552 case VCPU_SREG_LDTR:
1553 if (seg_desc.s || seg_desc.type != 2)
1554 goto exception;
1555 break;
1556 default: /* DS, ES, FS, or GS */
1557 /*
1558 * segment is not a data or readable code segment or
1559 * ((segment is a data or nonconforming code segment)
1560 * and (both RPL and CPL > DPL))
1561 */
1562 if ((seg_desc.type & 0xa) == 0x8 ||
1563 (((seg_desc.type & 0xc) != 0xc) &&
1564 (rpl > dpl && cpl > dpl)))
1565 goto exception;
1566 break;
1567 }
1568
1569 if (seg_desc.s) {
1570 /* mark segment as accessed */
1571 seg_desc.type |= 1;
1572 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1573 if (ret != X86EMUL_CONTINUE)
1574 return ret;
1575 }
1576load:
1577 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1578 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1579 return X86EMUL_CONTINUE;
1580exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001581 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001582 return X86EMUL_PROPAGATE_FAULT;
1583}
1584
Wei Yongjunc37eda12010-06-15 09:03:33 +08001585static inline int writeback(struct x86_emulate_ctxt *ctxt,
1586 struct x86_emulate_ops *ops)
1587{
1588 int rc;
1589 struct decode_cache *c = &ctxt->decode;
1590 u32 err;
1591
1592 switch (c->dst.type) {
1593 case OP_REG:
1594 /* The 4-byte case *is* correct:
1595 * in 64-bit mode we zero-extend.
1596 */
1597 switch (c->dst.bytes) {
1598 case 1:
1599 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1600 break;
1601 case 2:
1602 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1603 break;
1604 case 4:
1605 *c->dst.ptr = (u32)c->dst.val;
1606 break; /* 64b: zero-ext */
1607 case 8:
1608 *c->dst.ptr = c->dst.val;
1609 break;
1610 }
1611 break;
1612 case OP_MEM:
1613 if (c->lock_prefix)
1614 rc = ops->cmpxchg_emulated(
1615 (unsigned long)c->dst.ptr,
1616 &c->dst.orig_val,
1617 &c->dst.val,
1618 c->dst.bytes,
1619 &err,
1620 ctxt->vcpu);
1621 else
1622 rc = ops->write_emulated(
1623 (unsigned long)c->dst.ptr,
1624 &c->dst.val,
1625 c->dst.bytes,
1626 &err,
1627 ctxt->vcpu);
1628 if (rc == X86EMUL_PROPAGATE_FAULT)
1629 emulate_pf(ctxt,
1630 (unsigned long)c->dst.ptr, err);
1631 if (rc != X86EMUL_CONTINUE)
1632 return rc;
1633 break;
1634 case OP_NONE:
1635 /* no writeback */
1636 break;
1637 default:
1638 break;
1639 }
1640 return X86EMUL_CONTINUE;
1641}
1642
Gleb Natapov79168fd2010-04-28 19:15:30 +03001643static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1644 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645{
1646 struct decode_cache *c = &ctxt->decode;
1647
1648 c->dst.type = OP_MEM;
1649 c->dst.bytes = c->op_bytes;
1650 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001651 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001652 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653 c->regs[VCPU_REGS_RSP]);
1654}
1655
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001656static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001657 struct x86_emulate_ops *ops,
1658 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001659{
1660 struct decode_cache *c = &ctxt->decode;
1661 int rc;
1662
Gleb Natapov79168fd2010-04-28 19:15:30 +03001663 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001664 c->regs[VCPU_REGS_RSP]),
1665 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001666 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667 return rc;
1668
Avi Kivity350f69d2009-01-05 11:12:40 +02001669 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001670 return rc;
1671}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001672
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001673static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1674 struct x86_emulate_ops *ops,
1675 void *dest, int len)
1676{
1677 int rc;
1678 unsigned long val, change_mask;
1679 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001680 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001681
1682 rc = emulate_pop(ctxt, ops, &val, len);
1683 if (rc != X86EMUL_CONTINUE)
1684 return rc;
1685
1686 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1687 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1688
1689 switch(ctxt->mode) {
1690 case X86EMUL_MODE_PROT64:
1691 case X86EMUL_MODE_PROT32:
1692 case X86EMUL_MODE_PROT16:
1693 if (cpl == 0)
1694 change_mask |= EFLG_IOPL;
1695 if (cpl <= iopl)
1696 change_mask |= EFLG_IF;
1697 break;
1698 case X86EMUL_MODE_VM86:
1699 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001700 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001701 return X86EMUL_PROPAGATE_FAULT;
1702 }
1703 change_mask |= EFLG_IF;
1704 break;
1705 default: /* real mode */
1706 change_mask |= (EFLG_IOPL | EFLG_IF);
1707 break;
1708 }
1709
1710 *(unsigned long *)dest =
1711 (ctxt->eflags & ~change_mask) | (val & change_mask);
1712
1713 return rc;
1714}
1715
Gleb Natapov79168fd2010-04-28 19:15:30 +03001716static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1717 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001718{
1719 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001720
Gleb Natapov79168fd2010-04-28 19:15:30 +03001721 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001722
Gleb Natapov79168fd2010-04-28 19:15:30 +03001723 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001724}
1725
1726static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1727 struct x86_emulate_ops *ops, int seg)
1728{
1729 struct decode_cache *c = &ctxt->decode;
1730 unsigned long selector;
1731 int rc;
1732
1733 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001734 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001735 return rc;
1736
Gleb Natapov2e873022010-03-18 15:20:18 +02001737 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001738 return rc;
1739}
1740
Wei Yongjunc37eda12010-06-15 09:03:33 +08001741static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001742 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001743{
1744 struct decode_cache *c = &ctxt->decode;
1745 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001746 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001747 int reg = VCPU_REGS_RAX;
1748
1749 while (reg <= VCPU_REGS_RDI) {
1750 (reg == VCPU_REGS_RSP) ?
1751 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1752
Gleb Natapov79168fd2010-04-28 19:15:30 +03001753 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001754
1755 rc = writeback(ctxt, ops);
1756 if (rc != X86EMUL_CONTINUE)
1757 return rc;
1758
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001759 ++reg;
1760 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001761
1762 /* Disable writeback. */
1763 c->dst.type = OP_NONE;
1764
1765 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001766}
1767
1768static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1769 struct x86_emulate_ops *ops)
1770{
1771 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001772 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001773 int reg = VCPU_REGS_RDI;
1774
1775 while (reg >= VCPU_REGS_RAX) {
1776 if (reg == VCPU_REGS_RSP) {
1777 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1778 c->op_bytes);
1779 --reg;
1780 }
1781
1782 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001783 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001784 break;
1785 --reg;
1786 }
1787 return rc;
1788}
1789
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001790static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1791 struct x86_emulate_ops *ops)
1792{
1793 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001794
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001795 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796}
1797
Laurent Vivier05f086f2007-09-24 11:10:55 +02001798static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001799{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001800 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801 switch (c->modrm_reg) {
1802 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001803 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804 break;
1805 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001806 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001807 break;
1808 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001809 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001810 break;
1811 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001812 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813 break;
1814 case 4: /* sal/shl */
1815 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001816 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001817 break;
1818 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001819 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001820 break;
1821 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001822 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823 break;
1824 }
1825}
1826
1827static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001828 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001829{
1830 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001831
1832 switch (c->modrm_reg) {
1833 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001834 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001835 break;
1836 case 2: /* not */
1837 c->dst.val = ~c->dst.val;
1838 break;
1839 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001840 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001841 break;
1842 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001843 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001845 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001846}
1847
1848static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001849 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001850{
1851 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001852
1853 switch (c->modrm_reg) {
1854 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001855 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856 break;
1857 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001858 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001860 case 2: /* call near abs */ {
1861 long int old_eip;
1862 old_eip = c->eip;
1863 c->eip = c->src.val;
1864 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001865 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001866 break;
1867 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001868 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001869 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001870 break;
1871 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001872 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001874 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001875 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001876}
1877
1878static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001879 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001880{
1881 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001882 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883
1884 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1885 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1887 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001888 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001889 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001890 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1891 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001892
Laurent Vivier05f086f2007-09-24 11:10:55 +02001893 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001894 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001895 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001896}
1897
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001898static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1899 struct x86_emulate_ops *ops)
1900{
1901 struct decode_cache *c = &ctxt->decode;
1902 int rc;
1903 unsigned long cs;
1904
1905 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001906 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001907 return rc;
1908 if (c->op_bytes == 4)
1909 c->eip = (u32)c->eip;
1910 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001911 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001912 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001913 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001914 return rc;
1915}
1916
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001917static inline void
1918setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 struct x86_emulate_ops *ops, struct desc_struct *cs,
1920 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001921{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001922 memset(cs, 0, sizeof(struct desc_struct));
1923 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1924 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001925
1926 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001927 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001928 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001929 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001930 cs->type = 0x0b; /* Read, Execute, Accessed */
1931 cs->s = 1;
1932 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001933 cs->p = 1;
1934 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001935
Gleb Natapov79168fd2010-04-28 19:15:30 +03001936 set_desc_base(ss, 0); /* flat segment */
1937 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001938 ss->g = 1; /* 4kb granularity */
1939 ss->s = 1;
1940 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001941 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001942 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001943 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001944}
1945
1946static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001947emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001948{
1949 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001950 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001951 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001952 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001953
1954 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001955 if (ctxt->mode == X86EMUL_MODE_REAL ||
1956 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001957 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001958 return X86EMUL_PROPAGATE_FAULT;
1959 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001960
Gleb Natapov79168fd2010-04-28 19:15:30 +03001961 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001962
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001963 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001964 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001965 cs_sel = (u16)(msr_data & 0xfffc);
1966 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001967
1968 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001969 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001970 cs.l = 1;
1971 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001972 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1973 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1974 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1975 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001976
1977 c->regs[VCPU_REGS_RCX] = c->eip;
1978 if (is_long_mode(ctxt->vcpu)) {
1979#ifdef CONFIG_X86_64
1980 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1981
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001982 ops->get_msr(ctxt->vcpu,
1983 ctxt->mode == X86EMUL_MODE_PROT64 ?
1984 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001985 c->eip = msr_data;
1986
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001987 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001988 ctxt->eflags &= ~(msr_data | EFLG_RF);
1989#endif
1990 } else {
1991 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001992 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001993 c->eip = (u32)msr_data;
1994
1995 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1996 }
1997
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001998 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001999}
2000
Andre Przywara8c604352009-06-18 12:56:01 +02002001static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002002emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002003{
2004 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002006 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002007 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002008
Gleb Natapova0044752010-02-10 14:21:31 +02002009 /* inject #GP if in real mode */
2010 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002011 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002012 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002013 }
2014
2015 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2016 * Therefore, we inject an #UD.
2017 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002018 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002019 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002020 return X86EMUL_PROPAGATE_FAULT;
2021 }
Andre Przywara8c604352009-06-18 12:56:01 +02002022
Gleb Natapov79168fd2010-04-28 19:15:30 +03002023 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002024
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002025 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002026 switch (ctxt->mode) {
2027 case X86EMUL_MODE_PROT32:
2028 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002029 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002030 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002031 }
2032 break;
2033 case X86EMUL_MODE_PROT64:
2034 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002035 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002036 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002037 }
2038 break;
2039 }
2040
2041 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002042 cs_sel = (u16)msr_data;
2043 cs_sel &= ~SELECTOR_RPL_MASK;
2044 ss_sel = cs_sel + 8;
2045 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002046 if (ctxt->mode == X86EMUL_MODE_PROT64
2047 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002048 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002049 cs.l = 1;
2050 }
2051
Gleb Natapov79168fd2010-04-28 19:15:30 +03002052 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2053 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2054 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2055 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002056
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002057 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002058 c->eip = msr_data;
2059
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002060 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002061 c->regs[VCPU_REGS_RSP] = msr_data;
2062
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002063 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002064}
2065
Andre Przywara4668f052009-06-18 12:56:02 +02002066static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002067emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002068{
2069 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002070 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002071 u64 msr_data;
2072 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002073 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002074
Gleb Natapova0044752010-02-10 14:21:31 +02002075 /* inject #GP if in real mode or Virtual 8086 mode */
2076 if (ctxt->mode == X86EMUL_MODE_REAL ||
2077 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002078 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002079 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002080 }
2081
Gleb Natapov79168fd2010-04-28 19:15:30 +03002082 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002083
2084 if ((c->rex_prefix & 0x8) != 0x0)
2085 usermode = X86EMUL_MODE_PROT64;
2086 else
2087 usermode = X86EMUL_MODE_PROT32;
2088
2089 cs.dpl = 3;
2090 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002091 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002092 switch (usermode) {
2093 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002094 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002095 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002096 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002097 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002098 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002099 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002100 break;
2101 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002103 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002104 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002105 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002106 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002107 ss_sel = cs_sel + 8;
2108 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002109 cs.l = 1;
2110 break;
2111 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002112 cs_sel |= SELECTOR_RPL_MASK;
2113 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002114
Gleb Natapov79168fd2010-04-28 19:15:30 +03002115 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2116 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2117 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2118 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002119
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002120 c->eip = c->regs[VCPU_REGS_RDX];
2121 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002122
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002123 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002124}
2125
Gleb Natapov9c537242010-03-18 15:20:05 +02002126static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2127 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002128{
2129 int iopl;
2130 if (ctxt->mode == X86EMUL_MODE_REAL)
2131 return false;
2132 if (ctxt->mode == X86EMUL_MODE_VM86)
2133 return true;
2134 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002135 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002136}
2137
2138static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2139 struct x86_emulate_ops *ops,
2140 u16 port, u16 len)
2141{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002142 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002143 int r;
2144 u16 io_bitmap_ptr;
2145 u8 perm, bit_idx = port & 0x7;
2146 unsigned mask = (1 << len) - 1;
2147
Gleb Natapov79168fd2010-04-28 19:15:30 +03002148 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2149 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002150 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002151 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002152 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002153 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2154 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002155 if (r != X86EMUL_CONTINUE)
2156 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002157 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002158 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002159 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2160 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002161 if (r != X86EMUL_CONTINUE)
2162 return false;
2163 if ((perm >> bit_idx) & mask)
2164 return false;
2165 return true;
2166}
2167
2168static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2169 struct x86_emulate_ops *ops,
2170 u16 port, u16 len)
2171{
Gleb Natapov9c537242010-03-18 15:20:05 +02002172 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002173 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2174 return false;
2175 return true;
2176}
2177
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2179 struct x86_emulate_ops *ops,
2180 struct tss_segment_16 *tss)
2181{
2182 struct decode_cache *c = &ctxt->decode;
2183
2184 tss->ip = c->eip;
2185 tss->flag = ctxt->eflags;
2186 tss->ax = c->regs[VCPU_REGS_RAX];
2187 tss->cx = c->regs[VCPU_REGS_RCX];
2188 tss->dx = c->regs[VCPU_REGS_RDX];
2189 tss->bx = c->regs[VCPU_REGS_RBX];
2190 tss->sp = c->regs[VCPU_REGS_RSP];
2191 tss->bp = c->regs[VCPU_REGS_RBP];
2192 tss->si = c->regs[VCPU_REGS_RSI];
2193 tss->di = c->regs[VCPU_REGS_RDI];
2194
2195 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2196 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2197 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2198 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2199 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2200}
2201
2202static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2203 struct x86_emulate_ops *ops,
2204 struct tss_segment_16 *tss)
2205{
2206 struct decode_cache *c = &ctxt->decode;
2207 int ret;
2208
2209 c->eip = tss->ip;
2210 ctxt->eflags = tss->flag | 2;
2211 c->regs[VCPU_REGS_RAX] = tss->ax;
2212 c->regs[VCPU_REGS_RCX] = tss->cx;
2213 c->regs[VCPU_REGS_RDX] = tss->dx;
2214 c->regs[VCPU_REGS_RBX] = tss->bx;
2215 c->regs[VCPU_REGS_RSP] = tss->sp;
2216 c->regs[VCPU_REGS_RBP] = tss->bp;
2217 c->regs[VCPU_REGS_RSI] = tss->si;
2218 c->regs[VCPU_REGS_RDI] = tss->di;
2219
2220 /*
2221 * SDM says that segment selectors are loaded before segment
2222 * descriptors
2223 */
2224 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2225 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2226 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2227 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2228 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2229
2230 /*
2231 * Now load segment descriptors. If fault happenes at this stage
2232 * it is handled in a context of new task
2233 */
2234 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2235 if (ret != X86EMUL_CONTINUE)
2236 return ret;
2237 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2238 if (ret != X86EMUL_CONTINUE)
2239 return ret;
2240 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2241 if (ret != X86EMUL_CONTINUE)
2242 return ret;
2243 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2244 if (ret != X86EMUL_CONTINUE)
2245 return ret;
2246 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2247 if (ret != X86EMUL_CONTINUE)
2248 return ret;
2249
2250 return X86EMUL_CONTINUE;
2251}
2252
2253static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2254 struct x86_emulate_ops *ops,
2255 u16 tss_selector, u16 old_tss_sel,
2256 ulong old_tss_base, struct desc_struct *new_desc)
2257{
2258 struct tss_segment_16 tss_seg;
2259 int ret;
2260 u32 err, new_tss_base = get_desc_base(new_desc);
2261
2262 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2263 &err);
2264 if (ret == X86EMUL_PROPAGATE_FAULT) {
2265 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002266 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002267 return ret;
2268 }
2269
2270 save_state_to_tss16(ctxt, ops, &tss_seg);
2271
2272 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2273 &err);
2274 if (ret == X86EMUL_PROPAGATE_FAULT) {
2275 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002276 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002277 return ret;
2278 }
2279
2280 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2281 &err);
2282 if (ret == X86EMUL_PROPAGATE_FAULT) {
2283 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002284 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002285 return ret;
2286 }
2287
2288 if (old_tss_sel != 0xffff) {
2289 tss_seg.prev_task_link = old_tss_sel;
2290
2291 ret = ops->write_std(new_tss_base,
2292 &tss_seg.prev_task_link,
2293 sizeof tss_seg.prev_task_link,
2294 ctxt->vcpu, &err);
2295 if (ret == X86EMUL_PROPAGATE_FAULT) {
2296 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002297 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 return ret;
2299 }
2300 }
2301
2302 return load_state_from_tss16(ctxt, ops, &tss_seg);
2303}
2304
2305static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2306 struct x86_emulate_ops *ops,
2307 struct tss_segment_32 *tss)
2308{
2309 struct decode_cache *c = &ctxt->decode;
2310
2311 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2312 tss->eip = c->eip;
2313 tss->eflags = ctxt->eflags;
2314 tss->eax = c->regs[VCPU_REGS_RAX];
2315 tss->ecx = c->regs[VCPU_REGS_RCX];
2316 tss->edx = c->regs[VCPU_REGS_RDX];
2317 tss->ebx = c->regs[VCPU_REGS_RBX];
2318 tss->esp = c->regs[VCPU_REGS_RSP];
2319 tss->ebp = c->regs[VCPU_REGS_RBP];
2320 tss->esi = c->regs[VCPU_REGS_RSI];
2321 tss->edi = c->regs[VCPU_REGS_RDI];
2322
2323 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2324 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2325 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2326 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2327 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2328 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2329 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2330}
2331
2332static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2333 struct x86_emulate_ops *ops,
2334 struct tss_segment_32 *tss)
2335{
2336 struct decode_cache *c = &ctxt->decode;
2337 int ret;
2338
Gleb Natapov0f122442010-04-28 19:15:31 +03002339 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002340 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002341 return X86EMUL_PROPAGATE_FAULT;
2342 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 c->eip = tss->eip;
2344 ctxt->eflags = tss->eflags | 2;
2345 c->regs[VCPU_REGS_RAX] = tss->eax;
2346 c->regs[VCPU_REGS_RCX] = tss->ecx;
2347 c->regs[VCPU_REGS_RDX] = tss->edx;
2348 c->regs[VCPU_REGS_RBX] = tss->ebx;
2349 c->regs[VCPU_REGS_RSP] = tss->esp;
2350 c->regs[VCPU_REGS_RBP] = tss->ebp;
2351 c->regs[VCPU_REGS_RSI] = tss->esi;
2352 c->regs[VCPU_REGS_RDI] = tss->edi;
2353
2354 /*
2355 * SDM says that segment selectors are loaded before segment
2356 * descriptors
2357 */
2358 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2359 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2360 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2361 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2362 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2363 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2364 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2365
2366 /*
2367 * Now load segment descriptors. If fault happenes at this stage
2368 * it is handled in a context of new task
2369 */
2370 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2371 if (ret != X86EMUL_CONTINUE)
2372 return ret;
2373 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2374 if (ret != X86EMUL_CONTINUE)
2375 return ret;
2376 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2377 if (ret != X86EMUL_CONTINUE)
2378 return ret;
2379 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2380 if (ret != X86EMUL_CONTINUE)
2381 return ret;
2382 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2383 if (ret != X86EMUL_CONTINUE)
2384 return ret;
2385 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2386 if (ret != X86EMUL_CONTINUE)
2387 return ret;
2388 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2389 if (ret != X86EMUL_CONTINUE)
2390 return ret;
2391
2392 return X86EMUL_CONTINUE;
2393}
2394
2395static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2396 struct x86_emulate_ops *ops,
2397 u16 tss_selector, u16 old_tss_sel,
2398 ulong old_tss_base, struct desc_struct *new_desc)
2399{
2400 struct tss_segment_32 tss_seg;
2401 int ret;
2402 u32 err, new_tss_base = get_desc_base(new_desc);
2403
2404 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2405 &err);
2406 if (ret == X86EMUL_PROPAGATE_FAULT) {
2407 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002408 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 return ret;
2410 }
2411
2412 save_state_to_tss32(ctxt, ops, &tss_seg);
2413
2414 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2415 &err);
2416 if (ret == X86EMUL_PROPAGATE_FAULT) {
2417 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002418 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419 return ret;
2420 }
2421
2422 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2423 &err);
2424 if (ret == X86EMUL_PROPAGATE_FAULT) {
2425 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002426 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 return ret;
2428 }
2429
2430 if (old_tss_sel != 0xffff) {
2431 tss_seg.prev_task_link = old_tss_sel;
2432
2433 ret = ops->write_std(new_tss_base,
2434 &tss_seg.prev_task_link,
2435 sizeof tss_seg.prev_task_link,
2436 ctxt->vcpu, &err);
2437 if (ret == X86EMUL_PROPAGATE_FAULT) {
2438 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002439 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 return ret;
2441 }
2442 }
2443
2444 return load_state_from_tss32(ctxt, ops, &tss_seg);
2445}
2446
2447static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002448 struct x86_emulate_ops *ops,
2449 u16 tss_selector, int reason,
2450 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002451{
2452 struct desc_struct curr_tss_desc, next_tss_desc;
2453 int ret;
2454 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2455 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002456 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002457 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458
2459 /* FIXME: old_tss_base == ~0 ? */
2460
2461 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2462 if (ret != X86EMUL_CONTINUE)
2463 return ret;
2464 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2465 if (ret != X86EMUL_CONTINUE)
2466 return ret;
2467
2468 /* FIXME: check that next_tss_desc is tss */
2469
2470 if (reason != TASK_SWITCH_IRET) {
2471 if ((tss_selector & 3) > next_tss_desc.dpl ||
2472 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002473 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474 return X86EMUL_PROPAGATE_FAULT;
2475 }
2476 }
2477
Gleb Natapovceffb452010-03-18 15:20:19 +02002478 desc_limit = desc_limit_scaled(&next_tss_desc);
2479 if (!next_tss_desc.p ||
2480 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2481 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002482 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 return X86EMUL_PROPAGATE_FAULT;
2484 }
2485
2486 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2487 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2488 write_segment_descriptor(ctxt, ops, old_tss_sel,
2489 &curr_tss_desc);
2490 }
2491
2492 if (reason == TASK_SWITCH_IRET)
2493 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2494
2495 /* set back link to prev task only if NT bit is set in eflags
2496 note that old_tss_sel is not used afetr this point */
2497 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2498 old_tss_sel = 0xffff;
2499
2500 if (next_tss_desc.type & 8)
2501 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2502 old_tss_base, &next_tss_desc);
2503 else
2504 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2505 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002506 if (ret != X86EMUL_CONTINUE)
2507 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508
2509 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2510 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2511
2512 if (reason != TASK_SWITCH_IRET) {
2513 next_tss_desc.type |= (1 << 1); /* set busy flag */
2514 write_segment_descriptor(ctxt, ops, tss_selector,
2515 &next_tss_desc);
2516 }
2517
2518 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2519 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2520 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2521
Jan Kiszkae269fb22010-04-14 15:51:09 +02002522 if (has_error_code) {
2523 struct decode_cache *c = &ctxt->decode;
2524
2525 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2526 c->lock_prefix = 0;
2527 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002528 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002529 }
2530
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 return ret;
2532}
2533
2534int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2535 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002536 u16 tss_selector, int reason,
2537 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538{
2539 struct decode_cache *c = &ctxt->decode;
2540 int rc;
2541
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002543 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544
Jan Kiszkae269fb22010-04-14 15:51:09 +02002545 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2546 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547
2548 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002549 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002550 if (rc == X86EMUL_CONTINUE)
2551 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 }
2553
Gleb Natapov19d04432010-04-15 12:29:50 +03002554 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555}
2556
Gleb Natapova682e352010-03-18 15:20:21 +02002557static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002558 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002559{
2560 struct decode_cache *c = &ctxt->decode;
2561 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2562
Gleb Natapovd9271122010-03-18 15:20:22 +02002563 register_address_increment(c, &c->regs[reg], df * op->bytes);
2564 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002565}
2566
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002567int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002568x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002569{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002570 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002571 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002572 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002573 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002574
Gleb Natapov9de41572010-04-28 19:15:22 +03002575 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002576
Gleb Natapov11616242010-02-11 14:43:14 +02002577 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002578 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002579 goto done;
2580 }
2581
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002582 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002583 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002584 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002585 goto done;
2586 }
2587
Gleb Natapove92805a2010-02-10 14:21:35 +02002588 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002589 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002590 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002591 goto done;
2592 }
2593
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002594 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002595 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002596 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002597 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002598 string_done:
2599 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002600 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002601 goto done;
2602 }
2603 /* The second termination condition only applies for REPE
2604 * and REPNE. Test if the repeat string operation prefix is
2605 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2606 * corresponding termination condition according to:
2607 * - if REPE/REPZ and ZF = 0 then done
2608 * - if REPNE/REPNZ and ZF = 1 then done
2609 */
2610 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002611 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002612 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002613 ((ctxt->eflags & EFLG_ZF) == 0))
2614 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002615 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002616 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2617 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002618 }
Gleb Natapov063db062010-03-18 15:20:06 +02002619 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002620 }
2621
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002622 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002623 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002624 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002625 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002626 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002627 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002628 }
2629
Gleb Natapove35b7b92010-02-25 16:36:42 +02002630 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002631 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2632 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002633 if (rc != X86EMUL_CONTINUE)
2634 goto done;
2635 }
2636
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002637 if ((c->d & DstMask) == ImplicitOps)
2638 goto special_insn;
2639
2640
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002641 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2642 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002643 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2644 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002645 if (rc != X86EMUL_CONTINUE)
2646 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002647 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002648 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002649
Avi Kivity018a98d2007-11-27 19:30:56 +02002650special_insn:
2651
Laurent Viviere4e03de2007-09-18 11:52:50 +02002652 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 goto twobyte_insn;
2654
Laurent Viviere4e03de2007-09-18 11:52:50 +02002655 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 case 0x00 ... 0x05:
2657 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002658 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002660 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002661 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002662 break;
2663 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002664 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002665 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002666 goto done;
2667 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 case 0x08 ... 0x0d:
2669 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002670 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002672 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002673 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002674 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 case 0x10 ... 0x15:
2676 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002677 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002679 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002680 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002681 break;
2682 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002683 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002684 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002685 goto done;
2686 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 case 0x18 ... 0x1d:
2688 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002689 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002691 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002692 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002693 break;
2694 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002695 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002696 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002697 goto done;
2698 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002699 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002701 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 break;
2703 case 0x28 ... 0x2d:
2704 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002705 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 break;
2707 case 0x30 ... 0x35:
2708 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002709 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 break;
2711 case 0x38 ... 0x3d:
2712 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002713 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002715 case 0x40 ... 0x47: /* inc r16/r32 */
2716 emulate_1op("inc", c->dst, ctxt->eflags);
2717 break;
2718 case 0x48 ... 0x4f: /* dec r16/r32 */
2719 emulate_1op("dec", c->dst, ctxt->eflags);
2720 break;
2721 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002722 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002723 break;
2724 case 0x58 ... 0x5f: /* pop reg */
2725 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002726 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002727 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002728 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002729 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002730 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002731 rc = emulate_pusha(ctxt, ops);
2732 if (rc != X86EMUL_CONTINUE)
2733 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002734 break;
2735 case 0x61: /* popa */
2736 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002737 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002738 goto done;
2739 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002741 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002743 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002745 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002746 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002747 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002748 break;
2749 case 0x6c: /* insb */
2750 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002751 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002752 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002753 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002754 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002755 goto done;
2756 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002757 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2758 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002759 goto done; /* IO is needed, skip writeback */
2760 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002761 case 0x6e: /* outsb */
2762 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002763 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002764 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002765 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002766 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002767 goto done;
2768 }
Gleb Natapov79729952010-03-18 15:20:24 +02002769 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2770 &c->src.val, 1, ctxt->vcpu);
2771
2772 c->dst.type = OP_NONE; /* nothing to writeback */
2773 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002774 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002775 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002776 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002777 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002779 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 case 0:
2781 goto add;
2782 case 1:
2783 goto or;
2784 case 2:
2785 goto adc;
2786 case 3:
2787 goto sbb;
2788 case 4:
2789 goto and;
2790 case 5:
2791 goto sub;
2792 case 6:
2793 goto xor;
2794 case 7:
2795 goto cmp;
2796 }
2797 break;
2798 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002799 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002800 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 break;
2802 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002803 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002805 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002807 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 break;
2809 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002810 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 break;
2812 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002813 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 break; /* 64b reg: zero-extend */
2815 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002816 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 break;
2818 }
2819 /*
2820 * Write back the memory destination with implicit LOCK
2821 * prefix.
2822 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002823 c->dst.val = c->src.val;
2824 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002827 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002828 case 0x8c: /* mov r/m, sreg */
2829 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002830 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002831 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002832 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002833 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002834 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002835 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002836 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002837 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002838 case 0x8e: { /* mov seg, r/m16 */
2839 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002840
2841 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002842
Gleb Natapovc6975182010-02-18 12:15:01 +02002843 if (c->modrm_reg == VCPU_SREG_CS ||
2844 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002845 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002846 goto done;
2847 }
2848
Glauber Costa310b5d32009-05-12 16:21:06 -04002849 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002850 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002851
Gleb Natapov2e873022010-03-18 15:20:18 +02002852 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002853
2854 c->dst.type = OP_NONE; /* Disable writeback. */
2855 break;
2856 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002858 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002859 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002862 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002863 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2864 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002865 break;
2866 }
2867 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002868 c->src.type = OP_REG;
2869 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002870 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2871 c->src.val = *(c->src.ptr);
2872 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002873 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002874 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002875 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002876 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002877 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002878 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002879 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002880 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002881 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2882 if (rc != X86EMUL_CONTINUE)
2883 goto done;
2884 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002885 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002887 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002889 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002890 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002891 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002892 case 0xa8 ... 0xa9: /* test ax, imm */
2893 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002895 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 break;
2897 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002898 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 case 0xae ... 0xaf: /* scas */
2900 DPRINTF("Urk! I don't handle SCAS.\n");
2901 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002902 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002903 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002904 case 0xc0 ... 0xc1:
2905 emulate_grp2(ctxt);
2906 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002907 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002908 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002909 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002910 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002911 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002912 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2913 mov:
2914 c->dst.val = c->src.val;
2915 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002916 case 0xcb: /* ret far */
2917 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002918 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002919 goto done;
2920 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002921 case 0xd0 ... 0xd1: /* Grp2 */
2922 c->src.val = 1;
2923 emulate_grp2(ctxt);
2924 break;
2925 case 0xd2 ... 0xd3: /* Grp2 */
2926 c->src.val = c->regs[VCPU_REGS_RCX];
2927 emulate_grp2(ctxt);
2928 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002929 case 0xe4: /* inb */
2930 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002931 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002932 case 0xe6: /* outb */
2933 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002934 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002935 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002936 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002937 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002938 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002939 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002940 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002941 }
2942 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002943 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002944 case 0xea: { /* jmp far */
2945 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002946 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002947 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2948
2949 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002950 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002951
Gleb Natapov414e6272010-04-28 19:15:26 +03002952 c->eip = 0;
2953 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002954 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002955 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002956 case 0xeb:
2957 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002958 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002959 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002960 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002961 case 0xec: /* in al,dx */
2962 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002963 c->src.val = c->regs[VCPU_REGS_RDX];
2964 do_io_in:
2965 c->dst.bytes = min(c->dst.bytes, 4u);
2966 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002967 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002968 goto done;
2969 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002970 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2971 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002972 goto done; /* IO is needed */
2973 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08002974 case 0xee: /* out dx,al */
2975 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002976 c->src.val = c->regs[VCPU_REGS_RDX];
2977 do_io_out:
2978 c->dst.bytes = min(c->dst.bytes, 4u);
2979 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002980 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002981 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002982 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002983 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2984 ctxt->vcpu);
2985 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002986 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002987 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002988 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002989 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002990 case 0xf5: /* cmc */
2991 /* complement carry flag from eflags reg */
2992 ctxt->eflags ^= EFLG_CF;
2993 c->dst.type = OP_NONE; /* Disable writeback. */
2994 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002995 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002996 if (!emulate_grp3(ctxt, ops))
2997 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002998 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002999 case 0xf8: /* clc */
3000 ctxt->eflags &= ~EFLG_CF;
3001 c->dst.type = OP_NONE; /* Disable writeback. */
3002 break;
3003 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003004 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003005 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003006 goto done;
3007 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003008 ctxt->eflags &= ~X86_EFLAGS_IF;
3009 c->dst.type = OP_NONE; /* Disable writeback. */
3010 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003011 break;
3012 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003013 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003014 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003015 goto done;
3016 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003017 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003018 ctxt->eflags |= X86_EFLAGS_IF;
3019 c->dst.type = OP_NONE; /* Disable writeback. */
3020 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003021 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003022 case 0xfc: /* cld */
3023 ctxt->eflags &= ~EFLG_DF;
3024 c->dst.type = OP_NONE; /* Disable writeback. */
3025 break;
3026 case 0xfd: /* std */
3027 ctxt->eflags |= EFLG_DF;
3028 c->dst.type = OP_NONE; /* Disable writeback. */
3029 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003030 case 0xfe: /* Grp4 */
3031 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003032 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003033 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003034 goto done;
3035 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003036 case 0xff: /* Grp5 */
3037 if (c->modrm_reg == 5)
3038 goto jump_far;
3039 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003040 default:
3041 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003043
3044writeback:
3045 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003046 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003047 goto done;
3048
Gleb Natapov5cd21912010-03-18 15:20:26 +02003049 /*
3050 * restore dst type in case the decoding will be reused
3051 * (happens for string instruction )
3052 */
3053 c->dst.type = saved_dst_type;
3054
Gleb Natapova682e352010-03-18 15:20:21 +02003055 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003056 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3057 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003058
3059 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003060 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3061 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003062
Gleb Natapov5cd21912010-03-18 15:20:26 +02003063 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003064 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003065 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003066 /*
3067 * Re-enter guest when pio read ahead buffer is empty or,
3068 * if it is not used, after each 1024 iteration.
3069 */
3070 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3071 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003072 ctxt->restart = false;
3073 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003074 /*
3075 * reset read cache here in case string instruction is restared
3076 * without decoding
3077 */
3078 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003079 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003080
3081done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003082 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083
3084twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003085 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003087 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 u16 size;
3089 unsigned long address;
3090
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003091 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003092 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003093 goto cannot_emulate;
3094
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003095 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003096 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003097 goto done;
3098
Avi Kivity33e38852008-05-21 15:34:25 +03003099 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003100 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003101 /* Disable writeback. */
3102 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003103 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003105 rc = read_descriptor(ctxt, ops, c->src.ptr,
3106 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003107 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 goto done;
3109 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003110 /* Disable writeback. */
3111 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003113 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003114 if (c->modrm_mod == 3) {
3115 switch (c->modrm_rm) {
3116 case 1:
3117 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003118 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003119 goto done;
3120 break;
3121 default:
3122 goto cannot_emulate;
3123 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003124 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003125 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003126 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003127 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003128 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003129 goto done;
3130 realmode_lidt(ctxt->vcpu, size, address);
3131 }
Avi Kivity16286d02008-04-14 14:40:50 +03003132 /* Disable writeback. */
3133 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 break;
3135 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003136 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003137 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 break;
3139 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003140 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3141 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003142 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003144 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003145 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003146 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003148 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003149 /* Disable writeback. */
3150 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 break;
3152 default:
3153 goto cannot_emulate;
3154 }
3155 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003156 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003157 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003158 if (rc != X86EMUL_CONTINUE)
3159 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003160 else
3161 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003162 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003163 case 0x06:
3164 emulate_clts(ctxt->vcpu);
3165 c->dst.type = OP_NONE;
3166 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003167 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003168 kvm_emulate_wbinvd(ctxt->vcpu);
3169 c->dst.type = OP_NONE;
3170 break;
3171 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003172 case 0x0d: /* GrpP (prefetch) */
3173 case 0x18: /* Grp16 (prefetch/nop) */
3174 c->dst.type = OP_NONE;
3175 break;
3176 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003177 switch (c->modrm_reg) {
3178 case 1:
3179 case 5 ... 7:
3180 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003181 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003182 goto done;
3183 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003184 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003185 c->dst.type = OP_NONE; /* no writeback */
3186 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003188 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3189 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003190 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003191 goto done;
3192 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003193 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
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 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003197 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003198 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003199 goto done;
3200 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003201 c->dst.type = OP_NONE;
3202 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003204 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3205 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003206 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003207 goto done;
3208 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003209
Gleb Natapov338dbc92010-04-28 19:15:32 +03003210 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3211 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3212 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3213 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003214 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003215 goto done;
3216 }
3217
Laurent Viviera01af5e2007-09-24 11:10:56 +02003218 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003220 case 0x30:
3221 /* wrmsr */
3222 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3223 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003224 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003225 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003226 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003227 }
3228 rc = X86EMUL_CONTINUE;
3229 c->dst.type = OP_NONE;
3230 break;
3231 case 0x32:
3232 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003233 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003234 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003235 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003236 } else {
3237 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3238 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3239 }
3240 rc = X86EMUL_CONTINUE;
3241 c->dst.type = OP_NONE;
3242 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003243 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003244 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003245 if (rc != X86EMUL_CONTINUE)
3246 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003247 else
3248 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003249 break;
3250 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003251 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003252 if (rc != X86EMUL_CONTINUE)
3253 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003254 else
3255 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003256 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003258 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003259 if (!test_cc(c->b, ctxt->eflags))
3260 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003262 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003263 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003264 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003265 c->dst.type = OP_NONE;
3266 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003267 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003268 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003269 break;
3270 case 0xa1: /* pop fs */
3271 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003272 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003273 goto done;
3274 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003275 case 0xa3:
3276 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003277 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003278 /* only subword offset */
3279 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003280 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003281 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003282 case 0xa4: /* shld imm8, r, r/m */
3283 case 0xa5: /* shld cl, r, r/m */
3284 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3285 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003286 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003287 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003288 break;
3289 case 0xa9: /* pop gs */
3290 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003291 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003292 goto done;
3293 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003294 case 0xab:
3295 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003296 /* only subword offset */
3297 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003298 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003299 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003300 case 0xac: /* shrd imm8, r, r/m */
3301 case 0xad: /* shrd cl, r, r/m */
3302 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3303 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003304 case 0xae: /* clflush */
3305 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 case 0xb0 ... 0xb1: /* cmpxchg */
3307 /*
3308 * Save real source value, then compare EAX against
3309 * destination.
3310 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003311 c->src.orig_val = c->src.val;
3312 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003313 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3314 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003316 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 } else {
3318 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003319 c->dst.type = OP_REG;
3320 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 }
3322 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 case 0xb3:
3324 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003325 /* only subword offset */
3326 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003327 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003330 c->dst.bytes = c->op_bytes;
3331 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3332 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003335 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336 case 0:
3337 goto bt;
3338 case 1:
3339 goto bts;
3340 case 2:
3341 goto btr;
3342 case 3:
3343 goto btc;
3344 }
3345 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003346 case 0xbb:
3347 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003348 /* only subword offset */
3349 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003350 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003351 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003353 c->dst.bytes = c->op_bytes;
3354 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3355 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003357 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003358 c->dst.bytes = c->op_bytes;
3359 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3360 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003361 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003363 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003364 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003365 goto done;
3366 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003367 default:
3368 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 }
3370 goto writeback;
3371
3372cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003373 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 return -1;
3375}