blob: 4dade6ac08276537b07824ab7c23224d93946905 [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
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020079#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020080#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082/* Source 2 operand type */
83#define Src2None (0<<29)
84#define Src2CL (1<<29)
85#define Src2ImmByte (2<<29)
86#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030087#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010088#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089
Avi Kivity43bb19c2008-01-18 12:46:50 +020090enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020091 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020092 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020093 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020094};
95
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010096static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080097 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020098 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300100 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300101 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200103 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200105 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
106 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200108 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300110 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300111 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 /* 0x18 - 0x1F */
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 /* 0x20 - 0x27 */
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,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200120 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200122 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
124 0, 0, 0, 0,
125 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200126 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
128 0, 0, 0, 0,
129 /* 0x38 - 0x3F */
130 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
131 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200132 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
133 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700134 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200135 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700136 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200137 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300138 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200139 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
140 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300141 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200142 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
143 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700144 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200145 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
146 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700147 0, 0, 0, 0,
148 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300149 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300150 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
151 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300152 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300153 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
154 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300155 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300156 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
157 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200159 Group | Group1_80, Group | Group1_81,
160 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200162 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 /* 0x88 - 0x8F */
164 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
165 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200166 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200167 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300168 /* 0x90 - 0x97 */
169 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
170 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300171 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300172 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200174 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
175 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200176 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
177 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200179 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
180 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
181 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300182 /* 0xB0 - 0xB7 */
183 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
184 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
185 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
186 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
187 /* 0xB8 - 0xBF */
188 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
189 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
190 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
191 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300193 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200194 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300195 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300197 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300198 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xD0 - 0xD7 */
200 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
201 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
202 0, 0, 0, 0,
203 /* 0xD8 - 0xDF */
204 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300205 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300206 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300207 ByteOp | SrcImmUByte, SrcImmUByte,
208 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300209 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300210 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300211 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300212 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
213 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0xF0 - 0xF7 */
215 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200216 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700218 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300219 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220};
221
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100222static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200224 0, Group | GroupDual | Group7, 0, 0,
225 0, ImplicitOps, ImplicitOps | Priv, 0,
226 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
227 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x10 - 0x1F */
229 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
230 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200231 ModRM | ImplicitOps | Priv, ModRM | Priv,
232 ModRM | ImplicitOps | Priv, ModRM | Priv,
233 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 0, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200236 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
237 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200238 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 /* 0x40 - 0x47 */
240 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
241 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
242 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
243 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
244 /* 0x48 - 0x4F */
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 /* 0x50 - 0x5F */
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0x60 - 0x6F */
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
253 /* 0x70 - 0x7F */
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300256 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
257 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 /* 0x90 - 0x9F */
259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300261 ImplicitOps | Stack, ImplicitOps | Stack,
262 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100263 DstMem | SrcReg | Src2ImmByte | ModRM,
264 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300266 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200267 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100268 DstMem | SrcReg | Src2ImmByte | ModRM,
269 DstMem | SrcReg | Src2CL | ModRM,
270 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200272 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
273 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
275 DstReg | SrcMem16 | ModRM | Mov,
276 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200277 0, 0,
278 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
280 DstReg | SrcMem16 | ModRM | Mov,
281 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200282 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
283 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800284 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 /* 0xD0 - 0xDF */
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 /* 0xE0 - 0xEF */
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 /* 0xF0 - 0xFF */
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
291};
292
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100293static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200294 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200295 ByteOp | DstMem | SrcImm | ModRM | Lock,
296 ByteOp | DstMem | SrcImm | ModRM | Lock,
297 ByteOp | DstMem | SrcImm | ModRM | Lock,
298 ByteOp | DstMem | SrcImm | ModRM | Lock,
299 ByteOp | DstMem | SrcImm | ModRM | Lock,
300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200303 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200304 DstMem | SrcImm | ModRM | Lock,
305 DstMem | SrcImm | ModRM | Lock,
306 DstMem | SrcImm | ModRM | Lock,
307 DstMem | SrcImm | ModRM | Lock,
308 DstMem | SrcImm | ModRM | Lock,
309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200312 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200313 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
314 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
315 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
316 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
317 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
318 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200321 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200322 DstMem | SrcImmByte | ModRM | Lock,
323 DstMem | SrcImmByte | ModRM | Lock,
324 DstMem | SrcImmByte | ModRM | Lock,
325 DstMem | SrcImmByte | ModRM | Lock,
326 DstMem | SrcImmByte | ModRM | Lock,
327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200330 [Group1A*8] =
331 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200332 [Group3_Byte*8] =
333 ByteOp | SrcImm | DstMem | ModRM, 0,
334 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
335 0, 0, 0, 0,
336 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400337 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300338 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200339 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200340 [Group4*8] =
341 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
342 0, 0, 0, 0, 0, 0,
343 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300344 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
345 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300346 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200347 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200348 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300349 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200350 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200351 [Group8*8] =
352 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200353 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
354 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200355 [Group9*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200356 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200357};
358
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100359static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200360 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200361 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300362 SrcNone | ModRM | DstMem | Mov, 0,
363 SrcMem16 | ModRM | Mov, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200364 [Group9*8] =
365 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200366};
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200369#define EFLG_ID (1<<21)
370#define EFLG_VIP (1<<20)
371#define EFLG_VIF (1<<19)
372#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200373#define EFLG_VM (1<<17)
374#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200375#define EFLG_IOPL (3<<12)
376#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377#define EFLG_OF (1<<11)
378#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200379#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200380#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381#define EFLG_SF (1<<7)
382#define EFLG_ZF (1<<6)
383#define EFLG_AF (1<<4)
384#define EFLG_PF (1<<2)
385#define EFLG_CF (1<<0)
386
387/*
388 * Instruction emulation:
389 * Most instructions are emulated directly via a fragment of inline assembly
390 * code. This allows us to save/restore EFLAGS and thus very easily pick up
391 * any modified flags.
392 */
393
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800394#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395#define _LO32 "k" /* force 32-bit operand */
396#define _STK "%%rsp" /* stack pointer */
397#elif defined(__i386__)
398#define _LO32 "" /* force 32-bit operand */
399#define _STK "%%esp" /* stack pointer */
400#endif
401
402/*
403 * These EFLAGS bits are restored from saved value during emulation, and
404 * any changes are written back to the saved value after emulation.
405 */
406#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
407
408/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200409#define _PRE_EFLAGS(_sav, _msk, _tmp) \
410 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
411 "movl %"_sav",%"_LO32 _tmp"; " \
412 "push %"_tmp"; " \
413 "push %"_tmp"; " \
414 "movl %"_msk",%"_LO32 _tmp"; " \
415 "andl %"_LO32 _tmp",("_STK"); " \
416 "pushf; " \
417 "notl %"_LO32 _tmp"; " \
418 "andl %"_LO32 _tmp",("_STK"); " \
419 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
420 "pop %"_tmp"; " \
421 "orl %"_LO32 _tmp",("_STK"); " \
422 "popf; " \
423 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
425/* After executing instruction: write-back necessary bits in EFLAGS. */
426#define _POST_EFLAGS(_sav, _msk, _tmp) \
427 /* _sav |= EFLAGS & _msk; */ \
428 "pushf; " \
429 "pop %"_tmp"; " \
430 "andl %"_msk",%"_LO32 _tmp"; " \
431 "orl %"_LO32 _tmp",%"_sav"; "
432
Avi Kivitydda96d82008-11-26 15:14:10 +0200433#ifdef CONFIG_X86_64
434#define ON64(x) x
435#else
436#define ON64(x)
437#endif
438
Avi Kivity6b7ad612008-11-26 15:30:45 +0200439#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
440 do { \
441 __asm__ __volatile__ ( \
442 _PRE_EFLAGS("0", "4", "2") \
443 _op _suffix " %"_x"3,%1; " \
444 _POST_EFLAGS("0", "4", "2") \
445 : "=m" (_eflags), "=m" ((_dst).val), \
446 "=&r" (_tmp) \
447 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200448 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200449
450
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451/* Raw emulation: instruction has two explicit operands. */
452#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200453 do { \
454 unsigned long _tmp; \
455 \
456 switch ((_dst).bytes) { \
457 case 2: \
458 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
459 break; \
460 case 4: \
461 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
462 break; \
463 case 8: \
464 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
465 break; \
466 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 } while (0)
468
469#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
470 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200471 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400472 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200474 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 break; \
476 default: \
477 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
478 _wx, _wy, _lx, _ly, _qx, _qy); \
479 break; \
480 } \
481 } while (0)
482
483/* Source operand is byte-sized and may be restricted to just %cl. */
484#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
485 __emulate_2op(_op, _src, _dst, _eflags, \
486 "b", "c", "b", "c", "b", "c", "b", "c")
487
488/* Source operand is byte, word, long or quad sized. */
489#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
490 __emulate_2op(_op, _src, _dst, _eflags, \
491 "b", "q", "w", "r", _LO32, "r", "", "r")
492
493/* Source operand is word, long or quad sized. */
494#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
495 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
496 "w", "r", _LO32, "r", "", "r")
497
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100498/* Instruction has three operands and one operand is stored in ECX register */
499#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
500 do { \
501 unsigned long _tmp; \
502 _type _clv = (_cl).val; \
503 _type _srcv = (_src).val; \
504 _type _dstv = (_dst).val; \
505 \
506 __asm__ __volatile__ ( \
507 _PRE_EFLAGS("0", "5", "2") \
508 _op _suffix " %4,%1 \n" \
509 _POST_EFLAGS("0", "5", "2") \
510 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
511 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
512 ); \
513 \
514 (_cl).val = (unsigned long) _clv; \
515 (_src).val = (unsigned long) _srcv; \
516 (_dst).val = (unsigned long) _dstv; \
517 } while (0)
518
519#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
520 do { \
521 switch ((_dst).bytes) { \
522 case 2: \
523 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
524 "w", unsigned short); \
525 break; \
526 case 4: \
527 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
528 "l", unsigned int); \
529 break; \
530 case 8: \
531 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
532 "q", unsigned long)); \
533 break; \
534 } \
535 } while (0)
536
Avi Kivitydda96d82008-11-26 15:14:10 +0200537#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 do { \
539 unsigned long _tmp; \
540 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200541 __asm__ __volatile__ ( \
542 _PRE_EFLAGS("0", "3", "2") \
543 _op _suffix " %1; " \
544 _POST_EFLAGS("0", "3", "2") \
545 : "=m" (_eflags), "+m" ((_dst).val), \
546 "=&r" (_tmp) \
547 : "i" (EFLAGS_MASK)); \
548 } while (0)
549
550/* Instruction has only one explicit operand (no source operand). */
551#define emulate_1op(_op, _dst, _eflags) \
552 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400553 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200554 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
555 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
556 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
557 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 } \
559 } while (0)
560
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561/* Fetch next part of the instruction being emulated. */
562#define insn_fetch(_type, _size, _eip) \
563({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200564 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400565 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 goto done; \
567 (_eip) += (_size); \
568 (_type)_x; \
569})
570
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800571static inline unsigned long ad_mask(struct decode_cache *c)
572{
573 return (1UL << (c->ad_bytes << 3)) - 1;
574}
575
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800577static inline unsigned long
578address_mask(struct decode_cache *c, unsigned long reg)
579{
580 if (c->ad_bytes == sizeof(unsigned long))
581 return reg;
582 else
583 return reg & ad_mask(c);
584}
585
586static inline unsigned long
587register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
588{
589 return base + address_mask(c, reg);
590}
591
Harvey Harrison7a9572752008-02-19 07:40:41 -0800592static inline void
593register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
594{
595 if (c->ad_bytes == sizeof(unsigned long))
596 *reg += inc;
597 else
598 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
599}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600
Harvey Harrison7a9572752008-02-19 07:40:41 -0800601static inline void jmp_rel(struct decode_cache *c, int rel)
602{
603 register_address_increment(c, &c->eip, rel);
604}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300605
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606static void set_seg_override(struct decode_cache *c, int seg)
607{
608 c->has_seg_override = true;
609 c->seg_override = seg;
610}
611
612static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
613{
614 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
615 return 0;
616
617 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
618}
619
620static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
621 struct decode_cache *c)
622{
623 if (!c->has_seg_override)
624 return 0;
625
626 return seg_base(ctxt, c->seg_override);
627}
628
629static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
630{
631 return seg_base(ctxt, VCPU_SREG_ES);
632}
633
634static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
635{
636 return seg_base(ctxt, VCPU_SREG_SS);
637}
638
Avi Kivity62266862007-11-20 13:15:52 +0200639static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
640 struct x86_emulate_ops *ops,
641 unsigned long linear, u8 *dest)
642{
643 struct fetch_cache *fc = &ctxt->decode.fetch;
644 int rc;
645 int size;
646
647 if (linear < fc->start || linear >= fc->end) {
648 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200649 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Avi Kivity62266862007-11-20 13:15:52 +0200650 if (rc)
651 return rc;
652 fc->start = linear;
653 fc->end = linear + size;
654 }
655 *dest = fc->data[linear - fc->start];
656 return 0;
657}
658
659static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
660 struct x86_emulate_ops *ops,
661 unsigned long eip, void *dest, unsigned size)
662{
663 int rc = 0;
664
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200665 /* x86 instructions are limited to 15 bytes. */
666 if (eip + size - ctxt->decode.eip_orig > 15)
667 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200668 eip += ctxt->cs_base;
669 while (size--) {
670 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
671 if (rc)
672 return rc;
673 }
674 return 0;
675}
676
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000677/*
678 * Given the 'reg' portion of a ModRM byte, and a register block, return a
679 * pointer into the block that addresses the relevant register.
680 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
681 */
682static void *decode_register(u8 modrm_reg, unsigned long *regs,
683 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684{
685 void *p;
686
687 p = &regs[modrm_reg];
688 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
689 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
690 return p;
691}
692
693static int read_descriptor(struct x86_emulate_ctxt *ctxt,
694 struct x86_emulate_ops *ops,
695 void *ptr,
696 u16 *size, unsigned long *address, int op_bytes)
697{
698 int rc;
699
700 if (op_bytes == 2)
701 op_bytes = 3;
702 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300703 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200704 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 if (rc)
706 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300707 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200708 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 return rc;
710}
711
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300712static int test_cc(unsigned int condition, unsigned int flags)
713{
714 int rc = 0;
715
716 switch ((condition & 15) >> 1) {
717 case 0: /* o */
718 rc |= (flags & EFLG_OF);
719 break;
720 case 1: /* b/c/nae */
721 rc |= (flags & EFLG_CF);
722 break;
723 case 2: /* z/e */
724 rc |= (flags & EFLG_ZF);
725 break;
726 case 3: /* be/na */
727 rc |= (flags & (EFLG_CF|EFLG_ZF));
728 break;
729 case 4: /* s */
730 rc |= (flags & EFLG_SF);
731 break;
732 case 5: /* p/pe */
733 rc |= (flags & EFLG_PF);
734 break;
735 case 7: /* le/ng */
736 rc |= (flags & EFLG_ZF);
737 /* fall through */
738 case 6: /* l/nge */
739 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
740 break;
741 }
742
743 /* Odd condition identifiers (lsb == 1) have inverted sense. */
744 return (!!rc ^ (condition & 1));
745}
746
Avi Kivity3c118e22007-10-31 10:27:04 +0200747static void decode_register_operand(struct operand *op,
748 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200749 int inhibit_bytereg)
750{
Avi Kivity33615aa2007-10-31 11:15:56 +0200751 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200752 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200753
754 if (!(c->d & ModRM))
755 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200756 op->type = OP_REG;
757 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200758 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200759 op->val = *(u8 *)op->ptr;
760 op->bytes = 1;
761 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200762 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200763 op->bytes = c->op_bytes;
764 switch (op->bytes) {
765 case 2:
766 op->val = *(u16 *)op->ptr;
767 break;
768 case 4:
769 op->val = *(u32 *)op->ptr;
770 break;
771 case 8:
772 op->val = *(u64 *) op->ptr;
773 break;
774 }
775 }
776 op->orig_val = op->val;
777}
778
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200779static int decode_modrm(struct x86_emulate_ctxt *ctxt,
780 struct x86_emulate_ops *ops)
781{
782 struct decode_cache *c = &ctxt->decode;
783 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700784 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200785 int rc = 0;
786
787 if (c->rex_prefix) {
788 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
789 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
790 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
791 }
792
793 c->modrm = insn_fetch(u8, 1, c->eip);
794 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
795 c->modrm_reg |= (c->modrm & 0x38) >> 3;
796 c->modrm_rm |= (c->modrm & 0x07);
797 c->modrm_ea = 0;
798 c->use_modrm_ea = 1;
799
800 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300801 c->modrm_ptr = decode_register(c->modrm_rm,
802 c->regs, c->d & ByteOp);
803 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804 return rc;
805 }
806
807 if (c->ad_bytes == 2) {
808 unsigned bx = c->regs[VCPU_REGS_RBX];
809 unsigned bp = c->regs[VCPU_REGS_RBP];
810 unsigned si = c->regs[VCPU_REGS_RSI];
811 unsigned di = c->regs[VCPU_REGS_RDI];
812
813 /* 16-bit ModR/M decode. */
814 switch (c->modrm_mod) {
815 case 0:
816 if (c->modrm_rm == 6)
817 c->modrm_ea += insn_fetch(u16, 2, c->eip);
818 break;
819 case 1:
820 c->modrm_ea += insn_fetch(s8, 1, c->eip);
821 break;
822 case 2:
823 c->modrm_ea += insn_fetch(u16, 2, c->eip);
824 break;
825 }
826 switch (c->modrm_rm) {
827 case 0:
828 c->modrm_ea += bx + si;
829 break;
830 case 1:
831 c->modrm_ea += bx + di;
832 break;
833 case 2:
834 c->modrm_ea += bp + si;
835 break;
836 case 3:
837 c->modrm_ea += bp + di;
838 break;
839 case 4:
840 c->modrm_ea += si;
841 break;
842 case 5:
843 c->modrm_ea += di;
844 break;
845 case 6:
846 if (c->modrm_mod != 0)
847 c->modrm_ea += bp;
848 break;
849 case 7:
850 c->modrm_ea += bx;
851 break;
852 }
853 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
854 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300855 if (!c->has_seg_override)
856 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200857 c->modrm_ea = (u16)c->modrm_ea;
858 } else {
859 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700860 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200861 sib = insn_fetch(u8, 1, c->eip);
862 index_reg |= (sib >> 3) & 7;
863 base_reg |= sib & 7;
864 scale = sib >> 6;
865
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700866 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
867 c->modrm_ea += insn_fetch(s32, 4, c->eip);
868 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200869 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700870 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700872 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
873 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700874 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700875 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877 switch (c->modrm_mod) {
878 case 0:
879 if (c->modrm_rm == 5)
880 c->modrm_ea += insn_fetch(s32, 4, c->eip);
881 break;
882 case 1:
883 c->modrm_ea += insn_fetch(s8, 1, c->eip);
884 break;
885 case 2:
886 c->modrm_ea += insn_fetch(s32, 4, c->eip);
887 break;
888 }
889 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890done:
891 return rc;
892}
893
894static int decode_abs(struct x86_emulate_ctxt *ctxt,
895 struct x86_emulate_ops *ops)
896{
897 struct decode_cache *c = &ctxt->decode;
898 int rc = 0;
899
900 switch (c->ad_bytes) {
901 case 2:
902 c->modrm_ea = insn_fetch(u16, 2, c->eip);
903 break;
904 case 4:
905 c->modrm_ea = insn_fetch(u32, 4, c->eip);
906 break;
907 case 8:
908 c->modrm_ea = insn_fetch(u64, 8, c->eip);
909 break;
910 }
911done:
912 return rc;
913}
914
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200916x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200918 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200921 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922
923 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
Laurent Viviere4e03de2007-09-18 11:52:50 +0200925 memset(c, 0, sizeof(struct decode_cache));
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200926 c->eip = c->eip_orig = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300927 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800928 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 switch (mode) {
931 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200932 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200934 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 break;
936 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200937 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800939#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200941 def_op_bytes = 4;
942 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
944#endif
945 default:
946 return -1;
947 }
948
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200949 c->op_bytes = def_op_bytes;
950 c->ad_bytes = def_ad_bytes;
951
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200953 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200954 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200956 /* switch between 2/4 bytes */
957 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 break;
959 case 0x67: /* address-size override */
960 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200961 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200962 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200965 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300968 case 0x2e: /* CS override */
969 case 0x36: /* SS override */
970 case 0x3e: /* DS override */
971 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 break;
973 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300975 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200977 case 0x40 ... 0x4f: /* REX */
978 if (mode != X86EMUL_MODE_PROT64)
979 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200980 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200981 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200983 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200985 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100986 c->rep_prefix = REPNE_PREFIX;
987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100989 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 default:
992 goto done_prefixes;
993 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200994
995 /* Any legacy prefix after a REX prefix nullifies its effect. */
996
Avi Kivity33615aa2007-10-31 11:15:56 +0200997 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 }
999
1000done_prefixes:
1001
1002 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001004 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001005 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006
1007 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->d = opcode_table[c->b];
1009 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 if (c->b == 0x0f) {
1012 c->twobyte = 1;
1013 c->b = insn_fetch(u8, 1, c->eip);
1014 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001016 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
Mohammed Gamald8769fe2009-08-23 14:24:25 +03001018 if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Wei Yongjun1d327ea2010-02-11 11:12:07 +08001019 kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");
Mohammed Gamald8769fe2009-08-23 14:24:25 +03001020 return -1;
1021 }
1022
Avi Kivitye09d0822008-01-18 12:38:59 +02001023 if (c->d & Group) {
1024 group = c->d & GroupMask;
1025 c->modrm = insn_fetch(u8, 1, c->eip);
1026 --c->eip;
1027
1028 group = (group << 3) + ((c->modrm >> 3) & 7);
1029 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1030 c->d = group2_table[group];
1031 else
1032 c->d = group_table[group];
1033 }
1034
1035 /* Unrecognised? */
1036 if (c->d == 0) {
1037 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);
1049 if (rc)
1050 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))
1056 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001057
1058 if (c->ad_bytes != 8)
1059 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 /*
1061 * Decode and fetch the source operand: register, memory
1062 * or immediate.
1063 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001064 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 case SrcNone:
1066 break;
1067 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001068 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 break;
1070 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001071 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 goto srcmem_common;
1073 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001074 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 goto srcmem_common;
1076 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.bytes = (c->d & ByteOp) ? 1 :
1078 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001079 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001080 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001081 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001082 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001083 /*
1084 * For instructions with a ModR/M byte, switch to register
1085 * access if Mod = 3.
1086 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001087 if ((c->d & ModRM) && c->modrm_mod == 3) {
1088 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001089 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001090 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001091 break;
1092 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 break;
1095 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001096 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001097 c->src.type = OP_IMM;
1098 c->src.ptr = (unsigned long *)c->eip;
1099 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1100 if (c->src.bytes == 8)
1101 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001103 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001105 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 break;
1107 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001108 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 break;
1110 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001111 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001114 if ((c->d & SrcMask) == SrcImmU) {
1115 switch (c->src.bytes) {
1116 case 1:
1117 c->src.val &= 0xff;
1118 break;
1119 case 2:
1120 c->src.val &= 0xffff;
1121 break;
1122 case 4:
1123 c->src.val &= 0xffffffff;
1124 break;
1125 }
1126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 break;
1128 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001129 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 c->src.type = OP_IMM;
1131 c->src.ptr = (unsigned long *)c->eip;
1132 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001133 if ((c->d & SrcMask) == SrcImmByte)
1134 c->src.val = insn_fetch(s8, 1, c->eip);
1135 else
1136 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001138 case SrcOne:
1139 c->src.bytes = 1;
1140 c->src.val = 1;
1141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 }
1143
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001144 /*
1145 * Decode and fetch the second source operand: register, memory
1146 * or immediate.
1147 */
1148 switch (c->d & Src2Mask) {
1149 case Src2None:
1150 break;
1151 case Src2CL:
1152 c->src2.bytes = 1;
1153 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1154 break;
1155 case Src2ImmByte:
1156 c->src2.type = OP_IMM;
1157 c->src2.ptr = (unsigned long *)c->eip;
1158 c->src2.bytes = 1;
1159 c->src2.val = insn_fetch(u8, 1, c->eip);
1160 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001161 case Src2Imm16:
1162 c->src2.type = OP_IMM;
1163 c->src2.ptr = (unsigned long *)c->eip;
1164 c->src2.bytes = 2;
1165 c->src2.val = insn_fetch(u16, 2, c->eip);
1166 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001167 case Src2One:
1168 c->src2.bytes = 1;
1169 c->src2.val = 1;
1170 break;
1171 }
1172
Avi Kivity038e51d2007-01-22 20:40:40 -08001173 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001174 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001175 case ImplicitOps:
1176 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001177 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001178 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001179 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001180 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001181 break;
1182 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001183 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001184 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001185 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001186 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001187 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001188 break;
1189 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001190 c->dst.type = OP_MEM;
1191 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001192 case DstAcc:
1193 c->dst.type = OP_REG;
1194 c->dst.bytes = c->op_bytes;
1195 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1196 switch (c->op_bytes) {
1197 case 1:
1198 c->dst.val = *(u8 *)c->dst.ptr;
1199 break;
1200 case 2:
1201 c->dst.val = *(u16 *)c->dst.ptr;
1202 break;
1203 case 4:
1204 c->dst.val = *(u32 *)c->dst.ptr;
1205 break;
1206 }
1207 c->dst.orig_val = c->dst.val;
1208 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001209 }
1210
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001211 if (c->rip_relative)
1212 c->modrm_ea += c->eip;
1213
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001214done:
1215 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1216}
1217
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001218static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1219{
1220 struct decode_cache *c = &ctxt->decode;
1221
1222 c->dst.type = OP_MEM;
1223 c->dst.bytes = c->op_bytes;
1224 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001225 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001226 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001227 c->regs[VCPU_REGS_RSP]);
1228}
1229
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001230static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001231 struct x86_emulate_ops *ops,
1232 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233{
1234 struct decode_cache *c = &ctxt->decode;
1235 int rc;
1236
Avi Kivity781d0ed2008-11-27 18:00:28 +02001237 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1238 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001239 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001240 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001241 return rc;
1242
Avi Kivity350f69d2009-01-05 11:12:40 +02001243 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001244 return rc;
1245}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001246
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001247static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1248 struct x86_emulate_ops *ops,
1249 void *dest, int len)
1250{
1251 int rc;
1252 unsigned long val, change_mask;
1253 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1254 int cpl = kvm_x86_ops->get_cpl(ctxt->vcpu);
1255
1256 rc = emulate_pop(ctxt, ops, &val, len);
1257 if (rc != X86EMUL_CONTINUE)
1258 return rc;
1259
1260 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1261 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1262
1263 switch(ctxt->mode) {
1264 case X86EMUL_MODE_PROT64:
1265 case X86EMUL_MODE_PROT32:
1266 case X86EMUL_MODE_PROT16:
1267 if (cpl == 0)
1268 change_mask |= EFLG_IOPL;
1269 if (cpl <= iopl)
1270 change_mask |= EFLG_IF;
1271 break;
1272 case X86EMUL_MODE_VM86:
1273 if (iopl < 3) {
1274 kvm_inject_gp(ctxt->vcpu, 0);
1275 return X86EMUL_PROPAGATE_FAULT;
1276 }
1277 change_mask |= EFLG_IF;
1278 break;
1279 default: /* real mode */
1280 change_mask |= (EFLG_IOPL | EFLG_IF);
1281 break;
1282 }
1283
1284 *(unsigned long *)dest =
1285 (ctxt->eflags & ~change_mask) | (val & change_mask);
1286
1287 return rc;
1288}
1289
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001290static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1291{
1292 struct decode_cache *c = &ctxt->decode;
1293 struct kvm_segment segment;
1294
1295 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1296
1297 c->src.val = segment.selector;
1298 emulate_push(ctxt);
1299}
1300
1301static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1302 struct x86_emulate_ops *ops, int seg)
1303{
1304 struct decode_cache *c = &ctxt->decode;
1305 unsigned long selector;
1306 int rc;
1307
1308 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1309 if (rc != 0)
1310 return rc;
1311
Gleb Natapovc6975182010-02-18 12:15:01 +02001312 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001313 return rc;
1314}
1315
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001316static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1317{
1318 struct decode_cache *c = &ctxt->decode;
1319 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1320 int reg = VCPU_REGS_RAX;
1321
1322 while (reg <= VCPU_REGS_RDI) {
1323 (reg == VCPU_REGS_RSP) ?
1324 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1325
1326 emulate_push(ctxt);
1327 ++reg;
1328 }
1329}
1330
1331static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1332 struct x86_emulate_ops *ops)
1333{
1334 struct decode_cache *c = &ctxt->decode;
1335 int rc = 0;
1336 int reg = VCPU_REGS_RDI;
1337
1338 while (reg >= VCPU_REGS_RAX) {
1339 if (reg == VCPU_REGS_RSP) {
1340 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1341 c->op_bytes);
1342 --reg;
1343 }
1344
1345 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
1346 if (rc != 0)
1347 break;
1348 --reg;
1349 }
1350 return rc;
1351}
1352
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001353static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1354 struct x86_emulate_ops *ops)
1355{
1356 struct decode_cache *c = &ctxt->decode;
1357 int rc;
1358
Avi Kivity350f69d2009-01-05 11:12:40 +02001359 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001360 if (rc != 0)
1361 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001362 return 0;
1363}
1364
Laurent Vivier05f086f2007-09-24 11:10:55 +02001365static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001366{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001367 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368 switch (c->modrm_reg) {
1369 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001370 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001371 break;
1372 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001373 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001374 break;
1375 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001376 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377 break;
1378 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001379 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001380 break;
1381 case 4: /* sal/shl */
1382 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001383 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384 break;
1385 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001386 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001387 break;
1388 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390 break;
1391 }
1392}
1393
1394static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001395 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396{
1397 struct decode_cache *c = &ctxt->decode;
1398 int rc = 0;
1399
1400 switch (c->modrm_reg) {
1401 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001402 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403 break;
1404 case 2: /* not */
1405 c->dst.val = ~c->dst.val;
1406 break;
1407 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001408 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001409 break;
1410 default:
1411 DPRINTF("Cannot emulate %02x\n", c->b);
1412 rc = X86EMUL_UNHANDLEABLE;
1413 break;
1414 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001415 return rc;
1416}
1417
1418static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001419 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001420{
1421 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422
1423 switch (c->modrm_reg) {
1424 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001425 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001426 break;
1427 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001430 case 2: /* call near abs */ {
1431 long int old_eip;
1432 old_eip = c->eip;
1433 c->eip = c->src.val;
1434 c->src.val = old_eip;
1435 emulate_push(ctxt);
1436 break;
1437 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001439 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 break;
1441 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001442 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001444 }
1445 return 0;
1446}
1447
1448static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1449 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001450 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001451{
1452 struct decode_cache *c = &ctxt->decode;
1453 u64 old, new;
1454 int rc;
1455
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001456 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001457 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001458 return rc;
1459
1460 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1461 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1462
1463 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1464 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001465 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001466
1467 } else {
1468 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1469 (u32) c->regs[VCPU_REGS_RBX];
1470
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001471 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001472 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001474 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475 }
1476 return 0;
1477}
1478
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001479static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1480 struct x86_emulate_ops *ops)
1481{
1482 struct decode_cache *c = &ctxt->decode;
1483 int rc;
1484 unsigned long cs;
1485
1486 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1487 if (rc)
1488 return rc;
1489 if (c->op_bytes == 4)
1490 c->eip = (u32)c->eip;
1491 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1492 if (rc)
1493 return rc;
Gleb Natapovc6975182010-02-18 12:15:01 +02001494 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001495 return rc;
1496}
1497
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001498static inline int writeback(struct x86_emulate_ctxt *ctxt,
1499 struct x86_emulate_ops *ops)
1500{
1501 int rc;
1502 struct decode_cache *c = &ctxt->decode;
1503
1504 switch (c->dst.type) {
1505 case OP_REG:
1506 /* The 4-byte case *is* correct:
1507 * in 64-bit mode we zero-extend.
1508 */
1509 switch (c->dst.bytes) {
1510 case 1:
1511 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1512 break;
1513 case 2:
1514 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1515 break;
1516 case 4:
1517 *c->dst.ptr = (u32)c->dst.val;
1518 break; /* 64b: zero-ext */
1519 case 8:
1520 *c->dst.ptr = c->dst.val;
1521 break;
1522 }
1523 break;
1524 case OP_MEM:
1525 if (c->lock_prefix)
1526 rc = ops->cmpxchg_emulated(
1527 (unsigned long)c->dst.ptr,
1528 &c->dst.orig_val,
1529 &c->dst.val,
1530 c->dst.bytes,
1531 ctxt->vcpu);
1532 else
1533 rc = ops->write_emulated(
1534 (unsigned long)c->dst.ptr,
1535 &c->dst.val,
1536 c->dst.bytes,
1537 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001538 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001540 break;
1541 case OP_NONE:
1542 /* no writeback */
1543 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001544 default:
1545 break;
1546 }
1547 return 0;
1548}
1549
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301550static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001551{
1552 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1553 /*
1554 * an sti; sti; sequence only disable interrupts for the first
1555 * instruction. So, if the last instruction, be it emulated or
1556 * not, left the system with the INT_STI flag enabled, it
1557 * means that the last instruction is an sti. We should not
1558 * leave the flag on in this case. The same goes for mov ss
1559 */
1560 if (!(int_shadow & mask))
1561 ctxt->interruptibility = mask;
1562}
1563
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001564static inline void
1565setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1566 struct kvm_segment *cs, struct kvm_segment *ss)
1567{
1568 memset(cs, 0, sizeof(struct kvm_segment));
1569 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1570 memset(ss, 0, sizeof(struct kvm_segment));
1571
1572 cs->l = 0; /* will be adjusted later */
1573 cs->base = 0; /* flat segment */
1574 cs->g = 1; /* 4kb granularity */
1575 cs->limit = 0xffffffff; /* 4GB limit */
1576 cs->type = 0x0b; /* Read, Execute, Accessed */
1577 cs->s = 1;
1578 cs->dpl = 0; /* will be adjusted later */
1579 cs->present = 1;
1580 cs->db = 1;
1581
1582 ss->unusable = 0;
1583 ss->base = 0; /* flat segment */
1584 ss->limit = 0xffffffff; /* 4GB limit */
1585 ss->g = 1; /* 4kb granularity */
1586 ss->s = 1;
1587 ss->type = 0x03; /* Read/Write, Accessed */
1588 ss->db = 1; /* 32bit stack segment */
1589 ss->dpl = 0;
1590 ss->present = 1;
1591}
1592
1593static int
1594emulate_syscall(struct x86_emulate_ctxt *ctxt)
1595{
1596 struct decode_cache *c = &ctxt->decode;
1597 struct kvm_segment cs, ss;
1598 u64 msr_data;
1599
1600 /* syscall is not available in real mode */
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001601 if (ctxt->mode == X86EMUL_MODE_REAL || ctxt->mode == X86EMUL_MODE_VM86)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001602 return X86EMUL_UNHANDLEABLE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001603
1604 setup_syscalls_segments(ctxt, &cs, &ss);
1605
1606 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1607 msr_data >>= 32;
1608 cs.selector = (u16)(msr_data & 0xfffc);
1609 ss.selector = (u16)(msr_data + 8);
1610
1611 if (is_long_mode(ctxt->vcpu)) {
1612 cs.db = 0;
1613 cs.l = 1;
1614 }
1615 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1616 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1617
1618 c->regs[VCPU_REGS_RCX] = c->eip;
1619 if (is_long_mode(ctxt->vcpu)) {
1620#ifdef CONFIG_X86_64
1621 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1622
1623 kvm_x86_ops->get_msr(ctxt->vcpu,
1624 ctxt->mode == X86EMUL_MODE_PROT64 ?
1625 MSR_LSTAR : MSR_CSTAR, &msr_data);
1626 c->eip = msr_data;
1627
1628 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1629 ctxt->eflags &= ~(msr_data | EFLG_RF);
1630#endif
1631 } else {
1632 /* legacy mode */
1633 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1634 c->eip = (u32)msr_data;
1635
1636 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1637 }
1638
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001639 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001640}
1641
Andre Przywara8c604352009-06-18 12:56:01 +02001642static int
1643emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1644{
1645 struct decode_cache *c = &ctxt->decode;
1646 struct kvm_segment cs, ss;
1647 u64 msr_data;
1648
Gleb Natapova0044752010-02-10 14:21:31 +02001649 /* inject #GP if in real mode */
1650 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001651 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001652 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001653 }
1654
1655 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1656 * Therefore, we inject an #UD.
1657 */
1658 if (ctxt->mode == X86EMUL_MODE_PROT64)
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001659 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02001660
1661 setup_syscalls_segments(ctxt, &cs, &ss);
1662
1663 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1664 switch (ctxt->mode) {
1665 case X86EMUL_MODE_PROT32:
1666 if ((msr_data & 0xfffc) == 0x0) {
1667 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001668 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001669 }
1670 break;
1671 case X86EMUL_MODE_PROT64:
1672 if (msr_data == 0x0) {
1673 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001674 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001675 }
1676 break;
1677 }
1678
1679 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1680 cs.selector = (u16)msr_data;
1681 cs.selector &= ~SELECTOR_RPL_MASK;
1682 ss.selector = cs.selector + 8;
1683 ss.selector &= ~SELECTOR_RPL_MASK;
1684 if (ctxt->mode == X86EMUL_MODE_PROT64
1685 || is_long_mode(ctxt->vcpu)) {
1686 cs.db = 0;
1687 cs.l = 1;
1688 }
1689
1690 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1691 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1692
1693 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1694 c->eip = msr_data;
1695
1696 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1697 c->regs[VCPU_REGS_RSP] = msr_data;
1698
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001699 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001700}
1701
Andre Przywara4668f052009-06-18 12:56:02 +02001702static int
1703emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1704{
1705 struct decode_cache *c = &ctxt->decode;
1706 struct kvm_segment cs, ss;
1707 u64 msr_data;
1708 int usermode;
1709
Gleb Natapova0044752010-02-10 14:21:31 +02001710 /* inject #GP if in real mode or Virtual 8086 mode */
1711 if (ctxt->mode == X86EMUL_MODE_REAL ||
1712 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001713 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001714 return X86EMUL_UNHANDLEABLE;
Andre Przywara4668f052009-06-18 12:56:02 +02001715 }
1716
Andre Przywara4668f052009-06-18 12:56:02 +02001717 setup_syscalls_segments(ctxt, &cs, &ss);
1718
1719 if ((c->rex_prefix & 0x8) != 0x0)
1720 usermode = X86EMUL_MODE_PROT64;
1721 else
1722 usermode = X86EMUL_MODE_PROT32;
1723
1724 cs.dpl = 3;
1725 ss.dpl = 3;
1726 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1727 switch (usermode) {
1728 case X86EMUL_MODE_PROT32:
1729 cs.selector = (u16)(msr_data + 16);
1730 if ((msr_data & 0xfffc) == 0x0) {
1731 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001732 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001733 }
1734 ss.selector = (u16)(msr_data + 24);
1735 break;
1736 case X86EMUL_MODE_PROT64:
1737 cs.selector = (u16)(msr_data + 32);
1738 if (msr_data == 0x0) {
1739 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001740 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001741 }
1742 ss.selector = cs.selector + 8;
1743 cs.db = 0;
1744 cs.l = 1;
1745 break;
1746 }
1747 cs.selector |= SELECTOR_RPL_MASK;
1748 ss.selector |= SELECTOR_RPL_MASK;
1749
1750 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1751 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1752
1753 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1754 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1755
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001756 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001757}
1758
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001759static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
1760{
1761 int iopl;
1762 if (ctxt->mode == X86EMUL_MODE_REAL)
1763 return false;
1764 if (ctxt->mode == X86EMUL_MODE_VM86)
1765 return true;
1766 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1767 return kvm_x86_ops->get_cpl(ctxt->vcpu) > iopl;
1768}
1769
1770static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1771 struct x86_emulate_ops *ops,
1772 u16 port, u16 len)
1773{
1774 struct kvm_segment tr_seg;
1775 int r;
1776 u16 io_bitmap_ptr;
1777 u8 perm, bit_idx = port & 0x7;
1778 unsigned mask = (1 << len) - 1;
1779
1780 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1781 if (tr_seg.unusable)
1782 return false;
1783 if (tr_seg.limit < 103)
1784 return false;
1785 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1786 NULL);
1787 if (r != X86EMUL_CONTINUE)
1788 return false;
1789 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1790 return false;
1791 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1792 ctxt->vcpu, NULL);
1793 if (r != X86EMUL_CONTINUE)
1794 return false;
1795 if ((perm >> bit_idx) & mask)
1796 return false;
1797 return true;
1798}
1799
1800static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1801 struct x86_emulate_ops *ops,
1802 u16 port, u16 len)
1803{
1804 if (emulator_bad_iopl(ctxt))
1805 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1806 return false;
1807 return true;
1808}
1809
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001810int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001811x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001812{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001813 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001814 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001815 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001816 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001817 unsigned int port;
1818 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001819 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001820
Glauber Costa310b5d32009-05-12 16:21:06 -04001821 ctxt->interruptibility = 0;
1822
Laurent Vivier34273182007-09-18 11:27:37 +02001823 /* Shadow copy of register state. Committed on successful emulation.
1824 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1825 * modify them.
1826 */
1827
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001828 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001829 saved_eip = c->eip;
1830
Gleb Natapovd380a5e2010-02-10 14:21:36 +02001831 /* LOCK prefix is allowed only with some instructions */
1832 if (c->lock_prefix && !(c->d & Lock)) {
1833 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1834 goto done;
1835 }
1836
Gleb Natapove92805a2010-02-10 14:21:35 +02001837 /* Privileged instruction can be executed only in CPL=0 */
1838 if ((c->d & Priv) && kvm_x86_ops->get_cpl(ctxt->vcpu)) {
1839 kvm_inject_gp(ctxt->vcpu, 0);
1840 goto done;
1841 }
1842
Avi Kivityc7e75a32007-10-28 16:34:25 +02001843 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001844 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001845
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001846 if (c->rep_prefix && (c->d & String)) {
1847 /* All REP prefixes have the same first termination condition */
1848 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001849 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001850 goto done;
1851 }
1852 /* The second termination condition only applies for REPE
1853 * and REPNE. Test if the repeat string operation prefix is
1854 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1855 * corresponding termination condition according to:
1856 * - if REPE/REPZ and ZF = 0 then done
1857 * - if REPNE/REPNZ and ZF = 1 then done
1858 */
1859 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1860 (c->b == 0xae) || (c->b == 0xaf)) {
1861 if ((c->rep_prefix == REPE_PREFIX) &&
1862 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001863 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001864 goto done;
1865 }
1866 if ((c->rep_prefix == REPNE_PREFIX) &&
1867 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001868 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001869 goto done;
1870 }
1871 }
1872 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001873 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001874 }
1875
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001876 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001877 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001878 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001879 rc = ops->read_emulated((unsigned long)c->src.ptr,
1880 &c->src.val,
1881 c->src.bytes,
1882 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001883 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001884 goto done;
1885 c->src.orig_val = c->src.val;
1886 }
1887
1888 if ((c->d & DstMask) == ImplicitOps)
1889 goto special_insn;
1890
1891
1892 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001893 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001894 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1895 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001896 if (c->d & BitOp) {
1897 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001898
Laurent Viviere4e03de2007-09-18 11:52:50 +02001899 c->dst.ptr = (void *)c->dst.ptr +
1900 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001901 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001902 if (!(c->d & Mov)) {
1903 /* optimisation - avoid slow emulated read */
1904 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1905 &c->dst.val,
1906 c->dst.bytes,
1907 ctxt->vcpu);
1908 if (rc != X86EMUL_CONTINUE)
1909 goto done;
1910 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001911 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001913
Avi Kivity018a98d2007-11-27 19:30:56 +02001914special_insn:
1915
Laurent Viviere4e03de2007-09-18 11:52:50 +02001916 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 goto twobyte_insn;
1918
Laurent Viviere4e03de2007-09-18 11:52:50 +02001919 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 case 0x00 ... 0x05:
1921 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001922 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001924 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001925 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1926 break;
1927 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001928 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1929 if (rc != 0)
1930 goto done;
1931 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 case 0x08 ... 0x0d:
1933 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001934 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001936 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001937 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1938 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 case 0x10 ... 0x15:
1940 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001941 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001943 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001944 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1945 break;
1946 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001947 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1948 if (rc != 0)
1949 goto done;
1950 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951 case 0x18 ... 0x1d:
1952 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001953 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001955 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001956 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1957 break;
1958 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001959 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1960 if (rc != 0)
1961 goto done;
1962 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001963 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001965 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
1967 case 0x28 ... 0x2d:
1968 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001969 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 break;
1971 case 0x30 ... 0x35:
1972 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001973 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 break;
1975 case 0x38 ... 0x3d:
1976 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001977 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001979 case 0x40 ... 0x47: /* inc r16/r32 */
1980 emulate_1op("inc", c->dst, ctxt->eflags);
1981 break;
1982 case 0x48 ... 0x4f: /* dec r16/r32 */
1983 emulate_1op("dec", c->dst, ctxt->eflags);
1984 break;
1985 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001986 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001987 break;
1988 case 0x58 ... 0x5f: /* pop reg */
1989 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001990 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001991 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001992 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001993 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001994 case 0x60: /* pusha */
1995 emulate_pusha(ctxt);
1996 break;
1997 case 0x61: /* popa */
1998 rc = emulate_popa(ctxt, ops);
1999 if (rc != 0)
2000 goto done;
2001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002003 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002005 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002007 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002008 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02002009 emulate_push(ctxt);
2010 break;
2011 case 0x6c: /* insb */
2012 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002013 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2014 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2015 kvm_inject_gp(ctxt->vcpu, 0);
2016 goto done;
2017 }
2018 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002019 1,
2020 (c->d & ByteOp) ? 1 : c->op_bytes,
2021 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002022 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002023 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002024 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02002025 c->regs[VCPU_REGS_RDI]),
2026 c->rep_prefix,
2027 c->regs[VCPU_REGS_RDX]) == 0) {
2028 c->eip = saved_eip;
2029 return -1;
2030 }
2031 return 0;
2032 case 0x6e: /* outsb */
2033 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
2035 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2036 kvm_inject_gp(ctxt->vcpu, 0);
2037 goto done;
2038 }
Avi Kivity851ba692009-08-24 11:10:17 +03002039 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02002040 0,
2041 (c->d & ByteOp) ? 1 : c->op_bytes,
2042 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08002043 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02002044 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002045 register_address(c,
2046 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02002047 c->regs[VCPU_REGS_RSI]),
2048 c->rep_prefix,
2049 c->regs[VCPU_REGS_RDX]) == 0) {
2050 c->eip = saved_eip;
2051 return -1;
2052 }
2053 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002054 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002055 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002056 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002057 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002059 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 case 0:
2061 goto add;
2062 case 1:
2063 goto or;
2064 case 2:
2065 goto adc;
2066 case 3:
2067 goto sbb;
2068 case 4:
2069 goto and;
2070 case 5:
2071 goto sub;
2072 case 6:
2073 goto xor;
2074 case 7:
2075 goto cmp;
2076 }
2077 break;
2078 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002079 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080 break;
2081 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002082 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002084 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002086 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 break;
2088 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002089 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 break;
2091 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002092 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 break; /* 64b reg: zero-extend */
2094 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002095 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 break;
2097 }
2098 /*
2099 * Write back the memory destination with implicit LOCK
2100 * prefix.
2101 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002102 c->dst.val = c->src.val;
2103 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002106 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002107 case 0x8c: { /* mov r/m, sreg */
2108 struct kvm_segment segreg;
2109
2110 if (c->modrm_reg <= 5)
2111 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2112 else {
2113 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
2114 c->modrm);
2115 goto cannot_emulate;
2116 }
2117 c->dst.val = segreg.selector;
2118 break;
2119 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002120 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002121 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002122 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002123 case 0x8e: { /* mov seg, r/m16 */
2124 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002125
2126 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002127
Gleb Natapovc6975182010-02-18 12:15:01 +02002128 if (c->modrm_reg == VCPU_SREG_CS ||
2129 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002130 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2131 goto done;
2132 }
2133
Glauber Costa310b5d32009-05-12 16:21:06 -04002134 if (c->modrm_reg == VCPU_SREG_SS)
2135 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
2136
Gleb Natapovc6975182010-02-18 12:15:01 +02002137 rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002138
2139 c->dst.type = OP_NONE; /* Disable writeback. */
2140 break;
2141 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143 rc = emulate_grp1a(ctxt, ops);
2144 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002147 case 0x90: /* nop / xchg r8,rax */
2148 if (!(c->rex_prefix & 1)) { /* nop */
2149 c->dst.type = OP_NONE;
2150 break;
2151 }
2152 case 0x91 ... 0x97: /* xchg reg,rax */
2153 c->src.type = c->dst.type = OP_REG;
2154 c->src.bytes = c->dst.bytes = c->op_bytes;
2155 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2156 c->src.val = *(c->src.ptr);
2157 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002158 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002159 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002160 emulate_push(ctxt);
2161 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002162 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002163 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002164 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002165 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002166 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2167 if (rc != X86EMUL_CONTINUE)
2168 goto done;
2169 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002170 case 0xa0 ... 0xa1: /* mov */
2171 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2172 c->dst.val = c->src.val;
2173 break;
2174 case 0xa2 ... 0xa3: /* mov */
2175 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2176 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002178 c->dst.type = OP_MEM;
2179 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002180 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002181 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002182 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002183 rc = ops->read_emulated(register_address(c,
2184 seg_override_base(ctxt, c),
2185 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002186 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002187 c->dst.bytes, ctxt->vcpu);
2188 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002190 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002191 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002192 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002193 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002194 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002195 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196 break;
2197 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002198 c->src.type = OP_NONE; /* Disable writeback. */
2199 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002200 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002201 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002202 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002203 rc = ops->read_emulated((unsigned long)c->src.ptr,
2204 &c->src.val,
2205 c->src.bytes,
2206 ctxt->vcpu);
2207 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002208 goto done;
2209
2210 c->dst.type = OP_NONE; /* Disable writeback. */
2211 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002212 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002213 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002214 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002215 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2216 &c->dst.val,
2217 c->dst.bytes,
2218 ctxt->vcpu);
2219 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002220 goto done;
2221
2222 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2223
2224 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2225
Harvey Harrison7a9572752008-02-19 07:40:41 -08002226 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002227 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2228 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002229 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002230 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2231 : c->dst.bytes);
2232
2233 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002235 c->dst.type = OP_MEM;
2236 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002237 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002238 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002239 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002240 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002241 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002242 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002243 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 break;
2245 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002246 c->dst.type = OP_REG;
2247 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2248 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002249 rc = ops->read_emulated(register_address(c,
2250 seg_override_base(ctxt, c),
2251 c->regs[VCPU_REGS_RSI]),
2252 &c->dst.val,
2253 c->dst.bytes,
2254 ctxt->vcpu);
2255 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002257 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002258 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002259 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 break;
2261 case 0xae ... 0xaf: /* scas */
2262 DPRINTF("Urk! I don't handle SCAS.\n");
2263 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002264 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002265 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002266 case 0xc0 ... 0xc1:
2267 emulate_grp2(ctxt);
2268 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002269 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002270 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002271 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002272 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002273 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002274 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2275 mov:
2276 c->dst.val = c->src.val;
2277 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002278 case 0xcb: /* ret far */
2279 rc = emulate_ret_far(ctxt, ops);
2280 if (rc)
2281 goto done;
2282 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002283 case 0xd0 ... 0xd1: /* Grp2 */
2284 c->src.val = 1;
2285 emulate_grp2(ctxt);
2286 break;
2287 case 0xd2 ... 0xd3: /* Grp2 */
2288 c->src.val = c->regs[VCPU_REGS_RCX];
2289 emulate_grp2(ctxt);
2290 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002291 case 0xe4: /* inb */
2292 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002293 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002294 io_dir_in = 1;
2295 goto do_io;
2296 case 0xe6: /* outb */
2297 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002298 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002299 io_dir_in = 0;
2300 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002301 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002302 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002303 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002304 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002305 emulate_push(ctxt);
2306 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002307 }
2308 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002309 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002310 case 0xea: /* jmp far */
Gleb Natapovc6975182010-02-18 12:15:01 +02002311 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val,
2312 VCPU_SREG_CS))
2313 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002314
Gleb Natapov782b8772009-04-12 13:36:25 +03002315 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002316 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002317 case 0xeb:
2318 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002319 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002320 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002321 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002322 case 0xec: /* in al,dx */
2323 case 0xed: /* in (e/r)ax,dx */
2324 port = c->regs[VCPU_REGS_RDX];
2325 io_dir_in = 1;
2326 goto do_io;
2327 case 0xee: /* out al,dx */
2328 case 0xef: /* out (e/r)ax,dx */
2329 port = c->regs[VCPU_REGS_RDX];
2330 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002331 do_io:
2332 if (!emulator_io_permited(ctxt, ops, port,
2333 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2334 kvm_inject_gp(ctxt->vcpu, 0);
2335 goto done;
2336 }
2337 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002338 (c->d & ByteOp) ? 1 : c->op_bytes,
2339 port) != 0) {
2340 c->eip = saved_eip;
2341 goto cannot_emulate;
2342 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002343 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002344 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002345 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002346 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002347 case 0xf5: /* cmc */
2348 /* complement carry flag from eflags reg */
2349 ctxt->eflags ^= EFLG_CF;
2350 c->dst.type = OP_NONE; /* Disable writeback. */
2351 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002352 case 0xf6 ... 0xf7: /* Grp3 */
2353 rc = emulate_grp3(ctxt, ops);
2354 if (rc != 0)
2355 goto done;
2356 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002357 case 0xf8: /* clc */
2358 ctxt->eflags &= ~EFLG_CF;
2359 c->dst.type = OP_NONE; /* Disable writeback. */
2360 break;
2361 case 0xfa: /* cli */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002362 if (emulator_bad_iopl(ctxt))
2363 kvm_inject_gp(ctxt->vcpu, 0);
2364 else {
2365 ctxt->eflags &= ~X86_EFLAGS_IF;
2366 c->dst.type = OP_NONE; /* Disable writeback. */
2367 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002368 break;
2369 case 0xfb: /* sti */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370 if (emulator_bad_iopl(ctxt))
2371 kvm_inject_gp(ctxt->vcpu, 0);
2372 else {
2373 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
2374 ctxt->eflags |= X86_EFLAGS_IF;
2375 c->dst.type = OP_NONE; /* Disable writeback. */
2376 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002377 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002378 case 0xfc: /* cld */
2379 ctxt->eflags &= ~EFLG_DF;
2380 c->dst.type = OP_NONE; /* Disable writeback. */
2381 break;
2382 case 0xfd: /* std */
2383 ctxt->eflags |= EFLG_DF;
2384 c->dst.type = OP_NONE; /* Disable writeback. */
2385 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002386 case 0xfe ... 0xff: /* Grp4/Grp5 */
2387 rc = emulate_grp45(ctxt, ops);
2388 if (rc != 0)
2389 goto done;
2390 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002392
2393writeback:
2394 rc = writeback(ctxt, ops);
2395 if (rc != 0)
2396 goto done;
2397
2398 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002399 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002400 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002401
2402done:
2403 if (rc == X86EMUL_UNHANDLEABLE) {
2404 c->eip = saved_eip;
2405 return -1;
2406 }
2407 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408
2409twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002410 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002412 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 u16 size;
2414 unsigned long address;
2415
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002416 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002417 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002418 goto cannot_emulate;
2419
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002420 rc = kvm_fix_hypercall(ctxt->vcpu);
2421 if (rc)
2422 goto done;
2423
Avi Kivity33e38852008-05-21 15:34:25 +03002424 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002425 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002426 /* Disable writeback. */
2427 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002428 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002430 rc = read_descriptor(ctxt, ops, c->src.ptr,
2431 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 if (rc)
2433 goto done;
2434 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002435 /* Disable writeback. */
2436 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002438 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002439 if (c->modrm_mod == 3) {
2440 switch (c->modrm_rm) {
2441 case 1:
2442 rc = kvm_fix_hypercall(ctxt->vcpu);
2443 if (rc)
2444 goto done;
2445 break;
2446 default:
2447 goto cannot_emulate;
2448 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002449 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002450 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002451 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002452 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002453 if (rc)
2454 goto done;
2455 realmode_lidt(ctxt->vcpu, size, address);
2456 }
Avi Kivity16286d02008-04-14 14:40:50 +03002457 /* Disable writeback. */
2458 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 break;
2460 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002461 c->dst.bytes = 2;
2462 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 break;
2464 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002465 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2466 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002467 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 break;
2469 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002470 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002471 /* Disable writeback. */
2472 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 break;
2474 default:
2475 goto cannot_emulate;
2476 }
2477 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002478 case 0x05: /* syscall */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002479 rc = emulate_syscall(ctxt);
2480 if (rc != X86EMUL_CONTINUE)
2481 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002482 else
2483 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002484 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002485 case 0x06:
2486 emulate_clts(ctxt->vcpu);
2487 c->dst.type = OP_NONE;
2488 break;
2489 case 0x08: /* invd */
2490 case 0x09: /* wbinvd */
2491 case 0x0d: /* GrpP (prefetch) */
2492 case 0x18: /* Grp16 (prefetch/nop) */
2493 c->dst.type = OP_NONE;
2494 break;
2495 case 0x20: /* mov cr, reg */
2496 if (c->modrm_mod != 3)
2497 goto cannot_emulate;
2498 c->regs[c->modrm_rm] =
2499 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2500 c->dst.type = OP_NONE; /* no writeback */
2501 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002503 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002505 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002506 if (rc)
2507 goto cannot_emulate;
2508 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002510 case 0x22: /* mov reg, cr */
2511 if (c->modrm_mod != 3)
2512 goto cannot_emulate;
2513 realmode_set_cr(ctxt->vcpu,
2514 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2515 c->dst.type = OP_NONE;
2516 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002518 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002520 rc = emulator_set_dr(ctxt, c->modrm_reg,
2521 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002522 if (rc)
2523 goto cannot_emulate;
2524 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002526 case 0x30:
2527 /* wrmsr */
2528 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2529 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2530 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2531 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002532 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002533 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002534 }
2535 rc = X86EMUL_CONTINUE;
2536 c->dst.type = OP_NONE;
2537 break;
2538 case 0x32:
2539 /* rdmsr */
2540 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2541 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002542 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002543 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002544 } else {
2545 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2546 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2547 }
2548 rc = X86EMUL_CONTINUE;
2549 c->dst.type = OP_NONE;
2550 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002551 case 0x34: /* sysenter */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002552 rc = emulate_sysenter(ctxt);
2553 if (rc != X86EMUL_CONTINUE)
2554 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02002555 else
2556 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002557 break;
2558 case 0x35: /* sysexit */
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002559 rc = emulate_sysexit(ctxt);
2560 if (rc != X86EMUL_CONTINUE)
2561 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02002562 else
2563 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002564 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002566 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002567 if (!test_cc(c->b, ctxt->eflags))
2568 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002570 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002571 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002572 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002573 c->dst.type = OP_NONE;
2574 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002575 case 0xa0: /* push fs */
2576 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2577 break;
2578 case 0xa1: /* pop fs */
2579 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2580 if (rc != 0)
2581 goto done;
2582 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002583 case 0xa3:
2584 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002585 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002586 /* only subword offset */
2587 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002588 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002589 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002590 case 0xa4: /* shld imm8, r, r/m */
2591 case 0xa5: /* shld cl, r, r/m */
2592 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2593 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002594 case 0xa8: /* push gs */
2595 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2596 break;
2597 case 0xa9: /* pop gs */
2598 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2599 if (rc != 0)
2600 goto done;
2601 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002602 case 0xab:
2603 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002604 /* only subword offset */
2605 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002606 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002607 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002608 case 0xac: /* shrd imm8, r, r/m */
2609 case 0xad: /* shrd cl, r, r/m */
2610 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2611 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002612 case 0xae: /* clflush */
2613 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 case 0xb0 ... 0xb1: /* cmpxchg */
2615 /*
2616 * Save real source value, then compare EAX against
2617 * destination.
2618 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002619 c->src.orig_val = c->src.val;
2620 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002621 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2622 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002624 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 } else {
2626 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002627 c->dst.type = OP_REG;
2628 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 }
2630 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 case 0xb3:
2632 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002633 /* only subword offset */
2634 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002635 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002638 c->dst.bytes = c->op_bytes;
2639 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2640 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002643 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 case 0:
2645 goto bt;
2646 case 1:
2647 goto bts;
2648 case 2:
2649 goto btr;
2650 case 3:
2651 goto btc;
2652 }
2653 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002654 case 0xbb:
2655 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002656 /* only subword offset */
2657 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002658 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002659 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002661 c->dst.bytes = c->op_bytes;
2662 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2663 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002665 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002666 c->dst.bytes = c->op_bytes;
2667 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2668 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002669 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002671 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002672 if (rc != 0)
2673 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002674 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002675 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 }
2677 goto writeback;
2678
2679cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002680 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002681 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 return -1;
2683}