blob: da7df34036ca2af82125e07acf3f84c09bc6c602 [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. */
Avi Kivity2ce49532010-07-26 14:37:46 +030049#define ByteOp (1<<16) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivity2ce49532010-07-26 14:37:46 +030051#define ImplicitOps (1<<17) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<17) /* Register operand. */
53#define DstMem (3<<17) /* Memory operand. */
54#define DstAcc (4<<17) /* Destination Accumulator */
55#define DstDI (5<<17) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<17) /* 64bit memory operand */
57#define DstMask (7<<17)
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 */
Avi Kivity2ce49532010-07-26 14:37:46 +030085#define GroupMask 0x0f /* Group number stored in bits 0:3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086/* Misc flags */
Avi Kivity047a4812010-07-26 14:37:47 +030087#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020088#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020089#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030090#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091/* Source 2 operand type */
92#define Src2None (0<<29)
93#define Src2CL (1<<29)
94#define Src2ImmByte (2<<29)
95#define Src2One (3<<29)
96#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Avi Kivityea9ef042010-07-29 15:11:34 +030098#define X2(x) x, x
99#define X3(x) X2(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +0300100#define X4(x) X2(x), X2(x)
Avi Kivityea9ef042010-07-29 15:11:34 +0300101#define X5(x) X4(x), x
Avi Kivity83babbc2010-07-26 14:37:39 +0300102#define X6(x) X4(x), X2(x)
103#define X7(x) X4(x), X3(x)
104#define X8(x) X4(x), X4(x)
105#define X16(x) X8(x), X8(x)
106
Avi Kivity43bb19c2008-01-18 12:46:50 +0200107enum {
Avi Kivitye071edd2010-07-26 14:37:51 +0300108 Group1, Group1A, Group3, Group4, Group5, Group7, Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200109};
110
Avi Kivityd65b1de2010-07-29 15:11:35 +0300111struct opcode {
112 u32 flags;
113};
114
115static struct opcode opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200117 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300119 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300120 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200122 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200124 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
125 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300129 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300130 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200132 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300134 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Wei Yongjune97e8832010-07-06 16:51:09 +0800139 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200141 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamalabc19082010-05-12 01:39:21 +0300143 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200145 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal222b7c52010-05-12 01:39:22 +0300147 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 /* 0x38 - 0x3F */
149 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
150 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200151 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
152 0, 0,
Avi Kivity749358a2010-07-26 14:37:40 +0300153 /* 0x40 - 0x4F */
154 X16(DstReg),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300155 /* 0x50 - 0x57 */
Avi Kivity38491862010-07-26 14:37:41 +0300156 X8(SrcReg | Stack),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300157 /* 0x58 - 0x5F */
Avi Kivity38491862010-07-26 14:37:41 +0300158 X8(DstReg | Stack),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700159 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200160 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
161 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700162 0, 0, 0, 0,
163 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300164 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200165 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
166 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300167 /* 0x70 - 0x7F */
168 X16(SrcImmByte),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0x80 - 0x87 */
Avi Kivity4968ec42010-07-26 14:37:49 +0300170 ByteOp | DstMem | SrcImm | ModRM | Group | Group1,
171 DstMem | SrcImm | ModRM | Group | Group1,
172 ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1,
173 DstMem | SrcImmByte | ModRM | Group | Group1,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200175 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0x88 - 0x8F */
177 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
178 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Wei Yongjunb16b2b72010-07-06 16:52:53 +0800179 DstMem | SrcNone | ModRM | Mov, ModRM | DstReg,
Wei Yongjuna5046e62010-07-06 16:49:05 +0800180 ImplicitOps | SrcMem16 | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300181 /* 0x90 - 0x97 */
182 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
183 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300184 0, 0, SrcImmFAddr | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300185 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 /* 0xA0 - 0xA7 */
Wei Yongjun5d55f292010-07-07 17:43:35 +0800187 ByteOp | DstAcc | SrcMem | Mov | MemAbs, DstAcc | SrcMem | Mov | MemAbs,
188 ByteOp | DstMem | SrcAcc | Mov | MemAbs, DstMem | SrcAcc | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200189 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
190 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191 /* 0xA8 - 0xAF */
Mohammed Gamaldfb507c2010-05-11 22:22:40 +0300192 DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
Gleb Natapova682e352010-03-18 15:20:21 +0200193 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
194 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300195 /* 0xB0 - 0xB7 */
Avi Kivityb6e61532010-07-26 14:37:43 +0300196 X8(ByteOp | DstReg | SrcImm | Mov),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300197 /* 0xB8 - 0xBF */
Avi Kivityb6e61532010-07-26 14:37:43 +0300198 X8(DstReg | SrcImm | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300200 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200201 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300202 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300204 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300205 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206 /* 0xD0 - 0xD7 */
207 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
208 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
209 0, 0, 0, 0,
210 /* 0xD8 - 0xDF */
211 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300212 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300213 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200214 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
215 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300216 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300217 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov414e6272010-04-28 19:15:26 +0300218 SrcImmFAddr | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200219 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
220 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xF0 - 0xF7 */
222 0, 0, 0, 0,
Avi Kivitye071edd2010-07-26 14:37:51 +0300223 ImplicitOps | Priv, ImplicitOps, ByteOp | Group | Group3, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700225 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300226 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227};
228
Avi Kivityd65b1de2010-07-29 15:11:35 +0300229static struct opcode twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200231 0, Group | GroupDual | Group7, 0, 0,
232 0, ImplicitOps, ImplicitOps | Priv, 0,
233 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
234 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 /* 0x10 - 0x1F */
236 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
237 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200238 ModRM | ImplicitOps | Priv, ModRM | Priv,
239 ModRM | ImplicitOps | Priv, ModRM | Priv,
240 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 0, 0, 0, 0, 0, 0, 0, 0,
242 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200243 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
244 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200245 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300246 /* 0x40 - 0x4F */
247 X16(DstReg | SrcMem | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* 0x50 - 0x5F */
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /* 0x60 - 0x6F */
251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 /* 0x70 - 0x7F */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 /* 0x80 - 0x8F */
Avi Kivity880a1882010-07-26 14:37:45 +0300255 X16(SrcImm),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 /* 0x90 - 0x9F */
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300259 ImplicitOps | Stack, ImplicitOps | Stack,
260 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100261 DstMem | SrcReg | Src2ImmByte | ModRM,
262 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300264 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200265 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100266 DstMem | SrcReg | Src2ImmByte | ModRM,
267 DstMem | SrcReg | Src2CL | ModRM,
268 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200270 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
271 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
273 DstReg | SrcMem16 | ModRM | Mov,
274 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200275 0, 0,
276 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
278 DstReg | SrcMem16 | ModRM | Mov,
279 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200280 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
281 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800282 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 /* 0xD0 - 0xDF */
284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
285 /* 0xE0 - 0xEF */
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 /* 0xF0 - 0xFF */
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
289};
290
Avi Kivityd65b1de2010-07-29 15:11:35 +0300291static struct opcode group_table[] = {
Avi Kivity4968ec42010-07-26 14:37:49 +0300292 [Group1*8] =
293 X7(Lock), 0,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200294 [Group1A*8] =
295 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200296 [Group3*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800297 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
Avi Kivitydfe11482010-07-26 14:37:50 +0300298 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Avi Kivitye071edd2010-07-26 14:37:51 +0300299 X4(Undefined),
Avi Kivityfd607542008-01-18 13:12:26 +0200300 [Group4*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300301 ByteOp | DstMem | SrcNone | ModRM | Lock, ByteOp | DstMem | SrcNone | ModRM | Lock,
Avi Kivityfd607542008-01-18 13:12:26 +0200302 0, 0, 0, 0, 0, 0,
303 [Group5*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300304 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Mohammed Gamald19292e2008-09-08 21:47:19 +0300305 SrcMem | ModRM | Stack, 0,
Gleb Natapov414e6272010-04-28 19:15:26 +0300306 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200307 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200308 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200309 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300310 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200311 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200312 [Group8*8] =
313 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200314 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
315 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200316 [Group9*8] =
Gleb Natapov6550e1f2010-03-21 13:08:21 +0200317 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200318};
319
Avi Kivityd65b1de2010-07-29 15:11:35 +0300320static struct opcode group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200321 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200322 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300323 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200324 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200325 [Group9*8] =
326 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200327};
328
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200330#define EFLG_ID (1<<21)
331#define EFLG_VIP (1<<20)
332#define EFLG_VIF (1<<19)
333#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200334#define EFLG_VM (1<<17)
335#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200336#define EFLG_IOPL (3<<12)
337#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338#define EFLG_OF (1<<11)
339#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200340#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200341#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342#define EFLG_SF (1<<7)
343#define EFLG_ZF (1<<6)
344#define EFLG_AF (1<<4)
345#define EFLG_PF (1<<2)
346#define EFLG_CF (1<<0)
347
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300348#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
349#define EFLG_RESERVED_ONE_MASK 2
350
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351/*
352 * Instruction emulation:
353 * Most instructions are emulated directly via a fragment of inline assembly
354 * code. This allows us to save/restore EFLAGS and thus very easily pick up
355 * any modified flags.
356 */
357
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800358#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359#define _LO32 "k" /* force 32-bit operand */
360#define _STK "%%rsp" /* stack pointer */
361#elif defined(__i386__)
362#define _LO32 "" /* force 32-bit operand */
363#define _STK "%%esp" /* stack pointer */
364#endif
365
366/*
367 * These EFLAGS bits are restored from saved value during emulation, and
368 * any changes are written back to the saved value after emulation.
369 */
370#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
371
372/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200373#define _PRE_EFLAGS(_sav, _msk, _tmp) \
374 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
375 "movl %"_sav",%"_LO32 _tmp"; " \
376 "push %"_tmp"; " \
377 "push %"_tmp"; " \
378 "movl %"_msk",%"_LO32 _tmp"; " \
379 "andl %"_LO32 _tmp",("_STK"); " \
380 "pushf; " \
381 "notl %"_LO32 _tmp"; " \
382 "andl %"_LO32 _tmp",("_STK"); " \
383 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
384 "pop %"_tmp"; " \
385 "orl %"_LO32 _tmp",("_STK"); " \
386 "popf; " \
387 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
389/* After executing instruction: write-back necessary bits in EFLAGS. */
390#define _POST_EFLAGS(_sav, _msk, _tmp) \
391 /* _sav |= EFLAGS & _msk; */ \
392 "pushf; " \
393 "pop %"_tmp"; " \
394 "andl %"_msk",%"_LO32 _tmp"; " \
395 "orl %"_LO32 _tmp",%"_sav"; "
396
Avi Kivitydda96d82008-11-26 15:14:10 +0200397#ifdef CONFIG_X86_64
398#define ON64(x) x
399#else
400#define ON64(x)
401#endif
402
Avi Kivity6b7ad612008-11-26 15:30:45 +0200403#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
404 do { \
405 __asm__ __volatile__ ( \
406 _PRE_EFLAGS("0", "4", "2") \
407 _op _suffix " %"_x"3,%1; " \
408 _POST_EFLAGS("0", "4", "2") \
409 : "=m" (_eflags), "=m" ((_dst).val), \
410 "=&r" (_tmp) \
411 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200412 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200413
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415/* Raw emulation: instruction has two explicit operands. */
416#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200417 do { \
418 unsigned long _tmp; \
419 \
420 switch ((_dst).bytes) { \
421 case 2: \
422 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
423 break; \
424 case 4: \
425 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
426 break; \
427 case 8: \
428 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
429 break; \
430 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 } while (0)
432
433#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
434 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200435 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400436 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200438 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 break; \
440 default: \
441 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
442 _wx, _wy, _lx, _ly, _qx, _qy); \
443 break; \
444 } \
445 } while (0)
446
447/* Source operand is byte-sized and may be restricted to just %cl. */
448#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
449 __emulate_2op(_op, _src, _dst, _eflags, \
450 "b", "c", "b", "c", "b", "c", "b", "c")
451
452/* Source operand is byte, word, long or quad sized. */
453#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
454 __emulate_2op(_op, _src, _dst, _eflags, \
455 "b", "q", "w", "r", _LO32, "r", "", "r")
456
457/* Source operand is word, long or quad sized. */
458#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
459 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
460 "w", "r", _LO32, "r", "", "r")
461
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100462/* Instruction has three operands and one operand is stored in ECX register */
463#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
464 do { \
465 unsigned long _tmp; \
466 _type _clv = (_cl).val; \
467 _type _srcv = (_src).val; \
468 _type _dstv = (_dst).val; \
469 \
470 __asm__ __volatile__ ( \
471 _PRE_EFLAGS("0", "5", "2") \
472 _op _suffix " %4,%1 \n" \
473 _POST_EFLAGS("0", "5", "2") \
474 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
475 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
476 ); \
477 \
478 (_cl).val = (unsigned long) _clv; \
479 (_src).val = (unsigned long) _srcv; \
480 (_dst).val = (unsigned long) _dstv; \
481 } while (0)
482
483#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
484 do { \
485 switch ((_dst).bytes) { \
486 case 2: \
487 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
488 "w", unsigned short); \
489 break; \
490 case 4: \
491 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
492 "l", unsigned int); \
493 break; \
494 case 8: \
495 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
496 "q", unsigned long)); \
497 break; \
498 } \
499 } while (0)
500
Avi Kivitydda96d82008-11-26 15:14:10 +0200501#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 do { \
503 unsigned long _tmp; \
504 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200505 __asm__ __volatile__ ( \
506 _PRE_EFLAGS("0", "3", "2") \
507 _op _suffix " %1; " \
508 _POST_EFLAGS("0", "3", "2") \
509 : "=m" (_eflags), "+m" ((_dst).val), \
510 "=&r" (_tmp) \
511 : "i" (EFLAGS_MASK)); \
512 } while (0)
513
514/* Instruction has only one explicit operand (no source operand). */
515#define emulate_1op(_op, _dst, _eflags) \
516 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400517 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200518 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
519 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
520 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
521 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 } \
523 } while (0)
524
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525/* Fetch next part of the instruction being emulated. */
526#define insn_fetch(_type, _size, _eip) \
527({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200528 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200529 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 goto done; \
531 (_eip) += (_size); \
532 (_type)_x; \
533})
534
Gleb Natapov414e6272010-04-28 19:15:26 +0300535#define insn_fetch_arr(_arr, _size, _eip) \
536({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
537 if (rc != X86EMUL_CONTINUE) \
538 goto done; \
539 (_eip) += (_size); \
540})
541
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800542static inline unsigned long ad_mask(struct decode_cache *c)
543{
544 return (1UL << (c->ad_bytes << 3)) - 1;
545}
546
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800548static inline unsigned long
549address_mask(struct decode_cache *c, unsigned long reg)
550{
551 if (c->ad_bytes == sizeof(unsigned long))
552 return reg;
553 else
554 return reg & ad_mask(c);
555}
556
557static inline unsigned long
558register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
559{
560 return base + address_mask(c, reg);
561}
562
Harvey Harrison7a9572752008-02-19 07:40:41 -0800563static inline void
564register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
565{
566 if (c->ad_bytes == sizeof(unsigned long))
567 *reg += inc;
568 else
569 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
570}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571
Harvey Harrison7a9572752008-02-19 07:40:41 -0800572static inline void jmp_rel(struct decode_cache *c, int rel)
573{
574 register_address_increment(c, &c->eip, rel);
575}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300576
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300577static void set_seg_override(struct decode_cache *c, int seg)
578{
579 c->has_seg_override = true;
580 c->seg_override = seg;
581}
582
Gleb Natapov79168fd2010-04-28 19:15:30 +0300583static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
584 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300585{
586 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
587 return 0;
588
Gleb Natapov79168fd2010-04-28 19:15:30 +0300589 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300590}
591
592static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300593 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300594 struct decode_cache *c)
595{
596 if (!c->has_seg_override)
597 return 0;
598
Gleb Natapov79168fd2010-04-28 19:15:30 +0300599 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300600}
601
Gleb Natapov79168fd2010-04-28 19:15:30 +0300602static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
603 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300604{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300605 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606}
607
Gleb Natapov79168fd2010-04-28 19:15:30 +0300608static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
609 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300610{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300611 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300612}
613
Gleb Natapov54b84862010-04-28 19:15:44 +0300614static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
615 u32 error, bool valid)
616{
617 ctxt->exception = vec;
618 ctxt->error_code = error;
619 ctxt->error_code_valid = valid;
620 ctxt->restart = false;
621}
622
623static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
624{
625 emulate_exception(ctxt, GP_VECTOR, err, true);
626}
627
628static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
629 int err)
630{
631 ctxt->cr2 = addr;
632 emulate_exception(ctxt, PF_VECTOR, err, true);
633}
634
635static void emulate_ud(struct x86_emulate_ctxt *ctxt)
636{
637 emulate_exception(ctxt, UD_VECTOR, 0, false);
638}
639
640static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
641{
642 emulate_exception(ctxt, TS_VECTOR, err, true);
643}
644
Avi Kivity62266862007-11-20 13:15:52 +0200645static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
646 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300647 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200648{
649 struct fetch_cache *fc = &ctxt->decode.fetch;
650 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300651 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200652
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300653 if (eip == fc->end) {
654 cur_size = fc->end - fc->start;
655 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
656 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
657 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900658 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200659 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300660 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200661 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300662 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200664}
665
666static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
667 struct x86_emulate_ops *ops,
668 unsigned long eip, void *dest, unsigned size)
669{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900670 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200671
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200673 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200674 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200675 while (size--) {
676 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900677 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200678 return rc;
679 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900680 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200681}
682
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000683/*
684 * Given the 'reg' portion of a ModRM byte, and a register block, return a
685 * pointer into the block that addresses the relevant register.
686 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
687 */
688static void *decode_register(u8 modrm_reg, unsigned long *regs,
689 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690{
691 void *p;
692
693 p = &regs[modrm_reg];
694 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
695 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
696 return p;
697}
698
699static int read_descriptor(struct x86_emulate_ctxt *ctxt,
700 struct x86_emulate_ops *ops,
701 void *ptr,
702 u16 *size, unsigned long *address, int op_bytes)
703{
704 int rc;
705
706 if (op_bytes == 2)
707 op_bytes = 3;
708 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300709 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200710 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900711 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300713 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200714 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 return rc;
716}
717
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300718static int test_cc(unsigned int condition, unsigned int flags)
719{
720 int rc = 0;
721
722 switch ((condition & 15) >> 1) {
723 case 0: /* o */
724 rc |= (flags & EFLG_OF);
725 break;
726 case 1: /* b/c/nae */
727 rc |= (flags & EFLG_CF);
728 break;
729 case 2: /* z/e */
730 rc |= (flags & EFLG_ZF);
731 break;
732 case 3: /* be/na */
733 rc |= (flags & (EFLG_CF|EFLG_ZF));
734 break;
735 case 4: /* s */
736 rc |= (flags & EFLG_SF);
737 break;
738 case 5: /* p/pe */
739 rc |= (flags & EFLG_PF);
740 break;
741 case 7: /* le/ng */
742 rc |= (flags & EFLG_ZF);
743 /* fall through */
744 case 6: /* l/nge */
745 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
746 break;
747 }
748
749 /* Odd condition identifiers (lsb == 1) have inverted sense. */
750 return (!!rc ^ (condition & 1));
751}
752
Avi Kivity3c118e22007-10-31 10:27:04 +0200753static void decode_register_operand(struct operand *op,
754 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200755 int inhibit_bytereg)
756{
Avi Kivity33615aa2007-10-31 11:15:56 +0200757 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200758 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200759
760 if (!(c->d & ModRM))
761 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200762 op->type = OP_REG;
763 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200764 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200765 op->val = *(u8 *)op->ptr;
766 op->bytes = 1;
767 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200768 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200769 op->bytes = c->op_bytes;
770 switch (op->bytes) {
771 case 2:
772 op->val = *(u16 *)op->ptr;
773 break;
774 case 4:
775 op->val = *(u32 *)op->ptr;
776 break;
777 case 8:
778 op->val = *(u64 *) op->ptr;
779 break;
780 }
781 }
782 op->orig_val = op->val;
783}
784
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200785static int decode_modrm(struct x86_emulate_ctxt *ctxt,
786 struct x86_emulate_ops *ops)
787{
788 struct decode_cache *c = &ctxt->decode;
789 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700790 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900791 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200792
793 if (c->rex_prefix) {
794 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
795 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
796 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
797 }
798
799 c->modrm = insn_fetch(u8, 1, c->eip);
800 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
801 c->modrm_reg |= (c->modrm & 0x38) >> 3;
802 c->modrm_rm |= (c->modrm & 0x07);
803 c->modrm_ea = 0;
804 c->use_modrm_ea = 1;
805
806 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300807 c->modrm_ptr = decode_register(c->modrm_rm,
808 c->regs, c->d & ByteOp);
809 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 return rc;
811 }
812
813 if (c->ad_bytes == 2) {
814 unsigned bx = c->regs[VCPU_REGS_RBX];
815 unsigned bp = c->regs[VCPU_REGS_RBP];
816 unsigned si = c->regs[VCPU_REGS_RSI];
817 unsigned di = c->regs[VCPU_REGS_RDI];
818
819 /* 16-bit ModR/M decode. */
820 switch (c->modrm_mod) {
821 case 0:
822 if (c->modrm_rm == 6)
823 c->modrm_ea += insn_fetch(u16, 2, c->eip);
824 break;
825 case 1:
826 c->modrm_ea += insn_fetch(s8, 1, c->eip);
827 break;
828 case 2:
829 c->modrm_ea += insn_fetch(u16, 2, c->eip);
830 break;
831 }
832 switch (c->modrm_rm) {
833 case 0:
834 c->modrm_ea += bx + si;
835 break;
836 case 1:
837 c->modrm_ea += bx + di;
838 break;
839 case 2:
840 c->modrm_ea += bp + si;
841 break;
842 case 3:
843 c->modrm_ea += bp + di;
844 break;
845 case 4:
846 c->modrm_ea += si;
847 break;
848 case 5:
849 c->modrm_ea += di;
850 break;
851 case 6:
852 if (c->modrm_mod != 0)
853 c->modrm_ea += bp;
854 break;
855 case 7:
856 c->modrm_ea += bx;
857 break;
858 }
859 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
860 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300861 if (!c->has_seg_override)
862 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200863 c->modrm_ea = (u16)c->modrm_ea;
864 } else {
865 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700866 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200867 sib = insn_fetch(u8, 1, c->eip);
868 index_reg |= (sib >> 3) & 7;
869 base_reg |= sib & 7;
870 scale = sib >> 6;
871
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700872 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
873 c->modrm_ea += insn_fetch(s32, 4, c->eip);
874 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200875 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700876 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700878 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
879 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700880 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700881 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200882 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200883 switch (c->modrm_mod) {
884 case 0:
885 if (c->modrm_rm == 5)
886 c->modrm_ea += insn_fetch(s32, 4, c->eip);
887 break;
888 case 1:
889 c->modrm_ea += insn_fetch(s8, 1, c->eip);
890 break;
891 case 2:
892 c->modrm_ea += insn_fetch(s32, 4, c->eip);
893 break;
894 }
895 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896done:
897 return rc;
898}
899
900static int decode_abs(struct x86_emulate_ctxt *ctxt,
901 struct x86_emulate_ops *ops)
902{
903 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900904 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200905
906 switch (c->ad_bytes) {
907 case 2:
908 c->modrm_ea = insn_fetch(u16, 2, c->eip);
909 break;
910 case 4:
911 c->modrm_ea = insn_fetch(u32, 4, c->eip);
912 break;
913 case 8:
914 c->modrm_ea = insn_fetch(u64, 8, c->eip);
915 break;
916 }
917done:
918 return rc;
919}
920
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200922x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200924 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900925 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 int mode = ctxt->mode;
Avi Kivity52811d72010-07-26 14:37:48 +0300927 int def_op_bytes, def_ad_bytes, group, dual;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Gleb Natapov5cd21912010-03-18 15:20:26 +0200930 /* we cannot decode insn before we complete previous rep insn */
931 WARN_ON(ctxt->restart);
932
Gleb Natapov063db062010-03-18 15:20:06 +0200933 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300934 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300935 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
937 switch (mode) {
938 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200939 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
943 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200944 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800946#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200948 def_op_bytes = 4;
949 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 break;
951#endif
952 default:
953 return -1;
954 }
955
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200956 c->op_bytes = def_op_bytes;
957 c->ad_bytes = def_ad_bytes;
958
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200960 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200961 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200963 /* switch between 2/4 bytes */
964 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 break;
966 case 0x67: /* address-size override */
967 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200969 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200971 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200972 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300975 case 0x2e: /* CS override */
976 case 0x36: /* SS override */
977 case 0x3e: /* DS override */
978 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 break;
980 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300982 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200984 case 0x40 ... 0x4f: /* REX */
985 if (mode != X86EMUL_MODE_PROT64)
986 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200987 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200988 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200990 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200992 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100993 c->rep_prefix = REPNE_PREFIX;
994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100996 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 default:
999 goto done_prefixes;
1000 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001001
1002 /* Any legacy prefix after a REX prefix nullifies its effect. */
1003
Avi Kivity33615aa2007-10-31 11:15:56 +02001004 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 }
1006
1007done_prefixes:
1008
1009 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001010 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001011 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013
1014 /* Opcode byte(s). */
Avi Kivityd65b1de2010-07-29 15:11:35 +03001015 c->d = opcode_table[c->b].flags;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 if (c->b == 0x0f) {
1019 c->twobyte = 1;
1020 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivityd65b1de2010-07-29 15:11:35 +03001021 c->d = twobyte_table[c->b].flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Avi Kivitye09d0822008-01-18 12:38:59 +02001025 if (c->d & Group) {
1026 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001027 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001028 c->modrm = insn_fetch(u8, 1, c->eip);
1029 --c->eip;
1030
1031 group = (group << 3) + ((c->modrm >> 3) & 7);
Avi Kivity52811d72010-07-26 14:37:48 +03001032 c->d &= ~(Group | GroupDual | GroupMask);
1033 if (dual && (c->modrm >> 6) == 3)
Avi Kivityd65b1de2010-07-29 15:11:35 +03001034 c->d |= group2_table[group].flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001035 else
Avi Kivityd65b1de2010-07-29 15:11:35 +03001036 c->d |= group_table[group].flags;
Avi Kivitye09d0822008-01-18 12:38:59 +02001037 }
1038
1039 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001040 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001041 DPRINTF("Cannot emulate %02x\n", c->b);
1042 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 }
1044
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001045 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1046 c->op_bytes = 8;
1047
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001049 if (c->d & ModRM)
1050 rc = decode_modrm(ctxt, ops);
1051 else if (c->d & MemAbs)
1052 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001053 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001056 if (!c->has_seg_override)
1057 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001058
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001059 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001060 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001061
1062 if (c->ad_bytes != 8)
1063 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001064
1065 if (c->rip_relative)
1066 c->modrm_ea += c->eip;
1067
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 /*
1069 * Decode and fetch the source operand: register, memory
1070 * or immediate.
1071 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001072 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 case SrcNone:
1074 break;
1075 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001076 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 break;
1078 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001079 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 goto srcmem_common;
1081 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001082 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 goto srcmem_common;
1084 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001085 c->src.bytes = (c->d & ByteOp) ? 1 :
1086 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001087 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001088 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001089 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001090 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001091 /*
1092 * For instructions with a ModR/M byte, switch to register
1093 * access if Mod = 3.
1094 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001095 if ((c->d & ModRM) && c->modrm_mod == 3) {
1096 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001097 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001098 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001099 break;
1100 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001101 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001102 c->src.ptr = (unsigned long *)c->modrm_ea;
1103 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 break;
1105 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001106 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001107 c->src.type = OP_IMM;
1108 c->src.ptr = (unsigned long *)c->eip;
1109 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1110 if (c->src.bytes == 8)
1111 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001115 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 break;
1117 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001118 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 break;
1120 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 break;
1123 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001124 if ((c->d & SrcMask) == SrcImmU) {
1125 switch (c->src.bytes) {
1126 case 1:
1127 c->src.val &= 0xff;
1128 break;
1129 case 2:
1130 c->src.val &= 0xffff;
1131 break;
1132 case 4:
1133 c->src.val &= 0xffffffff;
1134 break;
1135 }
1136 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 break;
1138 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001139 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001140 c->src.type = OP_IMM;
1141 c->src.ptr = (unsigned long *)c->eip;
1142 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001143 if ((c->d & SrcMask) == SrcImmByte)
1144 c->src.val = insn_fetch(s8, 1, c->eip);
1145 else
1146 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001148 case SrcAcc:
1149 c->src.type = OP_REG;
1150 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1151 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1152 switch (c->src.bytes) {
1153 case 1:
1154 c->src.val = *(u8 *)c->src.ptr;
1155 break;
1156 case 2:
1157 c->src.val = *(u16 *)c->src.ptr;
1158 break;
1159 case 4:
1160 c->src.val = *(u32 *)c->src.ptr;
1161 break;
1162 case 8:
1163 c->src.val = *(u64 *)c->src.ptr;
1164 break;
1165 }
1166 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001167 case SrcOne:
1168 c->src.bytes = 1;
1169 c->src.val = 1;
1170 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001171 case SrcSI:
1172 c->src.type = OP_MEM;
1173 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1174 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001175 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001176 c->regs[VCPU_REGS_RSI]);
1177 c->src.val = 0;
1178 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001179 case SrcImmFAddr:
1180 c->src.type = OP_IMM;
1181 c->src.ptr = (unsigned long *)c->eip;
1182 c->src.bytes = c->op_bytes + 2;
1183 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1184 break;
1185 case SrcMemFAddr:
1186 c->src.type = OP_MEM;
1187 c->src.ptr = (unsigned long *)c->modrm_ea;
1188 c->src.bytes = c->op_bytes + 2;
1189 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 }
1191
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001192 /*
1193 * Decode and fetch the second source operand: register, memory
1194 * or immediate.
1195 */
1196 switch (c->d & Src2Mask) {
1197 case Src2None:
1198 break;
1199 case Src2CL:
1200 c->src2.bytes = 1;
1201 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1202 break;
1203 case Src2ImmByte:
1204 c->src2.type = OP_IMM;
1205 c->src2.ptr = (unsigned long *)c->eip;
1206 c->src2.bytes = 1;
1207 c->src2.val = insn_fetch(u8, 1, c->eip);
1208 break;
1209 case Src2One:
1210 c->src2.bytes = 1;
1211 c->src2.val = 1;
1212 break;
1213 }
1214
Avi Kivity038e51d2007-01-22 20:40:40 -08001215 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001216 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001217 case ImplicitOps:
1218 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001219 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001220 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001221 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001222 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001223 break;
1224 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001225 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001226 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001227 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001228 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001229 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001230 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001231 break;
1232 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001233 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001234 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001235 if ((c->d & DstMask) == DstMem64)
1236 c->dst.bytes = 8;
1237 else
1238 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001239 c->dst.val = 0;
1240 if (c->d & BitOp) {
1241 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1242
1243 c->dst.ptr = (void *)c->dst.ptr +
1244 (c->src.val & mask) / 8;
1245 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001246 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001247 case DstAcc:
1248 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001249 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001250 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001251 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001252 case 1:
1253 c->dst.val = *(u8 *)c->dst.ptr;
1254 break;
1255 case 2:
1256 c->dst.val = *(u16 *)c->dst.ptr;
1257 break;
1258 case 4:
1259 c->dst.val = *(u32 *)c->dst.ptr;
1260 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001261 case 8:
1262 c->dst.val = *(u64 *)c->dst.ptr;
1263 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001264 }
1265 c->dst.orig_val = c->dst.val;
1266 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001267 case DstDI:
1268 c->dst.type = OP_MEM;
1269 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1270 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001271 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001272 c->regs[VCPU_REGS_RDI]);
1273 c->dst.val = 0;
1274 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001275 }
1276
1277done:
1278 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1279}
1280
Gleb Natapov9de41572010-04-28 19:15:22 +03001281static int read_emulated(struct x86_emulate_ctxt *ctxt,
1282 struct x86_emulate_ops *ops,
1283 unsigned long addr, void *dest, unsigned size)
1284{
1285 int rc;
1286 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001287 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001288
1289 while (size) {
1290 int n = min(size, 8u);
1291 size -= n;
1292 if (mc->pos < mc->end)
1293 goto read_cached;
1294
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001295 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1296 ctxt->vcpu);
1297 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001298 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001299 if (rc != X86EMUL_CONTINUE)
1300 return rc;
1301 mc->end += n;
1302
1303 read_cached:
1304 memcpy(dest, mc->data + mc->pos, n);
1305 mc->pos += n;
1306 dest += n;
1307 addr += n;
1308 }
1309 return X86EMUL_CONTINUE;
1310}
1311
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1313 struct x86_emulate_ops *ops,
1314 unsigned int size, unsigned short port,
1315 void *dest)
1316{
1317 struct read_cache *rc = &ctxt->decode.io_read;
1318
1319 if (rc->pos == rc->end) { /* refill pio read ahead */
1320 struct decode_cache *c = &ctxt->decode;
1321 unsigned int in_page, n;
1322 unsigned int count = c->rep_prefix ?
1323 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1324 in_page = (ctxt->eflags & EFLG_DF) ?
1325 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1326 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1327 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1328 count);
1329 if (n == 0)
1330 n = 1;
1331 rc->pos = rc->end = 0;
1332 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1333 return 0;
1334 rc->end = n * size;
1335 }
1336
1337 memcpy(dest, rc->data + rc->pos, size);
1338 rc->pos += size;
1339 return 1;
1340}
1341
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001342static u32 desc_limit_scaled(struct desc_struct *desc)
1343{
1344 u32 limit = get_desc_limit(desc);
1345
1346 return desc->g ? (limit << 12) | 0xfff : limit;
1347}
1348
1349static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1350 struct x86_emulate_ops *ops,
1351 u16 selector, struct desc_ptr *dt)
1352{
1353 if (selector & 1 << 2) {
1354 struct desc_struct desc;
1355 memset (dt, 0, sizeof *dt);
1356 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1357 return;
1358
1359 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1360 dt->address = get_desc_base(&desc);
1361 } else
1362 ops->get_gdt(dt, ctxt->vcpu);
1363}
1364
1365/* allowed just for 8 bytes segments */
1366static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1367 struct x86_emulate_ops *ops,
1368 u16 selector, struct desc_struct *desc)
1369{
1370 struct desc_ptr dt;
1371 u16 index = selector >> 3;
1372 int ret;
1373 u32 err;
1374 ulong addr;
1375
1376 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1377
1378 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001379 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001380 return X86EMUL_PROPAGATE_FAULT;
1381 }
1382 addr = dt.address + index * 8;
1383 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1384 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001385 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386
1387 return ret;
1388}
1389
1390/* allowed just for 8 bytes segments */
1391static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1392 struct x86_emulate_ops *ops,
1393 u16 selector, struct desc_struct *desc)
1394{
1395 struct desc_ptr dt;
1396 u16 index = selector >> 3;
1397 u32 err;
1398 ulong addr;
1399 int ret;
1400
1401 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1402
1403 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001404 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405 return X86EMUL_PROPAGATE_FAULT;
1406 }
1407
1408 addr = dt.address + index * 8;
1409 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1410 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001411 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001412
1413 return ret;
1414}
1415
1416static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1417 struct x86_emulate_ops *ops,
1418 u16 selector, int seg)
1419{
1420 struct desc_struct seg_desc;
1421 u8 dpl, rpl, cpl;
1422 unsigned err_vec = GP_VECTOR;
1423 u32 err_code = 0;
1424 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1425 int ret;
1426
1427 memset(&seg_desc, 0, sizeof seg_desc);
1428
1429 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1430 || ctxt->mode == X86EMUL_MODE_REAL) {
1431 /* set real mode segment descriptor */
1432 set_desc_base(&seg_desc, selector << 4);
1433 set_desc_limit(&seg_desc, 0xffff);
1434 seg_desc.type = 3;
1435 seg_desc.p = 1;
1436 seg_desc.s = 1;
1437 goto load;
1438 }
1439
1440 /* NULL selector is not valid for TR, CS and SS */
1441 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1442 && null_selector)
1443 goto exception;
1444
1445 /* TR should be in GDT only */
1446 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1447 goto exception;
1448
1449 if (null_selector) /* for NULL selector skip all following checks */
1450 goto load;
1451
1452 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1453 if (ret != X86EMUL_CONTINUE)
1454 return ret;
1455
1456 err_code = selector & 0xfffc;
1457 err_vec = GP_VECTOR;
1458
1459 /* can't load system descriptor into segment selecor */
1460 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1461 goto exception;
1462
1463 if (!seg_desc.p) {
1464 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1465 goto exception;
1466 }
1467
1468 rpl = selector & 3;
1469 dpl = seg_desc.dpl;
1470 cpl = ops->cpl(ctxt->vcpu);
1471
1472 switch (seg) {
1473 case VCPU_SREG_SS:
1474 /*
1475 * segment is not a writable data segment or segment
1476 * selector's RPL != CPL or segment selector's RPL != CPL
1477 */
1478 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1479 goto exception;
1480 break;
1481 case VCPU_SREG_CS:
1482 if (!(seg_desc.type & 8))
1483 goto exception;
1484
1485 if (seg_desc.type & 4) {
1486 /* conforming */
1487 if (dpl > cpl)
1488 goto exception;
1489 } else {
1490 /* nonconforming */
1491 if (rpl > cpl || dpl != cpl)
1492 goto exception;
1493 }
1494 /* CS(RPL) <- CPL */
1495 selector = (selector & 0xfffc) | cpl;
1496 break;
1497 case VCPU_SREG_TR:
1498 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1499 goto exception;
1500 break;
1501 case VCPU_SREG_LDTR:
1502 if (seg_desc.s || seg_desc.type != 2)
1503 goto exception;
1504 break;
1505 default: /* DS, ES, FS, or GS */
1506 /*
1507 * segment is not a data or readable code segment or
1508 * ((segment is a data or nonconforming code segment)
1509 * and (both RPL and CPL > DPL))
1510 */
1511 if ((seg_desc.type & 0xa) == 0x8 ||
1512 (((seg_desc.type & 0xc) != 0xc) &&
1513 (rpl > dpl && cpl > dpl)))
1514 goto exception;
1515 break;
1516 }
1517
1518 if (seg_desc.s) {
1519 /* mark segment as accessed */
1520 seg_desc.type |= 1;
1521 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1522 if (ret != X86EMUL_CONTINUE)
1523 return ret;
1524 }
1525load:
1526 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1527 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1528 return X86EMUL_CONTINUE;
1529exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001530 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531 return X86EMUL_PROPAGATE_FAULT;
1532}
1533
Wei Yongjunc37eda12010-06-15 09:03:33 +08001534static inline int writeback(struct x86_emulate_ctxt *ctxt,
1535 struct x86_emulate_ops *ops)
1536{
1537 int rc;
1538 struct decode_cache *c = &ctxt->decode;
1539 u32 err;
1540
1541 switch (c->dst.type) {
1542 case OP_REG:
1543 /* The 4-byte case *is* correct:
1544 * in 64-bit mode we zero-extend.
1545 */
1546 switch (c->dst.bytes) {
1547 case 1:
1548 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1549 break;
1550 case 2:
1551 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1552 break;
1553 case 4:
1554 *c->dst.ptr = (u32)c->dst.val;
1555 break; /* 64b: zero-ext */
1556 case 8:
1557 *c->dst.ptr = c->dst.val;
1558 break;
1559 }
1560 break;
1561 case OP_MEM:
1562 if (c->lock_prefix)
1563 rc = ops->cmpxchg_emulated(
1564 (unsigned long)c->dst.ptr,
1565 &c->dst.orig_val,
1566 &c->dst.val,
1567 c->dst.bytes,
1568 &err,
1569 ctxt->vcpu);
1570 else
1571 rc = ops->write_emulated(
1572 (unsigned long)c->dst.ptr,
1573 &c->dst.val,
1574 c->dst.bytes,
1575 &err,
1576 ctxt->vcpu);
1577 if (rc == X86EMUL_PROPAGATE_FAULT)
1578 emulate_pf(ctxt,
1579 (unsigned long)c->dst.ptr, err);
1580 if (rc != X86EMUL_CONTINUE)
1581 return rc;
1582 break;
1583 case OP_NONE:
1584 /* no writeback */
1585 break;
1586 default:
1587 break;
1588 }
1589 return X86EMUL_CONTINUE;
1590}
1591
Gleb Natapov79168fd2010-04-28 19:15:30 +03001592static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1593 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001594{
1595 struct decode_cache *c = &ctxt->decode;
1596
1597 c->dst.type = OP_MEM;
1598 c->dst.bytes = c->op_bytes;
1599 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001600 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001601 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001602 c->regs[VCPU_REGS_RSP]);
1603}
1604
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001605static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001606 struct x86_emulate_ops *ops,
1607 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001608{
1609 struct decode_cache *c = &ctxt->decode;
1610 int rc;
1611
Gleb Natapov79168fd2010-04-28 19:15:30 +03001612 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001613 c->regs[VCPU_REGS_RSP]),
1614 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001615 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001616 return rc;
1617
Avi Kivity350f69d2009-01-05 11:12:40 +02001618 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001619 return rc;
1620}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001621
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001622static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1623 struct x86_emulate_ops *ops,
1624 void *dest, int len)
1625{
1626 int rc;
1627 unsigned long val, change_mask;
1628 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001629 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001630
1631 rc = emulate_pop(ctxt, ops, &val, len);
1632 if (rc != X86EMUL_CONTINUE)
1633 return rc;
1634
1635 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1636 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1637
1638 switch(ctxt->mode) {
1639 case X86EMUL_MODE_PROT64:
1640 case X86EMUL_MODE_PROT32:
1641 case X86EMUL_MODE_PROT16:
1642 if (cpl == 0)
1643 change_mask |= EFLG_IOPL;
1644 if (cpl <= iopl)
1645 change_mask |= EFLG_IF;
1646 break;
1647 case X86EMUL_MODE_VM86:
1648 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001649 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001650 return X86EMUL_PROPAGATE_FAULT;
1651 }
1652 change_mask |= EFLG_IF;
1653 break;
1654 default: /* real mode */
1655 change_mask |= (EFLG_IOPL | EFLG_IF);
1656 break;
1657 }
1658
1659 *(unsigned long *)dest =
1660 (ctxt->eflags & ~change_mask) | (val & change_mask);
1661
1662 return rc;
1663}
1664
Gleb Natapov79168fd2010-04-28 19:15:30 +03001665static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1666 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001667{
1668 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001669
Gleb Natapov79168fd2010-04-28 19:15:30 +03001670 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001671
Gleb Natapov79168fd2010-04-28 19:15:30 +03001672 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001673}
1674
1675static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1676 struct x86_emulate_ops *ops, int seg)
1677{
1678 struct decode_cache *c = &ctxt->decode;
1679 unsigned long selector;
1680 int rc;
1681
1682 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001683 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001684 return rc;
1685
Gleb Natapov2e873022010-03-18 15:20:18 +02001686 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001687 return rc;
1688}
1689
Wei Yongjunc37eda12010-06-15 09:03:33 +08001690static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001691 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001692{
1693 struct decode_cache *c = &ctxt->decode;
1694 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001695 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001696 int reg = VCPU_REGS_RAX;
1697
1698 while (reg <= VCPU_REGS_RDI) {
1699 (reg == VCPU_REGS_RSP) ?
1700 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1701
Gleb Natapov79168fd2010-04-28 19:15:30 +03001702 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001703
1704 rc = writeback(ctxt, ops);
1705 if (rc != X86EMUL_CONTINUE)
1706 return rc;
1707
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001708 ++reg;
1709 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001710
1711 /* Disable writeback. */
1712 c->dst.type = OP_NONE;
1713
1714 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001715}
1716
1717static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1718 struct x86_emulate_ops *ops)
1719{
1720 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001721 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001722 int reg = VCPU_REGS_RDI;
1723
1724 while (reg >= VCPU_REGS_RAX) {
1725 if (reg == VCPU_REGS_RSP) {
1726 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1727 c->op_bytes);
1728 --reg;
1729 }
1730
1731 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001732 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001733 break;
1734 --reg;
1735 }
1736 return rc;
1737}
1738
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001739static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1740 struct x86_emulate_ops *ops)
1741{
1742 struct decode_cache *c = &ctxt->decode;
1743 int rc = X86EMUL_CONTINUE;
1744 unsigned long temp_eip = 0;
1745 unsigned long temp_eflags = 0;
1746 unsigned long cs = 0;
1747 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1748 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1749 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1750 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1751
1752 /* TODO: Add stack limit check */
1753
1754 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1755
1756 if (rc != X86EMUL_CONTINUE)
1757 return rc;
1758
1759 if (temp_eip & ~0xffff) {
1760 emulate_gp(ctxt, 0);
1761 return X86EMUL_PROPAGATE_FAULT;
1762 }
1763
1764 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1765
1766 if (rc != X86EMUL_CONTINUE)
1767 return rc;
1768
1769 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1770
1771 if (rc != X86EMUL_CONTINUE)
1772 return rc;
1773
1774 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1775
1776 if (rc != X86EMUL_CONTINUE)
1777 return rc;
1778
1779 c->eip = temp_eip;
1780
1781
1782 if (c->op_bytes == 4)
1783 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1784 else if (c->op_bytes == 2) {
1785 ctxt->eflags &= ~0xffff;
1786 ctxt->eflags |= temp_eflags;
1787 }
1788
1789 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1790 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1791
1792 return rc;
1793}
1794
1795static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1796 struct x86_emulate_ops* ops)
1797{
1798 switch(ctxt->mode) {
1799 case X86EMUL_MODE_REAL:
1800 return emulate_iret_real(ctxt, ops);
1801 case X86EMUL_MODE_VM86:
1802 case X86EMUL_MODE_PROT16:
1803 case X86EMUL_MODE_PROT32:
1804 case X86EMUL_MODE_PROT64:
1805 default:
1806 /* iret from protected mode unimplemented yet */
1807 return X86EMUL_UNHANDLEABLE;
1808 }
1809}
1810
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001811static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1812 struct x86_emulate_ops *ops)
1813{
1814 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001815
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001816 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001817}
1818
Laurent Vivier05f086f2007-09-24 11:10:55 +02001819static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001820{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001821 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001822 switch (c->modrm_reg) {
1823 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001824 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001825 break;
1826 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001827 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001828 break;
1829 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001830 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001831 break;
1832 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001833 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001834 break;
1835 case 4: /* sal/shl */
1836 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001837 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001838 break;
1839 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001840 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001841 break;
1842 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001843 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844 break;
1845 }
1846}
1847
1848static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +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 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001855 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856 break;
1857 case 2: /* not */
1858 c->dst.val = ~c->dst.val;
1859 break;
1860 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001861 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001862 break;
1863 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001864 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001865 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001866 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001867}
1868
1869static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001870 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871{
1872 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873
1874 switch (c->modrm_reg) {
1875 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001876 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877 break;
1878 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001879 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001880 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001881 case 2: /* call near abs */ {
1882 long int old_eip;
1883 old_eip = c->eip;
1884 c->eip = c->src.val;
1885 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001887 break;
1888 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001889 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001890 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891 break;
1892 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001893 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001894 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001895 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001896 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897}
1898
1899static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001900 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901{
1902 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001903 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001904
1905 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1906 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1908 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001909 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001910 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001911 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1912 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001913
Laurent Vivier05f086f2007-09-24 11:10:55 +02001914 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001915 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001916 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001917}
1918
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001919static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1920 struct x86_emulate_ops *ops)
1921{
1922 struct decode_cache *c = &ctxt->decode;
1923 int rc;
1924 unsigned long cs;
1925
1926 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001927 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001928 return rc;
1929 if (c->op_bytes == 4)
1930 c->eip = (u32)c->eip;
1931 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001932 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001933 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001934 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001935 return rc;
1936}
1937
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001938static inline void
1939setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001940 struct x86_emulate_ops *ops, struct desc_struct *cs,
1941 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001942{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001943 memset(cs, 0, sizeof(struct desc_struct));
1944 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1945 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001946
1947 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001948 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001949 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001950 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001951 cs->type = 0x0b; /* Read, Execute, Accessed */
1952 cs->s = 1;
1953 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001954 cs->p = 1;
1955 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001956
Gleb Natapov79168fd2010-04-28 19:15:30 +03001957 set_desc_base(ss, 0); /* flat segment */
1958 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001959 ss->g = 1; /* 4kb granularity */
1960 ss->s = 1;
1961 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001962 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001963 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001964 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001965}
1966
1967static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001968emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001969{
1970 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001971 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001972 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001973 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001974
1975 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001976 if (ctxt->mode == X86EMUL_MODE_REAL ||
1977 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001978 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001979 return X86EMUL_PROPAGATE_FAULT;
1980 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001981
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001983
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001984 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001985 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001986 cs_sel = (u16)(msr_data & 0xfffc);
1987 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001988
1989 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001990 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991 cs.l = 1;
1992 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001993 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1994 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1995 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1996 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001997
1998 c->regs[VCPU_REGS_RCX] = c->eip;
1999 if (is_long_mode(ctxt->vcpu)) {
2000#ifdef CONFIG_X86_64
2001 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
2002
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002003 ops->get_msr(ctxt->vcpu,
2004 ctxt->mode == X86EMUL_MODE_PROT64 ?
2005 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002006 c->eip = msr_data;
2007
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002008 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002009 ctxt->eflags &= ~(msr_data | EFLG_RF);
2010#endif
2011 } else {
2012 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002013 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002014 c->eip = (u32)msr_data;
2015
2016 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2017 }
2018
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002019 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002020}
2021
Andre Przywara8c604352009-06-18 12:56:01 +02002022static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002023emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002024{
2025 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002026 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002027 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002028 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002029
Gleb Natapova0044752010-02-10 14:21:31 +02002030 /* inject #GP if in real mode */
2031 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002032 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002033 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002034 }
2035
2036 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2037 * Therefore, we inject an #UD.
2038 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002039 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002040 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002041 return X86EMUL_PROPAGATE_FAULT;
2042 }
Andre Przywara8c604352009-06-18 12:56:01 +02002043
Gleb Natapov79168fd2010-04-28 19:15:30 +03002044 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002045
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002046 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002047 switch (ctxt->mode) {
2048 case X86EMUL_MODE_PROT32:
2049 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002050 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002051 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002052 }
2053 break;
2054 case X86EMUL_MODE_PROT64:
2055 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002056 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002057 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002058 }
2059 break;
2060 }
2061
2062 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002063 cs_sel = (u16)msr_data;
2064 cs_sel &= ~SELECTOR_RPL_MASK;
2065 ss_sel = cs_sel + 8;
2066 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002067 if (ctxt->mode == X86EMUL_MODE_PROT64
2068 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002070 cs.l = 1;
2071 }
2072
Gleb Natapov79168fd2010-04-28 19:15:30 +03002073 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2074 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2075 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2076 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002077
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002078 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002079 c->eip = msr_data;
2080
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002081 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002082 c->regs[VCPU_REGS_RSP] = msr_data;
2083
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002084 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002085}
2086
Andre Przywara4668f052009-06-18 12:56:02 +02002087static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002088emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002089{
2090 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002091 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002092 u64 msr_data;
2093 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002094 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002095
Gleb Natapova0044752010-02-10 14:21:31 +02002096 /* inject #GP if in real mode or Virtual 8086 mode */
2097 if (ctxt->mode == X86EMUL_MODE_REAL ||
2098 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002099 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002100 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002101 }
2102
Gleb Natapov79168fd2010-04-28 19:15:30 +03002103 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002104
2105 if ((c->rex_prefix & 0x8) != 0x0)
2106 usermode = X86EMUL_MODE_PROT64;
2107 else
2108 usermode = X86EMUL_MODE_PROT32;
2109
2110 cs.dpl = 3;
2111 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002112 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002113 switch (usermode) {
2114 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002115 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002116 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002117 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002118 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002119 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002120 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002121 break;
2122 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002123 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002124 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002125 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002126 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002127 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002128 ss_sel = cs_sel + 8;
2129 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002130 cs.l = 1;
2131 break;
2132 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002133 cs_sel |= SELECTOR_RPL_MASK;
2134 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002135
Gleb Natapov79168fd2010-04-28 19:15:30 +03002136 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2137 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2138 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2139 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002140
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002141 c->eip = c->regs[VCPU_REGS_RDX];
2142 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002143
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002144 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002145}
2146
Gleb Natapov9c537242010-03-18 15:20:05 +02002147static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2148 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002149{
2150 int iopl;
2151 if (ctxt->mode == X86EMUL_MODE_REAL)
2152 return false;
2153 if (ctxt->mode == X86EMUL_MODE_VM86)
2154 return true;
2155 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002156 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002157}
2158
2159static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2160 struct x86_emulate_ops *ops,
2161 u16 port, u16 len)
2162{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002163 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002164 int r;
2165 u16 io_bitmap_ptr;
2166 u8 perm, bit_idx = port & 0x7;
2167 unsigned mask = (1 << len) - 1;
2168
Gleb Natapov79168fd2010-04-28 19:15:30 +03002169 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2170 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002171 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002172 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002173 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002174 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2175 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002176 if (r != X86EMUL_CONTINUE)
2177 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002179 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002180 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2181 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002182 if (r != X86EMUL_CONTINUE)
2183 return false;
2184 if ((perm >> bit_idx) & mask)
2185 return false;
2186 return true;
2187}
2188
2189static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2190 struct x86_emulate_ops *ops,
2191 u16 port, u16 len)
2192{
Gleb Natapov9c537242010-03-18 15:20:05 +02002193 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002194 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2195 return false;
2196 return true;
2197}
2198
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002199static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2200 struct x86_emulate_ops *ops,
2201 struct tss_segment_16 *tss)
2202{
2203 struct decode_cache *c = &ctxt->decode;
2204
2205 tss->ip = c->eip;
2206 tss->flag = ctxt->eflags;
2207 tss->ax = c->regs[VCPU_REGS_RAX];
2208 tss->cx = c->regs[VCPU_REGS_RCX];
2209 tss->dx = c->regs[VCPU_REGS_RDX];
2210 tss->bx = c->regs[VCPU_REGS_RBX];
2211 tss->sp = c->regs[VCPU_REGS_RSP];
2212 tss->bp = c->regs[VCPU_REGS_RBP];
2213 tss->si = c->regs[VCPU_REGS_RSI];
2214 tss->di = c->regs[VCPU_REGS_RDI];
2215
2216 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2217 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2218 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2219 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2220 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2221}
2222
2223static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2224 struct x86_emulate_ops *ops,
2225 struct tss_segment_16 *tss)
2226{
2227 struct decode_cache *c = &ctxt->decode;
2228 int ret;
2229
2230 c->eip = tss->ip;
2231 ctxt->eflags = tss->flag | 2;
2232 c->regs[VCPU_REGS_RAX] = tss->ax;
2233 c->regs[VCPU_REGS_RCX] = tss->cx;
2234 c->regs[VCPU_REGS_RDX] = tss->dx;
2235 c->regs[VCPU_REGS_RBX] = tss->bx;
2236 c->regs[VCPU_REGS_RSP] = tss->sp;
2237 c->regs[VCPU_REGS_RBP] = tss->bp;
2238 c->regs[VCPU_REGS_RSI] = tss->si;
2239 c->regs[VCPU_REGS_RDI] = tss->di;
2240
2241 /*
2242 * SDM says that segment selectors are loaded before segment
2243 * descriptors
2244 */
2245 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2246 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2247 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2248 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2249 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2250
2251 /*
2252 * Now load segment descriptors. If fault happenes at this stage
2253 * it is handled in a context of new task
2254 */
2255 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2256 if (ret != X86EMUL_CONTINUE)
2257 return ret;
2258 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2259 if (ret != X86EMUL_CONTINUE)
2260 return ret;
2261 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2262 if (ret != X86EMUL_CONTINUE)
2263 return ret;
2264 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2265 if (ret != X86EMUL_CONTINUE)
2266 return ret;
2267 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2268 if (ret != X86EMUL_CONTINUE)
2269 return ret;
2270
2271 return X86EMUL_CONTINUE;
2272}
2273
2274static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2275 struct x86_emulate_ops *ops,
2276 u16 tss_selector, u16 old_tss_sel,
2277 ulong old_tss_base, struct desc_struct *new_desc)
2278{
2279 struct tss_segment_16 tss_seg;
2280 int ret;
2281 u32 err, new_tss_base = get_desc_base(new_desc);
2282
2283 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2284 &err);
2285 if (ret == X86EMUL_PROPAGATE_FAULT) {
2286 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002287 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288 return ret;
2289 }
2290
2291 save_state_to_tss16(ctxt, ops, &tss_seg);
2292
2293 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2294 &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, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 return ret;
2299 }
2300
2301 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2302 &err);
2303 if (ret == X86EMUL_PROPAGATE_FAULT) {
2304 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002305 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002306 return ret;
2307 }
2308
2309 if (old_tss_sel != 0xffff) {
2310 tss_seg.prev_task_link = old_tss_sel;
2311
2312 ret = ops->write_std(new_tss_base,
2313 &tss_seg.prev_task_link,
2314 sizeof tss_seg.prev_task_link,
2315 ctxt->vcpu, &err);
2316 if (ret == X86EMUL_PROPAGATE_FAULT) {
2317 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002318 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 return ret;
2320 }
2321 }
2322
2323 return load_state_from_tss16(ctxt, ops, &tss_seg);
2324}
2325
2326static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2327 struct x86_emulate_ops *ops,
2328 struct tss_segment_32 *tss)
2329{
2330 struct decode_cache *c = &ctxt->decode;
2331
2332 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2333 tss->eip = c->eip;
2334 tss->eflags = ctxt->eflags;
2335 tss->eax = c->regs[VCPU_REGS_RAX];
2336 tss->ecx = c->regs[VCPU_REGS_RCX];
2337 tss->edx = c->regs[VCPU_REGS_RDX];
2338 tss->ebx = c->regs[VCPU_REGS_RBX];
2339 tss->esp = c->regs[VCPU_REGS_RSP];
2340 tss->ebp = c->regs[VCPU_REGS_RBP];
2341 tss->esi = c->regs[VCPU_REGS_RSI];
2342 tss->edi = c->regs[VCPU_REGS_RDI];
2343
2344 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2345 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2346 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2347 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2348 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2349 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2350 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2351}
2352
2353static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2354 struct x86_emulate_ops *ops,
2355 struct tss_segment_32 *tss)
2356{
2357 struct decode_cache *c = &ctxt->decode;
2358 int ret;
2359
Gleb Natapov0f122442010-04-28 19:15:31 +03002360 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002361 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002362 return X86EMUL_PROPAGATE_FAULT;
2363 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002364 c->eip = tss->eip;
2365 ctxt->eflags = tss->eflags | 2;
2366 c->regs[VCPU_REGS_RAX] = tss->eax;
2367 c->regs[VCPU_REGS_RCX] = tss->ecx;
2368 c->regs[VCPU_REGS_RDX] = tss->edx;
2369 c->regs[VCPU_REGS_RBX] = tss->ebx;
2370 c->regs[VCPU_REGS_RSP] = tss->esp;
2371 c->regs[VCPU_REGS_RBP] = tss->ebp;
2372 c->regs[VCPU_REGS_RSI] = tss->esi;
2373 c->regs[VCPU_REGS_RDI] = tss->edi;
2374
2375 /*
2376 * SDM says that segment selectors are loaded before segment
2377 * descriptors
2378 */
2379 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2380 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2381 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2382 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2383 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2384 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2385 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2386
2387 /*
2388 * Now load segment descriptors. If fault happenes at this stage
2389 * it is handled in a context of new task
2390 */
2391 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2392 if (ret != X86EMUL_CONTINUE)
2393 return ret;
2394 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2395 if (ret != X86EMUL_CONTINUE)
2396 return ret;
2397 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2398 if (ret != X86EMUL_CONTINUE)
2399 return ret;
2400 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2401 if (ret != X86EMUL_CONTINUE)
2402 return ret;
2403 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2404 if (ret != X86EMUL_CONTINUE)
2405 return ret;
2406 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2407 if (ret != X86EMUL_CONTINUE)
2408 return ret;
2409 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2410 if (ret != X86EMUL_CONTINUE)
2411 return ret;
2412
2413 return X86EMUL_CONTINUE;
2414}
2415
2416static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2417 struct x86_emulate_ops *ops,
2418 u16 tss_selector, u16 old_tss_sel,
2419 ulong old_tss_base, struct desc_struct *new_desc)
2420{
2421 struct tss_segment_32 tss_seg;
2422 int ret;
2423 u32 err, new_tss_base = get_desc_base(new_desc);
2424
2425 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2426 &err);
2427 if (ret == X86EMUL_PROPAGATE_FAULT) {
2428 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002429 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 return ret;
2431 }
2432
2433 save_state_to_tss32(ctxt, ops, &tss_seg);
2434
2435 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2436 &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, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 return ret;
2441 }
2442
2443 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2444 &err);
2445 if (ret == X86EMUL_PROPAGATE_FAULT) {
2446 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002447 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448 return ret;
2449 }
2450
2451 if (old_tss_sel != 0xffff) {
2452 tss_seg.prev_task_link = old_tss_sel;
2453
2454 ret = ops->write_std(new_tss_base,
2455 &tss_seg.prev_task_link,
2456 sizeof tss_seg.prev_task_link,
2457 ctxt->vcpu, &err);
2458 if (ret == X86EMUL_PROPAGATE_FAULT) {
2459 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002460 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 return ret;
2462 }
2463 }
2464
2465 return load_state_from_tss32(ctxt, ops, &tss_seg);
2466}
2467
2468static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002469 struct x86_emulate_ops *ops,
2470 u16 tss_selector, int reason,
2471 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472{
2473 struct desc_struct curr_tss_desc, next_tss_desc;
2474 int ret;
2475 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2476 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002477 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002478 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479
2480 /* FIXME: old_tss_base == ~0 ? */
2481
2482 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2483 if (ret != X86EMUL_CONTINUE)
2484 return ret;
2485 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2486 if (ret != X86EMUL_CONTINUE)
2487 return ret;
2488
2489 /* FIXME: check that next_tss_desc is tss */
2490
2491 if (reason != TASK_SWITCH_IRET) {
2492 if ((tss_selector & 3) > next_tss_desc.dpl ||
2493 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002494 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 return X86EMUL_PROPAGATE_FAULT;
2496 }
2497 }
2498
Gleb Natapovceffb452010-03-18 15:20:19 +02002499 desc_limit = desc_limit_scaled(&next_tss_desc);
2500 if (!next_tss_desc.p ||
2501 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2502 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002503 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 return X86EMUL_PROPAGATE_FAULT;
2505 }
2506
2507 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2508 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2509 write_segment_descriptor(ctxt, ops, old_tss_sel,
2510 &curr_tss_desc);
2511 }
2512
2513 if (reason == TASK_SWITCH_IRET)
2514 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2515
2516 /* set back link to prev task only if NT bit is set in eflags
2517 note that old_tss_sel is not used afetr this point */
2518 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2519 old_tss_sel = 0xffff;
2520
2521 if (next_tss_desc.type & 8)
2522 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2523 old_tss_base, &next_tss_desc);
2524 else
2525 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2526 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002527 if (ret != X86EMUL_CONTINUE)
2528 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529
2530 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2531 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2532
2533 if (reason != TASK_SWITCH_IRET) {
2534 next_tss_desc.type |= (1 << 1); /* set busy flag */
2535 write_segment_descriptor(ctxt, ops, tss_selector,
2536 &next_tss_desc);
2537 }
2538
2539 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2540 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2541 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2542
Jan Kiszkae269fb22010-04-14 15:51:09 +02002543 if (has_error_code) {
2544 struct decode_cache *c = &ctxt->decode;
2545
2546 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2547 c->lock_prefix = 0;
2548 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002549 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002550 }
2551
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 return ret;
2553}
2554
2555int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2556 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002557 u16 tss_selector, int reason,
2558 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559{
2560 struct decode_cache *c = &ctxt->decode;
2561 int rc;
2562
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002564 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565
Jan Kiszkae269fb22010-04-14 15:51:09 +02002566 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2567 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568
2569 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002570 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002571 if (rc == X86EMUL_CONTINUE)
2572 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 }
2574
Gleb Natapov19d04432010-04-15 12:29:50 +03002575 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576}
2577
Gleb Natapova682e352010-03-18 15:20:21 +02002578static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002579 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002580{
2581 struct decode_cache *c = &ctxt->decode;
2582 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2583
Gleb Natapovd9271122010-03-18 15:20:22 +02002584 register_address_increment(c, &c->regs[reg], df * op->bytes);
2585 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002586}
2587
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002588int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002589x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002590{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002591 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002592 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002593 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002594 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002595
Gleb Natapov9de41572010-04-28 19:15:22 +03002596 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002597
Gleb Natapov11616242010-02-11 14:43:14 +02002598 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002599 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002600 goto done;
2601 }
2602
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002603 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002604 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002605 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002606 goto done;
2607 }
2608
Gleb Natapove92805a2010-02-10 14:21:35 +02002609 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002610 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002611 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002612 goto done;
2613 }
2614
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002615 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002616 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002617 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002618 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002619 string_done:
2620 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002621 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002622 goto done;
2623 }
2624 /* The second termination condition only applies for REPE
2625 * and REPNE. Test if the repeat string operation prefix is
2626 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2627 * corresponding termination condition according to:
2628 * - if REPE/REPZ and ZF = 0 then done
2629 * - if REPNE/REPNZ and ZF = 1 then done
2630 */
2631 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002632 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002633 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002634 ((ctxt->eflags & EFLG_ZF) == 0))
2635 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002636 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002637 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2638 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002639 }
Gleb Natapov063db062010-03-18 15:20:06 +02002640 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002641 }
2642
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002643 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002644 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002645 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002646 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002647 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002648 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002649 }
2650
Gleb Natapove35b7b92010-02-25 16:36:42 +02002651 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002652 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2653 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002654 if (rc != X86EMUL_CONTINUE)
2655 goto done;
2656 }
2657
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002658 if ((c->d & DstMask) == ImplicitOps)
2659 goto special_insn;
2660
2661
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002662 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2663 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002664 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2665 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002666 if (rc != X86EMUL_CONTINUE)
2667 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002668 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002669 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002670
Avi Kivity018a98d2007-11-27 19:30:56 +02002671special_insn:
2672
Laurent Viviere4e03de2007-09-18 11:52:50 +02002673 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 goto twobyte_insn;
2675
Laurent Viviere4e03de2007-09-18 11:52:50 +02002676 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 case 0x00 ... 0x05:
2678 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002679 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002681 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002682 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002683 break;
2684 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002685 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002686 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002687 goto done;
2688 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 case 0x08 ... 0x0d:
2690 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002691 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002693 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002694 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002695 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 case 0x10 ... 0x15:
2697 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002698 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002700 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002701 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002702 break;
2703 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002704 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002705 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002706 goto done;
2707 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 case 0x18 ... 0x1d:
2709 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002710 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002712 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002713 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002714 break;
2715 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002716 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002717 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002718 goto done;
2719 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002720 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002722 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 break;
2724 case 0x28 ... 0x2d:
2725 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002726 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 break;
2728 case 0x30 ... 0x35:
2729 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002730 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 break;
2732 case 0x38 ... 0x3d:
2733 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002734 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002736 case 0x40 ... 0x47: /* inc r16/r32 */
2737 emulate_1op("inc", c->dst, ctxt->eflags);
2738 break;
2739 case 0x48 ... 0x4f: /* dec r16/r32 */
2740 emulate_1op("dec", c->dst, ctxt->eflags);
2741 break;
2742 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002743 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002744 break;
2745 case 0x58 ... 0x5f: /* pop reg */
2746 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002747 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002748 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002749 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002750 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002751 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002752 rc = emulate_pusha(ctxt, ops);
2753 if (rc != X86EMUL_CONTINUE)
2754 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002755 break;
2756 case 0x61: /* popa */
2757 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002758 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002759 goto done;
2760 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002762 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002764 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002766 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002767 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002768 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002769 break;
2770 case 0x6c: /* insb */
2771 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002772 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002773 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002774 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002775 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002776 goto done;
2777 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002778 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2779 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002780 goto done; /* IO is needed, skip writeback */
2781 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002782 case 0x6e: /* outsb */
2783 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002784 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002785 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002786 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002787 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002788 goto done;
2789 }
Gleb Natapov79729952010-03-18 15:20:24 +02002790 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2791 &c->src.val, 1, ctxt->vcpu);
2792
2793 c->dst.type = OP_NONE; /* nothing to writeback */
2794 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002795 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002796 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002797 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002798 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002800 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 case 0:
2802 goto add;
2803 case 1:
2804 goto or;
2805 case 2:
2806 goto adc;
2807 case 3:
2808 goto sbb;
2809 case 4:
2810 goto and;
2811 case 5:
2812 goto sub;
2813 case 6:
2814 goto xor;
2815 case 7:
2816 goto cmp;
2817 }
2818 break;
2819 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002820 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002821 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 break;
2823 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002824 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002826 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002828 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 break;
2830 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002831 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 break;
2833 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002834 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 break; /* 64b reg: zero-extend */
2836 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002837 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 break;
2839 }
2840 /*
2841 * Write back the memory destination with implicit LOCK
2842 * prefix.
2843 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002844 c->dst.val = c->src.val;
2845 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002848 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002849 case 0x8c: /* mov r/m, sreg */
2850 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002851 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002852 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002853 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002854 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002855 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002856 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002857 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002858 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002859 case 0x8e: { /* mov seg, r/m16 */
2860 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002861
2862 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002863
Gleb Natapovc6975182010-02-18 12:15:01 +02002864 if (c->modrm_reg == VCPU_SREG_CS ||
2865 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002866 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002867 goto done;
2868 }
2869
Glauber Costa310b5d32009-05-12 16:21:06 -04002870 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002871 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002872
Gleb Natapov2e873022010-03-18 15:20:18 +02002873 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002874
2875 c->dst.type = OP_NONE; /* Disable writeback. */
2876 break;
2877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002879 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002880 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002883 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002884 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2885 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002886 break;
2887 }
2888 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002889 c->src.type = OP_REG;
2890 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002891 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2892 c->src.val = *(c->src.ptr);
2893 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002894 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002895 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002896 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002897 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002898 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002899 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002900 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002901 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002902 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2903 if (rc != X86EMUL_CONTINUE)
2904 goto done;
2905 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002906 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002908 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002910 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002911 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002912 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002913 case 0xa8 ... 0xa9: /* test ax, imm */
2914 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002916 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 break;
2918 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002919 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 case 0xae ... 0xaf: /* scas */
2921 DPRINTF("Urk! I don't handle SCAS.\n");
2922 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002923 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002924 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002925 case 0xc0 ... 0xc1:
2926 emulate_grp2(ctxt);
2927 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002928 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002929 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002930 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002931 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002932 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002933 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2934 mov:
2935 c->dst.val = c->src.val;
2936 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002937 case 0xcb: /* ret far */
2938 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002939 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002940 goto done;
2941 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002942 case 0xcf: /* iret */
2943 rc = emulate_iret(ctxt, ops);
2944
2945 if (rc != X86EMUL_CONTINUE)
2946 goto done;
2947 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002948 case 0xd0 ... 0xd1: /* Grp2 */
2949 c->src.val = 1;
2950 emulate_grp2(ctxt);
2951 break;
2952 case 0xd2 ... 0xd3: /* Grp2 */
2953 c->src.val = c->regs[VCPU_REGS_RCX];
2954 emulate_grp2(ctxt);
2955 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002956 case 0xe4: /* inb */
2957 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002958 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002959 case 0xe6: /* outb */
2960 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002961 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002962 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002963 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002964 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002965 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002966 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002967 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002968 }
2969 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002970 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002971 case 0xea: { /* jmp far */
2972 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002973 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002974 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2975
2976 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002977 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002978
Gleb Natapov414e6272010-04-28 19:15:26 +03002979 c->eip = 0;
2980 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002981 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002982 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002983 case 0xeb:
2984 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002985 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002986 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002987 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002988 case 0xec: /* in al,dx */
2989 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002990 c->src.val = c->regs[VCPU_REGS_RDX];
2991 do_io_in:
2992 c->dst.bytes = min(c->dst.bytes, 4u);
2993 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002994 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002995 goto done;
2996 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002997 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2998 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002999 goto done; /* IO is needed */
3000 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003001 case 0xee: /* out dx,al */
3002 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003003 c->src.val = c->regs[VCPU_REGS_RDX];
3004 do_io_out:
3005 c->dst.bytes = min(c->dst.bytes, 4u);
3006 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003007 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003008 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003009 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003010 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3011 ctxt->vcpu);
3012 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003013 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003014 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003015 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003016 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003017 case 0xf5: /* cmc */
3018 /* complement carry flag from eflags reg */
3019 ctxt->eflags ^= EFLG_CF;
3020 c->dst.type = OP_NONE; /* Disable writeback. */
3021 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003022 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003023 if (!emulate_grp3(ctxt, ops))
3024 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003025 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003026 case 0xf8: /* clc */
3027 ctxt->eflags &= ~EFLG_CF;
3028 c->dst.type = OP_NONE; /* Disable writeback. */
3029 break;
3030 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003031 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003032 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003033 goto done;
3034 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003035 ctxt->eflags &= ~X86_EFLAGS_IF;
3036 c->dst.type = OP_NONE; /* Disable writeback. */
3037 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003038 break;
3039 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003040 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003041 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003042 goto done;
3043 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003044 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003045 ctxt->eflags |= X86_EFLAGS_IF;
3046 c->dst.type = OP_NONE; /* Disable writeback. */
3047 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003048 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003049 case 0xfc: /* cld */
3050 ctxt->eflags &= ~EFLG_DF;
3051 c->dst.type = OP_NONE; /* Disable writeback. */
3052 break;
3053 case 0xfd: /* std */
3054 ctxt->eflags |= EFLG_DF;
3055 c->dst.type = OP_NONE; /* Disable writeback. */
3056 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003057 case 0xfe: /* Grp4 */
3058 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003059 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003060 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003061 goto done;
3062 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003063 case 0xff: /* Grp5 */
3064 if (c->modrm_reg == 5)
3065 goto jump_far;
3066 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003067 default:
3068 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003070
3071writeback:
3072 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003073 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003074 goto done;
3075
Gleb Natapov5cd21912010-03-18 15:20:26 +02003076 /*
3077 * restore dst type in case the decoding will be reused
3078 * (happens for string instruction )
3079 */
3080 c->dst.type = saved_dst_type;
3081
Gleb Natapova682e352010-03-18 15:20:21 +02003082 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003083 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3084 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003085
3086 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003087 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3088 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003089
Gleb Natapov5cd21912010-03-18 15:20:26 +02003090 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003091 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003092 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003093 /*
3094 * Re-enter guest when pio read ahead buffer is empty or,
3095 * if it is not used, after each 1024 iteration.
3096 */
3097 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3098 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003099 ctxt->restart = false;
3100 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003101 /*
3102 * reset read cache here in case string instruction is restared
3103 * without decoding
3104 */
3105 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003106 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003107
3108done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003109 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110
3111twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003112 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003114 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 u16 size;
3116 unsigned long address;
3117
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003118 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003119 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003120 goto cannot_emulate;
3121
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003122 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003123 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003124 goto done;
3125
Avi Kivity33e38852008-05-21 15:34:25 +03003126 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003127 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003128 /* Disable writeback. */
3129 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003132 rc = read_descriptor(ctxt, ops, c->src.ptr,
3133 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003134 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135 goto done;
3136 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003137 /* Disable writeback. */
3138 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003140 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003141 if (c->modrm_mod == 3) {
3142 switch (c->modrm_rm) {
3143 case 1:
3144 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003145 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003146 goto done;
3147 break;
3148 default:
3149 goto cannot_emulate;
3150 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003151 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003152 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003153 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003154 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003155 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003156 goto done;
3157 realmode_lidt(ctxt->vcpu, size, address);
3158 }
Avi Kivity16286d02008-04-14 14:40:50 +03003159 /* Disable writeback. */
3160 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 break;
3162 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003163 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003164 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 break;
3166 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003167 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3168 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003169 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003171 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003172 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003173 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003175 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003176 /* Disable writeback. */
3177 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 break;
3179 default:
3180 goto cannot_emulate;
3181 }
3182 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003183 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003184 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003185 if (rc != X86EMUL_CONTINUE)
3186 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003187 else
3188 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003189 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003190 case 0x06:
3191 emulate_clts(ctxt->vcpu);
3192 c->dst.type = OP_NONE;
3193 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003194 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003195 kvm_emulate_wbinvd(ctxt->vcpu);
3196 c->dst.type = OP_NONE;
3197 break;
3198 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003199 case 0x0d: /* GrpP (prefetch) */
3200 case 0x18: /* Grp16 (prefetch/nop) */
3201 c->dst.type = OP_NONE;
3202 break;
3203 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003204 switch (c->modrm_reg) {
3205 case 1:
3206 case 5 ... 7:
3207 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003208 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003209 goto done;
3210 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003211 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003212 c->dst.type = OP_NONE; /* no writeback */
3213 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003215 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3216 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003217 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003218 goto done;
3219 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003220 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003221 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003223 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003224 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003225 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003226 goto done;
3227 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003228 c->dst.type = OP_NONE;
3229 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003231 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3232 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003233 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003234 goto done;
3235 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003236
Gleb Natapov338dbc92010-04-28 19:15:32 +03003237 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3238 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3239 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3240 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003241 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003242 goto done;
3243 }
3244
Laurent Viviera01af5e2007-09-24 11:10:56 +02003245 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003247 case 0x30:
3248 /* wrmsr */
3249 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3250 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003251 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003252 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003253 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003254 }
3255 rc = X86EMUL_CONTINUE;
3256 c->dst.type = OP_NONE;
3257 break;
3258 case 0x32:
3259 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003260 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003261 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003262 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003263 } else {
3264 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3265 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3266 }
3267 rc = X86EMUL_CONTINUE;
3268 c->dst.type = OP_NONE;
3269 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003270 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003271 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003272 if (rc != X86EMUL_CONTINUE)
3273 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003274 else
3275 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003276 break;
3277 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003278 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003279 if (rc != X86EMUL_CONTINUE)
3280 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003281 else
3282 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003283 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003285 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003286 if (!test_cc(c->b, ctxt->eflags))
3287 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003289 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003290 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003291 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003292 c->dst.type = OP_NONE;
3293 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003294 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003295 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003296 break;
3297 case 0xa1: /* pop fs */
3298 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003299 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003300 goto done;
3301 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003302 case 0xa3:
3303 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003304 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003305 /* only subword offset */
3306 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003307 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003308 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003309 case 0xa4: /* shld imm8, r, r/m */
3310 case 0xa5: /* shld cl, r, r/m */
3311 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3312 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003313 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003314 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003315 break;
3316 case 0xa9: /* pop gs */
3317 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003318 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003319 goto done;
3320 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003321 case 0xab:
3322 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003323 /* only subword offset */
3324 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003325 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003326 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003327 case 0xac: /* shrd imm8, r, r/m */
3328 case 0xad: /* shrd cl, r, r/m */
3329 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3330 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003331 case 0xae: /* clflush */
3332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 case 0xb0 ... 0xb1: /* cmpxchg */
3334 /*
3335 * Save real source value, then compare EAX against
3336 * destination.
3337 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003338 c->src.orig_val = c->src.val;
3339 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003340 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3341 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003343 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 } else {
3345 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003346 c->dst.type = OP_REG;
3347 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 }
3349 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 case 0xb3:
3351 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003352 /* only subword offset */
3353 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003354 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003357 c->dst.bytes = c->op_bytes;
3358 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3359 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003362 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 case 0:
3364 goto bt;
3365 case 1:
3366 goto bts;
3367 case 2:
3368 goto btr;
3369 case 3:
3370 goto btc;
3371 }
3372 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003373 case 0xbb:
3374 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003375 /* only subword offset */
3376 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003377 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003378 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003380 c->dst.bytes = c->op_bytes;
3381 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3382 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003384 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003385 c->dst.bytes = c->op_bytes;
3386 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3387 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003388 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003390 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003391 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003392 goto done;
3393 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003394 default:
3395 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396 }
3397 goto writeback;
3398
3399cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003400 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401 return -1;
3402}