blob: 1cdfec5231d0a5dea001563523c5e6f130dffb3f [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
Andre Przywarae99f0502009-06-17 15:50:33 +020035#include "mmu.h" /* for is_long_mode() */
36
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 Kivityc9eaf202009-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 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010078/* Source 2 operand type */
79#define Src2None (0<<29)
80#define Src2CL (1<<29)
81#define Src2ImmByte (2<<29)
82#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030083#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010084#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivity43bb19c2008-01-18 12:46:50 +020086enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020087 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020088 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020089};
90
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010091static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 /* 0x00 - 0x07 */
93 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
94 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +030095 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
96 ImplicitOps | Stack, ImplicitOps | Stack,
Avi Kivity6aa8b732006-12-10 02:21:36 -080097 /* 0x08 - 0x0F */
98 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
99 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300100 0, 0, ImplicitOps | Stack, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101 /* 0x10 - 0x17 */
102 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
103 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300104 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
105 ImplicitOps | Stack, ImplicitOps | Stack,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106 /* 0x18 - 0x1F */
107 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300109 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
110 ImplicitOps | Stack, ImplicitOps | Stack,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111 /* 0x20 - 0x27 */
112 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
113 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200114 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 /* 0x28 - 0x2F */
116 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
117 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
118 0, 0, 0, 0,
119 /* 0x30 - 0x37 */
120 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
121 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
122 0, 0, 0, 0,
123 /* 0x38 - 0x3F */
124 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
125 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200126 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
127 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700128 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200129 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700130 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200131 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300132 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200133 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
134 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300135 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200136 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
137 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700138 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700140 0, 0, 0, 0,
141 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300142 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300143 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
144 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300145 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300146 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
147 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300148 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300149 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
150 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200152 Group | Group1_80, Group | Group1_81,
153 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
155 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
156 /* 0x88 - 0x8F */
157 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
158 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200159 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200160 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300161 /* 0x90 - 0x97 */
162 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
163 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300164 0, 0, SrcImm | Src2Imm16, 0,
165 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200167 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
168 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200169 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
170 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200172 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
173 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
174 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300175 /* 0xB0 - 0xB7 */
176 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
177 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
178 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
179 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
180 /* 0xB8 - 0xBF */
181 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
182 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
183 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
184 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300186 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200187 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300188 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300190 0, 0, 0, ImplicitOps | Stack,
191 ImplicitOps, SrcImmByte, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0xD0 - 0xD7 */
193 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
194 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
195 0, 0, 0, 0,
196 /* 0xD8 - 0xDF */
197 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300198 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300199 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300200 ByteOp | SrcImmUByte, SrcImmUByte,
201 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300202 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300203 SrcImm | Stack, SrcImm | ImplicitOps,
Avi Kivityee3d29e2009-05-18 16:15:20 +0300204 SrcImmU | Src2Imm16, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300205 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
206 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 /* 0xF0 - 0xF7 */
208 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200209 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700211 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300212 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213};
214
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100215static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216 /* 0x00 - 0x0F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200217 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200218 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 /* 0x10 - 0x1F */
220 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
221 /* 0x20 - 0x2F */
222 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
223 0, 0, 0, 0, 0, 0, 0, 0,
224 /* 0x30 - 0x3F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200225 ImplicitOps, 0, ImplicitOps, 0,
226 ImplicitOps, ImplicitOps, 0, 0,
227 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x40 - 0x47 */
229 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
230 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
231 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
232 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
233 /* 0x48 - 0x4F */
234 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
235 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
236 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
237 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
238 /* 0x50 - 0x5F */
239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240 /* 0x60 - 0x6F */
241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 /* 0x70 - 0x7F */
243 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300245 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
246 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247 /* 0x90 - 0x9F */
248 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300250 ImplicitOps | Stack, ImplicitOps | Stack,
251 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100252 DstMem | SrcReg | Src2ImmByte | ModRM,
253 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300255 ImplicitOps | Stack, ImplicitOps | Stack,
256 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100257 DstMem | SrcReg | Src2ImmByte | ModRM,
258 DstMem | SrcReg | Src2CL | ModRM,
259 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 /* 0xB0 - 0xB7 */
261 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800262 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
264 DstReg | SrcMem16 | ModRM | Mov,
265 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800266 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
268 DstReg | SrcMem16 | ModRM | Mov,
269 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800270 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
271 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272 /* 0xD0 - 0xDF */
273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274 /* 0xE0 - 0xEF */
275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276 /* 0xF0 - 0xFF */
277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
278};
279
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100280static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200281 [Group1_80*8] =
282 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
286 [Group1_81*8] =
287 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
288 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
289 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
290 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
291 [Group1_82*8] =
292 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
293 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
294 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
295 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
296 [Group1_83*8] =
297 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
298 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
299 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
300 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200301 [Group1A*8] =
302 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200303 [Group3_Byte*8] =
304 ByteOp | SrcImm | DstMem | ModRM, 0,
305 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
306 0, 0, 0, 0,
307 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400308 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300309 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200310 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200311 [Group4*8] =
312 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
313 0, 0, 0, 0, 0, 0,
314 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300315 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
316 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300317 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200318 [Group7*8] =
319 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300320 SrcNone | ModRM | DstMem | Mov, 0,
321 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200322};
323
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100324static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200325 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000326 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300327 SrcNone | ModRM | DstMem | Mov, 0,
328 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200329};
330
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200332#define EFLG_VM (1<<17)
333#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334#define EFLG_OF (1<<11)
335#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200336#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337#define EFLG_SF (1<<7)
338#define EFLG_ZF (1<<6)
339#define EFLG_AF (1<<4)
340#define EFLG_PF (1<<2)
341#define EFLG_CF (1<<0)
342
343/*
344 * Instruction emulation:
345 * Most instructions are emulated directly via a fragment of inline assembly
346 * code. This allows us to save/restore EFLAGS and thus very easily pick up
347 * any modified flags.
348 */
349
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800350#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351#define _LO32 "k" /* force 32-bit operand */
352#define _STK "%%rsp" /* stack pointer */
353#elif defined(__i386__)
354#define _LO32 "" /* force 32-bit operand */
355#define _STK "%%esp" /* stack pointer */
356#endif
357
358/*
359 * These EFLAGS bits are restored from saved value during emulation, and
360 * any changes are written back to the saved value after emulation.
361 */
362#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
363
364/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200365#define _PRE_EFLAGS(_sav, _msk, _tmp) \
366 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
367 "movl %"_sav",%"_LO32 _tmp"; " \
368 "push %"_tmp"; " \
369 "push %"_tmp"; " \
370 "movl %"_msk",%"_LO32 _tmp"; " \
371 "andl %"_LO32 _tmp",("_STK"); " \
372 "pushf; " \
373 "notl %"_LO32 _tmp"; " \
374 "andl %"_LO32 _tmp",("_STK"); " \
375 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
376 "pop %"_tmp"; " \
377 "orl %"_LO32 _tmp",("_STK"); " \
378 "popf; " \
379 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380
381/* After executing instruction: write-back necessary bits in EFLAGS. */
382#define _POST_EFLAGS(_sav, _msk, _tmp) \
383 /* _sav |= EFLAGS & _msk; */ \
384 "pushf; " \
385 "pop %"_tmp"; " \
386 "andl %"_msk",%"_LO32 _tmp"; " \
387 "orl %"_LO32 _tmp",%"_sav"; "
388
Avi Kivitydda96d82008-11-26 15:14:10 +0200389#ifdef CONFIG_X86_64
390#define ON64(x) x
391#else
392#define ON64(x)
393#endif
394
Avi Kivity6b7ad612008-11-26 15:30:45 +0200395#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
396 do { \
397 __asm__ __volatile__ ( \
398 _PRE_EFLAGS("0", "4", "2") \
399 _op _suffix " %"_x"3,%1; " \
400 _POST_EFLAGS("0", "4", "2") \
401 : "=m" (_eflags), "=m" ((_dst).val), \
402 "=&r" (_tmp) \
403 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200404 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200405
406
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407/* Raw emulation: instruction has two explicit operands. */
408#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200409 do { \
410 unsigned long _tmp; \
411 \
412 switch ((_dst).bytes) { \
413 case 2: \
414 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
415 break; \
416 case 4: \
417 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
418 break; \
419 case 8: \
420 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
421 break; \
422 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423 } while (0)
424
425#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
426 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200427 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400428 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200430 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 break; \
432 default: \
433 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
434 _wx, _wy, _lx, _ly, _qx, _qy); \
435 break; \
436 } \
437 } while (0)
438
439/* Source operand is byte-sized and may be restricted to just %cl. */
440#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
441 __emulate_2op(_op, _src, _dst, _eflags, \
442 "b", "c", "b", "c", "b", "c", "b", "c")
443
444/* Source operand is byte, word, long or quad sized. */
445#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
446 __emulate_2op(_op, _src, _dst, _eflags, \
447 "b", "q", "w", "r", _LO32, "r", "", "r")
448
449/* Source operand is word, long or quad sized. */
450#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
451 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
452 "w", "r", _LO32, "r", "", "r")
453
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100454/* Instruction has three operands and one operand is stored in ECX register */
455#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
456 do { \
457 unsigned long _tmp; \
458 _type _clv = (_cl).val; \
459 _type _srcv = (_src).val; \
460 _type _dstv = (_dst).val; \
461 \
462 __asm__ __volatile__ ( \
463 _PRE_EFLAGS("0", "5", "2") \
464 _op _suffix " %4,%1 \n" \
465 _POST_EFLAGS("0", "5", "2") \
466 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
467 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
468 ); \
469 \
470 (_cl).val = (unsigned long) _clv; \
471 (_src).val = (unsigned long) _srcv; \
472 (_dst).val = (unsigned long) _dstv; \
473 } while (0)
474
475#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
476 do { \
477 switch ((_dst).bytes) { \
478 case 2: \
479 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
480 "w", unsigned short); \
481 break; \
482 case 4: \
483 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
484 "l", unsigned int); \
485 break; \
486 case 8: \
487 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
488 "q", unsigned long)); \
489 break; \
490 } \
491 } while (0)
492
Avi Kivitydda96d82008-11-26 15:14:10 +0200493#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 do { \
495 unsigned long _tmp; \
496 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200497 __asm__ __volatile__ ( \
498 _PRE_EFLAGS("0", "3", "2") \
499 _op _suffix " %1; " \
500 _POST_EFLAGS("0", "3", "2") \
501 : "=m" (_eflags), "+m" ((_dst).val), \
502 "=&r" (_tmp) \
503 : "i" (EFLAGS_MASK)); \
504 } while (0)
505
506/* Instruction has only one explicit operand (no source operand). */
507#define emulate_1op(_op, _dst, _eflags) \
508 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400509 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200510 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
511 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
512 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
513 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 } \
515 } while (0)
516
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517/* Fetch next part of the instruction being emulated. */
518#define insn_fetch(_type, _size, _eip) \
519({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200520 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400521 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 goto done; \
523 (_eip) += (_size); \
524 (_type)_x; \
525})
526
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800527static inline unsigned long ad_mask(struct decode_cache *c)
528{
529 return (1UL << (c->ad_bytes << 3)) - 1;
530}
531
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800533static inline unsigned long
534address_mask(struct decode_cache *c, unsigned long reg)
535{
536 if (c->ad_bytes == sizeof(unsigned long))
537 return reg;
538 else
539 return reg & ad_mask(c);
540}
541
542static inline unsigned long
543register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
544{
545 return base + address_mask(c, reg);
546}
547
Harvey Harrison7a9572752008-02-19 07:40:41 -0800548static inline void
549register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
550{
551 if (c->ad_bytes == sizeof(unsigned long))
552 *reg += inc;
553 else
554 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
555}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556
Harvey Harrison7a9572752008-02-19 07:40:41 -0800557static inline void jmp_rel(struct decode_cache *c, int rel)
558{
559 register_address_increment(c, &c->eip, rel);
560}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300561
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300562static void set_seg_override(struct decode_cache *c, int seg)
563{
564 c->has_seg_override = true;
565 c->seg_override = seg;
566}
567
568static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
569{
570 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
571 return 0;
572
573 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
574}
575
576static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
577 struct decode_cache *c)
578{
579 if (!c->has_seg_override)
580 return 0;
581
582 return seg_base(ctxt, c->seg_override);
583}
584
585static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
586{
587 return seg_base(ctxt, VCPU_SREG_ES);
588}
589
590static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
591{
592 return seg_base(ctxt, VCPU_SREG_SS);
593}
594
Avi Kivity62266862007-11-20 13:15:52 +0200595static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
596 struct x86_emulate_ops *ops,
597 unsigned long linear, u8 *dest)
598{
599 struct fetch_cache *fc = &ctxt->decode.fetch;
600 int rc;
601 int size;
602
603 if (linear < fc->start || linear >= fc->end) {
604 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
605 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
606 if (rc)
607 return rc;
608 fc->start = linear;
609 fc->end = linear + size;
610 }
611 *dest = fc->data[linear - fc->start];
612 return 0;
613}
614
615static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
616 struct x86_emulate_ops *ops,
617 unsigned long eip, void *dest, unsigned size)
618{
619 int rc = 0;
620
621 eip += ctxt->cs_base;
622 while (size--) {
623 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
624 if (rc)
625 return rc;
626 }
627 return 0;
628}
629
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000630/*
631 * Given the 'reg' portion of a ModRM byte, and a register block, return a
632 * pointer into the block that addresses the relevant register.
633 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
634 */
635static void *decode_register(u8 modrm_reg, unsigned long *regs,
636 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637{
638 void *p;
639
640 p = &regs[modrm_reg];
641 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
642 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
643 return p;
644}
645
646static int read_descriptor(struct x86_emulate_ctxt *ctxt,
647 struct x86_emulate_ops *ops,
648 void *ptr,
649 u16 *size, unsigned long *address, int op_bytes)
650{
651 int rc;
652
653 if (op_bytes == 2)
654 op_bytes = 3;
655 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300656 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
657 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 if (rc)
659 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300660 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
661 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 return rc;
663}
664
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300665static int test_cc(unsigned int condition, unsigned int flags)
666{
667 int rc = 0;
668
669 switch ((condition & 15) >> 1) {
670 case 0: /* o */
671 rc |= (flags & EFLG_OF);
672 break;
673 case 1: /* b/c/nae */
674 rc |= (flags & EFLG_CF);
675 break;
676 case 2: /* z/e */
677 rc |= (flags & EFLG_ZF);
678 break;
679 case 3: /* be/na */
680 rc |= (flags & (EFLG_CF|EFLG_ZF));
681 break;
682 case 4: /* s */
683 rc |= (flags & EFLG_SF);
684 break;
685 case 5: /* p/pe */
686 rc |= (flags & EFLG_PF);
687 break;
688 case 7: /* le/ng */
689 rc |= (flags & EFLG_ZF);
690 /* fall through */
691 case 6: /* l/nge */
692 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
693 break;
694 }
695
696 /* Odd condition identifiers (lsb == 1) have inverted sense. */
697 return (!!rc ^ (condition & 1));
698}
699
Avi Kivity3c118e22007-10-31 10:27:04 +0200700static void decode_register_operand(struct operand *op,
701 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200702 int inhibit_bytereg)
703{
Avi Kivity33615aa2007-10-31 11:15:56 +0200704 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200705 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200706
707 if (!(c->d & ModRM))
708 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200709 op->type = OP_REG;
710 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200711 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200712 op->val = *(u8 *)op->ptr;
713 op->bytes = 1;
714 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200715 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200716 op->bytes = c->op_bytes;
717 switch (op->bytes) {
718 case 2:
719 op->val = *(u16 *)op->ptr;
720 break;
721 case 4:
722 op->val = *(u32 *)op->ptr;
723 break;
724 case 8:
725 op->val = *(u64 *) op->ptr;
726 break;
727 }
728 }
729 op->orig_val = op->val;
730}
731
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732static int decode_modrm(struct x86_emulate_ctxt *ctxt,
733 struct x86_emulate_ops *ops)
734{
735 struct decode_cache *c = &ctxt->decode;
736 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700737 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200738 int rc = 0;
739
740 if (c->rex_prefix) {
741 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
742 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
743 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
744 }
745
746 c->modrm = insn_fetch(u8, 1, c->eip);
747 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
748 c->modrm_reg |= (c->modrm & 0x38) >> 3;
749 c->modrm_rm |= (c->modrm & 0x07);
750 c->modrm_ea = 0;
751 c->use_modrm_ea = 1;
752
753 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300754 c->modrm_ptr = decode_register(c->modrm_rm,
755 c->regs, c->d & ByteOp);
756 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200757 return rc;
758 }
759
760 if (c->ad_bytes == 2) {
761 unsigned bx = c->regs[VCPU_REGS_RBX];
762 unsigned bp = c->regs[VCPU_REGS_RBP];
763 unsigned si = c->regs[VCPU_REGS_RSI];
764 unsigned di = c->regs[VCPU_REGS_RDI];
765
766 /* 16-bit ModR/M decode. */
767 switch (c->modrm_mod) {
768 case 0:
769 if (c->modrm_rm == 6)
770 c->modrm_ea += insn_fetch(u16, 2, c->eip);
771 break;
772 case 1:
773 c->modrm_ea += insn_fetch(s8, 1, c->eip);
774 break;
775 case 2:
776 c->modrm_ea += insn_fetch(u16, 2, c->eip);
777 break;
778 }
779 switch (c->modrm_rm) {
780 case 0:
781 c->modrm_ea += bx + si;
782 break;
783 case 1:
784 c->modrm_ea += bx + di;
785 break;
786 case 2:
787 c->modrm_ea += bp + si;
788 break;
789 case 3:
790 c->modrm_ea += bp + di;
791 break;
792 case 4:
793 c->modrm_ea += si;
794 break;
795 case 5:
796 c->modrm_ea += di;
797 break;
798 case 6:
799 if (c->modrm_mod != 0)
800 c->modrm_ea += bp;
801 break;
802 case 7:
803 c->modrm_ea += bx;
804 break;
805 }
806 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
807 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300808 if (!c->has_seg_override)
809 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 c->modrm_ea = (u16)c->modrm_ea;
811 } else {
812 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700813 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200814 sib = insn_fetch(u8, 1, c->eip);
815 index_reg |= (sib >> 3) & 7;
816 base_reg |= sib & 7;
817 scale = sib >> 6;
818
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700819 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
820 c->modrm_ea += insn_fetch(s32, 4, c->eip);
821 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200822 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700823 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200824 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700825 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
826 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700827 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700828 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200829 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200830 switch (c->modrm_mod) {
831 case 0:
832 if (c->modrm_rm == 5)
833 c->modrm_ea += insn_fetch(s32, 4, c->eip);
834 break;
835 case 1:
836 c->modrm_ea += insn_fetch(s8, 1, c->eip);
837 break;
838 case 2:
839 c->modrm_ea += insn_fetch(s32, 4, c->eip);
840 break;
841 }
842 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200843done:
844 return rc;
845}
846
847static int decode_abs(struct x86_emulate_ctxt *ctxt,
848 struct x86_emulate_ops *ops)
849{
850 struct decode_cache *c = &ctxt->decode;
851 int rc = 0;
852
853 switch (c->ad_bytes) {
854 case 2:
855 c->modrm_ea = insn_fetch(u16, 2, c->eip);
856 break;
857 case 4:
858 c->modrm_ea = insn_fetch(u32, 4, c->eip);
859 break;
860 case 8:
861 c->modrm_ea = insn_fetch(u64, 8, c->eip);
862 break;
863 }
864done:
865 return rc;
866}
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200869x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200871 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200874 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
876 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
Laurent Viviere4e03de2007-09-18 11:52:50 +0200878 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300879 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300880 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800881 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882
883 switch (mode) {
884 case X86EMUL_MODE_REAL:
885 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200886 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 break;
888 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200889 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800891#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200893 def_op_bytes = 4;
894 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 break;
896#endif
897 default:
898 return -1;
899 }
900
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200901 c->op_bytes = def_op_bytes;
902 c->ad_bytes = def_ad_bytes;
903
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200905 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200906 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200908 /* switch between 2/4 bytes */
909 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
911 case 0x67: /* address-size override */
912 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200913 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200914 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200916 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200917 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300920 case 0x2e: /* CS override */
921 case 0x36: /* SS override */
922 case 0x3e: /* DS override */
923 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 break;
925 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300927 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200929 case 0x40 ... 0x4f: /* REX */
930 if (mode != X86EMUL_MODE_PROT64)
931 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200932 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200933 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200935 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200937 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100938 c->rep_prefix = REPNE_PREFIX;
939 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100941 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 default:
944 goto done_prefixes;
945 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200946
947 /* Any legacy prefix after a REX prefix nullifies its effect. */
948
Avi Kivity33615aa2007-10-31 11:15:56 +0200949 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 }
951
952done_prefixes:
953
954 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200955 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200956 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200957 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
959 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200960 c->d = opcode_table[c->b];
961 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200963 if (c->b == 0x0f) {
964 c->twobyte = 1;
965 c->b = insn_fetch(u8, 1, c->eip);
966 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200968 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Avi Kivitye09d0822008-01-18 12:38:59 +0200970 if (c->d & Group) {
971 group = c->d & GroupMask;
972 c->modrm = insn_fetch(u8, 1, c->eip);
973 --c->eip;
974
975 group = (group << 3) + ((c->modrm >> 3) & 7);
976 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
977 c->d = group2_table[group];
978 else
979 c->d = group_table[group];
980 }
981
982 /* Unrecognised? */
983 if (c->d == 0) {
984 DPRINTF("Cannot emulate %02x\n", c->b);
985 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 }
987
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200988 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
989 c->op_bytes = 8;
990
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200992 if (c->d & ModRM)
993 rc = decode_modrm(ctxt, ops);
994 else if (c->d & MemAbs)
995 rc = decode_abs(ctxt, ops);
996 if (rc)
997 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300999 if (!c->has_seg_override)
1000 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001001
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001002 if (!(!c->twobyte && c->b == 0x8d))
1003 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001004
1005 if (c->ad_bytes != 8)
1006 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 /*
1008 * Decode and fetch the source operand: register, memory
1009 * or immediate.
1010 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 case SrcNone:
1013 break;
1014 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001015 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 break;
1017 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 goto srcmem_common;
1020 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001021 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 goto srcmem_common;
1023 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001024 c->src.bytes = (c->d & ByteOp) ? 1 :
1025 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001026 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001027 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001028 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001029 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001030 /*
1031 * For instructions with a ModR/M byte, switch to register
1032 * access if Mod = 3.
1033 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001034 if ((c->d & ModRM) && c->modrm_mod == 3) {
1035 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001036 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001037 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001038 break;
1039 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001040 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 break;
1042 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001043 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001044 c->src.type = OP_IMM;
1045 c->src.ptr = (unsigned long *)c->eip;
1046 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1047 if (c->src.bytes == 8)
1048 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001050 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001052 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 break;
1054 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001055 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 break;
1057 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001058 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 break;
1060 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001061 if ((c->d & SrcMask) == SrcImmU) {
1062 switch (c->src.bytes) {
1063 case 1:
1064 c->src.val &= 0xff;
1065 break;
1066 case 2:
1067 c->src.val &= 0xffff;
1068 break;
1069 case 4:
1070 c->src.val &= 0xffffffff;
1071 break;
1072 }
1073 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 break;
1075 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001076 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.type = OP_IMM;
1078 c->src.ptr = (unsigned long *)c->eip;
1079 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001080 if ((c->d & SrcMask) == SrcImmByte)
1081 c->src.val = insn_fetch(s8, 1, c->eip);
1082 else
1083 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001085 case SrcOne:
1086 c->src.bytes = 1;
1087 c->src.val = 1;
1088 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 }
1090
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001091 /*
1092 * Decode and fetch the second source operand: register, memory
1093 * or immediate.
1094 */
1095 switch (c->d & Src2Mask) {
1096 case Src2None:
1097 break;
1098 case Src2CL:
1099 c->src2.bytes = 1;
1100 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1101 break;
1102 case Src2ImmByte:
1103 c->src2.type = OP_IMM;
1104 c->src2.ptr = (unsigned long *)c->eip;
1105 c->src2.bytes = 1;
1106 c->src2.val = insn_fetch(u8, 1, c->eip);
1107 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001108 case Src2Imm16:
1109 c->src2.type = OP_IMM;
1110 c->src2.ptr = (unsigned long *)c->eip;
1111 c->src2.bytes = 2;
1112 c->src2.val = insn_fetch(u16, 2, c->eip);
1113 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001114 case Src2One:
1115 c->src2.bytes = 1;
1116 c->src2.val = 1;
1117 break;
1118 }
1119
Avi Kivity038e51d2007-01-22 20:40:40 -08001120 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001122 case ImplicitOps:
1123 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001124 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001125 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001126 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001127 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001128 break;
1129 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001131 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001132 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001133 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001134 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001135 break;
1136 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001137 c->dst.type = OP_MEM;
1138 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001139 case DstAcc:
1140 c->dst.type = OP_REG;
1141 c->dst.bytes = c->op_bytes;
1142 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1143 switch (c->op_bytes) {
1144 case 1:
1145 c->dst.val = *(u8 *)c->dst.ptr;
1146 break;
1147 case 2:
1148 c->dst.val = *(u16 *)c->dst.ptr;
1149 break;
1150 case 4:
1151 c->dst.val = *(u32 *)c->dst.ptr;
1152 break;
1153 }
1154 c->dst.orig_val = c->dst.val;
1155 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001156 }
1157
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001158 if (c->rip_relative)
1159 c->modrm_ea += c->eip;
1160
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001161done:
1162 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1163}
1164
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1166{
1167 struct decode_cache *c = &ctxt->decode;
1168
1169 c->dst.type = OP_MEM;
1170 c->dst.bytes = c->op_bytes;
1171 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001172 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001173 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001174 c->regs[VCPU_REGS_RSP]);
1175}
1176
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001177static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001178 struct x86_emulate_ops *ops,
1179 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001180{
1181 struct decode_cache *c = &ctxt->decode;
1182 int rc;
1183
Avi Kivity781d0ed2008-11-27 18:00:28 +02001184 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1185 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001186 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187 if (rc != 0)
1188 return rc;
1189
Avi Kivity350f69d2009-01-05 11:12:40 +02001190 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001191 return rc;
1192}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001193
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001194static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1195{
1196 struct decode_cache *c = &ctxt->decode;
1197 struct kvm_segment segment;
1198
1199 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1200
1201 c->src.val = segment.selector;
1202 emulate_push(ctxt);
1203}
1204
1205static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1206 struct x86_emulate_ops *ops, int seg)
1207{
1208 struct decode_cache *c = &ctxt->decode;
1209 unsigned long selector;
1210 int rc;
1211
1212 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1213 if (rc != 0)
1214 return rc;
1215
1216 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg);
1217 return rc;
1218}
1219
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001220static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1221 struct x86_emulate_ops *ops)
1222{
1223 struct decode_cache *c = &ctxt->decode;
1224 int rc;
1225
Avi Kivity350f69d2009-01-05 11:12:40 +02001226 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001227 if (rc != 0)
1228 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001229 return 0;
1230}
1231
Laurent Vivier05f086f2007-09-24 11:10:55 +02001232static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001234 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001235 switch (c->modrm_reg) {
1236 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001237 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001238 break;
1239 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001240 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001241 break;
1242 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001243 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001244 break;
1245 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001246 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001247 break;
1248 case 4: /* sal/shl */
1249 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001250 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251 break;
1252 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001253 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001254 break;
1255 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001256 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001257 break;
1258 }
1259}
1260
1261static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001262 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001263{
1264 struct decode_cache *c = &ctxt->decode;
1265 int rc = 0;
1266
1267 switch (c->modrm_reg) {
1268 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001269 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270 break;
1271 case 2: /* not */
1272 c->dst.val = ~c->dst.val;
1273 break;
1274 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001275 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001276 break;
1277 default:
1278 DPRINTF("Cannot emulate %02x\n", c->b);
1279 rc = X86EMUL_UNHANDLEABLE;
1280 break;
1281 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001282 return rc;
1283}
1284
1285static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001286 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001287{
1288 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001289
1290 switch (c->modrm_reg) {
1291 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001292 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293 break;
1294 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001295 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001296 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001297 case 2: /* call near abs */ {
1298 long int old_eip;
1299 old_eip = c->eip;
1300 c->eip = c->src.val;
1301 c->src.val = old_eip;
1302 emulate_push(ctxt);
1303 break;
1304 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001305 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001306 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001307 break;
1308 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001309 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001310 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311 }
1312 return 0;
1313}
1314
1315static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1316 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001317 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001318{
1319 struct decode_cache *c = &ctxt->decode;
1320 u64 old, new;
1321 int rc;
1322
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001323 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001324 if (rc != 0)
1325 return rc;
1326
1327 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1328 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1329
1330 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1331 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001332 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001333
1334 } else {
1335 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1336 (u32) c->regs[VCPU_REGS_RBX];
1337
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001338 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001339 if (rc != 0)
1340 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001341 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001342 }
1343 return 0;
1344}
1345
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001346static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1347 struct x86_emulate_ops *ops)
1348{
1349 struct decode_cache *c = &ctxt->decode;
1350 int rc;
1351 unsigned long cs;
1352
1353 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1354 if (rc)
1355 return rc;
1356 if (c->op_bytes == 4)
1357 c->eip = (u32)c->eip;
1358 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1359 if (rc)
1360 return rc;
1361 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1362 return rc;
1363}
1364
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365static inline int writeback(struct x86_emulate_ctxt *ctxt,
1366 struct x86_emulate_ops *ops)
1367{
1368 int rc;
1369 struct decode_cache *c = &ctxt->decode;
1370
1371 switch (c->dst.type) {
1372 case OP_REG:
1373 /* The 4-byte case *is* correct:
1374 * in 64-bit mode we zero-extend.
1375 */
1376 switch (c->dst.bytes) {
1377 case 1:
1378 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1379 break;
1380 case 2:
1381 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1382 break;
1383 case 4:
1384 *c->dst.ptr = (u32)c->dst.val;
1385 break; /* 64b: zero-ext */
1386 case 8:
1387 *c->dst.ptr = c->dst.val;
1388 break;
1389 }
1390 break;
1391 case OP_MEM:
1392 if (c->lock_prefix)
1393 rc = ops->cmpxchg_emulated(
1394 (unsigned long)c->dst.ptr,
1395 &c->dst.orig_val,
1396 &c->dst.val,
1397 c->dst.bytes,
1398 ctxt->vcpu);
1399 else
1400 rc = ops->write_emulated(
1401 (unsigned long)c->dst.ptr,
1402 &c->dst.val,
1403 c->dst.bytes,
1404 ctxt->vcpu);
1405 if (rc != 0)
1406 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001407 break;
1408 case OP_NONE:
1409 /* no writeback */
1410 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001411 default:
1412 break;
1413 }
1414 return 0;
1415}
1416
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301417static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001418{
1419 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1420 /*
1421 * an sti; sti; sequence only disable interrupts for the first
1422 * instruction. So, if the last instruction, be it emulated or
1423 * not, left the system with the INT_STI flag enabled, it
1424 * means that the last instruction is an sti. We should not
1425 * leave the flag on in this case. The same goes for mov ss
1426 */
1427 if (!(int_shadow & mask))
1428 ctxt->interruptibility = mask;
1429}
1430
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001431static inline void
1432setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1433 struct kvm_segment *cs, struct kvm_segment *ss)
1434{
1435 memset(cs, 0, sizeof(struct kvm_segment));
1436 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1437 memset(ss, 0, sizeof(struct kvm_segment));
1438
1439 cs->l = 0; /* will be adjusted later */
1440 cs->base = 0; /* flat segment */
1441 cs->g = 1; /* 4kb granularity */
1442 cs->limit = 0xffffffff; /* 4GB limit */
1443 cs->type = 0x0b; /* Read, Execute, Accessed */
1444 cs->s = 1;
1445 cs->dpl = 0; /* will be adjusted later */
1446 cs->present = 1;
1447 cs->db = 1;
1448
1449 ss->unusable = 0;
1450 ss->base = 0; /* flat segment */
1451 ss->limit = 0xffffffff; /* 4GB limit */
1452 ss->g = 1; /* 4kb granularity */
1453 ss->s = 1;
1454 ss->type = 0x03; /* Read/Write, Accessed */
1455 ss->db = 1; /* 32bit stack segment */
1456 ss->dpl = 0;
1457 ss->present = 1;
1458}
1459
1460static int
1461emulate_syscall(struct x86_emulate_ctxt *ctxt)
1462{
1463 struct decode_cache *c = &ctxt->decode;
1464 struct kvm_segment cs, ss;
1465 u64 msr_data;
1466
1467 /* syscall is not available in real mode */
1468 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
1469 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE))
1470 return -1;
1471
1472 setup_syscalls_segments(ctxt, &cs, &ss);
1473
1474 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1475 msr_data >>= 32;
1476 cs.selector = (u16)(msr_data & 0xfffc);
1477 ss.selector = (u16)(msr_data + 8);
1478
1479 if (is_long_mode(ctxt->vcpu)) {
1480 cs.db = 0;
1481 cs.l = 1;
1482 }
1483 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1484 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1485
1486 c->regs[VCPU_REGS_RCX] = c->eip;
1487 if (is_long_mode(ctxt->vcpu)) {
1488#ifdef CONFIG_X86_64
1489 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1490
1491 kvm_x86_ops->get_msr(ctxt->vcpu,
1492 ctxt->mode == X86EMUL_MODE_PROT64 ?
1493 MSR_LSTAR : MSR_CSTAR, &msr_data);
1494 c->eip = msr_data;
1495
1496 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1497 ctxt->eflags &= ~(msr_data | EFLG_RF);
1498#endif
1499 } else {
1500 /* legacy mode */
1501 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1502 c->eip = (u32)msr_data;
1503
1504 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1505 }
1506
1507 return 0;
1508}
1509
Andre Przywara8c604352009-06-18 12:56:01 +02001510static int
1511emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1512{
1513 struct decode_cache *c = &ctxt->decode;
1514 struct kvm_segment cs, ss;
1515 u64 msr_data;
1516
1517 /* inject #UD if LOCK prefix is used */
1518 if (c->lock_prefix)
1519 return -1;
1520
1521 /* inject #GP if in real mode or paging is disabled */
1522 if (ctxt->mode == X86EMUL_MODE_REAL ||
1523 !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1524 kvm_inject_gp(ctxt->vcpu, 0);
1525 return -1;
1526 }
1527
1528 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1529 * Therefore, we inject an #UD.
1530 */
1531 if (ctxt->mode == X86EMUL_MODE_PROT64)
1532 return -1;
1533
1534 setup_syscalls_segments(ctxt, &cs, &ss);
1535
1536 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1537 switch (ctxt->mode) {
1538 case X86EMUL_MODE_PROT32:
1539 if ((msr_data & 0xfffc) == 0x0) {
1540 kvm_inject_gp(ctxt->vcpu, 0);
1541 return -1;
1542 }
1543 break;
1544 case X86EMUL_MODE_PROT64:
1545 if (msr_data == 0x0) {
1546 kvm_inject_gp(ctxt->vcpu, 0);
1547 return -1;
1548 }
1549 break;
1550 }
1551
1552 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1553 cs.selector = (u16)msr_data;
1554 cs.selector &= ~SELECTOR_RPL_MASK;
1555 ss.selector = cs.selector + 8;
1556 ss.selector &= ~SELECTOR_RPL_MASK;
1557 if (ctxt->mode == X86EMUL_MODE_PROT64
1558 || is_long_mode(ctxt->vcpu)) {
1559 cs.db = 0;
1560 cs.l = 1;
1561 }
1562
1563 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1564 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1565
1566 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1567 c->eip = msr_data;
1568
1569 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1570 c->regs[VCPU_REGS_RSP] = msr_data;
1571
1572 return 0;
1573}
1574
Andre Przywara4668f052009-06-18 12:56:02 +02001575static int
1576emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1577{
1578 struct decode_cache *c = &ctxt->decode;
1579 struct kvm_segment cs, ss;
1580 u64 msr_data;
1581 int usermode;
1582
1583 /* inject #UD if LOCK prefix is used */
1584 if (c->lock_prefix)
1585 return -1;
1586
1587 /* inject #GP if in real mode or paging is disabled */
1588 if (ctxt->mode == X86EMUL_MODE_REAL
1589 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1590 kvm_inject_gp(ctxt->vcpu, 0);
1591 return -1;
1592 }
1593
1594 /* sysexit must be called from CPL 0 */
1595 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1596 kvm_inject_gp(ctxt->vcpu, 0);
1597 return -1;
1598 }
1599
1600 setup_syscalls_segments(ctxt, &cs, &ss);
1601
1602 if ((c->rex_prefix & 0x8) != 0x0)
1603 usermode = X86EMUL_MODE_PROT64;
1604 else
1605 usermode = X86EMUL_MODE_PROT32;
1606
1607 cs.dpl = 3;
1608 ss.dpl = 3;
1609 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1610 switch (usermode) {
1611 case X86EMUL_MODE_PROT32:
1612 cs.selector = (u16)(msr_data + 16);
1613 if ((msr_data & 0xfffc) == 0x0) {
1614 kvm_inject_gp(ctxt->vcpu, 0);
1615 return -1;
1616 }
1617 ss.selector = (u16)(msr_data + 24);
1618 break;
1619 case X86EMUL_MODE_PROT64:
1620 cs.selector = (u16)(msr_data + 32);
1621 if (msr_data == 0x0) {
1622 kvm_inject_gp(ctxt->vcpu, 0);
1623 return -1;
1624 }
1625 ss.selector = cs.selector + 8;
1626 cs.db = 0;
1627 cs.l = 1;
1628 break;
1629 }
1630 cs.selector |= SELECTOR_RPL_MASK;
1631 ss.selector |= SELECTOR_RPL_MASK;
1632
1633 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1634 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1635
1636 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1637 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1638
1639 return 0;
1640}
1641
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001642int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001643x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001644{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001645 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001646 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001647 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001648 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001649 unsigned int port;
1650 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001651 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001652
Glauber Costa310b5d32009-05-12 16:21:06 -04001653 ctxt->interruptibility = 0;
1654
Laurent Vivier34273182007-09-18 11:27:37 +02001655 /* Shadow copy of register state. Committed on successful emulation.
1656 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1657 * modify them.
1658 */
1659
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001660 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001661 saved_eip = c->eip;
1662
Avi Kivityc7e75a32007-10-28 16:34:25 +02001663 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001664 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001665
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001666 if (c->rep_prefix && (c->d & String)) {
1667 /* All REP prefixes have the same first termination condition */
1668 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001669 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001670 goto done;
1671 }
1672 /* The second termination condition only applies for REPE
1673 * and REPNE. Test if the repeat string operation prefix is
1674 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1675 * corresponding termination condition according to:
1676 * - if REPE/REPZ and ZF = 0 then done
1677 * - if REPNE/REPNZ and ZF = 1 then done
1678 */
1679 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1680 (c->b == 0xae) || (c->b == 0xaf)) {
1681 if ((c->rep_prefix == REPE_PREFIX) &&
1682 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001683 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001684 goto done;
1685 }
1686 if ((c->rep_prefix == REPNE_PREFIX) &&
1687 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001688 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001689 goto done;
1690 }
1691 }
1692 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001693 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001694 }
1695
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001696 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001697 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001698 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001699 rc = ops->read_emulated((unsigned long)c->src.ptr,
1700 &c->src.val,
1701 c->src.bytes,
1702 ctxt->vcpu);
1703 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001704 goto done;
1705 c->src.orig_val = c->src.val;
1706 }
1707
1708 if ((c->d & DstMask) == ImplicitOps)
1709 goto special_insn;
1710
1711
1712 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001713 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001714 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1715 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001716 if (c->d & BitOp) {
1717 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001718
Laurent Viviere4e03de2007-09-18 11:52:50 +02001719 c->dst.ptr = (void *)c->dst.ptr +
1720 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001721 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001722 if (!(c->d & Mov) &&
1723 /* optimisation - avoid slow emulated read */
1724 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1725 &c->dst.val,
1726 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001727 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001728 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001729 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001730
Avi Kivity018a98d2007-11-27 19:30:56 +02001731special_insn:
1732
Laurent Viviere4e03de2007-09-18 11:52:50 +02001733 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 goto twobyte_insn;
1735
Laurent Viviere4e03de2007-09-18 11:52:50 +02001736 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737 case 0x00 ... 0x05:
1738 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001739 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001741 case 0x06: /* push es */
1742 if (ctxt->mode == X86EMUL_MODE_PROT64)
1743 goto cannot_emulate;
1744
1745 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1746 break;
1747 case 0x07: /* pop es */
1748 if (ctxt->mode == X86EMUL_MODE_PROT64)
1749 goto cannot_emulate;
1750
1751 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1752 if (rc != 0)
1753 goto done;
1754 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755 case 0x08 ... 0x0d:
1756 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001757 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001759 case 0x0e: /* push cs */
1760 if (ctxt->mode == X86EMUL_MODE_PROT64)
1761 goto cannot_emulate;
1762
1763 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1764 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 case 0x10 ... 0x15:
1766 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001767 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001768 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001769 case 0x16: /* push ss */
1770 if (ctxt->mode == X86EMUL_MODE_PROT64)
1771 goto cannot_emulate;
1772
1773 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1774 break;
1775 case 0x17: /* pop ss */
1776 if (ctxt->mode == X86EMUL_MODE_PROT64)
1777 goto cannot_emulate;
1778
1779 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1780 if (rc != 0)
1781 goto done;
1782 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 case 0x18 ... 0x1d:
1784 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001785 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001787 case 0x1e: /* push ds */
1788 if (ctxt->mode == X86EMUL_MODE_PROT64)
1789 goto cannot_emulate;
1790
1791 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1792 break;
1793 case 0x1f: /* pop ds */
1794 if (ctxt->mode == X86EMUL_MODE_PROT64)
1795 goto cannot_emulate;
1796
1797 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1798 if (rc != 0)
1799 goto done;
1800 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001801 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001803 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 break;
1805 case 0x28 ... 0x2d:
1806 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001807 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808 break;
1809 case 0x30 ... 0x35:
1810 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001811 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812 break;
1813 case 0x38 ... 0x3d:
1814 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001815 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001817 case 0x40 ... 0x47: /* inc r16/r32 */
1818 emulate_1op("inc", c->dst, ctxt->eflags);
1819 break;
1820 case 0x48 ... 0x4f: /* dec r16/r32 */
1821 emulate_1op("dec", c->dst, ctxt->eflags);
1822 break;
1823 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001824 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001825 break;
1826 case 0x58 ... 0x5f: /* pop reg */
1827 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001828 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001829 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001830 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001831 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001833 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001835 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001837 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001838 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001839 emulate_push(ctxt);
1840 break;
1841 case 0x6c: /* insb */
1842 case 0x6d: /* insw/insd */
1843 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1844 1,
1845 (c->d & ByteOp) ? 1 : c->op_bytes,
1846 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001847 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001848 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001849 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001850 c->regs[VCPU_REGS_RDI]),
1851 c->rep_prefix,
1852 c->regs[VCPU_REGS_RDX]) == 0) {
1853 c->eip = saved_eip;
1854 return -1;
1855 }
1856 return 0;
1857 case 0x6e: /* outsb */
1858 case 0x6f: /* outsw/outsd */
1859 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1860 0,
1861 (c->d & ByteOp) ? 1 : c->op_bytes,
1862 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001863 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001864 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001865 register_address(c,
1866 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001867 c->regs[VCPU_REGS_RSI]),
1868 c->rep_prefix,
1869 c->regs[VCPU_REGS_RDX]) == 0) {
1870 c->eip = saved_eip;
1871 return -1;
1872 }
1873 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001874 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001875 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001876 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001879 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 case 0:
1881 goto add;
1882 case 1:
1883 goto or;
1884 case 2:
1885 goto adc;
1886 case 3:
1887 goto sbb;
1888 case 4:
1889 goto and;
1890 case 5:
1891 goto sub;
1892 case 6:
1893 goto xor;
1894 case 7:
1895 goto cmp;
1896 }
1897 break;
1898 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001899 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 break;
1901 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001902 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001904 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001906 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 break;
1908 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001909 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 break;
1911 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 break; /* 64b reg: zero-extend */
1914 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001915 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 break;
1917 }
1918 /*
1919 * Write back the memory destination with implicit LOCK
1920 * prefix.
1921 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001922 c->dst.val = c->src.val;
1923 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001926 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001927 case 0x8c: { /* mov r/m, sreg */
1928 struct kvm_segment segreg;
1929
1930 if (c->modrm_reg <= 5)
1931 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1932 else {
1933 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1934 c->modrm);
1935 goto cannot_emulate;
1936 }
1937 c->dst.val = segreg.selector;
1938 break;
1939 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001940 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001941 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001942 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001943 case 0x8e: { /* mov seg, r/m16 */
1944 uint16_t sel;
1945 int type_bits;
1946 int err;
1947
1948 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001949 if (c->modrm_reg == VCPU_SREG_SS)
1950 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1951
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001952 if (c->modrm_reg <= 5) {
1953 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1954 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1955 type_bits, c->modrm_reg);
1956 } else {
1957 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1958 c->modrm);
1959 goto cannot_emulate;
1960 }
1961
1962 if (err < 0)
1963 goto cannot_emulate;
1964
1965 c->dst.type = OP_NONE; /* Disable writeback. */
1966 break;
1967 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001969 rc = emulate_grp1a(ctxt, ops);
1970 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001973 case 0x90: /* nop / xchg r8,rax */
1974 if (!(c->rex_prefix & 1)) { /* nop */
1975 c->dst.type = OP_NONE;
1976 break;
1977 }
1978 case 0x91 ... 0x97: /* xchg reg,rax */
1979 c->src.type = c->dst.type = OP_REG;
1980 c->src.bytes = c->dst.bytes = c->op_bytes;
1981 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1982 c->src.val = *(c->src.ptr);
1983 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001984 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001985 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986 emulate_push(ctxt);
1987 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001988 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001989 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001990 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001991 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001992 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001993 case 0xa0 ... 0xa1: /* mov */
1994 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1995 c->dst.val = c->src.val;
1996 break;
1997 case 0xa2 ... 0xa3: /* mov */
1998 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1999 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002001 c->dst.type = OP_MEM;
2002 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002003 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002004 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002005 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08002006 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002007 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002008 c->regs[VCPU_REGS_RSI]),
2009 &c->dst.val,
2010 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002012 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002013 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002014 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002015 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002016 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002017 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 break;
2019 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002020 c->src.type = OP_NONE; /* Disable writeback. */
2021 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002022 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002023 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002024 c->regs[VCPU_REGS_RSI]);
2025 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
2026 &c->src.val,
2027 c->src.bytes,
2028 ctxt->vcpu)) != 0)
2029 goto done;
2030
2031 c->dst.type = OP_NONE; /* Disable writeback. */
2032 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002033 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002034 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002035 c->regs[VCPU_REGS_RDI]);
2036 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
2037 &c->dst.val,
2038 c->dst.bytes,
2039 ctxt->vcpu)) != 0)
2040 goto done;
2041
2042 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2043
2044 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2045
Harvey Harrison7a9572752008-02-19 07:40:41 -08002046 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002047 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2048 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002049 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002050 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2051 : c->dst.bytes);
2052
2053 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002055 c->dst.type = OP_MEM;
2056 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002057 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002058 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002059 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002060 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002061 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002062 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002063 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 break;
2065 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002066 c->dst.type = OP_REG;
2067 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2068 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08002069 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002070 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002071 c->regs[VCPU_REGS_RSI]),
2072 &c->dst.val,
2073 c->dst.bytes,
2074 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002076 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002077 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002078 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 break;
2080 case 0xae ... 0xaf: /* scas */
2081 DPRINTF("Urk! I don't handle SCAS.\n");
2082 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002083 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002084 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002085 case 0xc0 ... 0xc1:
2086 emulate_grp2(ctxt);
2087 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002088 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002089 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002090 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002091 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002092 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002093 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2094 mov:
2095 c->dst.val = c->src.val;
2096 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002097 case 0xcb: /* ret far */
2098 rc = emulate_ret_far(ctxt, ops);
2099 if (rc)
2100 goto done;
2101 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002102 case 0xd0 ... 0xd1: /* Grp2 */
2103 c->src.val = 1;
2104 emulate_grp2(ctxt);
2105 break;
2106 case 0xd2 ... 0xd3: /* Grp2 */
2107 c->src.val = c->regs[VCPU_REGS_RCX];
2108 emulate_grp2(ctxt);
2109 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002110 case 0xe4: /* inb */
2111 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002112 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002113 io_dir_in = 1;
2114 goto do_io;
2115 case 0xe6: /* outb */
2116 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002117 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002118 io_dir_in = 0;
2119 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002120 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002121 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002122 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002123 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 emulate_push(ctxt);
2125 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002126 }
2127 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002128 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002129 case 0xea: /* jmp far */
2130 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2131 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002132 DPRINTF("jmp far: Failed to load CS descriptor\n");
2133 goto cannot_emulate;
2134 }
2135
Gleb Natapov782b8772009-04-12 13:36:25 +03002136 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002137 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002138 case 0xeb:
2139 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002140 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002141 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002142 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002143 case 0xec: /* in al,dx */
2144 case 0xed: /* in (e/r)ax,dx */
2145 port = c->regs[VCPU_REGS_RDX];
2146 io_dir_in = 1;
2147 goto do_io;
2148 case 0xee: /* out al,dx */
2149 case 0xef: /* out (e/r)ax,dx */
2150 port = c->regs[VCPU_REGS_RDX];
2151 io_dir_in = 0;
2152 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
2153 (c->d & ByteOp) ? 1 : c->op_bytes,
2154 port) != 0) {
2155 c->eip = saved_eip;
2156 goto cannot_emulate;
2157 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002158 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002159 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002160 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002161 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002162 case 0xf5: /* cmc */
2163 /* complement carry flag from eflags reg */
2164 ctxt->eflags ^= EFLG_CF;
2165 c->dst.type = OP_NONE; /* Disable writeback. */
2166 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002167 case 0xf6 ... 0xf7: /* Grp3 */
2168 rc = emulate_grp3(ctxt, ops);
2169 if (rc != 0)
2170 goto done;
2171 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002172 case 0xf8: /* clc */
2173 ctxt->eflags &= ~EFLG_CF;
2174 c->dst.type = OP_NONE; /* Disable writeback. */
2175 break;
2176 case 0xfa: /* cli */
2177 ctxt->eflags &= ~X86_EFLAGS_IF;
2178 c->dst.type = OP_NONE; /* Disable writeback. */
2179 break;
2180 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04002181 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02002182 ctxt->eflags |= X86_EFLAGS_IF;
2183 c->dst.type = OP_NONE; /* Disable writeback. */
2184 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002185 case 0xfc: /* cld */
2186 ctxt->eflags &= ~EFLG_DF;
2187 c->dst.type = OP_NONE; /* Disable writeback. */
2188 break;
2189 case 0xfd: /* std */
2190 ctxt->eflags |= EFLG_DF;
2191 c->dst.type = OP_NONE; /* Disable writeback. */
2192 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002193 case 0xfe ... 0xff: /* Grp4/Grp5 */
2194 rc = emulate_grp45(ctxt, ops);
2195 if (rc != 0)
2196 goto done;
2197 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002199
2200writeback:
2201 rc = writeback(ctxt, ops);
2202 if (rc != 0)
2203 goto done;
2204
2205 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002206 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002207 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002208
2209done:
2210 if (rc == X86EMUL_UNHANDLEABLE) {
2211 c->eip = saved_eip;
2212 return -1;
2213 }
2214 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215
2216twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002217 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002219 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 u16 size;
2221 unsigned long address;
2222
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002223 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002224 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002225 goto cannot_emulate;
2226
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002227 rc = kvm_fix_hypercall(ctxt->vcpu);
2228 if (rc)
2229 goto done;
2230
Avi Kivity33e38852008-05-21 15:34:25 +03002231 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002232 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002233 /* Disable writeback. */
2234 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002235 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002237 rc = read_descriptor(ctxt, ops, c->src.ptr,
2238 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239 if (rc)
2240 goto done;
2241 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002242 /* Disable writeback. */
2243 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002245 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002246 if (c->modrm_mod == 3) {
2247 switch (c->modrm_rm) {
2248 case 1:
2249 rc = kvm_fix_hypercall(ctxt->vcpu);
2250 if (rc)
2251 goto done;
2252 break;
2253 default:
2254 goto cannot_emulate;
2255 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002256 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002257 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002258 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002259 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002260 if (rc)
2261 goto done;
2262 realmode_lidt(ctxt->vcpu, size, address);
2263 }
Avi Kivity16286d02008-04-14 14:40:50 +03002264 /* Disable writeback. */
2265 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 break;
2267 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002268 c->dst.bytes = 2;
2269 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 break;
2271 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002272 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2273 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002274 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 break;
2276 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002277 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002278 /* Disable writeback. */
2279 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 break;
2281 default:
2282 goto cannot_emulate;
2283 }
2284 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002285 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286 if (emulate_syscall(ctxt) == -1)
2287 goto cannot_emulate;
2288 else
2289 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002290 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002291 case 0x06:
2292 emulate_clts(ctxt->vcpu);
2293 c->dst.type = OP_NONE;
2294 break;
2295 case 0x08: /* invd */
2296 case 0x09: /* wbinvd */
2297 case 0x0d: /* GrpP (prefetch) */
2298 case 0x18: /* Grp16 (prefetch/nop) */
2299 c->dst.type = OP_NONE;
2300 break;
2301 case 0x20: /* mov cr, reg */
2302 if (c->modrm_mod != 3)
2303 goto cannot_emulate;
2304 c->regs[c->modrm_rm] =
2305 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2306 c->dst.type = OP_NONE; /* no writeback */
2307 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002309 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002311 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002312 if (rc)
2313 goto cannot_emulate;
2314 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002316 case 0x22: /* mov reg, cr */
2317 if (c->modrm_mod != 3)
2318 goto cannot_emulate;
2319 realmode_set_cr(ctxt->vcpu,
2320 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2321 c->dst.type = OP_NONE;
2322 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002324 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002326 rc = emulator_set_dr(ctxt, c->modrm_reg,
2327 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002328 if (rc)
2329 goto cannot_emulate;
2330 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002332 case 0x30:
2333 /* wrmsr */
2334 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2335 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2336 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2337 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002338 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002339 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002340 }
2341 rc = X86EMUL_CONTINUE;
2342 c->dst.type = OP_NONE;
2343 break;
2344 case 0x32:
2345 /* rdmsr */
2346 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2347 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002348 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002349 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002350 } else {
2351 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2352 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2353 }
2354 rc = X86EMUL_CONTINUE;
2355 c->dst.type = OP_NONE;
2356 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002357 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002358 if (emulate_sysenter(ctxt) == -1)
2359 goto cannot_emulate;
2360 else
2361 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002362 break;
2363 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002364 if (emulate_sysexit(ctxt) == -1)
2365 goto cannot_emulate;
2366 else
2367 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002368 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002370 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002371 if (!test_cc(c->b, ctxt->eflags))
2372 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002374 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002375 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002376 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002377 c->dst.type = OP_NONE;
2378 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002379 case 0xa0: /* push fs */
2380 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2381 break;
2382 case 0xa1: /* pop fs */
2383 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2384 if (rc != 0)
2385 goto done;
2386 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002387 case 0xa3:
2388 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002389 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002390 /* only subword offset */
2391 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002392 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002393 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002394 case 0xa4: /* shld imm8, r, r/m */
2395 case 0xa5: /* shld cl, r, r/m */
2396 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2397 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002398 case 0xa8: /* push gs */
2399 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2400 break;
2401 case 0xa9: /* pop gs */
2402 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2403 if (rc != 0)
2404 goto done;
2405 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002406 case 0xab:
2407 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002408 /* only subword offset */
2409 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002410 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002411 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002412 case 0xac: /* shrd imm8, r, r/m */
2413 case 0xad: /* shrd cl, r, r/m */
2414 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2415 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002416 case 0xae: /* clflush */
2417 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 case 0xb0 ... 0xb1: /* cmpxchg */
2419 /*
2420 * Save real source value, then compare EAX against
2421 * destination.
2422 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002423 c->src.orig_val = c->src.val;
2424 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002425 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2426 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002428 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 } else {
2430 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002431 c->dst.type = OP_REG;
2432 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 }
2434 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 case 0xb3:
2436 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002437 /* only subword offset */
2438 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002439 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002442 c->dst.bytes = c->op_bytes;
2443 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2444 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002447 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448 case 0:
2449 goto bt;
2450 case 1:
2451 goto bts;
2452 case 2:
2453 goto btr;
2454 case 3:
2455 goto btc;
2456 }
2457 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002458 case 0xbb:
2459 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002460 /* only subword offset */
2461 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002462 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002463 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002465 c->dst.bytes = c->op_bytes;
2466 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2467 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002469 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002470 c->dst.bytes = c->op_bytes;
2471 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2472 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002473 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002475 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002476 if (rc != 0)
2477 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002478 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002479 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 }
2481 goto writeback;
2482
2483cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002484 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002485 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 return -1;
2487}