blob: 3721cfddc9737df3bf68773220e298106fa1c9a0 [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 Kivity91ed7a02008-05-29 14:38:38 +0300124 SrcImm | Mov | Stack, 0, SrcImmByte | 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,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200141 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200142 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300143 /* 0x90 - 0x97 */
144 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
145 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200146 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200148 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
149 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200150 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
151 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200153 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
154 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
155 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156 /* 0xB0 - 0xBF */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +0200157 0, 0, 0, 0, 0, 0, 0, 0,
158 DstReg | SrcImm | Mov, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300160 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200161 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300162 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 /* 0xC8 - 0xCF */
164 0, 0, 0, 0, 0, 0, 0, 0,
165 /* 0xD0 - 0xD7 */
166 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
167 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
168 0, 0, 0, 0,
169 /* 0xD8 - 0xDF */
170 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300171 /* 0xE0 - 0xE7 */
172 0, 0, 0, 0, 0, 0, 0, 0,
173 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200174 ImplicitOps | Stack, SrcImm | ImplicitOps,
175 ImplicitOps, SrcImmByte | ImplicitOps,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200176 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 /* 0xF0 - 0xF7 */
178 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200179 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700181 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Avi Kivityfd607542008-01-18 13:12:26 +0200182 0, 0, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183};
184
Avi Kivity038e51d2007-01-22 20:40:40 -0800185static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200187 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200188 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0x10 - 0x1F */
190 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
191 /* 0x20 - 0x2F */
192 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
193 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300195 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0x40 - 0x47 */
197 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
198 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
199 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
200 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
201 /* 0x48 - 0x4F */
202 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
203 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
204 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
205 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
206 /* 0x50 - 0x5F */
207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
208 /* 0x60 - 0x6F */
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 /* 0x70 - 0x7F */
211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300213 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
214 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
215 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
216 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0x90 - 0x9F */
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800220 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800222 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0xB0 - 0xB7 */
224 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800225 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
227 DstReg | SrcMem16 | ModRM | Mov,
228 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800229 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
231 DstReg | SrcMem16 | ModRM | Mov,
232 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800233 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
234 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 /* 0xD0 - 0xDF */
236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 /* 0xE0 - 0xEF */
238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0xF0 - 0xFF */
240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
241};
242
Avi Kivitye09d0822008-01-18 12:38:59 +0200243static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200244 [Group1_80*8] =
245 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
246 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
247 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
248 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
249 [Group1_81*8] =
250 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
251 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
252 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
253 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
254 [Group1_82*8] =
255 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
256 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
257 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
258 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
259 [Group1_83*8] =
260 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
261 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
262 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
263 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200264 [Group1A*8] =
265 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200266 [Group3_Byte*8] =
267 ByteOp | SrcImm | DstMem | ModRM, 0,
268 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
269 0, 0, 0, 0,
270 [Group3*8] =
271 DstMem | SrcImm | ModRM | SrcImm, 0,
272 DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
273 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200274 [Group4*8] =
275 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
276 0, 0, 0, 0, 0, 0,
277 [Group5*8] =
278 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, 0, 0,
279 SrcMem | ModRM, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200280 [Group7*8] =
281 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300282 SrcNone | ModRM | DstMem | Mov, 0,
283 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200284};
285
286static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200287 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300288 SrcNone | ModRM, 0, 0, 0,
289 SrcNone | ModRM | DstMem | Mov, 0,
290 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200291};
292
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293/* EFLAGS bit definitions. */
294#define EFLG_OF (1<<11)
295#define EFLG_DF (1<<10)
296#define EFLG_SF (1<<7)
297#define EFLG_ZF (1<<6)
298#define EFLG_AF (1<<4)
299#define EFLG_PF (1<<2)
300#define EFLG_CF (1<<0)
301
302/*
303 * Instruction emulation:
304 * Most instructions are emulated directly via a fragment of inline assembly
305 * code. This allows us to save/restore EFLAGS and thus very easily pick up
306 * any modified flags.
307 */
308
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800309#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310#define _LO32 "k" /* force 32-bit operand */
311#define _STK "%%rsp" /* stack pointer */
312#elif defined(__i386__)
313#define _LO32 "" /* force 32-bit operand */
314#define _STK "%%esp" /* stack pointer */
315#endif
316
317/*
318 * These EFLAGS bits are restored from saved value during emulation, and
319 * any changes are written back to the saved value after emulation.
320 */
321#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
322
323/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200324#define _PRE_EFLAGS(_sav, _msk, _tmp) \
325 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
326 "movl %"_sav",%"_LO32 _tmp"; " \
327 "push %"_tmp"; " \
328 "push %"_tmp"; " \
329 "movl %"_msk",%"_LO32 _tmp"; " \
330 "andl %"_LO32 _tmp",("_STK"); " \
331 "pushf; " \
332 "notl %"_LO32 _tmp"; " \
333 "andl %"_LO32 _tmp",("_STK"); " \
334 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
335 "pop %"_tmp"; " \
336 "orl %"_LO32 _tmp",("_STK"); " \
337 "popf; " \
338 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339
340/* After executing instruction: write-back necessary bits in EFLAGS. */
341#define _POST_EFLAGS(_sav, _msk, _tmp) \
342 /* _sav |= EFLAGS & _msk; */ \
343 "pushf; " \
344 "pop %"_tmp"; " \
345 "andl %"_msk",%"_LO32 _tmp"; " \
346 "orl %"_LO32 _tmp",%"_sav"; "
347
348/* Raw emulation: instruction has two explicit operands. */
349#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
350 do { \
351 unsigned long _tmp; \
352 \
353 switch ((_dst).bytes) { \
354 case 2: \
355 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400356 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400358 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 : "=m" (_eflags), "=m" ((_dst).val), \
360 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400361 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 break; \
363 case 4: \
364 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400365 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400367 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 : "=m" (_eflags), "=m" ((_dst).val), \
369 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400370 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 break; \
372 case 8: \
373 __emulate_2op_8byte(_op, _src, _dst, \
374 _eflags, _qx, _qy); \
375 break; \
376 } \
377 } while (0)
378
379#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
380 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800381 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400382 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 case 1: \
384 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400385 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400387 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800389 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400390 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 break; \
392 default: \
393 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
394 _wx, _wy, _lx, _ly, _qx, _qy); \
395 break; \
396 } \
397 } while (0)
398
399/* Source operand is byte-sized and may be restricted to just %cl. */
400#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
401 __emulate_2op(_op, _src, _dst, _eflags, \
402 "b", "c", "b", "c", "b", "c", "b", "c")
403
404/* Source operand is byte, word, long or quad sized. */
405#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
406 __emulate_2op(_op, _src, _dst, _eflags, \
407 "b", "q", "w", "r", _LO32, "r", "", "r")
408
409/* Source operand is word, long or quad sized. */
410#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
411 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
412 "w", "r", _LO32, "r", "", "r")
413
414/* Instruction has only one explicit operand (no source operand). */
415#define emulate_1op(_op, _dst, _eflags) \
416 do { \
417 unsigned long _tmp; \
418 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400419 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 case 1: \
421 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400422 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400424 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 : "=m" (_eflags), "=m" ((_dst).val), \
426 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400427 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 break; \
429 case 2: \
430 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400431 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400433 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 : "=m" (_eflags), "=m" ((_dst).val), \
435 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400436 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 break; \
438 case 4: \
439 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400440 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 : "=m" (_eflags), "=m" ((_dst).val), \
444 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400445 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 break; \
447 case 8: \
448 __emulate_1op_8byte(_op, _dst, _eflags); \
449 break; \
450 } \
451 } while (0)
452
453/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800454#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
456 do { \
457 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400458 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400460 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400462 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 } while (0)
464
465#define __emulate_1op_8byte(_op, _dst, _eflags) \
466 do { \
467 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400468 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400470 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400472 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 } while (0)
474
475#elif defined(__i386__)
476#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
477#define __emulate_1op_8byte(_op, _dst, _eflags)
478#endif /* __i386__ */
479
480/* Fetch next part of the instruction being emulated. */
481#define insn_fetch(_type, _size, _eip) \
482({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200483 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400484 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 goto done; \
486 (_eip) += (_size); \
487 (_type)_x; \
488})
489
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800490static inline unsigned long ad_mask(struct decode_cache *c)
491{
492 return (1UL << (c->ad_bytes << 3)) - 1;
493}
494
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800496static inline unsigned long
497address_mask(struct decode_cache *c, unsigned long reg)
498{
499 if (c->ad_bytes == sizeof(unsigned long))
500 return reg;
501 else
502 return reg & ad_mask(c);
503}
504
505static inline unsigned long
506register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
507{
508 return base + address_mask(c, reg);
509}
510
Harvey Harrison7a9572752008-02-19 07:40:41 -0800511static inline void
512register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
513{
514 if (c->ad_bytes == sizeof(unsigned long))
515 *reg += inc;
516 else
517 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
518}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519
Harvey Harrison7a9572752008-02-19 07:40:41 -0800520static inline void jmp_rel(struct decode_cache *c, int rel)
521{
522 register_address_increment(c, &c->eip, rel);
523}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300524
Avi Kivity62266862007-11-20 13:15:52 +0200525static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
526 struct x86_emulate_ops *ops,
527 unsigned long linear, u8 *dest)
528{
529 struct fetch_cache *fc = &ctxt->decode.fetch;
530 int rc;
531 int size;
532
533 if (linear < fc->start || linear >= fc->end) {
534 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
535 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
536 if (rc)
537 return rc;
538 fc->start = linear;
539 fc->end = linear + size;
540 }
541 *dest = fc->data[linear - fc->start];
542 return 0;
543}
544
545static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
546 struct x86_emulate_ops *ops,
547 unsigned long eip, void *dest, unsigned size)
548{
549 int rc = 0;
550
551 eip += ctxt->cs_base;
552 while (size--) {
553 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
554 if (rc)
555 return rc;
556 }
557 return 0;
558}
559
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000560/*
561 * Given the 'reg' portion of a ModRM byte, and a register block, return a
562 * pointer into the block that addresses the relevant register.
563 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
564 */
565static void *decode_register(u8 modrm_reg, unsigned long *regs,
566 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567{
568 void *p;
569
570 p = &regs[modrm_reg];
571 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
572 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
573 return p;
574}
575
576static int read_descriptor(struct x86_emulate_ctxt *ctxt,
577 struct x86_emulate_ops *ops,
578 void *ptr,
579 u16 *size, unsigned long *address, int op_bytes)
580{
581 int rc;
582
583 if (op_bytes == 2)
584 op_bytes = 3;
585 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300586 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
587 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 if (rc)
589 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300590 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
591 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 return rc;
593}
594
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300595static int test_cc(unsigned int condition, unsigned int flags)
596{
597 int rc = 0;
598
599 switch ((condition & 15) >> 1) {
600 case 0: /* o */
601 rc |= (flags & EFLG_OF);
602 break;
603 case 1: /* b/c/nae */
604 rc |= (flags & EFLG_CF);
605 break;
606 case 2: /* z/e */
607 rc |= (flags & EFLG_ZF);
608 break;
609 case 3: /* be/na */
610 rc |= (flags & (EFLG_CF|EFLG_ZF));
611 break;
612 case 4: /* s */
613 rc |= (flags & EFLG_SF);
614 break;
615 case 5: /* p/pe */
616 rc |= (flags & EFLG_PF);
617 break;
618 case 7: /* le/ng */
619 rc |= (flags & EFLG_ZF);
620 /* fall through */
621 case 6: /* l/nge */
622 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
623 break;
624 }
625
626 /* Odd condition identifiers (lsb == 1) have inverted sense. */
627 return (!!rc ^ (condition & 1));
628}
629
Avi Kivity3c118e22007-10-31 10:27:04 +0200630static void decode_register_operand(struct operand *op,
631 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200632 int inhibit_bytereg)
633{
Avi Kivity33615aa2007-10-31 11:15:56 +0200634 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200635 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200636
637 if (!(c->d & ModRM))
638 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200639 op->type = OP_REG;
640 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200641 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200642 op->val = *(u8 *)op->ptr;
643 op->bytes = 1;
644 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200645 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200646 op->bytes = c->op_bytes;
647 switch (op->bytes) {
648 case 2:
649 op->val = *(u16 *)op->ptr;
650 break;
651 case 4:
652 op->val = *(u32 *)op->ptr;
653 break;
654 case 8:
655 op->val = *(u64 *) op->ptr;
656 break;
657 }
658 }
659 op->orig_val = op->val;
660}
661
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200662static int decode_modrm(struct x86_emulate_ctxt *ctxt,
663 struct x86_emulate_ops *ops)
664{
665 struct decode_cache *c = &ctxt->decode;
666 u8 sib;
667 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
668 int rc = 0;
669
670 if (c->rex_prefix) {
671 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
672 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
673 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
674 }
675
676 c->modrm = insn_fetch(u8, 1, c->eip);
677 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
678 c->modrm_reg |= (c->modrm & 0x38) >> 3;
679 c->modrm_rm |= (c->modrm & 0x07);
680 c->modrm_ea = 0;
681 c->use_modrm_ea = 1;
682
683 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300684 c->modrm_ptr = decode_register(c->modrm_rm,
685 c->regs, c->d & ByteOp);
686 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200687 return rc;
688 }
689
690 if (c->ad_bytes == 2) {
691 unsigned bx = c->regs[VCPU_REGS_RBX];
692 unsigned bp = c->regs[VCPU_REGS_RBP];
693 unsigned si = c->regs[VCPU_REGS_RSI];
694 unsigned di = c->regs[VCPU_REGS_RDI];
695
696 /* 16-bit ModR/M decode. */
697 switch (c->modrm_mod) {
698 case 0:
699 if (c->modrm_rm == 6)
700 c->modrm_ea += insn_fetch(u16, 2, c->eip);
701 break;
702 case 1:
703 c->modrm_ea += insn_fetch(s8, 1, c->eip);
704 break;
705 case 2:
706 c->modrm_ea += insn_fetch(u16, 2, c->eip);
707 break;
708 }
709 switch (c->modrm_rm) {
710 case 0:
711 c->modrm_ea += bx + si;
712 break;
713 case 1:
714 c->modrm_ea += bx + di;
715 break;
716 case 2:
717 c->modrm_ea += bp + si;
718 break;
719 case 3:
720 c->modrm_ea += bp + di;
721 break;
722 case 4:
723 c->modrm_ea += si;
724 break;
725 case 5:
726 c->modrm_ea += di;
727 break;
728 case 6:
729 if (c->modrm_mod != 0)
730 c->modrm_ea += bp;
731 break;
732 case 7:
733 c->modrm_ea += bx;
734 break;
735 }
736 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
737 (c->modrm_rm == 6 && c->modrm_mod != 0))
738 if (!c->override_base)
739 c->override_base = &ctxt->ss_base;
740 c->modrm_ea = (u16)c->modrm_ea;
741 } else {
742 /* 32/64-bit ModR/M decode. */
743 switch (c->modrm_rm) {
744 case 4:
745 case 12:
746 sib = insn_fetch(u8, 1, c->eip);
747 index_reg |= (sib >> 3) & 7;
748 base_reg |= sib & 7;
749 scale = sib >> 6;
750
751 switch (base_reg) {
752 case 5:
Avi Kivity8684c0a2008-06-15 21:13:41 -0700753 case 13:
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200754 if (c->modrm_mod != 0)
755 c->modrm_ea += c->regs[base_reg];
756 else
757 c->modrm_ea +=
758 insn_fetch(s32, 4, c->eip);
759 break;
760 default:
761 c->modrm_ea += c->regs[base_reg];
762 }
763 switch (index_reg) {
764 case 4:
765 break;
766 default:
767 c->modrm_ea += c->regs[index_reg] << scale;
768 }
769 break;
770 case 5:
Avi Kivity8684c0a2008-06-15 21:13:41 -0700771 case 13:
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200772 if (c->modrm_mod != 0)
773 c->modrm_ea += c->regs[c->modrm_rm];
774 else if (ctxt->mode == X86EMUL_MODE_PROT64)
775 rip_relative = 1;
776 break;
777 default:
778 c->modrm_ea += c->regs[c->modrm_rm];
779 break;
780 }
781 switch (c->modrm_mod) {
782 case 0:
783 if (c->modrm_rm == 5)
784 c->modrm_ea += insn_fetch(s32, 4, c->eip);
785 break;
786 case 1:
787 c->modrm_ea += insn_fetch(s8, 1, c->eip);
788 break;
789 case 2:
790 c->modrm_ea += insn_fetch(s32, 4, c->eip);
791 break;
792 }
793 }
794 if (rip_relative) {
795 c->modrm_ea += c->eip;
796 switch (c->d & SrcMask) {
797 case SrcImmByte:
798 c->modrm_ea += 1;
799 break;
800 case SrcImm:
801 if (c->d & ByteOp)
802 c->modrm_ea += 1;
803 else
804 if (c->op_bytes == 8)
805 c->modrm_ea += 4;
806 else
807 c->modrm_ea += c->op_bytes;
808 }
809 }
810done:
811 return rc;
812}
813
814static int decode_abs(struct x86_emulate_ctxt *ctxt,
815 struct x86_emulate_ops *ops)
816{
817 struct decode_cache *c = &ctxt->decode;
818 int rc = 0;
819
820 switch (c->ad_bytes) {
821 case 2:
822 c->modrm_ea = insn_fetch(u16, 2, c->eip);
823 break;
824 case 4:
825 c->modrm_ea = insn_fetch(u32, 4, c->eip);
826 break;
827 case 8:
828 c->modrm_ea = insn_fetch(u64, 8, c->eip);
829 break;
830 }
831done:
832 return rc;
833}
834
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200836x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200838 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200841 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842
843 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
Laurent Viviere4e03de2007-09-18 11:52:50 +0200845 memset(c, 0, sizeof(struct decode_cache));
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800846 c->eip = ctxt->vcpu->arch.rip;
847 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
849 switch (mode) {
850 case X86EMUL_MODE_REAL:
851 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200852 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 break;
854 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200855 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800857#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200859 def_op_bytes = 4;
860 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 break;
862#endif
863 default:
864 return -1;
865 }
866
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200867 c->op_bytes = def_op_bytes;
868 c->ad_bytes = def_ad_bytes;
869
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200871 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200872 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200874 /* switch between 2/4 bytes */
875 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 break;
877 case 0x67: /* address-size override */
878 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200879 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200880 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200882 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200883 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 break;
885 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200886 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 break;
888 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
891 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 break;
894 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200895 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 break;
897 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200898 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 break;
900 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200903 case 0x40 ... 0x4f: /* REX */
904 if (mode != X86EMUL_MODE_PROT64)
905 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200906 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200907 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200909 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200911 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100912 c->rep_prefix = REPNE_PREFIX;
913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100915 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 default:
918 goto done_prefixes;
919 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200920
921 /* Any legacy prefix after a REX prefix nullifies its effect. */
922
Avi Kivity33615aa2007-10-31 11:15:56 +0200923 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 }
925
926done_prefixes:
927
928 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200930 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200931 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
933 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200934 c->d = opcode_table[c->b];
935 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200937 if (c->b == 0x0f) {
938 c->twobyte = 1;
939 c->b = insn_fetch(u8, 1, c->eip);
940 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200942 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
Avi Kivitye09d0822008-01-18 12:38:59 +0200944 if (c->d & Group) {
945 group = c->d & GroupMask;
946 c->modrm = insn_fetch(u8, 1, c->eip);
947 --c->eip;
948
949 group = (group << 3) + ((c->modrm >> 3) & 7);
950 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
951 c->d = group2_table[group];
952 else
953 c->d = group_table[group];
954 }
955
956 /* Unrecognised? */
957 if (c->d == 0) {
958 DPRINTF("Cannot emulate %02x\n", c->b);
959 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 }
961
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200962 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
963 c->op_bytes = 8;
964
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200966 if (c->d & ModRM)
967 rc = decode_modrm(ctxt, ops);
968 else if (c->d & MemAbs)
969 rc = decode_abs(ctxt, ops);
970 if (rc)
971 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
Avi Kivityc7e75a32007-10-28 16:34:25 +0200973 if (!c->override_base)
974 c->override_base = &ctxt->ds_base;
975 if (mode == X86EMUL_MODE_PROT64 &&
976 c->override_base != &ctxt->fs_base &&
977 c->override_base != &ctxt->gs_base)
978 c->override_base = NULL;
979
980 if (c->override_base)
981 c->modrm_ea += *c->override_base;
982
983 if (c->ad_bytes != 8)
984 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 /*
986 * Decode and fetch the source operand: register, memory
987 * or immediate.
988 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200989 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 case SrcNone:
991 break;
992 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200993 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 break;
995 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 goto srcmem_common;
998 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200999 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 goto srcmem_common;
1001 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001002 c->src.bytes = (c->d & ByteOp) ? 1 :
1003 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001004 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001005 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001006 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001007 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001008 /*
1009 * For instructions with a ModR/M byte, switch to register
1010 * access if Mod = 3.
1011 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 if ((c->d & ModRM) && c->modrm_mod == 3) {
1013 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001014 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001015 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001016 break;
1017 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 break;
1020 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001021 c->src.type = OP_IMM;
1022 c->src.ptr = (unsigned long *)c->eip;
1023 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1024 if (c->src.bytes == 8)
1025 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001027 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 break;
1031 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001032 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 break;
1034 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 break;
1037 }
1038 break;
1039 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001040 c->src.type = OP_IMM;
1041 c->src.ptr = (unsigned long *)c->eip;
1042 c->src.bytes = 1;
1043 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
1045 }
1046
Avi Kivity038e51d2007-01-22 20:40:40 -08001047 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001048 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001049 case ImplicitOps:
1050 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001051 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001052 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001053 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001054 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001055 break;
1056 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001057 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001058 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001059 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001060 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001061 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001062 break;
1063 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001064 c->dst.type = OP_MEM;
1065 break;
1066 }
1067
1068done:
1069 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1070}
1071
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001072static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1073{
1074 struct decode_cache *c = &ctxt->decode;
1075
1076 c->dst.type = OP_MEM;
1077 c->dst.bytes = c->op_bytes;
1078 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001079 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Harvey Harrisone4706772008-02-19 07:40:38 -08001080 c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001081 c->regs[VCPU_REGS_RSP]);
1082}
1083
1084static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1085 struct x86_emulate_ops *ops)
1086{
1087 struct decode_cache *c = &ctxt->decode;
1088 int rc;
1089
Harvey Harrisone4706772008-02-19 07:40:38 -08001090 rc = ops->read_std(register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001091 c->regs[VCPU_REGS_RSP]),
1092 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1093 if (rc != 0)
1094 return rc;
1095
Harvey Harrison7a9572752008-02-19 07:40:41 -08001096 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001097
1098 return 0;
1099}
1100
Laurent Vivier05f086f2007-09-24 11:10:55 +02001101static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001102{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001103 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001104 switch (c->modrm_reg) {
1105 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001106 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001107 break;
1108 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001109 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001110 break;
1111 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001112 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001113 break;
1114 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001115 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001116 break;
1117 case 4: /* sal/shl */
1118 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001119 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001120 break;
1121 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001122 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001123 break;
1124 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001125 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001126 break;
1127 }
1128}
1129
1130static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001131 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001132{
1133 struct decode_cache *c = &ctxt->decode;
1134 int rc = 0;
1135
1136 switch (c->modrm_reg) {
1137 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001138 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001139 break;
1140 case 2: /* not */
1141 c->dst.val = ~c->dst.val;
1142 break;
1143 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001144 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001145 break;
1146 default:
1147 DPRINTF("Cannot emulate %02x\n", c->b);
1148 rc = X86EMUL_UNHANDLEABLE;
1149 break;
1150 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001151 return rc;
1152}
1153
1154static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001155 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001156{
1157 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001158
1159 switch (c->modrm_reg) {
1160 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001161 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001162 break;
1163 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001164 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165 break;
1166 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001167 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001168 break;
1169 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001170 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001171 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001172 }
1173 return 0;
1174}
1175
1176static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1177 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001178 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001179{
1180 struct decode_cache *c = &ctxt->decode;
1181 u64 old, new;
1182 int rc;
1183
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001184 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001185 if (rc != 0)
1186 return rc;
1187
1188 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1189 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1190
1191 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1192 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001193 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194
1195 } else {
1196 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1197 (u32) c->regs[VCPU_REGS_RBX];
1198
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001199 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 if (rc != 0)
1201 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001202 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001203 }
1204 return 0;
1205}
1206
1207static inline int writeback(struct x86_emulate_ctxt *ctxt,
1208 struct x86_emulate_ops *ops)
1209{
1210 int rc;
1211 struct decode_cache *c = &ctxt->decode;
1212
1213 switch (c->dst.type) {
1214 case OP_REG:
1215 /* The 4-byte case *is* correct:
1216 * in 64-bit mode we zero-extend.
1217 */
1218 switch (c->dst.bytes) {
1219 case 1:
1220 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1221 break;
1222 case 2:
1223 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1224 break;
1225 case 4:
1226 *c->dst.ptr = (u32)c->dst.val;
1227 break; /* 64b: zero-ext */
1228 case 8:
1229 *c->dst.ptr = c->dst.val;
1230 break;
1231 }
1232 break;
1233 case OP_MEM:
1234 if (c->lock_prefix)
1235 rc = ops->cmpxchg_emulated(
1236 (unsigned long)c->dst.ptr,
1237 &c->dst.orig_val,
1238 &c->dst.val,
1239 c->dst.bytes,
1240 ctxt->vcpu);
1241 else
1242 rc = ops->write_emulated(
1243 (unsigned long)c->dst.ptr,
1244 &c->dst.val,
1245 c->dst.bytes,
1246 ctxt->vcpu);
1247 if (rc != 0)
1248 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001249 break;
1250 case OP_NONE:
1251 /* no writeback */
1252 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 default:
1254 break;
1255 }
1256 return 0;
1257}
1258
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001259int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001260x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001261{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001262 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001263 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001264 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001265 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001266 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001267
Laurent Vivier34273182007-09-18 11:27:37 +02001268 /* Shadow copy of register state. Committed on successful emulation.
1269 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1270 * modify them.
1271 */
1272
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001273 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001274 saved_eip = c->eip;
1275
Avi Kivityc7e75a32007-10-28 16:34:25 +02001276 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001277 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001278
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001279 if (c->rep_prefix && (c->d & String)) {
1280 /* All REP prefixes have the same first termination condition */
1281 if (c->regs[VCPU_REGS_RCX] == 0) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001282 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001283 goto done;
1284 }
1285 /* The second termination condition only applies for REPE
1286 * and REPNE. Test if the repeat string operation prefix is
1287 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1288 * corresponding termination condition according to:
1289 * - if REPE/REPZ and ZF = 0 then done
1290 * - if REPNE/REPNZ and ZF = 1 then done
1291 */
1292 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1293 (c->b == 0xae) || (c->b == 0xaf)) {
1294 if ((c->rep_prefix == REPE_PREFIX) &&
1295 ((ctxt->eflags & EFLG_ZF) == 0)) {
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 if ((c->rep_prefix == REPNE_PREFIX) &&
1300 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001302 goto done;
1303 }
1304 }
1305 c->regs[VCPU_REGS_RCX]--;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001306 c->eip = ctxt->vcpu->arch.rip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001307 }
1308
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001309 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001310 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001311 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001312 rc = ops->read_emulated((unsigned long)c->src.ptr,
1313 &c->src.val,
1314 c->src.bytes,
1315 ctxt->vcpu);
1316 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001317 goto done;
1318 c->src.orig_val = c->src.val;
1319 }
1320
1321 if ((c->d & DstMask) == ImplicitOps)
1322 goto special_insn;
1323
1324
1325 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001326 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001327 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1328 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001329 if (c->d & BitOp) {
1330 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001331
Laurent Viviere4e03de2007-09-18 11:52:50 +02001332 c->dst.ptr = (void *)c->dst.ptr +
1333 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001334 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001335 if (!(c->d & Mov) &&
1336 /* optimisation - avoid slow emulated read */
1337 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1338 &c->dst.val,
1339 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001340 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001341 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001342 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001343
Avi Kivity018a98d2007-11-27 19:30:56 +02001344special_insn:
1345
Laurent Viviere4e03de2007-09-18 11:52:50 +02001346 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 goto twobyte_insn;
1348
Laurent Viviere4e03de2007-09-18 11:52:50 +02001349 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 case 0x00 ... 0x05:
1351 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001352 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 break;
1354 case 0x08 ... 0x0d:
1355 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001356 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 break;
1358 case 0x10 ... 0x15:
1359 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001360 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 break;
1362 case 0x18 ... 0x1d:
1363 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001364 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001366 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001368 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001370 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001371 c->dst.type = OP_REG;
1372 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1373 c->dst.val = *(u8 *)c->dst.ptr;
1374 c->dst.bytes = 1;
1375 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001376 goto and;
1377 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001378 c->dst.type = OP_REG;
1379 c->dst.bytes = c->op_bytes;
1380 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1381 if (c->op_bytes == 2)
1382 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001383 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001384 c->dst.val = *(u32 *)c->dst.ptr;
1385 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001386 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 case 0x28 ... 0x2d:
1388 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 break;
1391 case 0x30 ... 0x35:
1392 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001393 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 break;
1395 case 0x38 ... 0x3d:
1396 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001399 case 0x40 ... 0x47: /* inc r16/r32 */
1400 emulate_1op("inc", c->dst, ctxt->eflags);
1401 break;
1402 case 0x48 ... 0x4f: /* dec r16/r32 */
1403 emulate_1op("dec", c->dst, ctxt->eflags);
1404 break;
1405 case 0x50 ... 0x57: /* push reg */
1406 c->dst.type = OP_MEM;
1407 c->dst.bytes = c->op_bytes;
1408 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001409 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001410 -c->op_bytes);
1411 c->dst.ptr = (void *) register_address(
Harvey Harrisone4706772008-02-19 07:40:38 -08001412 c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Avi Kivity33615aa2007-10-31 11:15:56 +02001413 break;
1414 case 0x58 ... 0x5f: /* pop reg */
1415 pop_instruction:
Harvey Harrisone4706772008-02-19 07:40:38 -08001416 if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
Avi Kivity33615aa2007-10-31 11:15:56 +02001417 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1418 c->op_bytes, ctxt->vcpu)) != 0)
1419 goto done;
1420
Harvey Harrison7a9572752008-02-19 07:40:41 -08001421 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001422 c->op_bytes);
1423 c->dst.type = OP_NONE; /* Disable writeback. */
1424 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001426 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001428 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001430 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001431 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001432 emulate_push(ctxt);
1433 break;
1434 case 0x6c: /* insb */
1435 case 0x6d: /* insw/insd */
1436 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1437 1,
1438 (c->d & ByteOp) ? 1 : c->op_bytes,
1439 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001440 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001441 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001442 register_address(c, ctxt->es_base,
Avi Kivity018a98d2007-11-27 19:30:56 +02001443 c->regs[VCPU_REGS_RDI]),
1444 c->rep_prefix,
1445 c->regs[VCPU_REGS_RDX]) == 0) {
1446 c->eip = saved_eip;
1447 return -1;
1448 }
1449 return 0;
1450 case 0x6e: /* outsb */
1451 case 0x6f: /* outsw/outsd */
1452 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1453 0,
1454 (c->d & ByteOp) ? 1 : c->op_bytes,
1455 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001456 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001457 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001458 register_address(c, c->override_base ?
Avi Kivity018a98d2007-11-27 19:30:56 +02001459 *c->override_base :
1460 ctxt->ds_base,
1461 c->regs[VCPU_REGS_RSI]),
1462 c->rep_prefix,
1463 c->regs[VCPU_REGS_RDX]) == 0) {
1464 c->eip = saved_eip;
1465 return -1;
1466 }
1467 return 0;
1468 case 0x70 ... 0x7f: /* jcc (short) */ {
1469 int rel = insn_fetch(s8, 1, c->eip);
1470
1471 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001472 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001473 break;
1474 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001476 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 case 0:
1478 goto add;
1479 case 1:
1480 goto or;
1481 case 2:
1482 goto adc;
1483 case 3:
1484 goto sbb;
1485 case 4:
1486 goto and;
1487 case 5:
1488 goto sub;
1489 case 6:
1490 goto xor;
1491 case 7:
1492 goto cmp;
1493 }
1494 break;
1495 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001496 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 break;
1498 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001499 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001501 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001503 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 break;
1505 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 break;
1508 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001509 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 break; /* 64b reg: zero-extend */
1511 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001512 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 break;
1514 }
1515 /*
1516 * Write back the memory destination with implicit LOCK
1517 * prefix.
1518 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001519 c->dst.val = c->src.val;
1520 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001523 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001524 case 0x8c: { /* mov r/m, sreg */
1525 struct kvm_segment segreg;
1526
1527 if (c->modrm_reg <= 5)
1528 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1529 else {
1530 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1531 c->modrm);
1532 goto cannot_emulate;
1533 }
1534 c->dst.val = segreg.selector;
1535 break;
1536 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001537 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001538 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001539 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001540 case 0x8e: { /* mov seg, r/m16 */
1541 uint16_t sel;
1542 int type_bits;
1543 int err;
1544
1545 sel = c->src.val;
1546 if (c->modrm_reg <= 5) {
1547 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1548 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1549 type_bits, c->modrm_reg);
1550 } else {
1551 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1552 c->modrm);
1553 goto cannot_emulate;
1554 }
1555
1556 if (err < 0)
1557 goto cannot_emulate;
1558
1559 c->dst.type = OP_NONE; /* Disable writeback. */
1560 break;
1561 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001563 rc = emulate_grp1a(ctxt, ops);
1564 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001567 case 0x90: /* nop / xchg r8,rax */
1568 if (!(c->rex_prefix & 1)) { /* nop */
1569 c->dst.type = OP_NONE;
1570 break;
1571 }
1572 case 0x91 ... 0x97: /* xchg reg,rax */
1573 c->src.type = c->dst.type = OP_REG;
1574 c->src.bytes = c->dst.bytes = c->op_bytes;
1575 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1576 c->src.val = *(c->src.ptr);
1577 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001578 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001579 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001580 emulate_push(ctxt);
1581 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001582 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001583 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001584 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001585 case 0xa0 ... 0xa1: /* mov */
1586 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1587 c->dst.val = c->src.val;
1588 break;
1589 case 0xa2 ... 0xa3: /* mov */
1590 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1591 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001593 c->dst.type = OP_MEM;
1594 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001595 c->dst.ptr = (unsigned long *)register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001596 ctxt->es_base,
1597 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001598 if ((rc = ops->read_emulated(register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001599 c->override_base ? *c->override_base :
1600 ctxt->ds_base,
1601 c->regs[VCPU_REGS_RSI]),
1602 &c->dst.val,
1603 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001605 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001606 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001607 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001608 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001609 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001610 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 break;
1612 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001613 c->src.type = OP_NONE; /* Disable writeback. */
1614 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001615 c->src.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001616 c->override_base ? *c->override_base :
1617 ctxt->ds_base,
1618 c->regs[VCPU_REGS_RSI]);
1619 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1620 &c->src.val,
1621 c->src.bytes,
1622 ctxt->vcpu)) != 0)
1623 goto done;
1624
1625 c->dst.type = OP_NONE; /* Disable writeback. */
1626 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001627 c->dst.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001628 ctxt->es_base,
1629 c->regs[VCPU_REGS_RDI]);
1630 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1631 &c->dst.val,
1632 c->dst.bytes,
1633 ctxt->vcpu)) != 0)
1634 goto done;
1635
1636 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1637
1638 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1639
Harvey Harrison7a9572752008-02-19 07:40:41 -08001640 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001641 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1642 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001643 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001644 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1645 : c->dst.bytes);
1646
1647 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001649 c->dst.type = OP_MEM;
1650 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001651 c->dst.ptr = (unsigned long *)register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001652 ctxt->es_base,
1653 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001654 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001655 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001656 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001657 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 break;
1659 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 c->dst.type = OP_REG;
1661 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1662 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001663 if ((rc = ops->read_emulated(register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001664 c->override_base ? *c->override_base :
1665 ctxt->ds_base,
1666 c->regs[VCPU_REGS_RSI]),
1667 &c->dst.val,
1668 c->dst.bytes,
1669 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001671 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001672 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001673 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 break;
1675 case 0xae ... 0xaf: /* scas */
1676 DPRINTF("Urk! I don't handle SCAS.\n");
1677 goto cannot_emulate;
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001678 case 0xb8: /* mov r, imm */
1679 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001680 case 0xc0 ... 0xc1:
1681 emulate_grp2(ctxt);
1682 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001683 case 0xc3: /* ret */
1684 c->dst.ptr = &c->eip;
1685 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001686 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1687 mov:
1688 c->dst.val = c->src.val;
1689 break;
1690 case 0xd0 ... 0xd1: /* Grp2 */
1691 c->src.val = 1;
1692 emulate_grp2(ctxt);
1693 break;
1694 case 0xd2 ... 0xd3: /* Grp2 */
1695 c->src.val = c->regs[VCPU_REGS_RCX];
1696 emulate_grp2(ctxt);
1697 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001698 case 0xe8: /* call (near) */ {
1699 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001700 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001701 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001702 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001703 break;
1704 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001705 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001706 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001707 default:
1708 DPRINTF("Call: Invalid op_bytes\n");
1709 goto cannot_emulate;
1710 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001711 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001712 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001713 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 emulate_push(ctxt);
1715 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001716 }
1717 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001718 goto jmp;
1719 case 0xea: /* jmp far */ {
1720 uint32_t eip;
1721 uint16_t sel;
1722
1723 switch (c->op_bytes) {
1724 case 2:
1725 eip = insn_fetch(u16, 2, c->eip);
1726 break;
1727 case 4:
1728 eip = insn_fetch(u32, 4, c->eip);
1729 break;
1730 default:
1731 DPRINTF("jmp far: Invalid op_bytes\n");
1732 goto cannot_emulate;
1733 }
1734 sel = insn_fetch(u16, 2, c->eip);
1735 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1736 DPRINTF("jmp far: Failed to load CS descriptor\n");
1737 goto cannot_emulate;
1738 }
1739
1740 c->eip = eip;
1741 break;
1742 }
1743 case 0xeb:
1744 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001745 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001746 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001747 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001748 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001749 ctxt->vcpu->arch.halt_request = 1;
Avi Kivity111de5d2007-11-27 19:14:21 +02001750 goto done;
1751 case 0xf5: /* cmc */
1752 /* complement carry flag from eflags reg */
1753 ctxt->eflags ^= EFLG_CF;
1754 c->dst.type = OP_NONE; /* Disable writeback. */
1755 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001756 case 0xf6 ... 0xf7: /* Grp3 */
1757 rc = emulate_grp3(ctxt, ops);
1758 if (rc != 0)
1759 goto done;
1760 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001761 case 0xf8: /* clc */
1762 ctxt->eflags &= ~EFLG_CF;
1763 c->dst.type = OP_NONE; /* Disable writeback. */
1764 break;
1765 case 0xfa: /* cli */
1766 ctxt->eflags &= ~X86_EFLAGS_IF;
1767 c->dst.type = OP_NONE; /* Disable writeback. */
1768 break;
1769 case 0xfb: /* sti */
1770 ctxt->eflags |= X86_EFLAGS_IF;
1771 c->dst.type = OP_NONE; /* Disable writeback. */
1772 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001773 case 0xfe ... 0xff: /* Grp4/Grp5 */
1774 rc = emulate_grp45(ctxt, ops);
1775 if (rc != 0)
1776 goto done;
1777 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001779
1780writeback:
1781 rc = writeback(ctxt, ops);
1782 if (rc != 0)
1783 goto done;
1784
1785 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001786 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
1787 ctxt->vcpu->arch.rip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001788
1789done:
1790 if (rc == X86EMUL_UNHANDLEABLE) {
1791 c->eip = saved_eip;
1792 return -1;
1793 }
1794 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795
1796twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001797 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001799 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 u16 size;
1801 unsigned long address;
1802
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001803 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001804 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001805 goto cannot_emulate;
1806
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001807 rc = kvm_fix_hypercall(ctxt->vcpu);
1808 if (rc)
1809 goto done;
1810
Avi Kivity33e38852008-05-21 15:34:25 +03001811 /* Let the processor re-execute the fixed hypercall */
1812 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity16286d02008-04-14 14:40:50 +03001813 /* Disable writeback. */
1814 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001817 rc = read_descriptor(ctxt, ops, c->src.ptr,
1818 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819 if (rc)
1820 goto done;
1821 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001822 /* Disable writeback. */
1823 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001825 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001826 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001827 rc = kvm_fix_hypercall(ctxt->vcpu);
1828 if (rc)
1829 goto done;
1830 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001831 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001832 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001833 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001834 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001835 if (rc)
1836 goto done;
1837 realmode_lidt(ctxt->vcpu, size, address);
1838 }
Avi Kivity16286d02008-04-14 14:40:50 +03001839 /* Disable writeback. */
1840 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 break;
1842 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001843 c->dst.bytes = 2;
1844 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 break;
1846 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001847 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1848 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001849 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 break;
1851 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001852 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001853 /* Disable writeback. */
1854 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 break;
1856 default:
1857 goto cannot_emulate;
1858 }
1859 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001860 case 0x06:
1861 emulate_clts(ctxt->vcpu);
1862 c->dst.type = OP_NONE;
1863 break;
1864 case 0x08: /* invd */
1865 case 0x09: /* wbinvd */
1866 case 0x0d: /* GrpP (prefetch) */
1867 case 0x18: /* Grp16 (prefetch/nop) */
1868 c->dst.type = OP_NONE;
1869 break;
1870 case 0x20: /* mov cr, reg */
1871 if (c->modrm_mod != 3)
1872 goto cannot_emulate;
1873 c->regs[c->modrm_rm] =
1874 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1875 c->dst.type = OP_NONE; /* no writeback */
1876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001878 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001880 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001881 if (rc)
1882 goto cannot_emulate;
1883 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001885 case 0x22: /* mov reg, cr */
1886 if (c->modrm_mod != 3)
1887 goto cannot_emulate;
1888 realmode_set_cr(ctxt->vcpu,
1889 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1890 c->dst.type = OP_NONE;
1891 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001893 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001895 rc = emulator_set_dr(ctxt, c->modrm_reg,
1896 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001897 if (rc)
1898 goto cannot_emulate;
1899 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001901 case 0x30:
1902 /* wrmsr */
1903 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1904 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1905 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1906 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001907 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001908 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001909 }
1910 rc = X86EMUL_CONTINUE;
1911 c->dst.type = OP_NONE;
1912 break;
1913 case 0x32:
1914 /* rdmsr */
1915 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1916 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001917 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001918 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001919 } else {
1920 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1921 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1922 }
1923 rc = X86EMUL_CONTINUE;
1924 c->dst.type = OP_NONE;
1925 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001927 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001928 if (!test_cc(c->b, ctxt->eflags))
1929 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001931 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1932 long int rel;
1933
1934 switch (c->op_bytes) {
1935 case 2:
1936 rel = insn_fetch(s16, 2, c->eip);
1937 break;
1938 case 4:
1939 rel = insn_fetch(s32, 4, c->eip);
1940 break;
1941 case 8:
1942 rel = insn_fetch(s64, 8, c->eip);
1943 break;
1944 default:
1945 DPRINTF("jnz: Invalid op_bytes\n");
1946 goto cannot_emulate;
1947 }
1948 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001949 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001950 c->dst.type = OP_NONE;
1951 break;
1952 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001953 case 0xa3:
1954 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001955 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001956 /* only subword offset */
1957 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001958 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001959 break;
1960 case 0xab:
1961 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001962 /* only subword offset */
1963 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001964 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001965 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 case 0xb0 ... 0xb1: /* cmpxchg */
1967 /*
1968 * Save real source value, then compare EAX against
1969 * destination.
1970 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001971 c->src.orig_val = c->src.val;
1972 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001973 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1974 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001976 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977 } else {
1978 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001979 c->dst.type = OP_REG;
1980 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 }
1982 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 case 0xb3:
1984 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001985 /* only subword offset */
1986 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001987 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001990 c->dst.bytes = c->op_bytes;
1991 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1992 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001994 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001995 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 case 0:
1997 goto bt;
1998 case 1:
1999 goto bts;
2000 case 2:
2001 goto btr;
2002 case 3:
2003 goto btc;
2004 }
2005 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002006 case 0xbb:
2007 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002008 /* only subword offset */
2009 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002010 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002013 c->dst.bytes = c->op_bytes;
2014 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2015 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002017 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002018 c->dst.bytes = c->op_bytes;
2019 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2020 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002021 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002023 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002024 if (rc != 0)
2025 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002026 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002027 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028 }
2029 goto writeback;
2030
2031cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002032 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002033 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 return -1;
2035}