blob: b0f45bc63e1c47e5f6f62030cd5ffd4f74f3d926 [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 Kivitye071edd2010-07-26 14:37:51 +0300108 Group1, Group1A, Group3, Group4, Group5, Group7, Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200109};
110
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100111static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200113 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300115 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300116 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200118 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200120 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
121 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200123 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300125 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300126 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200128 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300130 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200133 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Wei Yongjune97e8832010-07-06 16:51:09 +0800135 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamalabc19082010-05-12 01:39:21 +0300139 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200141 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal222b7c52010-05-12 01:39:22 +0300143 ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144 /* 0x38 - 0x3F */
145 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
146 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200147 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
148 0, 0,
Avi Kivity749358a2010-07-26 14:37:40 +0300149 /* 0x40 - 0x4F */
150 X16(DstReg),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300151 /* 0x50 - 0x57 */
Avi Kivity38491862010-07-26 14:37:41 +0300152 X8(SrcReg | Stack),
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300153 /* 0x58 - 0x5F */
Avi Kivity38491862010-07-26 14:37:41 +0300154 X8(DstReg | Stack),
Nitin A Kamble7d316912007-08-28 17:58:52 -0700155 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200156 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
157 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700158 0, 0, 0, 0,
159 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300160 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200161 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
162 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Avi Kivityb3ab3402010-07-26 14:37:42 +0300163 /* 0x70 - 0x7F */
164 X16(SrcImmByte),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 /* 0x80 - 0x87 */
Avi Kivity4968ec42010-07-26 14:37:49 +0300166 ByteOp | DstMem | SrcImm | ModRM | Group | Group1,
167 DstMem | SrcImm | ModRM | Group | Group1,
168 ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1,
169 DstMem | SrcImmByte | ModRM | Group | Group1,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200171 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 /* 0x88 - 0x8F */
173 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
174 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Wei Yongjunb16b2b72010-07-06 16:52:53 +0800175 DstMem | SrcNone | ModRM | Mov, ModRM | DstReg,
Wei Yongjuna5046e62010-07-06 16:49:05 +0800176 ImplicitOps | SrcMem16 | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300177 /* 0x90 - 0x97 */
178 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
179 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300180 0, 0, SrcImmFAddr | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300181 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xA0 - 0xA7 */
Wei Yongjun5d55f292010-07-07 17:43:35 +0800183 ByteOp | DstAcc | SrcMem | Mov | MemAbs, DstAcc | SrcMem | Mov | MemAbs,
184 ByteOp | DstMem | SrcAcc | Mov | MemAbs, DstMem | SrcAcc | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200185 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
186 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 /* 0xA8 - 0xAF */
Mohammed Gamaldfb507c2010-05-11 22:22:40 +0300188 DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
Gleb Natapova682e352010-03-18 15:20:21 +0200189 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
190 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300191 /* 0xB0 - 0xB7 */
Avi Kivityb6e61532010-07-26 14:37:43 +0300192 X8(ByteOp | DstReg | SrcImm | Mov),
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300193 /* 0xB8 - 0xBF */
Avi Kivityb6e61532010-07-26 14:37:43 +0300194 X8(DstReg | SrcImm | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300196 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200197 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300198 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300200 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300201 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xD0 - 0xD7 */
203 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
204 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
205 0, 0, 0, 0,
206 /* 0xD8 - 0xDF */
207 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300208 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300209 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200210 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
211 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300212 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300213 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov414e6272010-04-28 19:15:26 +0300214 SrcImmFAddr | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200215 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
216 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xF0 - 0xF7 */
218 0, 0, 0, 0,
Avi Kivitye071edd2010-07-26 14:37:51 +0300219 ImplicitOps | Priv, ImplicitOps, ByteOp | Group | Group3, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700221 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300222 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223};
224
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100225static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200227 0, Group | GroupDual | Group7, 0, 0,
228 0, ImplicitOps, ImplicitOps | Priv, 0,
229 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
230 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 /* 0x10 - 0x1F */
232 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200234 ModRM | ImplicitOps | Priv, ModRM | Priv,
235 ModRM | ImplicitOps | Priv, ModRM | Priv,
236 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 0, 0, 0, 0, 0, 0, 0, 0,
238 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200239 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
240 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200241 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitybe8eacd2010-07-26 14:37:44 +0300242 /* 0x40 - 0x4F */
243 X16(DstReg | SrcMem | ModRM | Mov),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0x50 - 0x5F */
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246 /* 0x60 - 0x6F */
247 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248 /* 0x70 - 0x7F */
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /* 0x80 - 0x8F */
Avi Kivity880a1882010-07-26 14:37:45 +0300251 X16(SrcImm),
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252 /* 0x90 - 0x9F */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300255 ImplicitOps | Stack, ImplicitOps | Stack,
256 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100257 DstMem | SrcReg | Src2ImmByte | ModRM,
258 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300260 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200261 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100262 DstMem | SrcReg | Src2ImmByte | ModRM,
263 DstMem | SrcReg | Src2CL | ModRM,
264 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200266 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
267 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
269 DstReg | SrcMem16 | ModRM | Mov,
270 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200271 0, 0,
272 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
274 DstReg | SrcMem16 | ModRM | Mov,
275 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200276 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
277 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800278 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 /* 0xD0 - 0xDF */
280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
281 /* 0xE0 - 0xEF */
282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
283 /* 0xF0 - 0xFF */
284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
285};
286
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100287static u32 group_table[] = {
Avi Kivity4968ec42010-07-26 14:37:49 +0300288 [Group1*8] =
289 X7(Lock), 0,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200290 [Group1A*8] =
291 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200292 [Group3*8] =
Wei Yongjun7d5993d2010-06-17 17:33:55 +0800293 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
Avi Kivitydfe11482010-07-26 14:37:50 +0300294 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Avi Kivitye071edd2010-07-26 14:37:51 +0300295 X4(Undefined),
Avi Kivityfd607542008-01-18 13:12:26 +0200296 [Group4*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300297 ByteOp | DstMem | SrcNone | ModRM | Lock, ByteOp | DstMem | SrcNone | ModRM | Lock,
Avi Kivityfd607542008-01-18 13:12:26 +0200298 0, 0, 0, 0, 0, 0,
299 [Group5*8] =
Gleb Natapovc0e06082010-07-13 16:40:23 +0300300 DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock,
Mohammed Gamald19292e2008-09-08 21:47:19 +0300301 SrcMem | ModRM | Stack, 0,
Gleb Natapov414e6272010-04-28 19:15:26 +0300302 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200303 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200304 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200305 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300306 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200307 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200308 [Group8*8] =
309 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200310 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
311 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200312 [Group9*8] =
Gleb Natapov6550e1f2010-03-21 13:08:21 +0200313 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200314};
315
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100316static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200317 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200318 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300319 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200320 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200321 [Group9*8] =
322 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200323};
324
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200326#define EFLG_ID (1<<21)
327#define EFLG_VIP (1<<20)
328#define EFLG_VIF (1<<19)
329#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200330#define EFLG_VM (1<<17)
331#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200332#define EFLG_IOPL (3<<12)
333#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334#define EFLG_OF (1<<11)
335#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200336#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200337#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338#define EFLG_SF (1<<7)
339#define EFLG_ZF (1<<6)
340#define EFLG_AF (1<<4)
341#define EFLG_PF (1<<2)
342#define EFLG_CF (1<<0)
343
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300344#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
345#define EFLG_RESERVED_ONE_MASK 2
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347/*
348 * Instruction emulation:
349 * Most instructions are emulated directly via a fragment of inline assembly
350 * code. This allows us to save/restore EFLAGS and thus very easily pick up
351 * any modified flags.
352 */
353
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800354#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355#define _LO32 "k" /* force 32-bit operand */
356#define _STK "%%rsp" /* stack pointer */
357#elif defined(__i386__)
358#define _LO32 "" /* force 32-bit operand */
359#define _STK "%%esp" /* stack pointer */
360#endif
361
362/*
363 * These EFLAGS bits are restored from saved value during emulation, and
364 * any changes are written back to the saved value after emulation.
365 */
366#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
367
368/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200369#define _PRE_EFLAGS(_sav, _msk, _tmp) \
370 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
371 "movl %"_sav",%"_LO32 _tmp"; " \
372 "push %"_tmp"; " \
373 "push %"_tmp"; " \
374 "movl %"_msk",%"_LO32 _tmp"; " \
375 "andl %"_LO32 _tmp",("_STK"); " \
376 "pushf; " \
377 "notl %"_LO32 _tmp"; " \
378 "andl %"_LO32 _tmp",("_STK"); " \
379 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
380 "pop %"_tmp"; " \
381 "orl %"_LO32 _tmp",("_STK"); " \
382 "popf; " \
383 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384
385/* After executing instruction: write-back necessary bits in EFLAGS. */
386#define _POST_EFLAGS(_sav, _msk, _tmp) \
387 /* _sav |= EFLAGS & _msk; */ \
388 "pushf; " \
389 "pop %"_tmp"; " \
390 "andl %"_msk",%"_LO32 _tmp"; " \
391 "orl %"_LO32 _tmp",%"_sav"; "
392
Avi Kivitydda96d82008-11-26 15:14:10 +0200393#ifdef CONFIG_X86_64
394#define ON64(x) x
395#else
396#define ON64(x)
397#endif
398
Avi Kivity6b7ad612008-11-26 15:30:45 +0200399#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
400 do { \
401 __asm__ __volatile__ ( \
402 _PRE_EFLAGS("0", "4", "2") \
403 _op _suffix " %"_x"3,%1; " \
404 _POST_EFLAGS("0", "4", "2") \
405 : "=m" (_eflags), "=m" ((_dst).val), \
406 "=&r" (_tmp) \
407 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200408 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200409
410
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411/* Raw emulation: instruction has two explicit operands. */
412#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200413 do { \
414 unsigned long _tmp; \
415 \
416 switch ((_dst).bytes) { \
417 case 2: \
418 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
419 break; \
420 case 4: \
421 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
422 break; \
423 case 8: \
424 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
425 break; \
426 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 } while (0)
428
429#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
430 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200431 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400432 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200434 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 break; \
436 default: \
437 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
438 _wx, _wy, _lx, _ly, _qx, _qy); \
439 break; \
440 } \
441 } while (0)
442
443/* Source operand is byte-sized and may be restricted to just %cl. */
444#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
445 __emulate_2op(_op, _src, _dst, _eflags, \
446 "b", "c", "b", "c", "b", "c", "b", "c")
447
448/* Source operand is byte, word, long or quad sized. */
449#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
450 __emulate_2op(_op, _src, _dst, _eflags, \
451 "b", "q", "w", "r", _LO32, "r", "", "r")
452
453/* Source operand is word, long or quad sized. */
454#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
455 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
456 "w", "r", _LO32, "r", "", "r")
457
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100458/* Instruction has three operands and one operand is stored in ECX register */
459#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
460 do { \
461 unsigned long _tmp; \
462 _type _clv = (_cl).val; \
463 _type _srcv = (_src).val; \
464 _type _dstv = (_dst).val; \
465 \
466 __asm__ __volatile__ ( \
467 _PRE_EFLAGS("0", "5", "2") \
468 _op _suffix " %4,%1 \n" \
469 _POST_EFLAGS("0", "5", "2") \
470 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
471 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
472 ); \
473 \
474 (_cl).val = (unsigned long) _clv; \
475 (_src).val = (unsigned long) _srcv; \
476 (_dst).val = (unsigned long) _dstv; \
477 } while (0)
478
479#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
480 do { \
481 switch ((_dst).bytes) { \
482 case 2: \
483 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
484 "w", unsigned short); \
485 break; \
486 case 4: \
487 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
488 "l", unsigned int); \
489 break; \
490 case 8: \
491 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
492 "q", unsigned long)); \
493 break; \
494 } \
495 } while (0)
496
Avi Kivitydda96d82008-11-26 15:14:10 +0200497#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 do { \
499 unsigned long _tmp; \
500 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200501 __asm__ __volatile__ ( \
502 _PRE_EFLAGS("0", "3", "2") \
503 _op _suffix " %1; " \
504 _POST_EFLAGS("0", "3", "2") \
505 : "=m" (_eflags), "+m" ((_dst).val), \
506 "=&r" (_tmp) \
507 : "i" (EFLAGS_MASK)); \
508 } while (0)
509
510/* Instruction has only one explicit operand (no source operand). */
511#define emulate_1op(_op, _dst, _eflags) \
512 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400513 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200514 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
515 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
516 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
517 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 } \
519 } while (0)
520
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521/* Fetch next part of the instruction being emulated. */
522#define insn_fetch(_type, _size, _eip) \
523({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200524 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200525 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 goto done; \
527 (_eip) += (_size); \
528 (_type)_x; \
529})
530
Gleb Natapov414e6272010-04-28 19:15:26 +0300531#define insn_fetch_arr(_arr, _size, _eip) \
532({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
533 if (rc != X86EMUL_CONTINUE) \
534 goto done; \
535 (_eip) += (_size); \
536})
537
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800538static inline unsigned long ad_mask(struct decode_cache *c)
539{
540 return (1UL << (c->ad_bytes << 3)) - 1;
541}
542
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800544static inline unsigned long
545address_mask(struct decode_cache *c, unsigned long reg)
546{
547 if (c->ad_bytes == sizeof(unsigned long))
548 return reg;
549 else
550 return reg & ad_mask(c);
551}
552
553static inline unsigned long
554register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
555{
556 return base + address_mask(c, reg);
557}
558
Harvey Harrison7a9572752008-02-19 07:40:41 -0800559static inline void
560register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
561{
562 if (c->ad_bytes == sizeof(unsigned long))
563 *reg += inc;
564 else
565 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
566}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Harvey Harrison7a9572752008-02-19 07:40:41 -0800568static inline void jmp_rel(struct decode_cache *c, int rel)
569{
570 register_address_increment(c, &c->eip, rel);
571}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300572
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300573static void set_seg_override(struct decode_cache *c, int seg)
574{
575 c->has_seg_override = true;
576 c->seg_override = seg;
577}
578
Gleb Natapov79168fd2010-04-28 19:15:30 +0300579static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
580 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300581{
582 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
583 return 0;
584
Gleb Natapov79168fd2010-04-28 19:15:30 +0300585 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300586}
587
588static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300589 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300590 struct decode_cache *c)
591{
592 if (!c->has_seg_override)
593 return 0;
594
Gleb Natapov79168fd2010-04-28 19:15:30 +0300595 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300596}
597
Gleb Natapov79168fd2010-04-28 19:15:30 +0300598static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
599 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300600{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300601 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300602}
603
Gleb Natapov79168fd2010-04-28 19:15:30 +0300604static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
605 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300607 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300608}
609
Gleb Natapov54b84862010-04-28 19:15:44 +0300610static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
611 u32 error, bool valid)
612{
613 ctxt->exception = vec;
614 ctxt->error_code = error;
615 ctxt->error_code_valid = valid;
616 ctxt->restart = false;
617}
618
619static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
620{
621 emulate_exception(ctxt, GP_VECTOR, err, true);
622}
623
624static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
625 int err)
626{
627 ctxt->cr2 = addr;
628 emulate_exception(ctxt, PF_VECTOR, err, true);
629}
630
631static void emulate_ud(struct x86_emulate_ctxt *ctxt)
632{
633 emulate_exception(ctxt, UD_VECTOR, 0, false);
634}
635
636static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
637{
638 emulate_exception(ctxt, TS_VECTOR, err, true);
639}
640
Avi Kivity62266862007-11-20 13:15:52 +0200641static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
642 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300643 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200644{
645 struct fetch_cache *fc = &ctxt->decode.fetch;
646 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300647 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200648
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300649 if (eip == fc->end) {
650 cur_size = fc->end - fc->start;
651 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
652 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
653 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900654 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200655 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300656 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200657 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300658 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900659 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200660}
661
662static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
663 struct x86_emulate_ops *ops,
664 unsigned long eip, void *dest, unsigned size)
665{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900666 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200667
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200668 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200669 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200670 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200671 while (size--) {
672 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900673 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200674 return rc;
675 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900676 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200677}
678
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000679/*
680 * Given the 'reg' portion of a ModRM byte, and a register block, return a
681 * pointer into the block that addresses the relevant register.
682 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
683 */
684static void *decode_register(u8 modrm_reg, unsigned long *regs,
685 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686{
687 void *p;
688
689 p = &regs[modrm_reg];
690 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
691 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
692 return p;
693}
694
695static int read_descriptor(struct x86_emulate_ctxt *ctxt,
696 struct x86_emulate_ops *ops,
697 void *ptr,
698 u16 *size, unsigned long *address, int op_bytes)
699{
700 int rc;
701
702 if (op_bytes == 2)
703 op_bytes = 3;
704 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300705 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200706 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900707 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300709 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200710 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 return rc;
712}
713
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300714static int test_cc(unsigned int condition, unsigned int flags)
715{
716 int rc = 0;
717
718 switch ((condition & 15) >> 1) {
719 case 0: /* o */
720 rc |= (flags & EFLG_OF);
721 break;
722 case 1: /* b/c/nae */
723 rc |= (flags & EFLG_CF);
724 break;
725 case 2: /* z/e */
726 rc |= (flags & EFLG_ZF);
727 break;
728 case 3: /* be/na */
729 rc |= (flags & (EFLG_CF|EFLG_ZF));
730 break;
731 case 4: /* s */
732 rc |= (flags & EFLG_SF);
733 break;
734 case 5: /* p/pe */
735 rc |= (flags & EFLG_PF);
736 break;
737 case 7: /* le/ng */
738 rc |= (flags & EFLG_ZF);
739 /* fall through */
740 case 6: /* l/nge */
741 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
742 break;
743 }
744
745 /* Odd condition identifiers (lsb == 1) have inverted sense. */
746 return (!!rc ^ (condition & 1));
747}
748
Avi Kivity3c118e22007-10-31 10:27:04 +0200749static void decode_register_operand(struct operand *op,
750 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200751 int inhibit_bytereg)
752{
Avi Kivity33615aa2007-10-31 11:15:56 +0200753 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200754 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200755
756 if (!(c->d & ModRM))
757 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200758 op->type = OP_REG;
759 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200760 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200761 op->val = *(u8 *)op->ptr;
762 op->bytes = 1;
763 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200764 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200765 op->bytes = c->op_bytes;
766 switch (op->bytes) {
767 case 2:
768 op->val = *(u16 *)op->ptr;
769 break;
770 case 4:
771 op->val = *(u32 *)op->ptr;
772 break;
773 case 8:
774 op->val = *(u64 *) op->ptr;
775 break;
776 }
777 }
778 op->orig_val = op->val;
779}
780
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200781static int decode_modrm(struct x86_emulate_ctxt *ctxt,
782 struct x86_emulate_ops *ops)
783{
784 struct decode_cache *c = &ctxt->decode;
785 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700786 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900787 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200788
789 if (c->rex_prefix) {
790 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
791 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
792 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
793 }
794
795 c->modrm = insn_fetch(u8, 1, c->eip);
796 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
797 c->modrm_reg |= (c->modrm & 0x38) >> 3;
798 c->modrm_rm |= (c->modrm & 0x07);
799 c->modrm_ea = 0;
800 c->use_modrm_ea = 1;
801
802 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300803 c->modrm_ptr = decode_register(c->modrm_rm,
804 c->regs, c->d & ByteOp);
805 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200806 return rc;
807 }
808
809 if (c->ad_bytes == 2) {
810 unsigned bx = c->regs[VCPU_REGS_RBX];
811 unsigned bp = c->regs[VCPU_REGS_RBP];
812 unsigned si = c->regs[VCPU_REGS_RSI];
813 unsigned di = c->regs[VCPU_REGS_RDI];
814
815 /* 16-bit ModR/M decode. */
816 switch (c->modrm_mod) {
817 case 0:
818 if (c->modrm_rm == 6)
819 c->modrm_ea += insn_fetch(u16, 2, c->eip);
820 break;
821 case 1:
822 c->modrm_ea += insn_fetch(s8, 1, c->eip);
823 break;
824 case 2:
825 c->modrm_ea += insn_fetch(u16, 2, c->eip);
826 break;
827 }
828 switch (c->modrm_rm) {
829 case 0:
830 c->modrm_ea += bx + si;
831 break;
832 case 1:
833 c->modrm_ea += bx + di;
834 break;
835 case 2:
836 c->modrm_ea += bp + si;
837 break;
838 case 3:
839 c->modrm_ea += bp + di;
840 break;
841 case 4:
842 c->modrm_ea += si;
843 break;
844 case 5:
845 c->modrm_ea += di;
846 break;
847 case 6:
848 if (c->modrm_mod != 0)
849 c->modrm_ea += bp;
850 break;
851 case 7:
852 c->modrm_ea += bx;
853 break;
854 }
855 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
856 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300857 if (!c->has_seg_override)
858 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200859 c->modrm_ea = (u16)c->modrm_ea;
860 } else {
861 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700862 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200863 sib = insn_fetch(u8, 1, c->eip);
864 index_reg |= (sib >> 3) & 7;
865 base_reg |= sib & 7;
866 scale = sib >> 6;
867
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700868 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
869 c->modrm_ea += insn_fetch(s32, 4, c->eip);
870 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700872 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700874 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
875 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700876 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700877 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200878 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200879 switch (c->modrm_mod) {
880 case 0:
881 if (c->modrm_rm == 5)
882 c->modrm_ea += insn_fetch(s32, 4, c->eip);
883 break;
884 case 1:
885 c->modrm_ea += insn_fetch(s8, 1, c->eip);
886 break;
887 case 2:
888 c->modrm_ea += insn_fetch(s32, 4, c->eip);
889 break;
890 }
891 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200892done:
893 return rc;
894}
895
896static int decode_abs(struct x86_emulate_ctxt *ctxt,
897 struct x86_emulate_ops *ops)
898{
899 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900900 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200901
902 switch (c->ad_bytes) {
903 case 2:
904 c->modrm_ea = insn_fetch(u16, 2, c->eip);
905 break;
906 case 4:
907 c->modrm_ea = insn_fetch(u32, 4, c->eip);
908 break;
909 case 8:
910 c->modrm_ea = insn_fetch(u64, 8, c->eip);
911 break;
912 }
913done:
914 return rc;
915}
916
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200918x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200920 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900921 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 int mode = ctxt->mode;
Avi Kivity52811d72010-07-26 14:37:48 +0300923 int def_op_bytes, def_ad_bytes, group, dual;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
Gleb Natapov5cd21912010-03-18 15:20:26 +0200926 /* we cannot decode insn before we complete previous rep insn */
927 WARN_ON(ctxt->restart);
928
Gleb Natapov063db062010-03-18 15:20:06 +0200929 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300930 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300931 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
933 switch (mode) {
934 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200935 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200937 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 break;
939 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200940 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800942#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200944 def_op_bytes = 4;
945 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 break;
947#endif
948 default:
949 return -1;
950 }
951
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200952 c->op_bytes = def_op_bytes;
953 c->ad_bytes = def_ad_bytes;
954
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200956 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200957 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200959 /* switch between 2/4 bytes */
960 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 break;
962 case 0x67: /* address-size override */
963 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200965 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200968 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300971 case 0x2e: /* CS override */
972 case 0x36: /* SS override */
973 case 0x3e: /* DS override */
974 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 break;
976 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300978 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200980 case 0x40 ... 0x4f: /* REX */
981 if (mode != X86EMUL_MODE_PROT64)
982 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200983 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200984 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200986 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200988 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100989 c->rep_prefix = REPNE_PREFIX;
990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100992 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 default:
995 goto done_prefixes;
996 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200997
998 /* Any legacy prefix after a REX prefix nullifies its effect. */
999
Avi Kivity33615aa2007-10-31 11:15:56 +02001000 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 }
1002
1003done_prefixes:
1004
1005 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001007 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
1010 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 c->d = opcode_table[c->b];
1012 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 if (c->b == 0x0f) {
1015 c->twobyte = 1;
1016 c->b = insn_fetch(u8, 1, c->eip);
1017 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001019 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
Avi Kivitye09d0822008-01-18 12:38:59 +02001021 if (c->d & Group) {
1022 group = c->d & GroupMask;
Avi Kivity52811d72010-07-26 14:37:48 +03001023 dual = c->d & GroupDual;
Avi Kivitye09d0822008-01-18 12:38:59 +02001024 c->modrm = insn_fetch(u8, 1, c->eip);
1025 --c->eip;
1026
1027 group = (group << 3) + ((c->modrm >> 3) & 7);
Avi Kivity52811d72010-07-26 14:37:48 +03001028 c->d &= ~(Group | GroupDual | GroupMask);
1029 if (dual && (c->modrm >> 6) == 3)
1030 c->d |= group2_table[group];
Avi Kivitye09d0822008-01-18 12:38:59 +02001031 else
Avi Kivity52811d72010-07-26 14:37:48 +03001032 c->d |= group_table[group];
Avi Kivitye09d0822008-01-18 12:38:59 +02001033 }
1034
1035 /* Unrecognised? */
Avi Kivity047a4812010-07-26 14:37:47 +03001036 if (c->d == 0 || (c->d & Undefined)) {
Avi Kivitye09d0822008-01-18 12:38:59 +02001037 DPRINTF("Cannot emulate %02x\n", c->b);
1038 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 }
1040
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001041 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1042 c->op_bytes = 8;
1043
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 if (c->d & ModRM)
1046 rc = decode_modrm(ctxt, ops);
1047 else if (c->d & MemAbs)
1048 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001049 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001050 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001052 if (!c->has_seg_override)
1053 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001054
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001055 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001056 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001057
1058 if (c->ad_bytes != 8)
1059 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001060
1061 if (c->rip_relative)
1062 c->modrm_ea += c->eip;
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 /*
1065 * Decode and fetch the source operand: register, memory
1066 * or immediate.
1067 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001068 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 case SrcNone:
1070 break;
1071 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001072 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 break;
1074 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001075 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 goto srcmem_common;
1077 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001078 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 goto srcmem_common;
1080 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001081 c->src.bytes = (c->d & ByteOp) ? 1 :
1082 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001083 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001084 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001085 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001086 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001087 /*
1088 * For instructions with a ModR/M byte, switch to register
1089 * access if Mod = 3.
1090 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 if ((c->d & ModRM) && c->modrm_mod == 3) {
1092 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001093 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001094 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001095 break;
1096 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001097 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001098 c->src.ptr = (unsigned long *)c->modrm_ea;
1099 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 break;
1101 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001102 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 c->src.type = OP_IMM;
1104 c->src.ptr = (unsigned long *)c->eip;
1105 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1106 if (c->src.bytes == 8)
1107 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001109 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001114 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 break;
1116 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001117 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 break;
1119 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001120 if ((c->d & SrcMask) == SrcImmU) {
1121 switch (c->src.bytes) {
1122 case 1:
1123 c->src.val &= 0xff;
1124 break;
1125 case 2:
1126 c->src.val &= 0xffff;
1127 break;
1128 case 4:
1129 c->src.val &= 0xffffffff;
1130 break;
1131 }
1132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 break;
1134 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001135 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 c->src.type = OP_IMM;
1137 c->src.ptr = (unsigned long *)c->eip;
1138 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001139 if ((c->d & SrcMask) == SrcImmByte)
1140 c->src.val = insn_fetch(s8, 1, c->eip);
1141 else
1142 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08001144 case SrcAcc:
1145 c->src.type = OP_REG;
1146 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1147 c->src.ptr = &c->regs[VCPU_REGS_RAX];
1148 switch (c->src.bytes) {
1149 case 1:
1150 c->src.val = *(u8 *)c->src.ptr;
1151 break;
1152 case 2:
1153 c->src.val = *(u16 *)c->src.ptr;
1154 break;
1155 case 4:
1156 c->src.val = *(u32 *)c->src.ptr;
1157 break;
1158 case 8:
1159 c->src.val = *(u64 *)c->src.ptr;
1160 break;
1161 }
1162 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001163 case SrcOne:
1164 c->src.bytes = 1;
1165 c->src.val = 1;
1166 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001167 case SrcSI:
1168 c->src.type = OP_MEM;
1169 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1170 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001171 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001172 c->regs[VCPU_REGS_RSI]);
1173 c->src.val = 0;
1174 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001175 case SrcImmFAddr:
1176 c->src.type = OP_IMM;
1177 c->src.ptr = (unsigned long *)c->eip;
1178 c->src.bytes = c->op_bytes + 2;
1179 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1180 break;
1181 case SrcMemFAddr:
1182 c->src.type = OP_MEM;
1183 c->src.ptr = (unsigned long *)c->modrm_ea;
1184 c->src.bytes = c->op_bytes + 2;
1185 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 }
1187
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001188 /*
1189 * Decode and fetch the second source operand: register, memory
1190 * or immediate.
1191 */
1192 switch (c->d & Src2Mask) {
1193 case Src2None:
1194 break;
1195 case Src2CL:
1196 c->src2.bytes = 1;
1197 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1198 break;
1199 case Src2ImmByte:
1200 c->src2.type = OP_IMM;
1201 c->src2.ptr = (unsigned long *)c->eip;
1202 c->src2.bytes = 1;
1203 c->src2.val = insn_fetch(u8, 1, c->eip);
1204 break;
1205 case Src2One:
1206 c->src2.bytes = 1;
1207 c->src2.val = 1;
1208 break;
1209 }
1210
Avi Kivity038e51d2007-01-22 20:40:40 -08001211 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001212 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001213 case ImplicitOps:
1214 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001215 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001216 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001217 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001218 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001219 break;
1220 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001221 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001222 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001223 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001224 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001225 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001226 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001227 break;
1228 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001229 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001230 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001231 if ((c->d & DstMask) == DstMem64)
1232 c->dst.bytes = 8;
1233 else
1234 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001235 c->dst.val = 0;
1236 if (c->d & BitOp) {
1237 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1238
1239 c->dst.ptr = (void *)c->dst.ptr +
1240 (c->src.val & mask) / 8;
1241 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001242 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001243 case DstAcc:
1244 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001245 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001246 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001247 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001248 case 1:
1249 c->dst.val = *(u8 *)c->dst.ptr;
1250 break;
1251 case 2:
1252 c->dst.val = *(u16 *)c->dst.ptr;
1253 break;
1254 case 4:
1255 c->dst.val = *(u32 *)c->dst.ptr;
1256 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001257 case 8:
1258 c->dst.val = *(u64 *)c->dst.ptr;
1259 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001260 }
1261 c->dst.orig_val = c->dst.val;
1262 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001263 case DstDI:
1264 c->dst.type = OP_MEM;
1265 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1266 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001267 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001268 c->regs[VCPU_REGS_RDI]);
1269 c->dst.val = 0;
1270 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001271 }
1272
1273done:
1274 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1275}
1276
Gleb Natapov9de41572010-04-28 19:15:22 +03001277static int read_emulated(struct x86_emulate_ctxt *ctxt,
1278 struct x86_emulate_ops *ops,
1279 unsigned long addr, void *dest, unsigned size)
1280{
1281 int rc;
1282 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001283 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001284
1285 while (size) {
1286 int n = min(size, 8u);
1287 size -= n;
1288 if (mc->pos < mc->end)
1289 goto read_cached;
1290
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001291 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1292 ctxt->vcpu);
1293 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001294 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001295 if (rc != X86EMUL_CONTINUE)
1296 return rc;
1297 mc->end += n;
1298
1299 read_cached:
1300 memcpy(dest, mc->data + mc->pos, n);
1301 mc->pos += n;
1302 dest += n;
1303 addr += n;
1304 }
1305 return X86EMUL_CONTINUE;
1306}
1307
Gleb Natapov7b262e92010-03-18 15:20:27 +02001308static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1309 struct x86_emulate_ops *ops,
1310 unsigned int size, unsigned short port,
1311 void *dest)
1312{
1313 struct read_cache *rc = &ctxt->decode.io_read;
1314
1315 if (rc->pos == rc->end) { /* refill pio read ahead */
1316 struct decode_cache *c = &ctxt->decode;
1317 unsigned int in_page, n;
1318 unsigned int count = c->rep_prefix ?
1319 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1320 in_page = (ctxt->eflags & EFLG_DF) ?
1321 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1322 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1323 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1324 count);
1325 if (n == 0)
1326 n = 1;
1327 rc->pos = rc->end = 0;
1328 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1329 return 0;
1330 rc->end = n * size;
1331 }
1332
1333 memcpy(dest, rc->data + rc->pos, size);
1334 rc->pos += size;
1335 return 1;
1336}
1337
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001338static u32 desc_limit_scaled(struct desc_struct *desc)
1339{
1340 u32 limit = get_desc_limit(desc);
1341
1342 return desc->g ? (limit << 12) | 0xfff : limit;
1343}
1344
1345static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1346 struct x86_emulate_ops *ops,
1347 u16 selector, struct desc_ptr *dt)
1348{
1349 if (selector & 1 << 2) {
1350 struct desc_struct desc;
1351 memset (dt, 0, sizeof *dt);
1352 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1353 return;
1354
1355 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1356 dt->address = get_desc_base(&desc);
1357 } else
1358 ops->get_gdt(dt, ctxt->vcpu);
1359}
1360
1361/* allowed just for 8 bytes segments */
1362static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1363 struct x86_emulate_ops *ops,
1364 u16 selector, struct desc_struct *desc)
1365{
1366 struct desc_ptr dt;
1367 u16 index = selector >> 3;
1368 int ret;
1369 u32 err;
1370 ulong addr;
1371
1372 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1373
1374 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001375 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376 return X86EMUL_PROPAGATE_FAULT;
1377 }
1378 addr = dt.address + index * 8;
1379 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1380 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001381 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382
1383 return ret;
1384}
1385
1386/* allowed just for 8 bytes segments */
1387static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1388 struct x86_emulate_ops *ops,
1389 u16 selector, struct desc_struct *desc)
1390{
1391 struct desc_ptr dt;
1392 u16 index = selector >> 3;
1393 u32 err;
1394 ulong addr;
1395 int ret;
1396
1397 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1398
1399 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001400 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 return X86EMUL_PROPAGATE_FAULT;
1402 }
1403
1404 addr = dt.address + index * 8;
1405 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1406 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001407 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408
1409 return ret;
1410}
1411
1412static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1413 struct x86_emulate_ops *ops,
1414 u16 selector, int seg)
1415{
1416 struct desc_struct seg_desc;
1417 u8 dpl, rpl, cpl;
1418 unsigned err_vec = GP_VECTOR;
1419 u32 err_code = 0;
1420 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1421 int ret;
1422
1423 memset(&seg_desc, 0, sizeof seg_desc);
1424
1425 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1426 || ctxt->mode == X86EMUL_MODE_REAL) {
1427 /* set real mode segment descriptor */
1428 set_desc_base(&seg_desc, selector << 4);
1429 set_desc_limit(&seg_desc, 0xffff);
1430 seg_desc.type = 3;
1431 seg_desc.p = 1;
1432 seg_desc.s = 1;
1433 goto load;
1434 }
1435
1436 /* NULL selector is not valid for TR, CS and SS */
1437 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1438 && null_selector)
1439 goto exception;
1440
1441 /* TR should be in GDT only */
1442 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1443 goto exception;
1444
1445 if (null_selector) /* for NULL selector skip all following checks */
1446 goto load;
1447
1448 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1449 if (ret != X86EMUL_CONTINUE)
1450 return ret;
1451
1452 err_code = selector & 0xfffc;
1453 err_vec = GP_VECTOR;
1454
1455 /* can't load system descriptor into segment selecor */
1456 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1457 goto exception;
1458
1459 if (!seg_desc.p) {
1460 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1461 goto exception;
1462 }
1463
1464 rpl = selector & 3;
1465 dpl = seg_desc.dpl;
1466 cpl = ops->cpl(ctxt->vcpu);
1467
1468 switch (seg) {
1469 case VCPU_SREG_SS:
1470 /*
1471 * segment is not a writable data segment or segment
1472 * selector's RPL != CPL or segment selector's RPL != CPL
1473 */
1474 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1475 goto exception;
1476 break;
1477 case VCPU_SREG_CS:
1478 if (!(seg_desc.type & 8))
1479 goto exception;
1480
1481 if (seg_desc.type & 4) {
1482 /* conforming */
1483 if (dpl > cpl)
1484 goto exception;
1485 } else {
1486 /* nonconforming */
1487 if (rpl > cpl || dpl != cpl)
1488 goto exception;
1489 }
1490 /* CS(RPL) <- CPL */
1491 selector = (selector & 0xfffc) | cpl;
1492 break;
1493 case VCPU_SREG_TR:
1494 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1495 goto exception;
1496 break;
1497 case VCPU_SREG_LDTR:
1498 if (seg_desc.s || seg_desc.type != 2)
1499 goto exception;
1500 break;
1501 default: /* DS, ES, FS, or GS */
1502 /*
1503 * segment is not a data or readable code segment or
1504 * ((segment is a data or nonconforming code segment)
1505 * and (both RPL and CPL > DPL))
1506 */
1507 if ((seg_desc.type & 0xa) == 0x8 ||
1508 (((seg_desc.type & 0xc) != 0xc) &&
1509 (rpl > dpl && cpl > dpl)))
1510 goto exception;
1511 break;
1512 }
1513
1514 if (seg_desc.s) {
1515 /* mark segment as accessed */
1516 seg_desc.type |= 1;
1517 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1518 if (ret != X86EMUL_CONTINUE)
1519 return ret;
1520 }
1521load:
1522 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1523 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1524 return X86EMUL_CONTINUE;
1525exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001526 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527 return X86EMUL_PROPAGATE_FAULT;
1528}
1529
Wei Yongjunc37eda12010-06-15 09:03:33 +08001530static inline int writeback(struct x86_emulate_ctxt *ctxt,
1531 struct x86_emulate_ops *ops)
1532{
1533 int rc;
1534 struct decode_cache *c = &ctxt->decode;
1535 u32 err;
1536
1537 switch (c->dst.type) {
1538 case OP_REG:
1539 /* The 4-byte case *is* correct:
1540 * in 64-bit mode we zero-extend.
1541 */
1542 switch (c->dst.bytes) {
1543 case 1:
1544 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1545 break;
1546 case 2:
1547 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1548 break;
1549 case 4:
1550 *c->dst.ptr = (u32)c->dst.val;
1551 break; /* 64b: zero-ext */
1552 case 8:
1553 *c->dst.ptr = c->dst.val;
1554 break;
1555 }
1556 break;
1557 case OP_MEM:
1558 if (c->lock_prefix)
1559 rc = ops->cmpxchg_emulated(
1560 (unsigned long)c->dst.ptr,
1561 &c->dst.orig_val,
1562 &c->dst.val,
1563 c->dst.bytes,
1564 &err,
1565 ctxt->vcpu);
1566 else
1567 rc = ops->write_emulated(
1568 (unsigned long)c->dst.ptr,
1569 &c->dst.val,
1570 c->dst.bytes,
1571 &err,
1572 ctxt->vcpu);
1573 if (rc == X86EMUL_PROPAGATE_FAULT)
1574 emulate_pf(ctxt,
1575 (unsigned long)c->dst.ptr, err);
1576 if (rc != X86EMUL_CONTINUE)
1577 return rc;
1578 break;
1579 case OP_NONE:
1580 /* no writeback */
1581 break;
1582 default:
1583 break;
1584 }
1585 return X86EMUL_CONTINUE;
1586}
1587
Gleb Natapov79168fd2010-04-28 19:15:30 +03001588static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1589 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001590{
1591 struct decode_cache *c = &ctxt->decode;
1592
1593 c->dst.type = OP_MEM;
1594 c->dst.bytes = c->op_bytes;
1595 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001596 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001597 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001598 c->regs[VCPU_REGS_RSP]);
1599}
1600
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001601static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001602 struct x86_emulate_ops *ops,
1603 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001604{
1605 struct decode_cache *c = &ctxt->decode;
1606 int rc;
1607
Gleb Natapov79168fd2010-04-28 19:15:30 +03001608 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001609 c->regs[VCPU_REGS_RSP]),
1610 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001611 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001612 return rc;
1613
Avi Kivity350f69d2009-01-05 11:12:40 +02001614 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001615 return rc;
1616}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001617
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001618static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1619 struct x86_emulate_ops *ops,
1620 void *dest, int len)
1621{
1622 int rc;
1623 unsigned long val, change_mask;
1624 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001625 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001626
1627 rc = emulate_pop(ctxt, ops, &val, len);
1628 if (rc != X86EMUL_CONTINUE)
1629 return rc;
1630
1631 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1632 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1633
1634 switch(ctxt->mode) {
1635 case X86EMUL_MODE_PROT64:
1636 case X86EMUL_MODE_PROT32:
1637 case X86EMUL_MODE_PROT16:
1638 if (cpl == 0)
1639 change_mask |= EFLG_IOPL;
1640 if (cpl <= iopl)
1641 change_mask |= EFLG_IF;
1642 break;
1643 case X86EMUL_MODE_VM86:
1644 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001645 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001646 return X86EMUL_PROPAGATE_FAULT;
1647 }
1648 change_mask |= EFLG_IF;
1649 break;
1650 default: /* real mode */
1651 change_mask |= (EFLG_IOPL | EFLG_IF);
1652 break;
1653 }
1654
1655 *(unsigned long *)dest =
1656 (ctxt->eflags & ~change_mask) | (val & change_mask);
1657
1658 return rc;
1659}
1660
Gleb Natapov79168fd2010-04-28 19:15:30 +03001661static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1662 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001663{
1664 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001665
Gleb Natapov79168fd2010-04-28 19:15:30 +03001666 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001667
Gleb Natapov79168fd2010-04-28 19:15:30 +03001668 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001669}
1670
1671static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1672 struct x86_emulate_ops *ops, int seg)
1673{
1674 struct decode_cache *c = &ctxt->decode;
1675 unsigned long selector;
1676 int rc;
1677
1678 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001679 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001680 return rc;
1681
Gleb Natapov2e873022010-03-18 15:20:18 +02001682 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001683 return rc;
1684}
1685
Wei Yongjunc37eda12010-06-15 09:03:33 +08001686static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001687 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001688{
1689 struct decode_cache *c = &ctxt->decode;
1690 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001691 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001692 int reg = VCPU_REGS_RAX;
1693
1694 while (reg <= VCPU_REGS_RDI) {
1695 (reg == VCPU_REGS_RSP) ?
1696 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1697
Gleb Natapov79168fd2010-04-28 19:15:30 +03001698 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001699
1700 rc = writeback(ctxt, ops);
1701 if (rc != X86EMUL_CONTINUE)
1702 return rc;
1703
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001704 ++reg;
1705 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001706
1707 /* Disable writeback. */
1708 c->dst.type = OP_NONE;
1709
1710 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001711}
1712
1713static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1714 struct x86_emulate_ops *ops)
1715{
1716 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001717 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001718 int reg = VCPU_REGS_RDI;
1719
1720 while (reg >= VCPU_REGS_RAX) {
1721 if (reg == VCPU_REGS_RSP) {
1722 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1723 c->op_bytes);
1724 --reg;
1725 }
1726
1727 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001728 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001729 break;
1730 --reg;
1731 }
1732 return rc;
1733}
1734
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001735static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1736 struct x86_emulate_ops *ops)
1737{
1738 struct decode_cache *c = &ctxt->decode;
1739 int rc = X86EMUL_CONTINUE;
1740 unsigned long temp_eip = 0;
1741 unsigned long temp_eflags = 0;
1742 unsigned long cs = 0;
1743 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1744 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1745 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1746 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1747
1748 /* TODO: Add stack limit check */
1749
1750 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1751
1752 if (rc != X86EMUL_CONTINUE)
1753 return rc;
1754
1755 if (temp_eip & ~0xffff) {
1756 emulate_gp(ctxt, 0);
1757 return X86EMUL_PROPAGATE_FAULT;
1758 }
1759
1760 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1761
1762 if (rc != X86EMUL_CONTINUE)
1763 return rc;
1764
1765 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1766
1767 if (rc != X86EMUL_CONTINUE)
1768 return rc;
1769
1770 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1771
1772 if (rc != X86EMUL_CONTINUE)
1773 return rc;
1774
1775 c->eip = temp_eip;
1776
1777
1778 if (c->op_bytes == 4)
1779 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1780 else if (c->op_bytes == 2) {
1781 ctxt->eflags &= ~0xffff;
1782 ctxt->eflags |= temp_eflags;
1783 }
1784
1785 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1786 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1787
1788 return rc;
1789}
1790
1791static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1792 struct x86_emulate_ops* ops)
1793{
1794 switch(ctxt->mode) {
1795 case X86EMUL_MODE_REAL:
1796 return emulate_iret_real(ctxt, ops);
1797 case X86EMUL_MODE_VM86:
1798 case X86EMUL_MODE_PROT16:
1799 case X86EMUL_MODE_PROT32:
1800 case X86EMUL_MODE_PROT64:
1801 default:
1802 /* iret from protected mode unimplemented yet */
1803 return X86EMUL_UNHANDLEABLE;
1804 }
1805}
1806
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001807static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1808 struct x86_emulate_ops *ops)
1809{
1810 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001811
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001812 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813}
1814
Laurent Vivier05f086f2007-09-24 11:10:55 +02001815static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001816{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001817 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001818 switch (c->modrm_reg) {
1819 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001820 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821 break;
1822 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001823 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001824 break;
1825 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001826 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 break;
1828 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001829 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830 break;
1831 case 4: /* sal/shl */
1832 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001833 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001834 break;
1835 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001836 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001837 break;
1838 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001839 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001840 break;
1841 }
1842}
1843
1844static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001845 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001846{
1847 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001848
1849 switch (c->modrm_reg) {
1850 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001851 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001852 break;
1853 case 2: /* not */
1854 c->dst.val = ~c->dst.val;
1855 break;
1856 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001857 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001858 break;
1859 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001860 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001862 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001863}
1864
1865static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001866 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001867{
1868 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001869
1870 switch (c->modrm_reg) {
1871 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001872 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 break;
1874 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001875 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001876 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001877 case 2: /* call near abs */ {
1878 long int old_eip;
1879 old_eip = c->eip;
1880 c->eip = c->src.val;
1881 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001883 break;
1884 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001885 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001886 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001887 break;
1888 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001889 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001890 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001892 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001893}
1894
1895static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001896 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897{
1898 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001899 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900
1901 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1902 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001903 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1904 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001905 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001907 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1908 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001909
Laurent Vivier05f086f2007-09-24 11:10:55 +02001910 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001911 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001912 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001913}
1914
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001915static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1916 struct x86_emulate_ops *ops)
1917{
1918 struct decode_cache *c = &ctxt->decode;
1919 int rc;
1920 unsigned long cs;
1921
1922 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001923 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001924 return rc;
1925 if (c->op_bytes == 4)
1926 c->eip = (u32)c->eip;
1927 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001928 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001929 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001930 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001931 return rc;
1932}
1933
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001934static inline void
1935setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001936 struct x86_emulate_ops *ops, struct desc_struct *cs,
1937 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001938{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001939 memset(cs, 0, sizeof(struct desc_struct));
1940 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1941 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001942
1943 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001944 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001945 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001946 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001947 cs->type = 0x0b; /* Read, Execute, Accessed */
1948 cs->s = 1;
1949 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001950 cs->p = 1;
1951 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001952
Gleb Natapov79168fd2010-04-28 19:15:30 +03001953 set_desc_base(ss, 0); /* flat segment */
1954 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001955 ss->g = 1; /* 4kb granularity */
1956 ss->s = 1;
1957 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001958 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001959 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001960 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001961}
1962
1963static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001964emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001965{
1966 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001967 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001968 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001969 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001970
1971 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001972 if (ctxt->mode == X86EMUL_MODE_REAL ||
1973 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001974 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001975 return X86EMUL_PROPAGATE_FAULT;
1976 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001977
Gleb Natapov79168fd2010-04-28 19:15:30 +03001978 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001979
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001980 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001981 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 cs_sel = (u16)(msr_data & 0xfffc);
1983 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001984
1985 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001986 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001987 cs.l = 1;
1988 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001989 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1990 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1991 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1992 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001993
1994 c->regs[VCPU_REGS_RCX] = c->eip;
1995 if (is_long_mode(ctxt->vcpu)) {
1996#ifdef CONFIG_X86_64
1997 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1998
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001999 ops->get_msr(ctxt->vcpu,
2000 ctxt->mode == X86EMUL_MODE_PROT64 ?
2001 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002002 c->eip = msr_data;
2003
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002004 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002005 ctxt->eflags &= ~(msr_data | EFLG_RF);
2006#endif
2007 } else {
2008 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002009 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002010 c->eip = (u32)msr_data;
2011
2012 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2013 }
2014
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002015 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002016}
2017
Andre Przywara8c604352009-06-18 12:56:01 +02002018static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002019emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02002020{
2021 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002022 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002023 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02002025
Gleb Natapova0044752010-02-10 14:21:31 +02002026 /* inject #GP if in real mode */
2027 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002028 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002029 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002030 }
2031
2032 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2033 * Therefore, we inject an #UD.
2034 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002035 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002036 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002037 return X86EMUL_PROPAGATE_FAULT;
2038 }
Andre Przywara8c604352009-06-18 12:56:01 +02002039
Gleb Natapov79168fd2010-04-28 19:15:30 +03002040 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002041
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002042 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002043 switch (ctxt->mode) {
2044 case X86EMUL_MODE_PROT32:
2045 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002046 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002047 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002048 }
2049 break;
2050 case X86EMUL_MODE_PROT64:
2051 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002052 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002053 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02002054 }
2055 break;
2056 }
2057
2058 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 cs_sel = (u16)msr_data;
2060 cs_sel &= ~SELECTOR_RPL_MASK;
2061 ss_sel = cs_sel + 8;
2062 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002063 if (ctxt->mode == X86EMUL_MODE_PROT64
2064 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002065 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002066 cs.l = 1;
2067 }
2068
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2070 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2071 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2072 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002073
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002074 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002075 c->eip = msr_data;
2076
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002077 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002078 c->regs[VCPU_REGS_RSP] = msr_data;
2079
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002080 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002081}
2082
Andre Przywara4668f052009-06-18 12:56:02 +02002083static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002084emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002085{
2086 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002087 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002088 u64 msr_data;
2089 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002090 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002091
Gleb Natapova0044752010-02-10 14:21:31 +02002092 /* inject #GP if in real mode or Virtual 8086 mode */
2093 if (ctxt->mode == X86EMUL_MODE_REAL ||
2094 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002095 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002096 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002097 }
2098
Gleb Natapov79168fd2010-04-28 19:15:30 +03002099 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002100
2101 if ((c->rex_prefix & 0x8) != 0x0)
2102 usermode = X86EMUL_MODE_PROT64;
2103 else
2104 usermode = X86EMUL_MODE_PROT32;
2105
2106 cs.dpl = 3;
2107 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002108 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002109 switch (usermode) {
2110 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002111 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002112 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002113 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002114 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002115 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002116 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002117 break;
2118 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002119 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002120 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002121 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002122 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002123 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002124 ss_sel = cs_sel + 8;
2125 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002126 cs.l = 1;
2127 break;
2128 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002129 cs_sel |= SELECTOR_RPL_MASK;
2130 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002131
Gleb Natapov79168fd2010-04-28 19:15:30 +03002132 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2133 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2134 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2135 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002136
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002137 c->eip = c->regs[VCPU_REGS_RDX];
2138 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002139
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002140 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002141}
2142
Gleb Natapov9c537242010-03-18 15:20:05 +02002143static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2144 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002145{
2146 int iopl;
2147 if (ctxt->mode == X86EMUL_MODE_REAL)
2148 return false;
2149 if (ctxt->mode == X86EMUL_MODE_VM86)
2150 return true;
2151 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002152 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002153}
2154
2155static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2156 struct x86_emulate_ops *ops,
2157 u16 port, u16 len)
2158{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002159 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002160 int r;
2161 u16 io_bitmap_ptr;
2162 u8 perm, bit_idx = port & 0x7;
2163 unsigned mask = (1 << len) - 1;
2164
Gleb Natapov79168fd2010-04-28 19:15:30 +03002165 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2166 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002167 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002168 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002169 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002170 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2171 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002172 if (r != X86EMUL_CONTINUE)
2173 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002174 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002175 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002176 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2177 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002178 if (r != X86EMUL_CONTINUE)
2179 return false;
2180 if ((perm >> bit_idx) & mask)
2181 return false;
2182 return true;
2183}
2184
2185static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2186 struct x86_emulate_ops *ops,
2187 u16 port, u16 len)
2188{
Gleb Natapov9c537242010-03-18 15:20:05 +02002189 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002190 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2191 return false;
2192 return true;
2193}
2194
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002195static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2196 struct x86_emulate_ops *ops,
2197 struct tss_segment_16 *tss)
2198{
2199 struct decode_cache *c = &ctxt->decode;
2200
2201 tss->ip = c->eip;
2202 tss->flag = ctxt->eflags;
2203 tss->ax = c->regs[VCPU_REGS_RAX];
2204 tss->cx = c->regs[VCPU_REGS_RCX];
2205 tss->dx = c->regs[VCPU_REGS_RDX];
2206 tss->bx = c->regs[VCPU_REGS_RBX];
2207 tss->sp = c->regs[VCPU_REGS_RSP];
2208 tss->bp = c->regs[VCPU_REGS_RBP];
2209 tss->si = c->regs[VCPU_REGS_RSI];
2210 tss->di = c->regs[VCPU_REGS_RDI];
2211
2212 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2213 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2214 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2215 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2216 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2217}
2218
2219static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2220 struct x86_emulate_ops *ops,
2221 struct tss_segment_16 *tss)
2222{
2223 struct decode_cache *c = &ctxt->decode;
2224 int ret;
2225
2226 c->eip = tss->ip;
2227 ctxt->eflags = tss->flag | 2;
2228 c->regs[VCPU_REGS_RAX] = tss->ax;
2229 c->regs[VCPU_REGS_RCX] = tss->cx;
2230 c->regs[VCPU_REGS_RDX] = tss->dx;
2231 c->regs[VCPU_REGS_RBX] = tss->bx;
2232 c->regs[VCPU_REGS_RSP] = tss->sp;
2233 c->regs[VCPU_REGS_RBP] = tss->bp;
2234 c->regs[VCPU_REGS_RSI] = tss->si;
2235 c->regs[VCPU_REGS_RDI] = tss->di;
2236
2237 /*
2238 * SDM says that segment selectors are loaded before segment
2239 * descriptors
2240 */
2241 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2242 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2243 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2244 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2245 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2246
2247 /*
2248 * Now load segment descriptors. If fault happenes at this stage
2249 * it is handled in a context of new task
2250 */
2251 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2252 if (ret != X86EMUL_CONTINUE)
2253 return ret;
2254 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2255 if (ret != X86EMUL_CONTINUE)
2256 return ret;
2257 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2258 if (ret != X86EMUL_CONTINUE)
2259 return ret;
2260 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2261 if (ret != X86EMUL_CONTINUE)
2262 return ret;
2263 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2264 if (ret != X86EMUL_CONTINUE)
2265 return ret;
2266
2267 return X86EMUL_CONTINUE;
2268}
2269
2270static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2271 struct x86_emulate_ops *ops,
2272 u16 tss_selector, u16 old_tss_sel,
2273 ulong old_tss_base, struct desc_struct *new_desc)
2274{
2275 struct tss_segment_16 tss_seg;
2276 int ret;
2277 u32 err, new_tss_base = get_desc_base(new_desc);
2278
2279 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2280 &err);
2281 if (ret == X86EMUL_PROPAGATE_FAULT) {
2282 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002283 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284 return ret;
2285 }
2286
2287 save_state_to_tss16(ctxt, ops, &tss_seg);
2288
2289 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2290 &err);
2291 if (ret == X86EMUL_PROPAGATE_FAULT) {
2292 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002293 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294 return ret;
2295 }
2296
2297 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2298 &err);
2299 if (ret == X86EMUL_PROPAGATE_FAULT) {
2300 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002301 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002302 return ret;
2303 }
2304
2305 if (old_tss_sel != 0xffff) {
2306 tss_seg.prev_task_link = old_tss_sel;
2307
2308 ret = ops->write_std(new_tss_base,
2309 &tss_seg.prev_task_link,
2310 sizeof tss_seg.prev_task_link,
2311 ctxt->vcpu, &err);
2312 if (ret == X86EMUL_PROPAGATE_FAULT) {
2313 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002314 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315 return ret;
2316 }
2317 }
2318
2319 return load_state_from_tss16(ctxt, ops, &tss_seg);
2320}
2321
2322static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2323 struct x86_emulate_ops *ops,
2324 struct tss_segment_32 *tss)
2325{
2326 struct decode_cache *c = &ctxt->decode;
2327
2328 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2329 tss->eip = c->eip;
2330 tss->eflags = ctxt->eflags;
2331 tss->eax = c->regs[VCPU_REGS_RAX];
2332 tss->ecx = c->regs[VCPU_REGS_RCX];
2333 tss->edx = c->regs[VCPU_REGS_RDX];
2334 tss->ebx = c->regs[VCPU_REGS_RBX];
2335 tss->esp = c->regs[VCPU_REGS_RSP];
2336 tss->ebp = c->regs[VCPU_REGS_RBP];
2337 tss->esi = c->regs[VCPU_REGS_RSI];
2338 tss->edi = c->regs[VCPU_REGS_RDI];
2339
2340 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2341 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2342 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2343 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2344 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2345 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2346 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2347}
2348
2349static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2350 struct x86_emulate_ops *ops,
2351 struct tss_segment_32 *tss)
2352{
2353 struct decode_cache *c = &ctxt->decode;
2354 int ret;
2355
Gleb Natapov0f122442010-04-28 19:15:31 +03002356 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002357 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002358 return X86EMUL_PROPAGATE_FAULT;
2359 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002360 c->eip = tss->eip;
2361 ctxt->eflags = tss->eflags | 2;
2362 c->regs[VCPU_REGS_RAX] = tss->eax;
2363 c->regs[VCPU_REGS_RCX] = tss->ecx;
2364 c->regs[VCPU_REGS_RDX] = tss->edx;
2365 c->regs[VCPU_REGS_RBX] = tss->ebx;
2366 c->regs[VCPU_REGS_RSP] = tss->esp;
2367 c->regs[VCPU_REGS_RBP] = tss->ebp;
2368 c->regs[VCPU_REGS_RSI] = tss->esi;
2369 c->regs[VCPU_REGS_RDI] = tss->edi;
2370
2371 /*
2372 * SDM says that segment selectors are loaded before segment
2373 * descriptors
2374 */
2375 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2376 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2377 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2378 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2379 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2380 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2381 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2382
2383 /*
2384 * Now load segment descriptors. If fault happenes at this stage
2385 * it is handled in a context of new task
2386 */
2387 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2388 if (ret != X86EMUL_CONTINUE)
2389 return ret;
2390 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2391 if (ret != X86EMUL_CONTINUE)
2392 return ret;
2393 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2394 if (ret != X86EMUL_CONTINUE)
2395 return ret;
2396 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2397 if (ret != X86EMUL_CONTINUE)
2398 return ret;
2399 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2400 if (ret != X86EMUL_CONTINUE)
2401 return ret;
2402 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2403 if (ret != X86EMUL_CONTINUE)
2404 return ret;
2405 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2406 if (ret != X86EMUL_CONTINUE)
2407 return ret;
2408
2409 return X86EMUL_CONTINUE;
2410}
2411
2412static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2413 struct x86_emulate_ops *ops,
2414 u16 tss_selector, u16 old_tss_sel,
2415 ulong old_tss_base, struct desc_struct *new_desc)
2416{
2417 struct tss_segment_32 tss_seg;
2418 int ret;
2419 u32 err, new_tss_base = get_desc_base(new_desc);
2420
2421 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2422 &err);
2423 if (ret == X86EMUL_PROPAGATE_FAULT) {
2424 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002425 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426 return ret;
2427 }
2428
2429 save_state_to_tss32(ctxt, ops, &tss_seg);
2430
2431 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2432 &err);
2433 if (ret == X86EMUL_PROPAGATE_FAULT) {
2434 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002435 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 return ret;
2437 }
2438
2439 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2440 &err);
2441 if (ret == X86EMUL_PROPAGATE_FAULT) {
2442 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002443 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002444 return ret;
2445 }
2446
2447 if (old_tss_sel != 0xffff) {
2448 tss_seg.prev_task_link = old_tss_sel;
2449
2450 ret = ops->write_std(new_tss_base,
2451 &tss_seg.prev_task_link,
2452 sizeof tss_seg.prev_task_link,
2453 ctxt->vcpu, &err);
2454 if (ret == X86EMUL_PROPAGATE_FAULT) {
2455 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002456 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 return ret;
2458 }
2459 }
2460
2461 return load_state_from_tss32(ctxt, ops, &tss_seg);
2462}
2463
2464static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002465 struct x86_emulate_ops *ops,
2466 u16 tss_selector, int reason,
2467 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002468{
2469 struct desc_struct curr_tss_desc, next_tss_desc;
2470 int ret;
2471 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2472 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002473 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002474 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475
2476 /* FIXME: old_tss_base == ~0 ? */
2477
2478 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2479 if (ret != X86EMUL_CONTINUE)
2480 return ret;
2481 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2482 if (ret != X86EMUL_CONTINUE)
2483 return ret;
2484
2485 /* FIXME: check that next_tss_desc is tss */
2486
2487 if (reason != TASK_SWITCH_IRET) {
2488 if ((tss_selector & 3) > next_tss_desc.dpl ||
2489 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002490 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 return X86EMUL_PROPAGATE_FAULT;
2492 }
2493 }
2494
Gleb Natapovceffb452010-03-18 15:20:19 +02002495 desc_limit = desc_limit_scaled(&next_tss_desc);
2496 if (!next_tss_desc.p ||
2497 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2498 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002499 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500 return X86EMUL_PROPAGATE_FAULT;
2501 }
2502
2503 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2504 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2505 write_segment_descriptor(ctxt, ops, old_tss_sel,
2506 &curr_tss_desc);
2507 }
2508
2509 if (reason == TASK_SWITCH_IRET)
2510 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2511
2512 /* set back link to prev task only if NT bit is set in eflags
2513 note that old_tss_sel is not used afetr this point */
2514 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2515 old_tss_sel = 0xffff;
2516
2517 if (next_tss_desc.type & 8)
2518 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2519 old_tss_base, &next_tss_desc);
2520 else
2521 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2522 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002523 if (ret != X86EMUL_CONTINUE)
2524 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525
2526 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2527 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2528
2529 if (reason != TASK_SWITCH_IRET) {
2530 next_tss_desc.type |= (1 << 1); /* set busy flag */
2531 write_segment_descriptor(ctxt, ops, tss_selector,
2532 &next_tss_desc);
2533 }
2534
2535 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2536 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2537 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2538
Jan Kiszkae269fb22010-04-14 15:51:09 +02002539 if (has_error_code) {
2540 struct decode_cache *c = &ctxt->decode;
2541
2542 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2543 c->lock_prefix = 0;
2544 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002545 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002546 }
2547
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548 return ret;
2549}
2550
2551int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2552 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002553 u16 tss_selector, int reason,
2554 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555{
2556 struct decode_cache *c = &ctxt->decode;
2557 int rc;
2558
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002560 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561
Jan Kiszkae269fb22010-04-14 15:51:09 +02002562 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2563 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564
2565 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002566 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002567 if (rc == X86EMUL_CONTINUE)
2568 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 }
2570
Gleb Natapov19d04432010-04-15 12:29:50 +03002571 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572}
2573
Gleb Natapova682e352010-03-18 15:20:21 +02002574static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002575 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002576{
2577 struct decode_cache *c = &ctxt->decode;
2578 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2579
Gleb Natapovd9271122010-03-18 15:20:22 +02002580 register_address_increment(c, &c->regs[reg], df * op->bytes);
2581 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002582}
2583
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002584int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002585x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002586{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002587 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002588 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002589 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002590 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002591
Gleb Natapov9de41572010-04-28 19:15:22 +03002592 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002593
Gleb Natapov11616242010-02-11 14:43:14 +02002594 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002595 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002596 goto done;
2597 }
2598
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002599 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002600 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002601 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002602 goto done;
2603 }
2604
Gleb Natapove92805a2010-02-10 14:21:35 +02002605 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002606 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002607 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002608 goto done;
2609 }
2610
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002611 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002612 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002613 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002614 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002615 string_done:
2616 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002617 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002618 goto done;
2619 }
2620 /* The second termination condition only applies for REPE
2621 * and REPNE. Test if the repeat string operation prefix is
2622 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2623 * corresponding termination condition according to:
2624 * - if REPE/REPZ and ZF = 0 then done
2625 * - if REPNE/REPNZ and ZF = 1 then done
2626 */
2627 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002628 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002629 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002630 ((ctxt->eflags & EFLG_ZF) == 0))
2631 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002632 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002633 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2634 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002635 }
Gleb Natapov063db062010-03-18 15:20:06 +02002636 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002637 }
2638
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002639 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002640 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002641 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002642 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002643 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002644 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002645 }
2646
Gleb Natapove35b7b92010-02-25 16:36:42 +02002647 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002648 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2649 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002650 if (rc != X86EMUL_CONTINUE)
2651 goto done;
2652 }
2653
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002654 if ((c->d & DstMask) == ImplicitOps)
2655 goto special_insn;
2656
2657
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002658 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2659 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002660 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2661 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002662 if (rc != X86EMUL_CONTINUE)
2663 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002664 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002665 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002666
Avi Kivity018a98d2007-11-27 19:30:56 +02002667special_insn:
2668
Laurent Viviere4e03de2007-09-18 11:52:50 +02002669 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 goto twobyte_insn;
2671
Laurent Viviere4e03de2007-09-18 11:52:50 +02002672 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 case 0x00 ... 0x05:
2674 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002675 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002677 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002678 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002679 break;
2680 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002681 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002682 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002683 goto done;
2684 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 case 0x08 ... 0x0d:
2686 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002687 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002689 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002690 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 case 0x10 ... 0x15:
2693 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002694 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002696 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002697 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002698 break;
2699 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002700 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002701 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002702 goto done;
2703 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 case 0x18 ... 0x1d:
2705 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002706 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002708 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002709 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002710 break;
2711 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002712 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002713 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002714 goto done;
2715 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002716 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002718 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 break;
2720 case 0x28 ... 0x2d:
2721 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002722 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 break;
2724 case 0x30 ... 0x35:
2725 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002726 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 break;
2728 case 0x38 ... 0x3d:
2729 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002730 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002732 case 0x40 ... 0x47: /* inc r16/r32 */
2733 emulate_1op("inc", c->dst, ctxt->eflags);
2734 break;
2735 case 0x48 ... 0x4f: /* dec r16/r32 */
2736 emulate_1op("dec", c->dst, ctxt->eflags);
2737 break;
2738 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002739 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002740 break;
2741 case 0x58 ... 0x5f: /* pop reg */
2742 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002743 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002744 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002745 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002746 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002747 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002748 rc = emulate_pusha(ctxt, ops);
2749 if (rc != X86EMUL_CONTINUE)
2750 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002751 break;
2752 case 0x61: /* popa */
2753 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002754 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002755 goto done;
2756 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002758 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002760 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002762 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002763 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002764 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002765 break;
2766 case 0x6c: /* insb */
2767 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002768 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002769 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002770 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002771 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002772 goto done;
2773 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002774 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2775 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002776 goto done; /* IO is needed, skip writeback */
2777 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002778 case 0x6e: /* outsb */
2779 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002780 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002781 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002782 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002783 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002784 goto done;
2785 }
Gleb Natapov79729952010-03-18 15:20:24 +02002786 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2787 &c->src.val, 1, ctxt->vcpu);
2788
2789 c->dst.type = OP_NONE; /* nothing to writeback */
2790 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002791 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002792 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002793 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002794 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002796 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 case 0:
2798 goto add;
2799 case 1:
2800 goto or;
2801 case 2:
2802 goto adc;
2803 case 3:
2804 goto sbb;
2805 case 4:
2806 goto and;
2807 case 5:
2808 goto sub;
2809 case 6:
2810 goto xor;
2811 case 7:
2812 goto cmp;
2813 }
2814 break;
2815 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002816 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002817 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 break;
2819 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002820 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002822 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002824 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 break;
2826 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002827 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 break;
2829 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002830 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 break; /* 64b reg: zero-extend */
2832 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002833 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 break;
2835 }
2836 /*
2837 * Write back the memory destination with implicit LOCK
2838 * prefix.
2839 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002840 c->dst.val = c->src.val;
2841 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002844 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002845 case 0x8c: /* mov r/m, sreg */
2846 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002847 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002848 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002849 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002850 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002851 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002852 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002853 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002854 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002855 case 0x8e: { /* mov seg, r/m16 */
2856 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002857
2858 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002859
Gleb Natapovc6975182010-02-18 12:15:01 +02002860 if (c->modrm_reg == VCPU_SREG_CS ||
2861 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002862 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002863 goto done;
2864 }
2865
Glauber Costa310b5d32009-05-12 16:21:06 -04002866 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002867 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002868
Gleb Natapov2e873022010-03-18 15:20:18 +02002869 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002870
2871 c->dst.type = OP_NONE; /* Disable writeback. */
2872 break;
2873 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002875 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002876 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002879 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002880 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2881 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002882 break;
2883 }
2884 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002885 c->src.type = OP_REG;
2886 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002887 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2888 c->src.val = *(c->src.ptr);
2889 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002890 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002891 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002892 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002893 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002894 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002895 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002896 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002897 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002898 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2899 if (rc != X86EMUL_CONTINUE)
2900 goto done;
2901 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002902 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002904 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002906 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002907 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002908 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002909 case 0xa8 ... 0xa9: /* test ax, imm */
2910 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002912 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 break;
2914 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002915 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 case 0xae ... 0xaf: /* scas */
2917 DPRINTF("Urk! I don't handle SCAS.\n");
2918 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002919 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002920 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002921 case 0xc0 ... 0xc1:
2922 emulate_grp2(ctxt);
2923 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002924 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002925 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002926 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002927 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002928 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002929 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2930 mov:
2931 c->dst.val = c->src.val;
2932 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002933 case 0xcb: /* ret far */
2934 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002935 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002936 goto done;
2937 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002938 case 0xcf: /* iret */
2939 rc = emulate_iret(ctxt, ops);
2940
2941 if (rc != X86EMUL_CONTINUE)
2942 goto done;
2943 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002944 case 0xd0 ... 0xd1: /* Grp2 */
2945 c->src.val = 1;
2946 emulate_grp2(ctxt);
2947 break;
2948 case 0xd2 ... 0xd3: /* Grp2 */
2949 c->src.val = c->regs[VCPU_REGS_RCX];
2950 emulate_grp2(ctxt);
2951 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002952 case 0xe4: /* inb */
2953 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002954 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002955 case 0xe6: /* outb */
2956 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002957 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002958 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002959 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002960 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002961 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002962 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002963 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002964 }
2965 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002966 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002967 case 0xea: { /* jmp far */
2968 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002969 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002970 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2971
2972 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002973 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002974
Gleb Natapov414e6272010-04-28 19:15:26 +03002975 c->eip = 0;
2976 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002977 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002978 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002979 case 0xeb:
2980 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002981 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002982 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002983 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002984 case 0xec: /* in al,dx */
2985 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002986 c->src.val = c->regs[VCPU_REGS_RDX];
2987 do_io_in:
2988 c->dst.bytes = min(c->dst.bytes, 4u);
2989 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002990 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002991 goto done;
2992 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002993 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2994 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002995 goto done; /* IO is needed */
2996 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08002997 case 0xee: /* out dx,al */
2998 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002999 c->src.val = c->regs[VCPU_REGS_RDX];
3000 do_io_out:
3001 c->dst.bytes = min(c->dst.bytes, 4u);
3002 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003003 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003004 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003005 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003006 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3007 ctxt->vcpu);
3008 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003009 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003010 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003011 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003012 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003013 case 0xf5: /* cmc */
3014 /* complement carry flag from eflags reg */
3015 ctxt->eflags ^= EFLG_CF;
3016 c->dst.type = OP_NONE; /* Disable writeback. */
3017 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003018 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003019 if (!emulate_grp3(ctxt, ops))
3020 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003021 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003022 case 0xf8: /* clc */
3023 ctxt->eflags &= ~EFLG_CF;
3024 c->dst.type = OP_NONE; /* Disable writeback. */
3025 break;
3026 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003027 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003028 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003029 goto done;
3030 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003031 ctxt->eflags &= ~X86_EFLAGS_IF;
3032 c->dst.type = OP_NONE; /* Disable writeback. */
3033 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003034 break;
3035 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003036 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003037 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003038 goto done;
3039 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003040 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003041 ctxt->eflags |= X86_EFLAGS_IF;
3042 c->dst.type = OP_NONE; /* Disable writeback. */
3043 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003044 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003045 case 0xfc: /* cld */
3046 ctxt->eflags &= ~EFLG_DF;
3047 c->dst.type = OP_NONE; /* Disable writeback. */
3048 break;
3049 case 0xfd: /* std */
3050 ctxt->eflags |= EFLG_DF;
3051 c->dst.type = OP_NONE; /* Disable writeback. */
3052 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003053 case 0xfe: /* Grp4 */
3054 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003055 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003056 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003057 goto done;
3058 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003059 case 0xff: /* Grp5 */
3060 if (c->modrm_reg == 5)
3061 goto jump_far;
3062 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003063 default:
3064 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003066
3067writeback:
3068 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003069 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003070 goto done;
3071
Gleb Natapov5cd21912010-03-18 15:20:26 +02003072 /*
3073 * restore dst type in case the decoding will be reused
3074 * (happens for string instruction )
3075 */
3076 c->dst.type = saved_dst_type;
3077
Gleb Natapova682e352010-03-18 15:20:21 +02003078 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003079 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3080 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003081
3082 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003083 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3084 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003085
Gleb Natapov5cd21912010-03-18 15:20:26 +02003086 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003087 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003088 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003089 /*
3090 * Re-enter guest when pio read ahead buffer is empty or,
3091 * if it is not used, after each 1024 iteration.
3092 */
3093 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3094 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003095 ctxt->restart = false;
3096 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003097 /*
3098 * reset read cache here in case string instruction is restared
3099 * without decoding
3100 */
3101 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003102 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003103
3104done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003105 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106
3107twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003108 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003110 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 u16 size;
3112 unsigned long address;
3113
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003114 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003115 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003116 goto cannot_emulate;
3117
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003118 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003119 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003120 goto done;
3121
Avi Kivity33e38852008-05-21 15:34:25 +03003122 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003123 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003124 /* Disable writeback. */
3125 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003126 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003128 rc = read_descriptor(ctxt, ops, c->src.ptr,
3129 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003130 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 goto done;
3132 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003133 /* Disable writeback. */
3134 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003136 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003137 if (c->modrm_mod == 3) {
3138 switch (c->modrm_rm) {
3139 case 1:
3140 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003141 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003142 goto done;
3143 break;
3144 default:
3145 goto cannot_emulate;
3146 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003147 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003148 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003149 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003150 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003151 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003152 goto done;
3153 realmode_lidt(ctxt->vcpu, size, address);
3154 }
Avi Kivity16286d02008-04-14 14:40:50 +03003155 /* Disable writeback. */
3156 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 break;
3158 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003159 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003160 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 break;
3162 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003163 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3164 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003165 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003167 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003168 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003169 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003171 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003172 /* Disable writeback. */
3173 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
3175 default:
3176 goto cannot_emulate;
3177 }
3178 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003179 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003180 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003181 if (rc != X86EMUL_CONTINUE)
3182 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003183 else
3184 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003185 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003186 case 0x06:
3187 emulate_clts(ctxt->vcpu);
3188 c->dst.type = OP_NONE;
3189 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003190 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003191 kvm_emulate_wbinvd(ctxt->vcpu);
3192 c->dst.type = OP_NONE;
3193 break;
3194 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003195 case 0x0d: /* GrpP (prefetch) */
3196 case 0x18: /* Grp16 (prefetch/nop) */
3197 c->dst.type = OP_NONE;
3198 break;
3199 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003200 switch (c->modrm_reg) {
3201 case 1:
3202 case 5 ... 7:
3203 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003204 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003205 goto done;
3206 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003207 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003208 c->dst.type = OP_NONE; /* no writeback */
3209 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003211 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3212 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003213 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003214 goto done;
3215 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003216 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003217 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003219 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003220 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003221 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003222 goto done;
3223 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003224 c->dst.type = OP_NONE;
3225 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003227 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3228 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003229 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003230 goto done;
3231 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003232
Gleb Natapov338dbc92010-04-28 19:15:32 +03003233 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3234 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3235 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3236 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003237 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003238 goto done;
3239 }
3240
Laurent Viviera01af5e2007-09-24 11:10:56 +02003241 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003243 case 0x30:
3244 /* wrmsr */
3245 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3246 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003247 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003248 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003249 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003250 }
3251 rc = X86EMUL_CONTINUE;
3252 c->dst.type = OP_NONE;
3253 break;
3254 case 0x32:
3255 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003256 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003257 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003258 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003259 } else {
3260 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3261 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3262 }
3263 rc = X86EMUL_CONTINUE;
3264 c->dst.type = OP_NONE;
3265 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003266 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003267 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003268 if (rc != X86EMUL_CONTINUE)
3269 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003270 else
3271 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003272 break;
3273 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003274 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003275 if (rc != X86EMUL_CONTINUE)
3276 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003277 else
3278 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003281 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003282 if (!test_cc(c->b, ctxt->eflags))
3283 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003285 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003286 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003287 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003288 c->dst.type = OP_NONE;
3289 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003290 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003291 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003292 break;
3293 case 0xa1: /* pop fs */
3294 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003295 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003296 goto done;
3297 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003298 case 0xa3:
3299 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003300 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003301 /* only subword offset */
3302 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003303 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003304 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003305 case 0xa4: /* shld imm8, r, r/m */
3306 case 0xa5: /* shld cl, r, r/m */
3307 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3308 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003309 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003310 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003311 break;
3312 case 0xa9: /* pop gs */
3313 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003314 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003315 goto done;
3316 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003317 case 0xab:
3318 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003319 /* only subword offset */
3320 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003321 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003322 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003323 case 0xac: /* shrd imm8, r, r/m */
3324 case 0xad: /* shrd cl, r, r/m */
3325 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3326 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003327 case 0xae: /* clflush */
3328 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 case 0xb0 ... 0xb1: /* cmpxchg */
3330 /*
3331 * Save real source value, then compare EAX against
3332 * destination.
3333 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003334 c->src.orig_val = c->src.val;
3335 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003336 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3337 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003339 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 } else {
3341 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003342 c->dst.type = OP_REG;
3343 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 }
3345 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346 case 0xb3:
3347 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003348 /* only subword offset */
3349 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003350 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003353 c->dst.bytes = c->op_bytes;
3354 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3355 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003358 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 case 0:
3360 goto bt;
3361 case 1:
3362 goto bts;
3363 case 2:
3364 goto btr;
3365 case 3:
3366 goto btc;
3367 }
3368 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003369 case 0xbb:
3370 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003371 /* only subword offset */
3372 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003373 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003374 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003376 c->dst.bytes = c->op_bytes;
3377 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3378 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003380 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003381 c->dst.bytes = c->op_bytes;
3382 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3383 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003386 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003387 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003388 goto done;
3389 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003390 default:
3391 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392 }
3393 goto writeback;
3394
3395cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003396 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 return -1;
3398}