blob: 1ce9c6de0aeaa8b1b26e4e75e75c7fba12e76160 [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 Kivity83babbc2010-07-26 14:37:39 +030098#define X2(x) (x), (x)
99#define X3(x) X2(x), (x)
100#define X4(x) X2(x), X2(x)
101#define X5(x) X4(x), (x)
102#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 Kivity4968ec42010-07-26 14:37:49 +0300108 Group1, Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200109 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200110};
111
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100112static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200114 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300116 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300117 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200119 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
122 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200124 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300126 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200129 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300131 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300132 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200134 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Wei Yongjune97e8832010-07-06 16:51:09 +0800136 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200138 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamalabc19082010-05-12 01:39:21 +0300140 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200142 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal222b7c52010-05-12 01:39:22 +0300144 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145 /* 0x38 - 0x3F */
146 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
147 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200148 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
149 0, 0,
Avi Kivity749358a2010-07-26 14:37:40 +0300150 /* 0x40 - 0x4F */
151 X16(DstReg),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300152 /* 0x50 - 0x57 */
Avi Kivity38491862010-07-26 14:37:41 +0300153 X8(SrcReg | Stack),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300154 /* 0x58 - 0x5F */
Avi Kivity38491862010-07-26 14:37:41 +0300155 X8(DstReg | Stack),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700156 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200157 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
158 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700159 0, 0, 0, 0,
160 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300161 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200162 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
163 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300164 /* 0x70 - 0x7F */
165 X16(SrcImmByte),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0x80 - 0x87 */
Avi Kivity4968ec42010-07-26 14:37:49 +0300167 ByteOp | DstMem | SrcImm | ModRM | Group | Group1,
168 DstMem | SrcImm | ModRM | Group | Group1,
169 ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1,
170 DstMem | SrcImmByte | ModRM | Group | Group1,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200172 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0x88 - 0x8F */
174 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
175 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Wei Yongjunb16b2b72010-07-06 16:52:53 +0800176 DstMem | SrcNone | ModRM | Mov, ModRM | DstReg,
Wei Yongjuna5046e62010-07-06 16:49:05 +0800177 ImplicitOps | SrcMem16 | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300178 /* 0x90 - 0x97 */
179 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
180 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300181 0, 0, SrcImmFAddr | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300182 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0xA0 - 0xA7 */
Wei Yongjun5d55f292010-07-07 17:43:35 +0800184 ByteOp | DstAcc | SrcMem | Mov | MemAbs, DstAcc | SrcMem | Mov | MemAbs,
185 ByteOp | DstMem | SrcAcc | Mov | MemAbs, DstMem | SrcAcc | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200186 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
187 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188 /* 0xA8 - 0xAF */
Mohammed Gamaldfb507c2010-05-11 22:22:40 +0300189 DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
Gleb Natapova682e352010-03-18 15:20:21 +0200190 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
191 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300192 /* 0xB0 - 0xB7 */
Avi Kivityb6e61532010-07-26 14:37:43 +0300193 X8(ByteOp | DstReg | SrcImm | Mov),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300194 /* 0xB8 - 0xBF */
Avi Kivityb6e61532010-07-26 14:37:43 +0300195 X8(DstReg | SrcImm | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300197 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200198 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300199 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300201 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300202 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xD0 - 0xD7 */
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
206 0, 0, 0, 0,
207 /* 0xD8 - 0xDF */
208 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300209 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300210 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200211 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
212 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300213 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300214 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov414e6272010-04-28 19:15:26 +0300215 SrcImmFAddr | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200216 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
217 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0xF0 - 0xF7 */
219 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200220 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700222 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300223 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224};
225
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100226static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200228 0, Group | GroupDual | Group7, 0, 0,
229 0, ImplicitOps, ImplicitOps | Priv, 0,
230 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
231 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0x10 - 0x1F */
233 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
234 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 ModRM | ImplicitOps | Priv, ModRM | Priv,
237 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200240 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
241 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200242 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300243 /* 0x40 - 0x4F */
244 X16(DstReg | SrcMem | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 /* 0x50 - 0x5F */
246 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
247 /* 0x60 - 0x6F */
248 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 /* 0x70 - 0x7F */
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0x80 - 0x8F */
Avi Kivity880a1882010-07-26 14:37:45 +0300252 X16(SrcImm),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253 /* 0x90 - 0x9F */
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300256 ImplicitOps | Stack, ImplicitOps | Stack,
257 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100258 DstMem | SrcReg | Src2ImmByte | ModRM,
259 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300261 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200262 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100263 DstMem | SrcReg | Src2ImmByte | ModRM,
264 DstMem | SrcReg | Src2CL | ModRM,
265 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200267 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
268 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
270 DstReg | SrcMem16 | ModRM | Mov,
271 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200272 0, 0,
273 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
275 DstReg | SrcMem16 | ModRM | Mov,
276 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200277 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
278 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800279 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 /* 0xD0 - 0xDF */
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282 /* 0xE0 - 0xEF */
283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
284 /* 0xF0 - 0xFF */
285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
286};
287
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100288static u32 group_table[] = {
Avi Kivity4968ec42010-07-26 14:37:49 +0300289 [Group1*8] =
290 X7(Lock), 0,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200291 [Group1A*8] =
292 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200293 [Group3_Byte*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800294 ByteOp | SrcImm | DstMem | ModRM, ByteOp | SrcImm | DstMem | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200295 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
296 0, 0, 0, 0,
297 [Group3*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800298 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300299 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200300 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200301 [Group4*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300302 ByteOp | DstMem | SrcNone | ModRM | Lock, ByteOp | DstMem | SrcNone | ModRM | Lock,
Avi Kivityfd607542008-01-18 13:12:26 +0200303 0, 0, 0, 0, 0, 0,
304 [Group5*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300305 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Mohammed Gamald19292e2008-09-08 21:47:19 +0300306 SrcMem | ModRM | Stack, 0,
Gleb Natapov414e6272010-04-28 19:15:26 +0300307 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200308 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200309 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200310 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300311 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200312 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200313 [Group8*8] =
314 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200315 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
316 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200317 [Group9*8] =
Gleb Natapov6550e1f2010-03-21 13:08:21 +0200318 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200319};
320
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100321static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200322 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200323 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300324 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200325 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200326 [Group9*8] =
327 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200328};
329
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200331#define EFLG_ID (1<<21)
332#define EFLG_VIP (1<<20)
333#define EFLG_VIF (1<<19)
334#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200335#define EFLG_VM (1<<17)
336#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200337#define EFLG_IOPL (3<<12)
338#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339#define EFLG_OF (1<<11)
340#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200341#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200342#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343#define EFLG_SF (1<<7)
344#define EFLG_ZF (1<<6)
345#define EFLG_AF (1<<4)
346#define EFLG_PF (1<<2)
347#define EFLG_CF (1<<0)
348
349/*
350 * Instruction emulation:
351 * Most instructions are emulated directly via a fragment of inline assembly
352 * code. This allows us to save/restore EFLAGS and thus very easily pick up
353 * any modified flags.
354 */
355
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800356#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357#define _LO32 "k" /* force 32-bit operand */
358#define _STK "%%rsp" /* stack pointer */
359#elif defined(__i386__)
360#define _LO32 "" /* force 32-bit operand */
361#define _STK "%%esp" /* stack pointer */
362#endif
363
364/*
365 * These EFLAGS bits are restored from saved value during emulation, and
366 * any changes are written back to the saved value after emulation.
367 */
368#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
369
370/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200371#define _PRE_EFLAGS(_sav, _msk, _tmp) \
372 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
373 "movl %"_sav",%"_LO32 _tmp"; " \
374 "push %"_tmp"; " \
375 "push %"_tmp"; " \
376 "movl %"_msk",%"_LO32 _tmp"; " \
377 "andl %"_LO32 _tmp",("_STK"); " \
378 "pushf; " \
379 "notl %"_LO32 _tmp"; " \
380 "andl %"_LO32 _tmp",("_STK"); " \
381 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
382 "pop %"_tmp"; " \
383 "orl %"_LO32 _tmp",("_STK"); " \
384 "popf; " \
385 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
387/* After executing instruction: write-back necessary bits in EFLAGS. */
388#define _POST_EFLAGS(_sav, _msk, _tmp) \
389 /* _sav |= EFLAGS & _msk; */ \
390 "pushf; " \
391 "pop %"_tmp"; " \
392 "andl %"_msk",%"_LO32 _tmp"; " \
393 "orl %"_LO32 _tmp",%"_sav"; "
394
Avi Kivitydda96d82008-11-26 15:14:10 +0200395#ifdef CONFIG_X86_64
396#define ON64(x) x
397#else
398#define ON64(x)
399#endif
400
Avi Kivity6b7ad612008-11-26 15:30:45 +0200401#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
402 do { \
403 __asm__ __volatile__ ( \
404 _PRE_EFLAGS("0", "4", "2") \
405 _op _suffix " %"_x"3,%1; " \
406 _POST_EFLAGS("0", "4", "2") \
407 : "=m" (_eflags), "=m" ((_dst).val), \
408 "=&r" (_tmp) \
409 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200410 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200411
412
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413/* Raw emulation: instruction has two explicit operands. */
414#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200415 do { \
416 unsigned long _tmp; \
417 \
418 switch ((_dst).bytes) { \
419 case 2: \
420 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
421 break; \
422 case 4: \
423 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
424 break; \
425 case 8: \
426 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
427 break; \
428 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 } while (0)
430
431#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
432 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200433 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400434 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200436 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 break; \
438 default: \
439 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
440 _wx, _wy, _lx, _ly, _qx, _qy); \
441 break; \
442 } \
443 } while (0)
444
445/* Source operand is byte-sized and may be restricted to just %cl. */
446#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
447 __emulate_2op(_op, _src, _dst, _eflags, \
448 "b", "c", "b", "c", "b", "c", "b", "c")
449
450/* Source operand is byte, word, long or quad sized. */
451#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
452 __emulate_2op(_op, _src, _dst, _eflags, \
453 "b", "q", "w", "r", _LO32, "r", "", "r")
454
455/* Source operand is word, long or quad sized. */
456#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
457 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
458 "w", "r", _LO32, "r", "", "r")
459
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100460/* Instruction has three operands and one operand is stored in ECX register */
461#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
462 do { \
463 unsigned long _tmp; \
464 _type _clv = (_cl).val; \
465 _type _srcv = (_src).val; \
466 _type _dstv = (_dst).val; \
467 \
468 __asm__ __volatile__ ( \
469 _PRE_EFLAGS("0", "5", "2") \
470 _op _suffix " %4,%1 \n" \
471 _POST_EFLAGS("0", "5", "2") \
472 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
473 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
474 ); \
475 \
476 (_cl).val = (unsigned long) _clv; \
477 (_src).val = (unsigned long) _srcv; \
478 (_dst).val = (unsigned long) _dstv; \
479 } while (0)
480
481#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
482 do { \
483 switch ((_dst).bytes) { \
484 case 2: \
485 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
486 "w", unsigned short); \
487 break; \
488 case 4: \
489 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
490 "l", unsigned int); \
491 break; \
492 case 8: \
493 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
494 "q", unsigned long)); \
495 break; \
496 } \
497 } while (0)
498
Avi Kivitydda96d82008-11-26 15:14:10 +0200499#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 do { \
501 unsigned long _tmp; \
502 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200503 __asm__ __volatile__ ( \
504 _PRE_EFLAGS("0", "3", "2") \
505 _op _suffix " %1; " \
506 _POST_EFLAGS("0", "3", "2") \
507 : "=m" (_eflags), "+m" ((_dst).val), \
508 "=&r" (_tmp) \
509 : "i" (EFLAGS_MASK)); \
510 } while (0)
511
512/* Instruction has only one explicit operand (no source operand). */
513#define emulate_1op(_op, _dst, _eflags) \
514 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400515 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200516 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
517 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
518 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
519 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 } \
521 } while (0)
522
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523/* Fetch next part of the instruction being emulated. */
524#define insn_fetch(_type, _size, _eip) \
525({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200526 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200527 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 goto done; \
529 (_eip) += (_size); \
530 (_type)_x; \
531})
532
Gleb Natapov414e6272010-04-28 19:15:26 +0300533#define insn_fetch_arr(_arr, _size, _eip) \
534({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
535 if (rc != X86EMUL_CONTINUE) \
536 goto done; \
537 (_eip) += (_size); \
538})
539
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800540static inline unsigned long ad_mask(struct decode_cache *c)
541{
542 return (1UL << (c->ad_bytes << 3)) - 1;
543}
544
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800546static inline unsigned long
547address_mask(struct decode_cache *c, unsigned long reg)
548{
549 if (c->ad_bytes == sizeof(unsigned long))
550 return reg;
551 else
552 return reg & ad_mask(c);
553}
554
555static inline unsigned long
556register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
557{
558 return base + address_mask(c, reg);
559}
560
Harvey Harrison7a9572752008-02-19 07:40:41 -0800561static inline void
562register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
563{
564 if (c->ad_bytes == sizeof(unsigned long))
565 *reg += inc;
566 else
567 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
568}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569
Harvey Harrison7a9572752008-02-19 07:40:41 -0800570static inline void jmp_rel(struct decode_cache *c, int rel)
571{
572 register_address_increment(c, &c->eip, rel);
573}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300574
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300575static void set_seg_override(struct decode_cache *c, int seg)
576{
577 c->has_seg_override = true;
578 c->seg_override = seg;
579}
580
Gleb Natapov79168fd2010-04-28 19:15:30 +0300581static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
582 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300583{
584 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
585 return 0;
586
Gleb Natapov79168fd2010-04-28 19:15:30 +0300587 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300588}
589
590static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300591 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300592 struct decode_cache *c)
593{
594 if (!c->has_seg_override)
595 return 0;
596
Gleb Natapov79168fd2010-04-28 19:15:30 +0300597 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300598}
599
Gleb Natapov79168fd2010-04-28 19:15:30 +0300600static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
601 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300602{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300603 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300604}
605
Gleb Natapov79168fd2010-04-28 19:15:30 +0300606static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
607 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300608{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300609 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300610}
611
Gleb Natapov54b84862010-04-28 19:15:44 +0300612static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
613 u32 error, bool valid)
614{
615 ctxt->exception = vec;
616 ctxt->error_code = error;
617 ctxt->error_code_valid = valid;
618 ctxt->restart = false;
619}
620
621static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
622{
623 emulate_exception(ctxt, GP_VECTOR, err, true);
624}
625
626static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
627 int err)
628{
629 ctxt->cr2 = addr;
630 emulate_exception(ctxt, PF_VECTOR, err, true);
631}
632
633static void emulate_ud(struct x86_emulate_ctxt *ctxt)
634{
635 emulate_exception(ctxt, UD_VECTOR, 0, false);
636}
637
638static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
639{
640 emulate_exception(ctxt, TS_VECTOR, err, true);
641}
642
Avi Kivity62266862007-11-20 13:15:52 +0200643static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
644 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300645 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200646{
647 struct fetch_cache *fc = &ctxt->decode.fetch;
648 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300649 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200650
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300651 if (eip == fc->end) {
652 cur_size = fc->end - fc->start;
653 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
654 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
655 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900656 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200657 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300658 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200659 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300660 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900661 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200662}
663
664static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
665 struct x86_emulate_ops *ops,
666 unsigned long eip, void *dest, unsigned size)
667{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900668 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200669
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200670 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200671 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200673 while (size--) {
674 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900675 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200676 return rc;
677 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200679}
680
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000681/*
682 * Given the 'reg' portion of a ModRM byte, and a register block, return a
683 * pointer into the block that addresses the relevant register.
684 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
685 */
686static void *decode_register(u8 modrm_reg, unsigned long *regs,
687 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
689 void *p;
690
691 p = &regs[modrm_reg];
692 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
693 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
694 return p;
695}
696
697static int read_descriptor(struct x86_emulate_ctxt *ctxt,
698 struct x86_emulate_ops *ops,
699 void *ptr,
700 u16 *size, unsigned long *address, int op_bytes)
701{
702 int rc;
703
704 if (op_bytes == 2)
705 op_bytes = 3;
706 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300707 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200708 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900709 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300711 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200712 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 return rc;
714}
715
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300716static int test_cc(unsigned int condition, unsigned int flags)
717{
718 int rc = 0;
719
720 switch ((condition & 15) >> 1) {
721 case 0: /* o */
722 rc |= (flags & EFLG_OF);
723 break;
724 case 1: /* b/c/nae */
725 rc |= (flags & EFLG_CF);
726 break;
727 case 2: /* z/e */
728 rc |= (flags & EFLG_ZF);
729 break;
730 case 3: /* be/na */
731 rc |= (flags & (EFLG_CF|EFLG_ZF));
732 break;
733 case 4: /* s */
734 rc |= (flags & EFLG_SF);
735 break;
736 case 5: /* p/pe */
737 rc |= (flags & EFLG_PF);
738 break;
739 case 7: /* le/ng */
740 rc |= (flags & EFLG_ZF);
741 /* fall through */
742 case 6: /* l/nge */
743 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
744 break;
745 }
746
747 /* Odd condition identifiers (lsb == 1) have inverted sense. */
748 return (!!rc ^ (condition & 1));
749}
750
Avi Kivity3c118e22007-10-31 10:27:04 +0200751static void decode_register_operand(struct operand *op,
752 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200753 int inhibit_bytereg)
754{
Avi Kivity33615aa2007-10-31 11:15:56 +0200755 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200756 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200757
758 if (!(c->d & ModRM))
759 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200760 op->type = OP_REG;
761 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200762 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->val = *(u8 *)op->ptr;
764 op->bytes = 1;
765 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200766 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200767 op->bytes = c->op_bytes;
768 switch (op->bytes) {
769 case 2:
770 op->val = *(u16 *)op->ptr;
771 break;
772 case 4:
773 op->val = *(u32 *)op->ptr;
774 break;
775 case 8:
776 op->val = *(u64 *) op->ptr;
777 break;
778 }
779 }
780 op->orig_val = op->val;
781}
782
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783static int decode_modrm(struct x86_emulate_ctxt *ctxt,
784 struct x86_emulate_ops *ops)
785{
786 struct decode_cache *c = &ctxt->decode;
787 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700788 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900789 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790
791 if (c->rex_prefix) {
792 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
793 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
794 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
795 }
796
797 c->modrm = insn_fetch(u8, 1, c->eip);
798 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
799 c->modrm_reg |= (c->modrm & 0x38) >> 3;
800 c->modrm_rm |= (c->modrm & 0x07);
801 c->modrm_ea = 0;
802 c->use_modrm_ea = 1;
803
804 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300805 c->modrm_ptr = decode_register(c->modrm_rm,
806 c->regs, c->d & ByteOp);
807 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808 return rc;
809 }
810
811 if (c->ad_bytes == 2) {
812 unsigned bx = c->regs[VCPU_REGS_RBX];
813 unsigned bp = c->regs[VCPU_REGS_RBP];
814 unsigned si = c->regs[VCPU_REGS_RSI];
815 unsigned di = c->regs[VCPU_REGS_RDI];
816
817 /* 16-bit ModR/M decode. */
818 switch (c->modrm_mod) {
819 case 0:
820 if (c->modrm_rm == 6)
821 c->modrm_ea += insn_fetch(u16, 2, c->eip);
822 break;
823 case 1:
824 c->modrm_ea += insn_fetch(s8, 1, c->eip);
825 break;
826 case 2:
827 c->modrm_ea += insn_fetch(u16, 2, c->eip);
828 break;
829 }
830 switch (c->modrm_rm) {
831 case 0:
832 c->modrm_ea += bx + si;
833 break;
834 case 1:
835 c->modrm_ea += bx + di;
836 break;
837 case 2:
838 c->modrm_ea += bp + si;
839 break;
840 case 3:
841 c->modrm_ea += bp + di;
842 break;
843 case 4:
844 c->modrm_ea += si;
845 break;
846 case 5:
847 c->modrm_ea += di;
848 break;
849 case 6:
850 if (c->modrm_mod != 0)
851 c->modrm_ea += bp;
852 break;
853 case 7:
854 c->modrm_ea += bx;
855 break;
856 }
857 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
858 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300859 if (!c->has_seg_override)
860 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200861 c->modrm_ea = (u16)c->modrm_ea;
862 } else {
863 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700864 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200865 sib = insn_fetch(u8, 1, c->eip);
866 index_reg |= (sib >> 3) & 7;
867 base_reg |= sib & 7;
868 scale = sib >> 6;
869
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700870 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
871 c->modrm_ea += insn_fetch(s32, 4, c->eip);
872 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700874 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200875 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700876 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
877 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700878 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700879 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200880 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 switch (c->modrm_mod) {
882 case 0:
883 if (c->modrm_rm == 5)
884 c->modrm_ea += insn_fetch(s32, 4, c->eip);
885 break;
886 case 1:
887 c->modrm_ea += insn_fetch(s8, 1, c->eip);
888 break;
889 case 2:
890 c->modrm_ea += insn_fetch(s32, 4, c->eip);
891 break;
892 }
893 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894done:
895 return rc;
896}
897
898static int decode_abs(struct x86_emulate_ctxt *ctxt,
899 struct x86_emulate_ops *ops)
900{
901 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900902 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200903
904 switch (c->ad_bytes) {
905 case 2:
906 c->modrm_ea = insn_fetch(u16, 2, c->eip);
907 break;
908 case 4:
909 c->modrm_ea = insn_fetch(u32, 4, c->eip);
910 break;
911 case 8:
912 c->modrm_ea = insn_fetch(u64, 8, c->eip);
913 break;
914 }
915done:
916 return rc;
917}
918
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200920x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200922 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 int mode = ctxt->mode;
Avi Kivity52811d72010-07-26 14:37:48 +0300925 int def_op_bytes, def_ad_bytes, group, dual;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
Gleb Natapov5cd21912010-03-18 15:20:26 +0200928 /* we cannot decode insn before we complete previous rep insn */
929 WARN_ON(ctxt->restart);
930
Gleb Natapov063db062010-03-18 15:20:06 +0200931 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300932 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300933 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 switch (mode) {
936 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200937 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200939 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 break;
941 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200942 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800944#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200946 def_op_bytes = 4;
947 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 break;
949#endif
950 default:
951 return -1;
952 }
953
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200954 c->op_bytes = def_op_bytes;
955 c->ad_bytes = def_ad_bytes;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200958 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200959 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200961 /* switch between 2/4 bytes */
962 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 break;
964 case 0x67: /* address-size override */
965 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200966 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200969 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200970 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300973 case 0x2e: /* CS override */
974 case 0x36: /* SS override */
975 case 0x3e: /* DS override */
976 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 break;
978 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300980 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200982 case 0x40 ... 0x4f: /* REX */
983 if (mode != X86EMUL_MODE_PROT64)
984 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200985 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200986 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200988 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200990 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100991 c->rep_prefix = REPNE_PREFIX;
992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100994 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 default:
997 goto done_prefixes;
998 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200999
1000 /* Any legacy prefix after a REX prefix nullifies its effect. */
1001
Avi Kivity33615aa2007-10-31 11:15:56 +02001002 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 }
1004
1005done_prefixes:
1006
1007 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001009 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->d = opcode_table[c->b];
1014 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 if (c->b == 0x0f) {
1017 c->twobyte = 1;
1018 c->b = insn_fetch(u8, 1, c->eip);
1019 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 if (c->d & Group) {
1024 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001025 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001026 c->modrm = insn_fetch(u8, 1, c->eip);
1027 --c->eip;
1028
1029 group = (group << 3) + ((c->modrm >> 3) & 7);
Avi Kivity52811d72010-07-26 14:37:48 +03001030 c->d &= ~(Group | GroupDual | GroupMask);
1031 if (dual && (c->modrm >> 6) == 3)
1032 c->d |= group2_table[group];
Avi Kivitye09d0822008-01-18 12:38:59 +02001033 else
Avi Kivity52811d72010-07-26 14:37:48 +03001034 c->d |= group_table[group];
Avi Kivitye09d0822008-01-18 12:38:59 +02001035 }
1036
1037 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001038 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001039 DPRINTF("Cannot emulate %02x\n", c->b);
1040 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 }
1042
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001043 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1044 c->op_bytes = 8;
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001047 if (c->d & ModRM)
1048 rc = decode_modrm(ctxt, ops);
1049 else if (c->d & MemAbs)
1050 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001051 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001052 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001054 if (!c->has_seg_override)
1055 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001056
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001057 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001058 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001059
1060 if (c->ad_bytes != 8)
1061 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001062
1063 if (c->rip_relative)
1064 c->modrm_ea += c->eip;
1065
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 /*
1067 * Decode and fetch the source operand: register, memory
1068 * or immediate.
1069 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 case SrcNone:
1072 break;
1073 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001074 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 break;
1076 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 goto srcmem_common;
1079 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001080 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 goto srcmem_common;
1082 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001083 c->src.bytes = (c->d & ByteOp) ? 1 :
1084 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001085 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001086 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001087 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001088 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001089 /*
1090 * For instructions with a ModR/M byte, switch to register
1091 * access if Mod = 3.
1092 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 if ((c->d & ModRM) && c->modrm_mod == 3) {
1094 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001095 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001096 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001097 break;
1098 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001099 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001100 c->src.ptr = (unsigned long *)c->modrm_ea;
1101 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 break;
1103 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001104 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001105 c->src.type = OP_IMM;
1106 c->src.ptr = (unsigned long *)c->eip;
1107 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1108 if (c->src.bytes == 8)
1109 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001113 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 break;
1115 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 break;
1118 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 break;
1121 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001122 if ((c->d & SrcMask) == SrcImmU) {
1123 switch (c->src.bytes) {
1124 case 1:
1125 c->src.val &= 0xff;
1126 break;
1127 case 2:
1128 c->src.val &= 0xffff;
1129 break;
1130 case 4:
1131 c->src.val &= 0xffffffff;
1132 break;
1133 }
1134 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 break;
1136 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001137 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001138 c->src.type = OP_IMM;
1139 c->src.ptr = (unsigned long *)c->eip;
1140 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001141 if ((c->d & SrcMask) == SrcImmByte)
1142 c->src.val = insn_fetch(s8, 1, c->eip);
1143 else
1144 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001146 case SrcAcc:
1147 c->src.type = OP_REG;
1148 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1149 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1150 switch (c->src.bytes) {
1151 case 1:
1152 c->src.val = *(u8 *)c->src.ptr;
1153 break;
1154 case 2:
1155 c->src.val = *(u16 *)c->src.ptr;
1156 break;
1157 case 4:
1158 c->src.val = *(u32 *)c->src.ptr;
1159 break;
1160 case 8:
1161 c->src.val = *(u64 *)c->src.ptr;
1162 break;
1163 }
1164 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001165 case SrcOne:
1166 c->src.bytes = 1;
1167 c->src.val = 1;
1168 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001169 case SrcSI:
1170 c->src.type = OP_MEM;
1171 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1172 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001173 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001174 c->regs[VCPU_REGS_RSI]);
1175 c->src.val = 0;
1176 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001177 case SrcImmFAddr:
1178 c->src.type = OP_IMM;
1179 c->src.ptr = (unsigned long *)c->eip;
1180 c->src.bytes = c->op_bytes + 2;
1181 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1182 break;
1183 case SrcMemFAddr:
1184 c->src.type = OP_MEM;
1185 c->src.ptr = (unsigned long *)c->modrm_ea;
1186 c->src.bytes = c->op_bytes + 2;
1187 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 }
1189
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001190 /*
1191 * Decode and fetch the second source operand: register, memory
1192 * or immediate.
1193 */
1194 switch (c->d & Src2Mask) {
1195 case Src2None:
1196 break;
1197 case Src2CL:
1198 c->src2.bytes = 1;
1199 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1200 break;
1201 case Src2ImmByte:
1202 c->src2.type = OP_IMM;
1203 c->src2.ptr = (unsigned long *)c->eip;
1204 c->src2.bytes = 1;
1205 c->src2.val = insn_fetch(u8, 1, c->eip);
1206 break;
1207 case Src2One:
1208 c->src2.bytes = 1;
1209 c->src2.val = 1;
1210 break;
1211 }
1212
Avi Kivity038e51d2007-01-22 20:40:40 -08001213 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001214 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001215 case ImplicitOps:
1216 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001217 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001218 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001219 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001220 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001221 break;
1222 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001223 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001224 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001225 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001226 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001227 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001228 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001229 break;
1230 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001231 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001232 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001233 if ((c->d & DstMask) == DstMem64)
1234 c->dst.bytes = 8;
1235 else
1236 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001237 c->dst.val = 0;
1238 if (c->d & BitOp) {
1239 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1240
1241 c->dst.ptr = (void *)c->dst.ptr +
1242 (c->src.val & mask) / 8;
1243 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001244 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001245 case DstAcc:
1246 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001247 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001248 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001249 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001250 case 1:
1251 c->dst.val = *(u8 *)c->dst.ptr;
1252 break;
1253 case 2:
1254 c->dst.val = *(u16 *)c->dst.ptr;
1255 break;
1256 case 4:
1257 c->dst.val = *(u32 *)c->dst.ptr;
1258 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001259 case 8:
1260 c->dst.val = *(u64 *)c->dst.ptr;
1261 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001262 }
1263 c->dst.orig_val = c->dst.val;
1264 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001265 case DstDI:
1266 c->dst.type = OP_MEM;
1267 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1268 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001269 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001270 c->regs[VCPU_REGS_RDI]);
1271 c->dst.val = 0;
1272 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001273 }
1274
1275done:
1276 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1277}
1278
Gleb Natapov9de41572010-04-28 19:15:22 +03001279static int read_emulated(struct x86_emulate_ctxt *ctxt,
1280 struct x86_emulate_ops *ops,
1281 unsigned long addr, void *dest, unsigned size)
1282{
1283 int rc;
1284 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001285 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001286
1287 while (size) {
1288 int n = min(size, 8u);
1289 size -= n;
1290 if (mc->pos < mc->end)
1291 goto read_cached;
1292
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001293 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1294 ctxt->vcpu);
1295 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001296 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001297 if (rc != X86EMUL_CONTINUE)
1298 return rc;
1299 mc->end += n;
1300
1301 read_cached:
1302 memcpy(dest, mc->data + mc->pos, n);
1303 mc->pos += n;
1304 dest += n;
1305 addr += n;
1306 }
1307 return X86EMUL_CONTINUE;
1308}
1309
Gleb Natapov7b262e92010-03-18 15:20:27 +02001310static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1311 struct x86_emulate_ops *ops,
1312 unsigned int size, unsigned short port,
1313 void *dest)
1314{
1315 struct read_cache *rc = &ctxt->decode.io_read;
1316
1317 if (rc->pos == rc->end) { /* refill pio read ahead */
1318 struct decode_cache *c = &ctxt->decode;
1319 unsigned int in_page, n;
1320 unsigned int count = c->rep_prefix ?
1321 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1322 in_page = (ctxt->eflags & EFLG_DF) ?
1323 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1324 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1325 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1326 count);
1327 if (n == 0)
1328 n = 1;
1329 rc->pos = rc->end = 0;
1330 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1331 return 0;
1332 rc->end = n * size;
1333 }
1334
1335 memcpy(dest, rc->data + rc->pos, size);
1336 rc->pos += size;
1337 return 1;
1338}
1339
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001340static u32 desc_limit_scaled(struct desc_struct *desc)
1341{
1342 u32 limit = get_desc_limit(desc);
1343
1344 return desc->g ? (limit << 12) | 0xfff : limit;
1345}
1346
1347static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1348 struct x86_emulate_ops *ops,
1349 u16 selector, struct desc_ptr *dt)
1350{
1351 if (selector & 1 << 2) {
1352 struct desc_struct desc;
1353 memset (dt, 0, sizeof *dt);
1354 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1355 return;
1356
1357 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1358 dt->address = get_desc_base(&desc);
1359 } else
1360 ops->get_gdt(dt, ctxt->vcpu);
1361}
1362
1363/* allowed just for 8 bytes segments */
1364static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1365 struct x86_emulate_ops *ops,
1366 u16 selector, struct desc_struct *desc)
1367{
1368 struct desc_ptr dt;
1369 u16 index = selector >> 3;
1370 int ret;
1371 u32 err;
1372 ulong addr;
1373
1374 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1375
1376 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001377 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378 return X86EMUL_PROPAGATE_FAULT;
1379 }
1380 addr = dt.address + index * 8;
1381 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1382 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001383 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001384
1385 return ret;
1386}
1387
1388/* allowed just for 8 bytes segments */
1389static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1390 struct x86_emulate_ops *ops,
1391 u16 selector, struct desc_struct *desc)
1392{
1393 struct desc_ptr dt;
1394 u16 index = selector >> 3;
1395 u32 err;
1396 ulong addr;
1397 int ret;
1398
1399 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1400
1401 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001402 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001403 return X86EMUL_PROPAGATE_FAULT;
1404 }
1405
1406 addr = dt.address + index * 8;
1407 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1408 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001409 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001410
1411 return ret;
1412}
1413
1414static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1415 struct x86_emulate_ops *ops,
1416 u16 selector, int seg)
1417{
1418 struct desc_struct seg_desc;
1419 u8 dpl, rpl, cpl;
1420 unsigned err_vec = GP_VECTOR;
1421 u32 err_code = 0;
1422 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1423 int ret;
1424
1425 memset(&seg_desc, 0, sizeof seg_desc);
1426
1427 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1428 || ctxt->mode == X86EMUL_MODE_REAL) {
1429 /* set real mode segment descriptor */
1430 set_desc_base(&seg_desc, selector << 4);
1431 set_desc_limit(&seg_desc, 0xffff);
1432 seg_desc.type = 3;
1433 seg_desc.p = 1;
1434 seg_desc.s = 1;
1435 goto load;
1436 }
1437
1438 /* NULL selector is not valid for TR, CS and SS */
1439 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1440 && null_selector)
1441 goto exception;
1442
1443 /* TR should be in GDT only */
1444 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1445 goto exception;
1446
1447 if (null_selector) /* for NULL selector skip all following checks */
1448 goto load;
1449
1450 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1451 if (ret != X86EMUL_CONTINUE)
1452 return ret;
1453
1454 err_code = selector & 0xfffc;
1455 err_vec = GP_VECTOR;
1456
1457 /* can't load system descriptor into segment selecor */
1458 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1459 goto exception;
1460
1461 if (!seg_desc.p) {
1462 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1463 goto exception;
1464 }
1465
1466 rpl = selector & 3;
1467 dpl = seg_desc.dpl;
1468 cpl = ops->cpl(ctxt->vcpu);
1469
1470 switch (seg) {
1471 case VCPU_SREG_SS:
1472 /*
1473 * segment is not a writable data segment or segment
1474 * selector's RPL != CPL or segment selector's RPL != CPL
1475 */
1476 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1477 goto exception;
1478 break;
1479 case VCPU_SREG_CS:
1480 if (!(seg_desc.type & 8))
1481 goto exception;
1482
1483 if (seg_desc.type & 4) {
1484 /* conforming */
1485 if (dpl > cpl)
1486 goto exception;
1487 } else {
1488 /* nonconforming */
1489 if (rpl > cpl || dpl != cpl)
1490 goto exception;
1491 }
1492 /* CS(RPL) <- CPL */
1493 selector = (selector & 0xfffc) | cpl;
1494 break;
1495 case VCPU_SREG_TR:
1496 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1497 goto exception;
1498 break;
1499 case VCPU_SREG_LDTR:
1500 if (seg_desc.s || seg_desc.type != 2)
1501 goto exception;
1502 break;
1503 default: /* DS, ES, FS, or GS */
1504 /*
1505 * segment is not a data or readable code segment or
1506 * ((segment is a data or nonconforming code segment)
1507 * and (both RPL and CPL > DPL))
1508 */
1509 if ((seg_desc.type & 0xa) == 0x8 ||
1510 (((seg_desc.type & 0xc) != 0xc) &&
1511 (rpl > dpl && cpl > dpl)))
1512 goto exception;
1513 break;
1514 }
1515
1516 if (seg_desc.s) {
1517 /* mark segment as accessed */
1518 seg_desc.type |= 1;
1519 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1520 if (ret != X86EMUL_CONTINUE)
1521 return ret;
1522 }
1523load:
1524 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1525 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1526 return X86EMUL_CONTINUE;
1527exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001528 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529 return X86EMUL_PROPAGATE_FAULT;
1530}
1531
Wei Yongjunc37eda12010-06-15 09:03:33 +08001532static inline int writeback(struct x86_emulate_ctxt *ctxt,
1533 struct x86_emulate_ops *ops)
1534{
1535 int rc;
1536 struct decode_cache *c = &ctxt->decode;
1537 u32 err;
1538
1539 switch (c->dst.type) {
1540 case OP_REG:
1541 /* The 4-byte case *is* correct:
1542 * in 64-bit mode we zero-extend.
1543 */
1544 switch (c->dst.bytes) {
1545 case 1:
1546 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1547 break;
1548 case 2:
1549 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1550 break;
1551 case 4:
1552 *c->dst.ptr = (u32)c->dst.val;
1553 break; /* 64b: zero-ext */
1554 case 8:
1555 *c->dst.ptr = c->dst.val;
1556 break;
1557 }
1558 break;
1559 case OP_MEM:
1560 if (c->lock_prefix)
1561 rc = ops->cmpxchg_emulated(
1562 (unsigned long)c->dst.ptr,
1563 &c->dst.orig_val,
1564 &c->dst.val,
1565 c->dst.bytes,
1566 &err,
1567 ctxt->vcpu);
1568 else
1569 rc = ops->write_emulated(
1570 (unsigned long)c->dst.ptr,
1571 &c->dst.val,
1572 c->dst.bytes,
1573 &err,
1574 ctxt->vcpu);
1575 if (rc == X86EMUL_PROPAGATE_FAULT)
1576 emulate_pf(ctxt,
1577 (unsigned long)c->dst.ptr, err);
1578 if (rc != X86EMUL_CONTINUE)
1579 return rc;
1580 break;
1581 case OP_NONE:
1582 /* no writeback */
1583 break;
1584 default:
1585 break;
1586 }
1587 return X86EMUL_CONTINUE;
1588}
1589
Gleb Natapov79168fd2010-04-28 19:15:30 +03001590static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1591 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001592{
1593 struct decode_cache *c = &ctxt->decode;
1594
1595 c->dst.type = OP_MEM;
1596 c->dst.bytes = c->op_bytes;
1597 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001598 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001599 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001600 c->regs[VCPU_REGS_RSP]);
1601}
1602
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001603static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001604 struct x86_emulate_ops *ops,
1605 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001606{
1607 struct decode_cache *c = &ctxt->decode;
1608 int rc;
1609
Gleb Natapov79168fd2010-04-28 19:15:30 +03001610 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001611 c->regs[VCPU_REGS_RSP]),
1612 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001613 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614 return rc;
1615
Avi Kivity350f69d2009-01-05 11:12:40 +02001616 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001617 return rc;
1618}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001619
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001620static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1621 struct x86_emulate_ops *ops,
1622 void *dest, int len)
1623{
1624 int rc;
1625 unsigned long val, change_mask;
1626 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001627 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001628
1629 rc = emulate_pop(ctxt, ops, &val, len);
1630 if (rc != X86EMUL_CONTINUE)
1631 return rc;
1632
1633 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1634 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1635
1636 switch(ctxt->mode) {
1637 case X86EMUL_MODE_PROT64:
1638 case X86EMUL_MODE_PROT32:
1639 case X86EMUL_MODE_PROT16:
1640 if (cpl == 0)
1641 change_mask |= EFLG_IOPL;
1642 if (cpl <= iopl)
1643 change_mask |= EFLG_IF;
1644 break;
1645 case X86EMUL_MODE_VM86:
1646 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001647 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001648 return X86EMUL_PROPAGATE_FAULT;
1649 }
1650 change_mask |= EFLG_IF;
1651 break;
1652 default: /* real mode */
1653 change_mask |= (EFLG_IOPL | EFLG_IF);
1654 break;
1655 }
1656
1657 *(unsigned long *)dest =
1658 (ctxt->eflags & ~change_mask) | (val & change_mask);
1659
1660 return rc;
1661}
1662
Gleb Natapov79168fd2010-04-28 19:15:30 +03001663static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1664 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001665{
1666 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001667
Gleb Natapov79168fd2010-04-28 19:15:30 +03001668 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001669
Gleb Natapov79168fd2010-04-28 19:15:30 +03001670 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001671}
1672
1673static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1674 struct x86_emulate_ops *ops, int seg)
1675{
1676 struct decode_cache *c = &ctxt->decode;
1677 unsigned long selector;
1678 int rc;
1679
1680 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001681 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001682 return rc;
1683
Gleb Natapov2e873022010-03-18 15:20:18 +02001684 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001685 return rc;
1686}
1687
Wei Yongjunc37eda12010-06-15 09:03:33 +08001688static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001689 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001690{
1691 struct decode_cache *c = &ctxt->decode;
1692 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001693 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001694 int reg = VCPU_REGS_RAX;
1695
1696 while (reg <= VCPU_REGS_RDI) {
1697 (reg == VCPU_REGS_RSP) ?
1698 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1699
Gleb Natapov79168fd2010-04-28 19:15:30 +03001700 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001701
1702 rc = writeback(ctxt, ops);
1703 if (rc != X86EMUL_CONTINUE)
1704 return rc;
1705
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001706 ++reg;
1707 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001708
1709 /* Disable writeback. */
1710 c->dst.type = OP_NONE;
1711
1712 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001713}
1714
1715static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1716 struct x86_emulate_ops *ops)
1717{
1718 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001719 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001720 int reg = VCPU_REGS_RDI;
1721
1722 while (reg >= VCPU_REGS_RAX) {
1723 if (reg == VCPU_REGS_RSP) {
1724 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1725 c->op_bytes);
1726 --reg;
1727 }
1728
1729 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001730 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001731 break;
1732 --reg;
1733 }
1734 return rc;
1735}
1736
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001737static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1738 struct x86_emulate_ops *ops)
1739{
1740 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001741
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001742 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743}
1744
Laurent Vivier05f086f2007-09-24 11:10:55 +02001745static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001747 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001748 switch (c->modrm_reg) {
1749 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001750 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751 break;
1752 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001753 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754 break;
1755 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001756 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757 break;
1758 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001759 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760 break;
1761 case 4: /* sal/shl */
1762 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001763 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001764 break;
1765 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001766 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767 break;
1768 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001769 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 break;
1771 }
1772}
1773
1774static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001775 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776{
1777 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778
1779 switch (c->modrm_reg) {
1780 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001781 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782 break;
1783 case 2: /* not */
1784 c->dst.val = ~c->dst.val;
1785 break;
1786 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001787 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 break;
1789 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001790 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001792 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793}
1794
1795static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001796 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001797{
1798 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001799
1800 switch (c->modrm_reg) {
1801 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001802 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803 break;
1804 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001805 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001807 case 2: /* call near abs */ {
1808 long int old_eip;
1809 old_eip = c->eip;
1810 c->eip = c->src.val;
1811 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001812 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001813 break;
1814 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001815 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001816 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001817 break;
1818 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001819 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001820 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001822 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823}
1824
1825static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001826 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827{
1828 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001829 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830
1831 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1832 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001833 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1834 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001835 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001836 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001837 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1838 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001839
Laurent Vivier05f086f2007-09-24 11:10:55 +02001840 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001841 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001842 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001843}
1844
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001845static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1846 struct x86_emulate_ops *ops)
1847{
1848 struct decode_cache *c = &ctxt->decode;
1849 int rc;
1850 unsigned long cs;
1851
1852 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001853 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001854 return rc;
1855 if (c->op_bytes == 4)
1856 c->eip = (u32)c->eip;
1857 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001858 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001859 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001860 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001861 return rc;
1862}
1863
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001864static inline void
1865setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001866 struct x86_emulate_ops *ops, struct desc_struct *cs,
1867 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001869 memset(cs, 0, sizeof(struct desc_struct));
1870 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1871 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872
1873 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001874 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001875 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001876 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877 cs->type = 0x0b; /* Read, Execute, Accessed */
1878 cs->s = 1;
1879 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 cs->p = 1;
1881 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001882
Gleb Natapov79168fd2010-04-28 19:15:30 +03001883 set_desc_base(ss, 0); /* flat segment */
1884 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885 ss->g = 1; /* 4kb granularity */
1886 ss->s = 1;
1887 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001888 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001890 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891}
1892
1893static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001894emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895{
1896 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001897 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001898 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001900
1901 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001902 if (ctxt->mode == X86EMUL_MODE_REAL ||
1903 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001904 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001905 return X86EMUL_PROPAGATE_FAULT;
1906 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907
Gleb Natapov79168fd2010-04-28 19:15:30 +03001908 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001909
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001910 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001912 cs_sel = (u16)(msr_data & 0xfffc);
1913 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914
1915 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001916 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001917 cs.l = 1;
1918 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1920 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1921 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1922 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001923
1924 c->regs[VCPU_REGS_RCX] = c->eip;
1925 if (is_long_mode(ctxt->vcpu)) {
1926#ifdef CONFIG_X86_64
1927 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1928
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001929 ops->get_msr(ctxt->vcpu,
1930 ctxt->mode == X86EMUL_MODE_PROT64 ?
1931 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001932 c->eip = msr_data;
1933
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001934 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001935 ctxt->eflags &= ~(msr_data | EFLG_RF);
1936#endif
1937 } else {
1938 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001939 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001940 c->eip = (u32)msr_data;
1941
1942 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1943 }
1944
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001945 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001946}
1947
Andre Przywara8c604352009-06-18 12:56:01 +02001948static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001949emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001950{
1951 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001952 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001953 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001954 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001955
Gleb Natapova0044752010-02-10 14:21:31 +02001956 /* inject #GP if in real mode */
1957 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001958 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001959 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001960 }
1961
1962 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1963 * Therefore, we inject an #UD.
1964 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001965 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001966 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001967 return X86EMUL_PROPAGATE_FAULT;
1968 }
Andre Przywara8c604352009-06-18 12:56:01 +02001969
Gleb Natapov79168fd2010-04-28 19:15:30 +03001970 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001971
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001972 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001973 switch (ctxt->mode) {
1974 case X86EMUL_MODE_PROT32:
1975 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001976 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001977 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001978 }
1979 break;
1980 case X86EMUL_MODE_PROT64:
1981 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001982 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001983 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001984 }
1985 break;
1986 }
1987
1988 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001989 cs_sel = (u16)msr_data;
1990 cs_sel &= ~SELECTOR_RPL_MASK;
1991 ss_sel = cs_sel + 8;
1992 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001993 if (ctxt->mode == X86EMUL_MODE_PROT64
1994 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001995 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001996 cs.l = 1;
1997 }
1998
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2000 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2001 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2002 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002003
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002004 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002005 c->eip = msr_data;
2006
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002007 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002008 c->regs[VCPU_REGS_RSP] = msr_data;
2009
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002010 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002011}
2012
Andre Przywara4668f052009-06-18 12:56:02 +02002013static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002014emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002015{
2016 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002017 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002018 u64 msr_data;
2019 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002020 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002021
Gleb Natapova0044752010-02-10 14:21:31 +02002022 /* inject #GP if in real mode or Virtual 8086 mode */
2023 if (ctxt->mode == X86EMUL_MODE_REAL ||
2024 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002025 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002026 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002027 }
2028
Gleb Natapov79168fd2010-04-28 19:15:30 +03002029 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002030
2031 if ((c->rex_prefix & 0x8) != 0x0)
2032 usermode = X86EMUL_MODE_PROT64;
2033 else
2034 usermode = X86EMUL_MODE_PROT32;
2035
2036 cs.dpl = 3;
2037 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002038 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002039 switch (usermode) {
2040 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002041 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002042 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002043 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002044 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002045 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002046 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002047 break;
2048 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002049 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002050 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002051 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002052 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002053 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 ss_sel = cs_sel + 8;
2055 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002056 cs.l = 1;
2057 break;
2058 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 cs_sel |= SELECTOR_RPL_MASK;
2060 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002061
Gleb Natapov79168fd2010-04-28 19:15:30 +03002062 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2063 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2064 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2065 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002066
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002067 c->eip = c->regs[VCPU_REGS_RDX];
2068 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002069
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002070 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002071}
2072
Gleb Natapov9c537242010-03-18 15:20:05 +02002073static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2074 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002075{
2076 int iopl;
2077 if (ctxt->mode == X86EMUL_MODE_REAL)
2078 return false;
2079 if (ctxt->mode == X86EMUL_MODE_VM86)
2080 return true;
2081 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002082 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002083}
2084
2085static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2086 struct x86_emulate_ops *ops,
2087 u16 port, u16 len)
2088{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002089 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002090 int r;
2091 u16 io_bitmap_ptr;
2092 u8 perm, bit_idx = port & 0x7;
2093 unsigned mask = (1 << len) - 1;
2094
Gleb Natapov79168fd2010-04-28 19:15:30 +03002095 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2096 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002097 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002098 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002099 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002100 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2101 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002102 if (r != X86EMUL_CONTINUE)
2103 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002104 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002105 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002106 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2107 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002108 if (r != X86EMUL_CONTINUE)
2109 return false;
2110 if ((perm >> bit_idx) & mask)
2111 return false;
2112 return true;
2113}
2114
2115static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2116 struct x86_emulate_ops *ops,
2117 u16 port, u16 len)
2118{
Gleb Natapov9c537242010-03-18 15:20:05 +02002119 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002120 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2121 return false;
2122 return true;
2123}
2124
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002125static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2126 struct x86_emulate_ops *ops,
2127 struct tss_segment_16 *tss)
2128{
2129 struct decode_cache *c = &ctxt->decode;
2130
2131 tss->ip = c->eip;
2132 tss->flag = ctxt->eflags;
2133 tss->ax = c->regs[VCPU_REGS_RAX];
2134 tss->cx = c->regs[VCPU_REGS_RCX];
2135 tss->dx = c->regs[VCPU_REGS_RDX];
2136 tss->bx = c->regs[VCPU_REGS_RBX];
2137 tss->sp = c->regs[VCPU_REGS_RSP];
2138 tss->bp = c->regs[VCPU_REGS_RBP];
2139 tss->si = c->regs[VCPU_REGS_RSI];
2140 tss->di = c->regs[VCPU_REGS_RDI];
2141
2142 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2143 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2144 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2145 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2146 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2147}
2148
2149static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2150 struct x86_emulate_ops *ops,
2151 struct tss_segment_16 *tss)
2152{
2153 struct decode_cache *c = &ctxt->decode;
2154 int ret;
2155
2156 c->eip = tss->ip;
2157 ctxt->eflags = tss->flag | 2;
2158 c->regs[VCPU_REGS_RAX] = tss->ax;
2159 c->regs[VCPU_REGS_RCX] = tss->cx;
2160 c->regs[VCPU_REGS_RDX] = tss->dx;
2161 c->regs[VCPU_REGS_RBX] = tss->bx;
2162 c->regs[VCPU_REGS_RSP] = tss->sp;
2163 c->regs[VCPU_REGS_RBP] = tss->bp;
2164 c->regs[VCPU_REGS_RSI] = tss->si;
2165 c->regs[VCPU_REGS_RDI] = tss->di;
2166
2167 /*
2168 * SDM says that segment selectors are loaded before segment
2169 * descriptors
2170 */
2171 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2172 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2173 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2174 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2175 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2176
2177 /*
2178 * Now load segment descriptors. If fault happenes at this stage
2179 * it is handled in a context of new task
2180 */
2181 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2182 if (ret != X86EMUL_CONTINUE)
2183 return ret;
2184 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2185 if (ret != X86EMUL_CONTINUE)
2186 return ret;
2187 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2188 if (ret != X86EMUL_CONTINUE)
2189 return ret;
2190 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2191 if (ret != X86EMUL_CONTINUE)
2192 return ret;
2193 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2194 if (ret != X86EMUL_CONTINUE)
2195 return ret;
2196
2197 return X86EMUL_CONTINUE;
2198}
2199
2200static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2201 struct x86_emulate_ops *ops,
2202 u16 tss_selector, u16 old_tss_sel,
2203 ulong old_tss_base, struct desc_struct *new_desc)
2204{
2205 struct tss_segment_16 tss_seg;
2206 int ret;
2207 u32 err, new_tss_base = get_desc_base(new_desc);
2208
2209 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2210 &err);
2211 if (ret == X86EMUL_PROPAGATE_FAULT) {
2212 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002213 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002214 return ret;
2215 }
2216
2217 save_state_to_tss16(ctxt, ops, &tss_seg);
2218
2219 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2220 &err);
2221 if (ret == X86EMUL_PROPAGATE_FAULT) {
2222 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002223 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002224 return ret;
2225 }
2226
2227 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2228 &err);
2229 if (ret == X86EMUL_PROPAGATE_FAULT) {
2230 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002231 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002232 return ret;
2233 }
2234
2235 if (old_tss_sel != 0xffff) {
2236 tss_seg.prev_task_link = old_tss_sel;
2237
2238 ret = ops->write_std(new_tss_base,
2239 &tss_seg.prev_task_link,
2240 sizeof tss_seg.prev_task_link,
2241 ctxt->vcpu, &err);
2242 if (ret == X86EMUL_PROPAGATE_FAULT) {
2243 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002244 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002245 return ret;
2246 }
2247 }
2248
2249 return load_state_from_tss16(ctxt, ops, &tss_seg);
2250}
2251
2252static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2253 struct x86_emulate_ops *ops,
2254 struct tss_segment_32 *tss)
2255{
2256 struct decode_cache *c = &ctxt->decode;
2257
2258 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2259 tss->eip = c->eip;
2260 tss->eflags = ctxt->eflags;
2261 tss->eax = c->regs[VCPU_REGS_RAX];
2262 tss->ecx = c->regs[VCPU_REGS_RCX];
2263 tss->edx = c->regs[VCPU_REGS_RDX];
2264 tss->ebx = c->regs[VCPU_REGS_RBX];
2265 tss->esp = c->regs[VCPU_REGS_RSP];
2266 tss->ebp = c->regs[VCPU_REGS_RBP];
2267 tss->esi = c->regs[VCPU_REGS_RSI];
2268 tss->edi = c->regs[VCPU_REGS_RDI];
2269
2270 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2271 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2272 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2273 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2274 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2275 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2276 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2277}
2278
2279static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2280 struct x86_emulate_ops *ops,
2281 struct tss_segment_32 *tss)
2282{
2283 struct decode_cache *c = &ctxt->decode;
2284 int ret;
2285
Gleb Natapov0f122442010-04-28 19:15:31 +03002286 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002287 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002288 return X86EMUL_PROPAGATE_FAULT;
2289 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290 c->eip = tss->eip;
2291 ctxt->eflags = tss->eflags | 2;
2292 c->regs[VCPU_REGS_RAX] = tss->eax;
2293 c->regs[VCPU_REGS_RCX] = tss->ecx;
2294 c->regs[VCPU_REGS_RDX] = tss->edx;
2295 c->regs[VCPU_REGS_RBX] = tss->ebx;
2296 c->regs[VCPU_REGS_RSP] = tss->esp;
2297 c->regs[VCPU_REGS_RBP] = tss->ebp;
2298 c->regs[VCPU_REGS_RSI] = tss->esi;
2299 c->regs[VCPU_REGS_RDI] = tss->edi;
2300
2301 /*
2302 * SDM says that segment selectors are loaded before segment
2303 * descriptors
2304 */
2305 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2306 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2307 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2308 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2309 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2310 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2311 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2312
2313 /*
2314 * Now load segment descriptors. If fault happenes at this stage
2315 * it is handled in a context of new task
2316 */
2317 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2318 if (ret != X86EMUL_CONTINUE)
2319 return ret;
2320 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2321 if (ret != X86EMUL_CONTINUE)
2322 return ret;
2323 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2324 if (ret != X86EMUL_CONTINUE)
2325 return ret;
2326 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2327 if (ret != X86EMUL_CONTINUE)
2328 return ret;
2329 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2330 if (ret != X86EMUL_CONTINUE)
2331 return ret;
2332 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2333 if (ret != X86EMUL_CONTINUE)
2334 return ret;
2335 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2336 if (ret != X86EMUL_CONTINUE)
2337 return ret;
2338
2339 return X86EMUL_CONTINUE;
2340}
2341
2342static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2343 struct x86_emulate_ops *ops,
2344 u16 tss_selector, u16 old_tss_sel,
2345 ulong old_tss_base, struct desc_struct *new_desc)
2346{
2347 struct tss_segment_32 tss_seg;
2348 int ret;
2349 u32 err, new_tss_base = get_desc_base(new_desc);
2350
2351 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2352 &err);
2353 if (ret == X86EMUL_PROPAGATE_FAULT) {
2354 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002355 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356 return ret;
2357 }
2358
2359 save_state_to_tss32(ctxt, ops, &tss_seg);
2360
2361 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2362 &err);
2363 if (ret == X86EMUL_PROPAGATE_FAULT) {
2364 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002365 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002366 return ret;
2367 }
2368
2369 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2370 &err);
2371 if (ret == X86EMUL_PROPAGATE_FAULT) {
2372 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002373 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374 return ret;
2375 }
2376
2377 if (old_tss_sel != 0xffff) {
2378 tss_seg.prev_task_link = old_tss_sel;
2379
2380 ret = ops->write_std(new_tss_base,
2381 &tss_seg.prev_task_link,
2382 sizeof tss_seg.prev_task_link,
2383 ctxt->vcpu, &err);
2384 if (ret == X86EMUL_PROPAGATE_FAULT) {
2385 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002386 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 return ret;
2388 }
2389 }
2390
2391 return load_state_from_tss32(ctxt, ops, &tss_seg);
2392}
2393
2394static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002395 struct x86_emulate_ops *ops,
2396 u16 tss_selector, int reason,
2397 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398{
2399 struct desc_struct curr_tss_desc, next_tss_desc;
2400 int ret;
2401 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2402 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002403 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002404 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405
2406 /* FIXME: old_tss_base == ~0 ? */
2407
2408 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2409 if (ret != X86EMUL_CONTINUE)
2410 return ret;
2411 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2412 if (ret != X86EMUL_CONTINUE)
2413 return ret;
2414
2415 /* FIXME: check that next_tss_desc is tss */
2416
2417 if (reason != TASK_SWITCH_IRET) {
2418 if ((tss_selector & 3) > next_tss_desc.dpl ||
2419 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002420 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421 return X86EMUL_PROPAGATE_FAULT;
2422 }
2423 }
2424
Gleb Natapovceffb452010-03-18 15:20:19 +02002425 desc_limit = desc_limit_scaled(&next_tss_desc);
2426 if (!next_tss_desc.p ||
2427 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2428 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002429 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 return X86EMUL_PROPAGATE_FAULT;
2431 }
2432
2433 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2434 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2435 write_segment_descriptor(ctxt, ops, old_tss_sel,
2436 &curr_tss_desc);
2437 }
2438
2439 if (reason == TASK_SWITCH_IRET)
2440 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2441
2442 /* set back link to prev task only if NT bit is set in eflags
2443 note that old_tss_sel is not used afetr this point */
2444 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2445 old_tss_sel = 0xffff;
2446
2447 if (next_tss_desc.type & 8)
2448 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2449 old_tss_base, &next_tss_desc);
2450 else
2451 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2452 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002453 if (ret != X86EMUL_CONTINUE)
2454 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002455
2456 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2457 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2458
2459 if (reason != TASK_SWITCH_IRET) {
2460 next_tss_desc.type |= (1 << 1); /* set busy flag */
2461 write_segment_descriptor(ctxt, ops, tss_selector,
2462 &next_tss_desc);
2463 }
2464
2465 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2466 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2467 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2468
Jan Kiszkae269fb22010-04-14 15:51:09 +02002469 if (has_error_code) {
2470 struct decode_cache *c = &ctxt->decode;
2471
2472 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2473 c->lock_prefix = 0;
2474 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002475 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002476 }
2477
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478 return ret;
2479}
2480
2481int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2482 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002483 u16 tss_selector, int reason,
2484 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002485{
2486 struct decode_cache *c = &ctxt->decode;
2487 int rc;
2488
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002490 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491
Jan Kiszkae269fb22010-04-14 15:51:09 +02002492 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2493 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002494
2495 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002496 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002497 if (rc == X86EMUL_CONTINUE)
2498 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499 }
2500
Gleb Natapov19d04432010-04-15 12:29:50 +03002501 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502}
2503
Gleb Natapova682e352010-03-18 15:20:21 +02002504static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002505 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002506{
2507 struct decode_cache *c = &ctxt->decode;
2508 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2509
Gleb Natapovd9271122010-03-18 15:20:22 +02002510 register_address_increment(c, &c->regs[reg], df * op->bytes);
2511 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002512}
2513
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002514int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002515x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002516{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002517 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002518 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002519 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002520 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002521
Gleb Natapov9de41572010-04-28 19:15:22 +03002522 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002523
Gleb Natapov11616242010-02-11 14:43:14 +02002524 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002525 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002526 goto done;
2527 }
2528
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002529 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002530 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002531 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002532 goto done;
2533 }
2534
Gleb Natapove92805a2010-02-10 14:21:35 +02002535 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002536 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002537 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002538 goto done;
2539 }
2540
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002541 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002542 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002543 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002544 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002545 string_done:
2546 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002547 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002548 goto done;
2549 }
2550 /* The second termination condition only applies for REPE
2551 * and REPNE. Test if the repeat string operation prefix is
2552 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2553 * corresponding termination condition according to:
2554 * - if REPE/REPZ and ZF = 0 then done
2555 * - if REPNE/REPNZ and ZF = 1 then done
2556 */
2557 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002558 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002559 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002560 ((ctxt->eflags & EFLG_ZF) == 0))
2561 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002562 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002563 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2564 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002565 }
Gleb Natapov063db062010-03-18 15:20:06 +02002566 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002567 }
2568
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002569 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002570 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002571 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002572 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002573 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002574 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002575 }
2576
Gleb Natapove35b7b92010-02-25 16:36:42 +02002577 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002578 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2579 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002580 if (rc != X86EMUL_CONTINUE)
2581 goto done;
2582 }
2583
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002584 if ((c->d & DstMask) == ImplicitOps)
2585 goto special_insn;
2586
2587
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002588 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2589 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002590 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2591 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002592 if (rc != X86EMUL_CONTINUE)
2593 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002594 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002595 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002596
Avi Kivity018a98d2007-11-27 19:30:56 +02002597special_insn:
2598
Laurent Viviere4e03de2007-09-18 11:52:50 +02002599 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 goto twobyte_insn;
2601
Laurent Viviere4e03de2007-09-18 11:52:50 +02002602 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603 case 0x00 ... 0x05:
2604 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002605 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002607 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002608 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002609 break;
2610 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002611 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002612 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002613 goto done;
2614 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 case 0x08 ... 0x0d:
2616 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002617 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002619 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002620 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002621 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622 case 0x10 ... 0x15:
2623 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002624 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002626 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002627 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002628 break;
2629 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002630 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002631 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002632 goto done;
2633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 case 0x18 ... 0x1d:
2635 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002636 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002638 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002639 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002640 break;
2641 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002642 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002643 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002644 goto done;
2645 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002646 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002648 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649 break;
2650 case 0x28 ... 0x2d:
2651 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002652 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 break;
2654 case 0x30 ... 0x35:
2655 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002656 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 break;
2658 case 0x38 ... 0x3d:
2659 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002660 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002662 case 0x40 ... 0x47: /* inc r16/r32 */
2663 emulate_1op("inc", c->dst, ctxt->eflags);
2664 break;
2665 case 0x48 ... 0x4f: /* dec r16/r32 */
2666 emulate_1op("dec", c->dst, ctxt->eflags);
2667 break;
2668 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002669 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002670 break;
2671 case 0x58 ... 0x5f: /* pop reg */
2672 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002673 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002674 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002675 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002676 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002677 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002678 rc = emulate_pusha(ctxt, ops);
2679 if (rc != X86EMUL_CONTINUE)
2680 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002681 break;
2682 case 0x61: /* popa */
2683 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002684 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002685 goto done;
2686 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002688 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002690 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002692 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002693 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002694 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002695 break;
2696 case 0x6c: /* insb */
2697 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002698 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002699 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002700 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002701 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002702 goto done;
2703 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002704 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2705 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002706 goto done; /* IO is needed, skip writeback */
2707 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002708 case 0x6e: /* outsb */
2709 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002710 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002711 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002712 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002713 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002714 goto done;
2715 }
Gleb Natapov79729952010-03-18 15:20:24 +02002716 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2717 &c->src.val, 1, ctxt->vcpu);
2718
2719 c->dst.type = OP_NONE; /* nothing to writeback */
2720 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002721 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002722 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002723 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002724 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002726 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 case 0:
2728 goto add;
2729 case 1:
2730 goto or;
2731 case 2:
2732 goto adc;
2733 case 3:
2734 goto sbb;
2735 case 4:
2736 goto and;
2737 case 5:
2738 goto sub;
2739 case 6:
2740 goto xor;
2741 case 7:
2742 goto cmp;
2743 }
2744 break;
2745 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002746 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002747 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 break;
2749 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002750 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002752 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002754 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 break;
2756 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002757 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
2759 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002760 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 break; /* 64b reg: zero-extend */
2762 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002763 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 break;
2765 }
2766 /*
2767 * Write back the memory destination with implicit LOCK
2768 * prefix.
2769 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002770 c->dst.val = c->src.val;
2771 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002774 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002775 case 0x8c: /* mov r/m, sreg */
2776 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002777 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002778 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002779 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002780 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002781 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002782 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002783 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002784 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002785 case 0x8e: { /* mov seg, r/m16 */
2786 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002787
2788 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002789
Gleb Natapovc6975182010-02-18 12:15:01 +02002790 if (c->modrm_reg == VCPU_SREG_CS ||
2791 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002792 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002793 goto done;
2794 }
2795
Glauber Costa310b5d32009-05-12 16:21:06 -04002796 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002797 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002798
Gleb Natapov2e873022010-03-18 15:20:18 +02002799 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002800
2801 c->dst.type = OP_NONE; /* Disable writeback. */
2802 break;
2803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002805 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002806 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002809 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002810 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2811 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002812 break;
2813 }
2814 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002815 c->src.type = OP_REG;
2816 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002817 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2818 c->src.val = *(c->src.ptr);
2819 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002820 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002821 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002822 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002823 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002824 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002825 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002826 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002827 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002828 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2829 if (rc != X86EMUL_CONTINUE)
2830 goto done;
2831 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002832 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002834 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002836 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002837 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002838 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002839 case 0xa8 ... 0xa9: /* test ax, imm */
2840 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002842 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 break;
2844 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002845 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 case 0xae ... 0xaf: /* scas */
2847 DPRINTF("Urk! I don't handle SCAS.\n");
2848 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002849 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002850 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002851 case 0xc0 ... 0xc1:
2852 emulate_grp2(ctxt);
2853 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002854 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002855 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002856 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002857 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002858 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002859 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2860 mov:
2861 c->dst.val = c->src.val;
2862 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002863 case 0xcb: /* ret far */
2864 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002865 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002866 goto done;
2867 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002868 case 0xd0 ... 0xd1: /* Grp2 */
2869 c->src.val = 1;
2870 emulate_grp2(ctxt);
2871 break;
2872 case 0xd2 ... 0xd3: /* Grp2 */
2873 c->src.val = c->regs[VCPU_REGS_RCX];
2874 emulate_grp2(ctxt);
2875 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002876 case 0xe4: /* inb */
2877 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002878 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002879 case 0xe6: /* outb */
2880 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002881 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002882 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002883 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002884 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002885 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002886 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002887 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002888 }
2889 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002890 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002891 case 0xea: { /* jmp far */
2892 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002893 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002894 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2895
2896 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002897 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002898
Gleb Natapov414e6272010-04-28 19:15:26 +03002899 c->eip = 0;
2900 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002901 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002902 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002903 case 0xeb:
2904 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002905 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002906 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002907 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002908 case 0xec: /* in al,dx */
2909 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002910 c->src.val = c->regs[VCPU_REGS_RDX];
2911 do_io_in:
2912 c->dst.bytes = min(c->dst.bytes, 4u);
2913 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002914 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002915 goto done;
2916 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002917 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2918 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002919 goto done; /* IO is needed */
2920 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08002921 case 0xee: /* out dx,al */
2922 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002923 c->src.val = c->regs[VCPU_REGS_RDX];
2924 do_io_out:
2925 c->dst.bytes = min(c->dst.bytes, 4u);
2926 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002927 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002928 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002929 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002930 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2931 ctxt->vcpu);
2932 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002933 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002934 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002935 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002936 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002937 case 0xf5: /* cmc */
2938 /* complement carry flag from eflags reg */
2939 ctxt->eflags ^= EFLG_CF;
2940 c->dst.type = OP_NONE; /* Disable writeback. */
2941 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002942 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002943 if (!emulate_grp3(ctxt, ops))
2944 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002945 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002946 case 0xf8: /* clc */
2947 ctxt->eflags &= ~EFLG_CF;
2948 c->dst.type = OP_NONE; /* Disable writeback. */
2949 break;
2950 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002951 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002952 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002953 goto done;
2954 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002955 ctxt->eflags &= ~X86_EFLAGS_IF;
2956 c->dst.type = OP_NONE; /* Disable writeback. */
2957 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002958 break;
2959 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002960 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002961 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08002962 goto done;
2963 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03002964 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002965 ctxt->eflags |= X86_EFLAGS_IF;
2966 c->dst.type = OP_NONE; /* Disable writeback. */
2967 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002968 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002969 case 0xfc: /* cld */
2970 ctxt->eflags &= ~EFLG_DF;
2971 c->dst.type = OP_NONE; /* Disable writeback. */
2972 break;
2973 case 0xfd: /* std */
2974 ctxt->eflags |= EFLG_DF;
2975 c->dst.type = OP_NONE; /* Disable writeback. */
2976 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002977 case 0xfe: /* Grp4 */
2978 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002979 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002980 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002981 goto done;
2982 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002983 case 0xff: /* Grp5 */
2984 if (c->modrm_reg == 5)
2985 goto jump_far;
2986 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03002987 default:
2988 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002990
2991writeback:
2992 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002993 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002994 goto done;
2995
Gleb Natapov5cd21912010-03-18 15:20:26 +02002996 /*
2997 * restore dst type in case the decoding will be reused
2998 * (happens for string instruction )
2999 */
3000 c->dst.type = saved_dst_type;
3001
Gleb Natapova682e352010-03-18 15:20:21 +02003002 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003003 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3004 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003005
3006 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003007 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3008 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003009
Gleb Natapov5cd21912010-03-18 15:20:26 +02003010 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003011 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003012 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003013 /*
3014 * Re-enter guest when pio read ahead buffer is empty or,
3015 * if it is not used, after each 1024 iteration.
3016 */
3017 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3018 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003019 ctxt->restart = false;
3020 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003021 /*
3022 * reset read cache here in case string instruction is restared
3023 * without decoding
3024 */
3025 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003026 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003027
3028done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003029 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030
3031twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003032 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003034 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 u16 size;
3036 unsigned long address;
3037
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003038 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003039 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003040 goto cannot_emulate;
3041
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003042 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003043 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003044 goto done;
3045
Avi Kivity33e38852008-05-21 15:34:25 +03003046 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003047 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003048 /* Disable writeback. */
3049 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003050 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003052 rc = read_descriptor(ctxt, ops, c->src.ptr,
3053 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003054 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 goto done;
3056 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003057 /* Disable writeback. */
3058 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003060 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003061 if (c->modrm_mod == 3) {
3062 switch (c->modrm_rm) {
3063 case 1:
3064 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003065 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003066 goto done;
3067 break;
3068 default:
3069 goto cannot_emulate;
3070 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003071 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003072 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003073 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003074 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003075 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003076 goto done;
3077 realmode_lidt(ctxt->vcpu, size, address);
3078 }
Avi Kivity16286d02008-04-14 14:40:50 +03003079 /* Disable writeback. */
3080 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 break;
3082 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003083 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003084 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085 break;
3086 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003087 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3088 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003089 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003091 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003092 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003093 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003095 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003096 /* Disable writeback. */
3097 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 break;
3099 default:
3100 goto cannot_emulate;
3101 }
3102 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003103 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003104 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003105 if (rc != X86EMUL_CONTINUE)
3106 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003107 else
3108 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003109 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003110 case 0x06:
3111 emulate_clts(ctxt->vcpu);
3112 c->dst.type = OP_NONE;
3113 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003114 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003115 kvm_emulate_wbinvd(ctxt->vcpu);
3116 c->dst.type = OP_NONE;
3117 break;
3118 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003119 case 0x0d: /* GrpP (prefetch) */
3120 case 0x18: /* Grp16 (prefetch/nop) */
3121 c->dst.type = OP_NONE;
3122 break;
3123 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003124 switch (c->modrm_reg) {
3125 case 1:
3126 case 5 ... 7:
3127 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003128 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003129 goto done;
3130 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003131 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003132 c->dst.type = OP_NONE; /* no writeback */
3133 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003135 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3136 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003137 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003138 goto done;
3139 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003140 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003141 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003143 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003144 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003145 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003146 goto done;
3147 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003148 c->dst.type = OP_NONE;
3149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003151 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3152 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003153 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003154 goto done;
3155 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003156
Gleb Natapov338dbc92010-04-28 19:15:32 +03003157 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3158 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3159 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3160 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003161 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003162 goto done;
3163 }
3164
Laurent Viviera01af5e2007-09-24 11:10:56 +02003165 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003167 case 0x30:
3168 /* wrmsr */
3169 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3170 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003171 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003172 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003173 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003174 }
3175 rc = X86EMUL_CONTINUE;
3176 c->dst.type = OP_NONE;
3177 break;
3178 case 0x32:
3179 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003180 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003181 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003182 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003183 } else {
3184 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3185 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3186 }
3187 rc = X86EMUL_CONTINUE;
3188 c->dst.type = OP_NONE;
3189 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003190 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003191 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003192 if (rc != X86EMUL_CONTINUE)
3193 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003194 else
3195 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003196 break;
3197 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003198 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003199 if (rc != X86EMUL_CONTINUE)
3200 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003201 else
3202 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003203 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003205 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003206 if (!test_cc(c->b, ctxt->eflags))
3207 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003209 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003210 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003211 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003212 c->dst.type = OP_NONE;
3213 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003214 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003215 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003216 break;
3217 case 0xa1: /* pop fs */
3218 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003219 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003220 goto done;
3221 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003222 case 0xa3:
3223 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003224 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003225 /* only subword offset */
3226 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003227 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003228 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003229 case 0xa4: /* shld imm8, r, r/m */
3230 case 0xa5: /* shld cl, r, r/m */
3231 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3232 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003233 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003234 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003235 break;
3236 case 0xa9: /* pop gs */
3237 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003238 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003239 goto done;
3240 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003241 case 0xab:
3242 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003243 /* only subword offset */
3244 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003245 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003246 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003247 case 0xac: /* shrd imm8, r, r/m */
3248 case 0xad: /* shrd cl, r, r/m */
3249 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3250 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003251 case 0xae: /* clflush */
3252 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 case 0xb0 ... 0xb1: /* cmpxchg */
3254 /*
3255 * Save real source value, then compare EAX against
3256 * destination.
3257 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003258 c->src.orig_val = c->src.val;
3259 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003260 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3261 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003263 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264 } else {
3265 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003266 c->dst.type = OP_REG;
3267 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 }
3269 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270 case 0xb3:
3271 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003272 /* only subword offset */
3273 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003274 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003277 c->dst.bytes = c->op_bytes;
3278 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3279 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003282 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 case 0:
3284 goto bt;
3285 case 1:
3286 goto bts;
3287 case 2:
3288 goto btr;
3289 case 3:
3290 goto btc;
3291 }
3292 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003293 case 0xbb:
3294 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003295 /* only subword offset */
3296 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003297 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003298 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003300 c->dst.bytes = c->op_bytes;
3301 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3302 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003304 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003305 c->dst.bytes = c->op_bytes;
3306 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3307 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003308 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003310 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003311 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003312 goto done;
3313 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003314 default:
3315 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 }
3317 goto writeback;
3318
3319cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003320 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 return -1;
3322}