blob: 48b62cc3bd0cffb810f390b92769bdb3bcee1144 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#define DPRINTF(x...) do {} while (0)
30#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/module.h>
Avi Kivityedf88412007-12-16 11:02:48 +020032#include <asm/kvm_x86_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
34/*
35 * Opcode effective-address decode tables.
36 * Note that we only emulate instructions that have at least one memory
37 * operand (excluding implicit stack references). We assume that stack
38 * references and instruction fetches will never occur in special memory
39 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
40 * not be handled.
41 */
42
43/* Operand sizes: 8-bit operands or specified/overridden size. */
44#define ByteOp (1<<0) /* 8-bit operands. */
45/* Destination operand type. */
46#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
47#define DstReg (2<<1) /* Register operand. */
48#define DstMem (3<<1) /* Memory operand. */
49#define DstMask (3<<1)
50/* Source operand type. */
51#define SrcNone (0<<3) /* No source operand. */
52#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
53#define SrcReg (1<<3) /* Register operand. */
54#define SrcMem (2<<3) /* Memory operand. */
55#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
57#define SrcImm (5<<3) /* Immediate operand. */
58#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
59#define SrcMask (7<<3)
60/* Generic ModRM decode. */
61#define ModRM (1<<6)
62/* Destination is only written; never read. */
63#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080064#define BitOp (1<<8)
Avi Kivityc7e75a32007-10-28 16:34:25 +020065#define MemAbs (1<<9) /* Memory operand is absolute displacement */
Avi Kivityb9fa9d62007-11-27 19:05:37 +020066#define String (1<<10) /* String instruction (rep capable) */
Avi Kivity6e3d5df2007-12-06 18:14:14 +020067#define Stack (1<<11) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020068#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
69#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
70#define GroupMask 0xff /* Group number stored in bits 0:7 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080071
Avi Kivity43bb19c2008-01-18 12:46:50 +020072enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020073 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020074 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020075};
76
Avi Kivityc7e75a32007-10-28 16:34:25 +020077static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080078 /* 0x00 - 0x07 */
79 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
80 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
81 0, 0, 0, 0,
82 /* 0x08 - 0x0F */
83 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
84 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
85 0, 0, 0, 0,
86 /* 0x10 - 0x17 */
87 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
88 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
89 0, 0, 0, 0,
90 /* 0x18 - 0x1F */
91 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
93 0, 0, 0, 0,
94 /* 0x20 - 0x27 */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030097 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080098 /* 0x28 - 0x2F */
99 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
101 0, 0, 0, 0,
102 /* 0x30 - 0x37 */
103 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
105 0, 0, 0, 0,
106 /* 0x38 - 0x3F */
107 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
109 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700110 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200111 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700112 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200113 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300114 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200115 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
116 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300117 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200118 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
119 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700120 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700122 0, 0, 0, 0,
123 /* 0x68 - 0x6F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200124 0, 0, ImplicitOps | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300125 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
126 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300127 /* 0x70 - 0x77 */
128 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
129 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
130 /* 0x78 - 0x7F */
131 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
132 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200134 Group | Group1_80, Group | Group1_81,
135 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
137 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
138 /* 0x88 - 0x8F */
139 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
140 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200141 0, ModRM | DstReg, 0, Group | Group1A,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 /* 0x90 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200143 0, 0, 0, 0, 0, 0, 0, 0,
144 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200146 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
147 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200148 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
149 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200151 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
152 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
153 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154 /* 0xB0 - 0xBF */
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300157 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200158 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300159 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160 /* 0xC8 - 0xCF */
161 0, 0, 0, 0, 0, 0, 0, 0,
162 /* 0xD0 - 0xD7 */
163 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
164 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
165 0, 0, 0, 0,
166 /* 0xD8 - 0xDF */
167 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300168 /* 0xE0 - 0xE7 */
169 0, 0, 0, 0, 0, 0, 0, 0,
170 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200171 ImplicitOps | Stack, SrcImm | ImplicitOps,
172 ImplicitOps, SrcImmByte | ImplicitOps,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200173 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 /* 0xF0 - 0xF7 */
175 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200176 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700178 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Avi Kivityfd607542008-01-18 13:12:26 +0200179 0, 0, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180};
181
Avi Kivity038e51d2007-01-22 20:40:40 -0800182static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200184 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200185 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 /* 0x10 - 0x1F */
187 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
188 /* 0x20 - 0x2F */
189 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
190 0, 0, 0, 0, 0, 0, 0, 0,
191 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300192 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 /* 0x40 - 0x47 */
194 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
195 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
196 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
197 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
198 /* 0x48 - 0x4F */
199 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
200 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
201 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
202 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
203 /* 0x50 - 0x5F */
204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205 /* 0x60 - 0x6F */
206 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207 /* 0x70 - 0x7F */
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300210 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
211 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
212 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
213 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0x90 - 0x9F */
215 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800217 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800219 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0xB0 - 0xB7 */
221 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800222 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
224 DstReg | SrcMem16 | ModRM | Mov,
225 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800226 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
228 DstReg | SrcMem16 | ModRM | Mov,
229 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800230 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
231 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0xD0 - 0xDF */
233 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
234 /* 0xE0 - 0xEF */
235 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
236 /* 0xF0 - 0xFF */
237 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
238};
239
Avi Kivitye09d0822008-01-18 12:38:59 +0200240static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200241 [Group1_80*8] =
242 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
243 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
244 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
245 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
246 [Group1_81*8] =
247 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
248 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
249 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
250 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
251 [Group1_82*8] =
252 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
253 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
254 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
255 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
256 [Group1_83*8] =
257 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
258 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
259 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
260 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200261 [Group1A*8] =
262 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200263 [Group3_Byte*8] =
264 ByteOp | SrcImm | DstMem | ModRM, 0,
265 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
266 0, 0, 0, 0,
267 [Group3*8] =
268 DstMem | SrcImm | ModRM | SrcImm, 0,
269 DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
270 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200271 [Group4*8] =
272 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
273 0, 0, 0, 0, 0, 0,
274 [Group5*8] =
275 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, 0, 0,
276 SrcMem | ModRM, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200277 [Group7*8] =
278 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300279 SrcNone | ModRM | DstMem | Mov, 0,
280 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200281};
282
283static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200284 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300285 SrcNone | ModRM, 0, 0, 0,
286 SrcNone | ModRM | DstMem | Mov, 0,
287 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200288};
289
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290/* EFLAGS bit definitions. */
291#define EFLG_OF (1<<11)
292#define EFLG_DF (1<<10)
293#define EFLG_SF (1<<7)
294#define EFLG_ZF (1<<6)
295#define EFLG_AF (1<<4)
296#define EFLG_PF (1<<2)
297#define EFLG_CF (1<<0)
298
299/*
300 * Instruction emulation:
301 * Most instructions are emulated directly via a fragment of inline assembly
302 * code. This allows us to save/restore EFLAGS and thus very easily pick up
303 * any modified flags.
304 */
305
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800306#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307#define _LO32 "k" /* force 32-bit operand */
308#define _STK "%%rsp" /* stack pointer */
309#elif defined(__i386__)
310#define _LO32 "" /* force 32-bit operand */
311#define _STK "%%esp" /* stack pointer */
312#endif
313
314/*
315 * These EFLAGS bits are restored from saved value during emulation, and
316 * any changes are written back to the saved value after emulation.
317 */
318#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
319
320/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200321#define _PRE_EFLAGS(_sav, _msk, _tmp) \
322 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
323 "movl %"_sav",%"_LO32 _tmp"; " \
324 "push %"_tmp"; " \
325 "push %"_tmp"; " \
326 "movl %"_msk",%"_LO32 _tmp"; " \
327 "andl %"_LO32 _tmp",("_STK"); " \
328 "pushf; " \
329 "notl %"_LO32 _tmp"; " \
330 "andl %"_LO32 _tmp",("_STK"); " \
331 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
332 "pop %"_tmp"; " \
333 "orl %"_LO32 _tmp",("_STK"); " \
334 "popf; " \
335 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336
337/* After executing instruction: write-back necessary bits in EFLAGS. */
338#define _POST_EFLAGS(_sav, _msk, _tmp) \
339 /* _sav |= EFLAGS & _msk; */ \
340 "pushf; " \
341 "pop %"_tmp"; " \
342 "andl %"_msk",%"_LO32 _tmp"; " \
343 "orl %"_LO32 _tmp",%"_sav"; "
344
345/* Raw emulation: instruction has two explicit operands. */
346#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
347 do { \
348 unsigned long _tmp; \
349 \
350 switch ((_dst).bytes) { \
351 case 2: \
352 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400353 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400355 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 : "=m" (_eflags), "=m" ((_dst).val), \
357 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400358 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 break; \
360 case 4: \
361 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400362 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400364 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 : "=m" (_eflags), "=m" ((_dst).val), \
366 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400367 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 break; \
369 case 8: \
370 __emulate_2op_8byte(_op, _src, _dst, \
371 _eflags, _qx, _qy); \
372 break; \
373 } \
374 } while (0)
375
376#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
377 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800378 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400379 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 case 1: \
381 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400382 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400384 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800386 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400387 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 break; \
389 default: \
390 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
391 _wx, _wy, _lx, _ly, _qx, _qy); \
392 break; \
393 } \
394 } while (0)
395
396/* Source operand is byte-sized and may be restricted to just %cl. */
397#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
398 __emulate_2op(_op, _src, _dst, _eflags, \
399 "b", "c", "b", "c", "b", "c", "b", "c")
400
401/* Source operand is byte, word, long or quad sized. */
402#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
403 __emulate_2op(_op, _src, _dst, _eflags, \
404 "b", "q", "w", "r", _LO32, "r", "", "r")
405
406/* Source operand is word, long or quad sized. */
407#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
408 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
409 "w", "r", _LO32, "r", "", "r")
410
411/* Instruction has only one explicit operand (no source operand). */
412#define emulate_1op(_op, _dst, _eflags) \
413 do { \
414 unsigned long _tmp; \
415 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400416 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 case 1: \
418 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400419 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400421 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 : "=m" (_eflags), "=m" ((_dst).val), \
423 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400424 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 break; \
426 case 2: \
427 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400428 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400430 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 : "=m" (_eflags), "=m" ((_dst).val), \
432 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400433 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 break; \
435 case 4: \
436 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400437 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400439 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 : "=m" (_eflags), "=m" ((_dst).val), \
441 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 break; \
444 case 8: \
445 __emulate_1op_8byte(_op, _dst, _eflags); \
446 break; \
447 } \
448 } while (0)
449
450/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800451#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
453 do { \
454 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400455 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400457 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400459 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 } while (0)
461
462#define __emulate_1op_8byte(_op, _dst, _eflags) \
463 do { \
464 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400465 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400467 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400469 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 } while (0)
471
472#elif defined(__i386__)
473#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
474#define __emulate_1op_8byte(_op, _dst, _eflags)
475#endif /* __i386__ */
476
477/* Fetch next part of the instruction being emulated. */
478#define insn_fetch(_type, _size, _eip) \
479({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200480 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400481 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 goto done; \
483 (_eip) += (_size); \
484 (_type)_x; \
485})
486
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800487static inline unsigned long ad_mask(struct decode_cache *c)
488{
489 return (1UL << (c->ad_bytes << 3)) - 1;
490}
491
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800493static inline unsigned long
494address_mask(struct decode_cache *c, unsigned long reg)
495{
496 if (c->ad_bytes == sizeof(unsigned long))
497 return reg;
498 else
499 return reg & ad_mask(c);
500}
501
502static inline unsigned long
503register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
504{
505 return base + address_mask(c, reg);
506}
507
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508static inline void
509register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
510{
511 if (c->ad_bytes == sizeof(unsigned long))
512 *reg += inc;
513 else
514 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
515}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516
Harvey Harrison7a9572752008-02-19 07:40:41 -0800517static inline void jmp_rel(struct decode_cache *c, int rel)
518{
519 register_address_increment(c, &c->eip, rel);
520}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300521
Avi Kivity62266862007-11-20 13:15:52 +0200522static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
523 struct x86_emulate_ops *ops,
524 unsigned long linear, u8 *dest)
525{
526 struct fetch_cache *fc = &ctxt->decode.fetch;
527 int rc;
528 int size;
529
530 if (linear < fc->start || linear >= fc->end) {
531 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
532 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
533 if (rc)
534 return rc;
535 fc->start = linear;
536 fc->end = linear + size;
537 }
538 *dest = fc->data[linear - fc->start];
539 return 0;
540}
541
542static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
543 struct x86_emulate_ops *ops,
544 unsigned long eip, void *dest, unsigned size)
545{
546 int rc = 0;
547
548 eip += ctxt->cs_base;
549 while (size--) {
550 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
551 if (rc)
552 return rc;
553 }
554 return 0;
555}
556
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000557/*
558 * Given the 'reg' portion of a ModRM byte, and a register block, return a
559 * pointer into the block that addresses the relevant register.
560 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
561 */
562static void *decode_register(u8 modrm_reg, unsigned long *regs,
563 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564{
565 void *p;
566
567 p = &regs[modrm_reg];
568 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
569 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
570 return p;
571}
572
573static int read_descriptor(struct x86_emulate_ctxt *ctxt,
574 struct x86_emulate_ops *ops,
575 void *ptr,
576 u16 *size, unsigned long *address, int op_bytes)
577{
578 int rc;
579
580 if (op_bytes == 2)
581 op_bytes = 3;
582 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300583 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
584 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 if (rc)
586 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300587 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
588 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 return rc;
590}
591
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300592static int test_cc(unsigned int condition, unsigned int flags)
593{
594 int rc = 0;
595
596 switch ((condition & 15) >> 1) {
597 case 0: /* o */
598 rc |= (flags & EFLG_OF);
599 break;
600 case 1: /* b/c/nae */
601 rc |= (flags & EFLG_CF);
602 break;
603 case 2: /* z/e */
604 rc |= (flags & EFLG_ZF);
605 break;
606 case 3: /* be/na */
607 rc |= (flags & (EFLG_CF|EFLG_ZF));
608 break;
609 case 4: /* s */
610 rc |= (flags & EFLG_SF);
611 break;
612 case 5: /* p/pe */
613 rc |= (flags & EFLG_PF);
614 break;
615 case 7: /* le/ng */
616 rc |= (flags & EFLG_ZF);
617 /* fall through */
618 case 6: /* l/nge */
619 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
620 break;
621 }
622
623 /* Odd condition identifiers (lsb == 1) have inverted sense. */
624 return (!!rc ^ (condition & 1));
625}
626
Avi Kivity3c118e22007-10-31 10:27:04 +0200627static void decode_register_operand(struct operand *op,
628 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200629 int inhibit_bytereg)
630{
Avi Kivity33615aa2007-10-31 11:15:56 +0200631 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200632 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200633
634 if (!(c->d & ModRM))
635 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200636 op->type = OP_REG;
637 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200638 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200639 op->val = *(u8 *)op->ptr;
640 op->bytes = 1;
641 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200642 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200643 op->bytes = c->op_bytes;
644 switch (op->bytes) {
645 case 2:
646 op->val = *(u16 *)op->ptr;
647 break;
648 case 4:
649 op->val = *(u32 *)op->ptr;
650 break;
651 case 8:
652 op->val = *(u64 *) op->ptr;
653 break;
654 }
655 }
656 op->orig_val = op->val;
657}
658
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200659static int decode_modrm(struct x86_emulate_ctxt *ctxt,
660 struct x86_emulate_ops *ops)
661{
662 struct decode_cache *c = &ctxt->decode;
663 u8 sib;
664 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
665 int rc = 0;
666
667 if (c->rex_prefix) {
668 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
669 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
670 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
671 }
672
673 c->modrm = insn_fetch(u8, 1, c->eip);
674 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
675 c->modrm_reg |= (c->modrm & 0x38) >> 3;
676 c->modrm_rm |= (c->modrm & 0x07);
677 c->modrm_ea = 0;
678 c->use_modrm_ea = 1;
679
680 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300681 c->modrm_ptr = decode_register(c->modrm_rm,
682 c->regs, c->d & ByteOp);
683 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200684 return rc;
685 }
686
687 if (c->ad_bytes == 2) {
688 unsigned bx = c->regs[VCPU_REGS_RBX];
689 unsigned bp = c->regs[VCPU_REGS_RBP];
690 unsigned si = c->regs[VCPU_REGS_RSI];
691 unsigned di = c->regs[VCPU_REGS_RDI];
692
693 /* 16-bit ModR/M decode. */
694 switch (c->modrm_mod) {
695 case 0:
696 if (c->modrm_rm == 6)
697 c->modrm_ea += insn_fetch(u16, 2, c->eip);
698 break;
699 case 1:
700 c->modrm_ea += insn_fetch(s8, 1, c->eip);
701 break;
702 case 2:
703 c->modrm_ea += insn_fetch(u16, 2, c->eip);
704 break;
705 }
706 switch (c->modrm_rm) {
707 case 0:
708 c->modrm_ea += bx + si;
709 break;
710 case 1:
711 c->modrm_ea += bx + di;
712 break;
713 case 2:
714 c->modrm_ea += bp + si;
715 break;
716 case 3:
717 c->modrm_ea += bp + di;
718 break;
719 case 4:
720 c->modrm_ea += si;
721 break;
722 case 5:
723 c->modrm_ea += di;
724 break;
725 case 6:
726 if (c->modrm_mod != 0)
727 c->modrm_ea += bp;
728 break;
729 case 7:
730 c->modrm_ea += bx;
731 break;
732 }
733 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
734 (c->modrm_rm == 6 && c->modrm_mod != 0))
735 if (!c->override_base)
736 c->override_base = &ctxt->ss_base;
737 c->modrm_ea = (u16)c->modrm_ea;
738 } else {
739 /* 32/64-bit ModR/M decode. */
740 switch (c->modrm_rm) {
741 case 4:
742 case 12:
743 sib = insn_fetch(u8, 1, c->eip);
744 index_reg |= (sib >> 3) & 7;
745 base_reg |= sib & 7;
746 scale = sib >> 6;
747
748 switch (base_reg) {
749 case 5:
750 if (c->modrm_mod != 0)
751 c->modrm_ea += c->regs[base_reg];
752 else
753 c->modrm_ea +=
754 insn_fetch(s32, 4, c->eip);
755 break;
756 default:
757 c->modrm_ea += c->regs[base_reg];
758 }
759 switch (index_reg) {
760 case 4:
761 break;
762 default:
763 c->modrm_ea += c->regs[index_reg] << scale;
764 }
765 break;
766 case 5:
767 if (c->modrm_mod != 0)
768 c->modrm_ea += c->regs[c->modrm_rm];
769 else if (ctxt->mode == X86EMUL_MODE_PROT64)
770 rip_relative = 1;
771 break;
772 default:
773 c->modrm_ea += c->regs[c->modrm_rm];
774 break;
775 }
776 switch (c->modrm_mod) {
777 case 0:
778 if (c->modrm_rm == 5)
779 c->modrm_ea += insn_fetch(s32, 4, c->eip);
780 break;
781 case 1:
782 c->modrm_ea += insn_fetch(s8, 1, c->eip);
783 break;
784 case 2:
785 c->modrm_ea += insn_fetch(s32, 4, c->eip);
786 break;
787 }
788 }
789 if (rip_relative) {
790 c->modrm_ea += c->eip;
791 switch (c->d & SrcMask) {
792 case SrcImmByte:
793 c->modrm_ea += 1;
794 break;
795 case SrcImm:
796 if (c->d & ByteOp)
797 c->modrm_ea += 1;
798 else
799 if (c->op_bytes == 8)
800 c->modrm_ea += 4;
801 else
802 c->modrm_ea += c->op_bytes;
803 }
804 }
805done:
806 return rc;
807}
808
809static int decode_abs(struct x86_emulate_ctxt *ctxt,
810 struct x86_emulate_ops *ops)
811{
812 struct decode_cache *c = &ctxt->decode;
813 int rc = 0;
814
815 switch (c->ad_bytes) {
816 case 2:
817 c->modrm_ea = insn_fetch(u16, 2, c->eip);
818 break;
819 case 4:
820 c->modrm_ea = insn_fetch(u32, 4, c->eip);
821 break;
822 case 8:
823 c->modrm_ea = insn_fetch(u64, 8, c->eip);
824 break;
825 }
826done:
827 return rc;
828}
829
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200831x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200833 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200836 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
838 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
Laurent Viviere4e03de2007-09-18 11:52:50 +0200840 memset(c, 0, sizeof(struct decode_cache));
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800841 c->eip = ctxt->vcpu->arch.rip;
842 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
844 switch (mode) {
845 case X86EMUL_MODE_REAL:
846 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200847 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 break;
849 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200850 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800852#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200854 def_op_bytes = 4;
855 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 break;
857#endif
858 default:
859 return -1;
860 }
861
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200862 c->op_bytes = def_op_bytes;
863 c->ad_bytes = def_ad_bytes;
864
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200866 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200867 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200869 /* switch between 2/4 bytes */
870 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 break;
872 case 0x67: /* address-size override */
873 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200874 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200875 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200877 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200878 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 break;
880 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200881 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 break;
883 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200884 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 break;
886 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200887 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 break;
889 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200890 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 break;
892 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 break;
895 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200896 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200898 case 0x40 ... 0x4f: /* REX */
899 if (mode != X86EMUL_MODE_PROT64)
900 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200901 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200902 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200904 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200906 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100907 c->rep_prefix = REPNE_PREFIX;
908 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100910 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 default:
913 goto done_prefixes;
914 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200915
916 /* Any legacy prefix after a REX prefix nullifies its effect. */
917
Avi Kivity33615aa2007-10-31 11:15:56 +0200918 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 }
920
921done_prefixes:
922
923 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200925 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200926 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200929 c->d = opcode_table[c->b];
930 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200932 if (c->b == 0x0f) {
933 c->twobyte = 1;
934 c->b = insn_fetch(u8, 1, c->eip);
935 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200937 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938
Avi Kivitye09d0822008-01-18 12:38:59 +0200939 if (c->d & Group) {
940 group = c->d & GroupMask;
941 c->modrm = insn_fetch(u8, 1, c->eip);
942 --c->eip;
943
944 group = (group << 3) + ((c->modrm >> 3) & 7);
945 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
946 c->d = group2_table[group];
947 else
948 c->d = group_table[group];
949 }
950
951 /* Unrecognised? */
952 if (c->d == 0) {
953 DPRINTF("Cannot emulate %02x\n", c->b);
954 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 }
956
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200957 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
958 c->op_bytes = 8;
959
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961 if (c->d & ModRM)
962 rc = decode_modrm(ctxt, ops);
963 else if (c->d & MemAbs)
964 rc = decode_abs(ctxt, ops);
965 if (rc)
966 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967
Avi Kivityc7e75a32007-10-28 16:34:25 +0200968 if (!c->override_base)
969 c->override_base = &ctxt->ds_base;
970 if (mode == X86EMUL_MODE_PROT64 &&
971 c->override_base != &ctxt->fs_base &&
972 c->override_base != &ctxt->gs_base)
973 c->override_base = NULL;
974
975 if (c->override_base)
976 c->modrm_ea += *c->override_base;
977
978 if (c->ad_bytes != 8)
979 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 /*
981 * Decode and fetch the source operand: register, memory
982 * or immediate.
983 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200984 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 case SrcNone:
986 break;
987 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200988 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
990 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200991 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 goto srcmem_common;
993 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200994 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 goto srcmem_common;
996 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200997 c->src.bytes = (c->d & ByteOp) ? 1 :
998 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300999 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001000 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001001 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001002 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001003 /*
1004 * For instructions with a ModR/M byte, switch to register
1005 * access if Mod = 3.
1006 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001007 if ((c->d & ModRM) && c->modrm_mod == 3) {
1008 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001009 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001010 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001011 break;
1012 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 break;
1015 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 c->src.type = OP_IMM;
1017 c->src.ptr = (unsigned long *)c->eip;
1018 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1019 if (c->src.bytes == 8)
1020 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001022 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001024 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 break;
1026 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001027 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 break;
1029 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001030 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 break;
1032 }
1033 break;
1034 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 c->src.type = OP_IMM;
1036 c->src.ptr = (unsigned long *)c->eip;
1037 c->src.bytes = 1;
1038 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 break;
1040 }
1041
Avi Kivity038e51d2007-01-22 20:40:40 -08001042 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001044 case ImplicitOps:
1045 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001046 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001047 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001048 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001049 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001050 break;
1051 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001052 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001053 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001054 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001055 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001056 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001057 break;
1058 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001059 c->dst.type = OP_MEM;
1060 break;
1061 }
1062
1063done:
1064 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1065}
1066
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001067static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1068{
1069 struct decode_cache *c = &ctxt->decode;
1070
1071 c->dst.type = OP_MEM;
1072 c->dst.bytes = c->op_bytes;
1073 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001074 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Harvey Harrisone4706772008-02-19 07:40:38 -08001075 c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001076 c->regs[VCPU_REGS_RSP]);
1077}
1078
1079static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1080 struct x86_emulate_ops *ops)
1081{
1082 struct decode_cache *c = &ctxt->decode;
1083 int rc;
1084
Harvey Harrisone4706772008-02-19 07:40:38 -08001085 rc = ops->read_std(register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001086 c->regs[VCPU_REGS_RSP]),
1087 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1088 if (rc != 0)
1089 return rc;
1090
Harvey Harrison7a9572752008-02-19 07:40:41 -08001091 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001092
1093 return 0;
1094}
1095
Laurent Vivier05f086f2007-09-24 11:10:55 +02001096static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001097{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001098 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001099 switch (c->modrm_reg) {
1100 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001101 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001102 break;
1103 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001104 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001105 break;
1106 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001107 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001108 break;
1109 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001110 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001111 break;
1112 case 4: /* sal/shl */
1113 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001114 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115 break;
1116 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001117 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001118 break;
1119 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001120 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121 break;
1122 }
1123}
1124
1125static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001126 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001127{
1128 struct decode_cache *c = &ctxt->decode;
1129 int rc = 0;
1130
1131 switch (c->modrm_reg) {
1132 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001133 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134 break;
1135 case 2: /* not */
1136 c->dst.val = ~c->dst.val;
1137 break;
1138 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001139 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001140 break;
1141 default:
1142 DPRINTF("Cannot emulate %02x\n", c->b);
1143 rc = X86EMUL_UNHANDLEABLE;
1144 break;
1145 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001146 return rc;
1147}
1148
1149static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001150 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001151{
1152 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001153
1154 switch (c->modrm_reg) {
1155 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001156 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001157 break;
1158 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001159 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001160 break;
1161 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001162 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001163 break;
1164 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001165 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001166 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001167 }
1168 return 0;
1169}
1170
1171static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1172 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001173 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001174{
1175 struct decode_cache *c = &ctxt->decode;
1176 u64 old, new;
1177 int rc;
1178
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001179 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001180 if (rc != 0)
1181 return rc;
1182
1183 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1184 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1185
1186 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1187 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001188 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001189
1190 } else {
1191 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1192 (u32) c->regs[VCPU_REGS_RBX];
1193
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001194 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001195 if (rc != 0)
1196 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001197 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198 }
1199 return 0;
1200}
1201
1202static inline int writeback(struct x86_emulate_ctxt *ctxt,
1203 struct x86_emulate_ops *ops)
1204{
1205 int rc;
1206 struct decode_cache *c = &ctxt->decode;
1207
1208 switch (c->dst.type) {
1209 case OP_REG:
1210 /* The 4-byte case *is* correct:
1211 * in 64-bit mode we zero-extend.
1212 */
1213 switch (c->dst.bytes) {
1214 case 1:
1215 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1216 break;
1217 case 2:
1218 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1219 break;
1220 case 4:
1221 *c->dst.ptr = (u32)c->dst.val;
1222 break; /* 64b: zero-ext */
1223 case 8:
1224 *c->dst.ptr = c->dst.val;
1225 break;
1226 }
1227 break;
1228 case OP_MEM:
1229 if (c->lock_prefix)
1230 rc = ops->cmpxchg_emulated(
1231 (unsigned long)c->dst.ptr,
1232 &c->dst.orig_val,
1233 &c->dst.val,
1234 c->dst.bytes,
1235 ctxt->vcpu);
1236 else
1237 rc = ops->write_emulated(
1238 (unsigned long)c->dst.ptr,
1239 &c->dst.val,
1240 c->dst.bytes,
1241 ctxt->vcpu);
1242 if (rc != 0)
1243 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001244 break;
1245 case OP_NONE:
1246 /* no writeback */
1247 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248 default:
1249 break;
1250 }
1251 return 0;
1252}
1253
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001254int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001255x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001256{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001257 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001258 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001259 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001260 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001261 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001262
Laurent Vivier34273182007-09-18 11:27:37 +02001263 /* Shadow copy of register state. Committed on successful emulation.
1264 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1265 * modify them.
1266 */
1267
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001268 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001269 saved_eip = c->eip;
1270
Avi Kivityc7e75a32007-10-28 16:34:25 +02001271 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001272 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001273
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001274 if (c->rep_prefix && (c->d & String)) {
1275 /* All REP prefixes have the same first termination condition */
1276 if (c->regs[VCPU_REGS_RCX] == 0) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001277 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001278 goto done;
1279 }
1280 /* The second termination condition only applies for REPE
1281 * and REPNE. Test if the repeat string operation prefix is
1282 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1283 * corresponding termination condition according to:
1284 * - if REPE/REPZ and ZF = 0 then done
1285 * - if REPNE/REPNZ and ZF = 1 then done
1286 */
1287 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1288 (c->b == 0xae) || (c->b == 0xaf)) {
1289 if ((c->rep_prefix == REPE_PREFIX) &&
1290 ((ctxt->eflags & EFLG_ZF) == 0)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001291 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001292 goto done;
1293 }
1294 if ((c->rep_prefix == REPNE_PREFIX) &&
1295 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001296 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001297 goto done;
1298 }
1299 }
1300 c->regs[VCPU_REGS_RCX]--;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 c->eip = ctxt->vcpu->arch.rip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001302 }
1303
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001304 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001305 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001306 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001307 rc = ops->read_emulated((unsigned long)c->src.ptr,
1308 &c->src.val,
1309 c->src.bytes,
1310 ctxt->vcpu);
1311 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001312 goto done;
1313 c->src.orig_val = c->src.val;
1314 }
1315
1316 if ((c->d & DstMask) == ImplicitOps)
1317 goto special_insn;
1318
1319
1320 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001321 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001322 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1323 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001324 if (c->d & BitOp) {
1325 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001326
Laurent Viviere4e03de2007-09-18 11:52:50 +02001327 c->dst.ptr = (void *)c->dst.ptr +
1328 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001329 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001330 if (!(c->d & Mov) &&
1331 /* optimisation - avoid slow emulated read */
1332 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1333 &c->dst.val,
1334 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001335 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001336 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001337 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001338
Avi Kivity018a98d2007-11-27 19:30:56 +02001339special_insn:
1340
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342 goto twobyte_insn;
1343
Laurent Viviere4e03de2007-09-18 11:52:50 +02001344 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 case 0x00 ... 0x05:
1346 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001347 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 break;
1349 case 0x08 ... 0x0d:
1350 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001351 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 break;
1353 case 0x10 ... 0x15:
1354 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001355 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 break;
1357 case 0x18 ... 0x1d:
1358 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001359 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001361 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001363 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001365 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001366 c->dst.type = OP_REG;
1367 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1368 c->dst.val = *(u8 *)c->dst.ptr;
1369 c->dst.bytes = 1;
1370 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001371 goto and;
1372 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001373 c->dst.type = OP_REG;
1374 c->dst.bytes = c->op_bytes;
1375 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1376 if (c->op_bytes == 2)
1377 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001378 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001379 c->dst.val = *(u32 *)c->dst.ptr;
1380 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001381 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 case 0x28 ... 0x2d:
1383 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001384 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 break;
1386 case 0x30 ... 0x35:
1387 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 break;
1390 case 0x38 ... 0x3d:
1391 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001392 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001394 case 0x40 ... 0x47: /* inc r16/r32 */
1395 emulate_1op("inc", c->dst, ctxt->eflags);
1396 break;
1397 case 0x48 ... 0x4f: /* dec r16/r32 */
1398 emulate_1op("dec", c->dst, ctxt->eflags);
1399 break;
1400 case 0x50 ... 0x57: /* push reg */
1401 c->dst.type = OP_MEM;
1402 c->dst.bytes = c->op_bytes;
1403 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001404 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001405 -c->op_bytes);
1406 c->dst.ptr = (void *) register_address(
Harvey Harrisone4706772008-02-19 07:40:38 -08001407 c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Avi Kivity33615aa2007-10-31 11:15:56 +02001408 break;
1409 case 0x58 ... 0x5f: /* pop reg */
1410 pop_instruction:
Harvey Harrisone4706772008-02-19 07:40:38 -08001411 if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
Avi Kivity33615aa2007-10-31 11:15:56 +02001412 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1413 c->op_bytes, ctxt->vcpu)) != 0)
1414 goto done;
1415
Harvey Harrison7a9572752008-02-19 07:40:41 -08001416 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001417 c->op_bytes);
1418 c->dst.type = OP_NONE; /* Disable writeback. */
1419 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001421 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001423 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001425 case 0x6a: /* push imm8 */
1426 c->src.val = 0L;
1427 c->src.val = insn_fetch(s8, 1, c->eip);
1428 emulate_push(ctxt);
1429 break;
1430 case 0x6c: /* insb */
1431 case 0x6d: /* insw/insd */
1432 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1433 1,
1434 (c->d & ByteOp) ? 1 : c->op_bytes,
1435 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001436 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001437 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001438 register_address(c, ctxt->es_base,
Avi Kivity018a98d2007-11-27 19:30:56 +02001439 c->regs[VCPU_REGS_RDI]),
1440 c->rep_prefix,
1441 c->regs[VCPU_REGS_RDX]) == 0) {
1442 c->eip = saved_eip;
1443 return -1;
1444 }
1445 return 0;
1446 case 0x6e: /* outsb */
1447 case 0x6f: /* outsw/outsd */
1448 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1449 0,
1450 (c->d & ByteOp) ? 1 : c->op_bytes,
1451 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001452 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001453 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001454 register_address(c, c->override_base ?
Avi Kivity018a98d2007-11-27 19:30:56 +02001455 *c->override_base :
1456 ctxt->ds_base,
1457 c->regs[VCPU_REGS_RSI]),
1458 c->rep_prefix,
1459 c->regs[VCPU_REGS_RDX]) == 0) {
1460 c->eip = saved_eip;
1461 return -1;
1462 }
1463 return 0;
1464 case 0x70 ... 0x7f: /* jcc (short) */ {
1465 int rel = insn_fetch(s8, 1, c->eip);
1466
1467 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001468 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001469 break;
1470 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001472 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 case 0:
1474 goto add;
1475 case 1:
1476 goto or;
1477 case 2:
1478 goto adc;
1479 case 3:
1480 goto sbb;
1481 case 4:
1482 goto and;
1483 case 5:
1484 goto sub;
1485 case 6:
1486 goto xor;
1487 case 7:
1488 goto cmp;
1489 }
1490 break;
1491 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001492 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493 break;
1494 case 0x86 ... 0x87: /* xchg */
1495 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001496 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001498 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 break;
1500 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001501 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 break;
1503 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001504 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 break; /* 64b reg: zero-extend */
1506 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001507 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 break;
1509 }
1510 /*
1511 * Write back the memory destination with implicit LOCK
1512 * prefix.
1513 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001514 c->dst.val = c->src.val;
1515 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001518 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001519 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001520 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001521 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523 rc = emulate_grp1a(ctxt, ops);
1524 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001527 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001528 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529 emulate_push(ctxt);
1530 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001531 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001532 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001533 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001534 case 0xa0 ... 0xa1: /* mov */
1535 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1536 c->dst.val = c->src.val;
1537 break;
1538 case 0xa2 ... 0xa3: /* mov */
1539 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1540 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001542 c->dst.type = OP_MEM;
1543 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001544 c->dst.ptr = (unsigned long *)register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001545 ctxt->es_base,
1546 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001547 if ((rc = ops->read_emulated(register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001548 c->override_base ? *c->override_base :
1549 ctxt->ds_base,
1550 c->regs[VCPU_REGS_RSI]),
1551 &c->dst.val,
1552 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001554 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001555 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001556 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001557 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001558 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001559 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560 break;
1561 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001562 c->src.type = OP_NONE; /* Disable writeback. */
1563 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001564 c->src.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001565 c->override_base ? *c->override_base :
1566 ctxt->ds_base,
1567 c->regs[VCPU_REGS_RSI]);
1568 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1569 &c->src.val,
1570 c->src.bytes,
1571 ctxt->vcpu)) != 0)
1572 goto done;
1573
1574 c->dst.type = OP_NONE; /* Disable writeback. */
1575 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001576 c->dst.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001577 ctxt->es_base,
1578 c->regs[VCPU_REGS_RDI]);
1579 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1580 &c->dst.val,
1581 c->dst.bytes,
1582 ctxt->vcpu)) != 0)
1583 goto done;
1584
1585 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1586
1587 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1588
Harvey Harrison7a9572752008-02-19 07:40:41 -08001589 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001590 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1591 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001592 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001593 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1594 : c->dst.bytes);
1595
1596 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001598 c->dst.type = OP_MEM;
1599 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001600 c->dst.ptr = (unsigned long *)register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001601 ctxt->es_base,
1602 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001603 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001604 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001605 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001606 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 break;
1608 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 c->dst.type = OP_REG;
1610 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1611 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001612 if ((rc = ops->read_emulated(register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001613 c->override_base ? *c->override_base :
1614 ctxt->ds_base,
1615 c->regs[VCPU_REGS_RSI]),
1616 &c->dst.val,
1617 c->dst.bytes,
1618 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001620 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001621 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001622 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 break;
1624 case 0xae ... 0xaf: /* scas */
1625 DPRINTF("Urk! I don't handle SCAS.\n");
1626 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02001627 case 0xc0 ... 0xc1:
1628 emulate_grp2(ctxt);
1629 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001630 case 0xc3: /* ret */
1631 c->dst.ptr = &c->eip;
1632 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001633 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1634 mov:
1635 c->dst.val = c->src.val;
1636 break;
1637 case 0xd0 ... 0xd1: /* Grp2 */
1638 c->src.val = 1;
1639 emulate_grp2(ctxt);
1640 break;
1641 case 0xd2 ... 0xd3: /* Grp2 */
1642 c->src.val = c->regs[VCPU_REGS_RCX];
1643 emulate_grp2(ctxt);
1644 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001645 case 0xe8: /* call (near) */ {
1646 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001647 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001648 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001649 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001650 break;
1651 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001652 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001653 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001654 default:
1655 DPRINTF("Call: Invalid op_bytes\n");
1656 goto cannot_emulate;
1657 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001658 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001659 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001661 emulate_push(ctxt);
1662 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001663 }
1664 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001665 goto jmp;
1666 case 0xea: /* jmp far */ {
1667 uint32_t eip;
1668 uint16_t sel;
1669
1670 switch (c->op_bytes) {
1671 case 2:
1672 eip = insn_fetch(u16, 2, c->eip);
1673 break;
1674 case 4:
1675 eip = insn_fetch(u32, 4, c->eip);
1676 break;
1677 default:
1678 DPRINTF("jmp far: Invalid op_bytes\n");
1679 goto cannot_emulate;
1680 }
1681 sel = insn_fetch(u16, 2, c->eip);
1682 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1683 DPRINTF("jmp far: Failed to load CS descriptor\n");
1684 goto cannot_emulate;
1685 }
1686
1687 c->eip = eip;
1688 break;
1689 }
1690 case 0xeb:
1691 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001692 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001693 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001694 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001695 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001696 ctxt->vcpu->arch.halt_request = 1;
Avi Kivity111de5d2007-11-27 19:14:21 +02001697 goto done;
1698 case 0xf5: /* cmc */
1699 /* complement carry flag from eflags reg */
1700 ctxt->eflags ^= EFLG_CF;
1701 c->dst.type = OP_NONE; /* Disable writeback. */
1702 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001703 case 0xf6 ... 0xf7: /* Grp3 */
1704 rc = emulate_grp3(ctxt, ops);
1705 if (rc != 0)
1706 goto done;
1707 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001708 case 0xf8: /* clc */
1709 ctxt->eflags &= ~EFLG_CF;
1710 c->dst.type = OP_NONE; /* Disable writeback. */
1711 break;
1712 case 0xfa: /* cli */
1713 ctxt->eflags &= ~X86_EFLAGS_IF;
1714 c->dst.type = OP_NONE; /* Disable writeback. */
1715 break;
1716 case 0xfb: /* sti */
1717 ctxt->eflags |= X86_EFLAGS_IF;
1718 c->dst.type = OP_NONE; /* Disable writeback. */
1719 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001720 case 0xfe ... 0xff: /* Grp4/Grp5 */
1721 rc = emulate_grp45(ctxt, ops);
1722 if (rc != 0)
1723 goto done;
1724 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001726
1727writeback:
1728 rc = writeback(ctxt, ops);
1729 if (rc != 0)
1730 goto done;
1731
1732 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001733 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
1734 ctxt->vcpu->arch.rip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001735
1736done:
1737 if (rc == X86EMUL_UNHANDLEABLE) {
1738 c->eip = saved_eip;
1739 return -1;
1740 }
1741 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742
1743twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001744 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001746 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 u16 size;
1748 unsigned long address;
1749
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001750 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001751 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001752 goto cannot_emulate;
1753
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001754 rc = kvm_fix_hypercall(ctxt->vcpu);
1755 if (rc)
1756 goto done;
1757
Avi Kivity33e38852008-05-21 15:34:25 +03001758 /* Let the processor re-execute the fixed hypercall */
1759 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity16286d02008-04-14 14:40:50 +03001760 /* Disable writeback. */
1761 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001762 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001764 rc = read_descriptor(ctxt, ops, c->src.ptr,
1765 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 if (rc)
1767 goto done;
1768 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001769 /* Disable writeback. */
1770 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001772 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001773 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001774 rc = kvm_fix_hypercall(ctxt->vcpu);
1775 if (rc)
1776 goto done;
1777 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001778 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001779 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001780 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001781 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001782 if (rc)
1783 goto done;
1784 realmode_lidt(ctxt->vcpu, size, address);
1785 }
Avi Kivity16286d02008-04-14 14:40:50 +03001786 /* Disable writeback. */
1787 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 break;
1789 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001790 c->dst.bytes = 2;
1791 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792 break;
1793 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001794 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1795 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001796 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 break;
1798 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001799 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001800 /* Disable writeback. */
1801 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 break;
1803 default:
1804 goto cannot_emulate;
1805 }
1806 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001807 case 0x06:
1808 emulate_clts(ctxt->vcpu);
1809 c->dst.type = OP_NONE;
1810 break;
1811 case 0x08: /* invd */
1812 case 0x09: /* wbinvd */
1813 case 0x0d: /* GrpP (prefetch) */
1814 case 0x18: /* Grp16 (prefetch/nop) */
1815 c->dst.type = OP_NONE;
1816 break;
1817 case 0x20: /* mov cr, reg */
1818 if (c->modrm_mod != 3)
1819 goto cannot_emulate;
1820 c->regs[c->modrm_rm] =
1821 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1822 c->dst.type = OP_NONE; /* no writeback */
1823 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001825 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001828 if (rc)
1829 goto cannot_emulate;
1830 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001832 case 0x22: /* mov reg, cr */
1833 if (c->modrm_mod != 3)
1834 goto cannot_emulate;
1835 realmode_set_cr(ctxt->vcpu,
1836 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1837 c->dst.type = OP_NONE;
1838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001840 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001842 rc = emulator_set_dr(ctxt, c->modrm_reg,
1843 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001844 if (rc)
1845 goto cannot_emulate;
1846 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001848 case 0x30:
1849 /* wrmsr */
1850 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1851 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1852 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1853 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001854 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001855 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001856 }
1857 rc = X86EMUL_CONTINUE;
1858 c->dst.type = OP_NONE;
1859 break;
1860 case 0x32:
1861 /* rdmsr */
1862 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1863 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001864 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001865 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001866 } else {
1867 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1868 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1869 }
1870 rc = X86EMUL_CONTINUE;
1871 c->dst.type = OP_NONE;
1872 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001874 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001875 if (!test_cc(c->b, ctxt->eflags))
1876 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001878 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1879 long int rel;
1880
1881 switch (c->op_bytes) {
1882 case 2:
1883 rel = insn_fetch(s16, 2, c->eip);
1884 break;
1885 case 4:
1886 rel = insn_fetch(s32, 4, c->eip);
1887 break;
1888 case 8:
1889 rel = insn_fetch(s64, 8, c->eip);
1890 break;
1891 default:
1892 DPRINTF("jnz: Invalid op_bytes\n");
1893 goto cannot_emulate;
1894 }
1895 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001896 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001897 c->dst.type = OP_NONE;
1898 break;
1899 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001900 case 0xa3:
1901 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001902 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001903 /* only subword offset */
1904 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001905 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001906 break;
1907 case 0xab:
1908 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001909 /* only subword offset */
1910 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001911 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001912 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 case 0xb0 ... 0xb1: /* cmpxchg */
1914 /*
1915 * Save real source value, then compare EAX against
1916 * destination.
1917 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001918 c->src.orig_val = c->src.val;
1919 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001920 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1921 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001923 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 } else {
1925 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 c->dst.type = OP_REG;
1927 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 }
1929 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 case 0xb3:
1931 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001932 /* only subword offset */
1933 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001934 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001937 c->dst.bytes = c->op_bytes;
1938 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1939 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001942 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 case 0:
1944 goto bt;
1945 case 1:
1946 goto bts;
1947 case 2:
1948 goto btr;
1949 case 3:
1950 goto btc;
1951 }
1952 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001953 case 0xbb:
1954 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001955 /* only subword offset */
1956 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001957 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001958 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001960 c->dst.bytes = c->op_bytes;
1961 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1962 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001964 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001965 c->dst.bytes = c->op_bytes;
1966 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1967 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001968 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001970 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001971 if (rc != 0)
1972 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02001973 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001974 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 }
1976 goto writeback;
1977
1978cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001979 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001980 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 return -1;
1982}