blob: b90857c765698430ea9e0c19683b529fa276afff [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,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143 /* 0x90 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200144 0, 0, 0, 0, 0, 0, 0, 0,
145 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200147 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
148 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200149 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
150 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200152 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
153 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
154 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155 /* 0xB0 - 0xBF */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +0200156 0, 0, 0, 0, 0, 0, 0, 0,
157 DstReg | SrcImm | Mov, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300159 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200160 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300161 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0xC8 - 0xCF */
163 0, 0, 0, 0, 0, 0, 0, 0,
164 /* 0xD0 - 0xD7 */
165 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
166 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
167 0, 0, 0, 0,
168 /* 0xD8 - 0xDF */
169 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300170 /* 0xE0 - 0xE7 */
171 0, 0, 0, 0, 0, 0, 0, 0,
172 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200173 ImplicitOps | Stack, SrcImm | ImplicitOps,
174 ImplicitOps, SrcImmByte | ImplicitOps,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200175 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0xF0 - 0xF7 */
177 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200178 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700180 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Avi Kivityfd607542008-01-18 13:12:26 +0200181 0, 0, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182};
183
Avi Kivity038e51d2007-01-22 20:40:40 -0800184static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200186 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200187 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188 /* 0x10 - 0x1F */
189 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
190 /* 0x20 - 0x2F */
191 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
192 0, 0, 0, 0, 0, 0, 0, 0,
193 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300194 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195 /* 0x40 - 0x47 */
196 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
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 /* 0x48 - 0x4F */
201 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
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 /* 0x50 - 0x5F */
206 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207 /* 0x60 - 0x6F */
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 /* 0x70 - 0x7F */
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300212 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
213 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
214 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
215 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216 /* 0x90 - 0x9F */
217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 /* 0xA0 - 0xA7 */
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 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800221 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 /* 0xB0 - 0xB7 */
223 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800224 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
226 DstReg | SrcMem16 | ModRM | Mov,
227 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800228 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
230 DstReg | SrcMem16 | ModRM | Mov,
231 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800232 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
233 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0xD0 - 0xDF */
235 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
236 /* 0xE0 - 0xEF */
237 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238 /* 0xF0 - 0xFF */
239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
240};
241
Avi Kivitye09d0822008-01-18 12:38:59 +0200242static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200243 [Group1_80*8] =
244 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
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 [Group1_81*8] =
249 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
250 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
251 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
252 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
253 [Group1_82*8] =
254 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
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 [Group1_83*8] =
259 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
260 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
261 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
262 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200263 [Group1A*8] =
264 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200265 [Group3_Byte*8] =
266 ByteOp | SrcImm | DstMem | ModRM, 0,
267 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
268 0, 0, 0, 0,
269 [Group3*8] =
270 DstMem | SrcImm | ModRM | SrcImm, 0,
271 DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
272 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200273 [Group4*8] =
274 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
275 0, 0, 0, 0, 0, 0,
276 [Group5*8] =
277 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, 0, 0,
278 SrcMem | ModRM, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200279 [Group7*8] =
280 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300281 SrcNone | ModRM | DstMem | Mov, 0,
282 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200283};
284
285static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200286 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300287 SrcNone | ModRM, 0, 0, 0,
288 SrcNone | ModRM | DstMem | Mov, 0,
289 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200290};
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292/* EFLAGS bit definitions. */
293#define EFLG_OF (1<<11)
294#define EFLG_DF (1<<10)
295#define EFLG_SF (1<<7)
296#define EFLG_ZF (1<<6)
297#define EFLG_AF (1<<4)
298#define EFLG_PF (1<<2)
299#define EFLG_CF (1<<0)
300
301/*
302 * Instruction emulation:
303 * Most instructions are emulated directly via a fragment of inline assembly
304 * code. This allows us to save/restore EFLAGS and thus very easily pick up
305 * any modified flags.
306 */
307
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800308#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309#define _LO32 "k" /* force 32-bit operand */
310#define _STK "%%rsp" /* stack pointer */
311#elif defined(__i386__)
312#define _LO32 "" /* force 32-bit operand */
313#define _STK "%%esp" /* stack pointer */
314#endif
315
316/*
317 * These EFLAGS bits are restored from saved value during emulation, and
318 * any changes are written back to the saved value after emulation.
319 */
320#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
321
322/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200323#define _PRE_EFLAGS(_sav, _msk, _tmp) \
324 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
325 "movl %"_sav",%"_LO32 _tmp"; " \
326 "push %"_tmp"; " \
327 "push %"_tmp"; " \
328 "movl %"_msk",%"_LO32 _tmp"; " \
329 "andl %"_LO32 _tmp",("_STK"); " \
330 "pushf; " \
331 "notl %"_LO32 _tmp"; " \
332 "andl %"_LO32 _tmp",("_STK"); " \
333 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
334 "pop %"_tmp"; " \
335 "orl %"_LO32 _tmp",("_STK"); " \
336 "popf; " \
337 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338
339/* After executing instruction: write-back necessary bits in EFLAGS. */
340#define _POST_EFLAGS(_sav, _msk, _tmp) \
341 /* _sav |= EFLAGS & _msk; */ \
342 "pushf; " \
343 "pop %"_tmp"; " \
344 "andl %"_msk",%"_LO32 _tmp"; " \
345 "orl %"_LO32 _tmp",%"_sav"; "
346
347/* Raw emulation: instruction has two explicit operands. */
348#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
349 do { \
350 unsigned long _tmp; \
351 \
352 switch ((_dst).bytes) { \
353 case 2: \
354 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400355 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400357 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 : "=m" (_eflags), "=m" ((_dst).val), \
359 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400360 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 break; \
362 case 4: \
363 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400364 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400366 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 : "=m" (_eflags), "=m" ((_dst).val), \
368 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400369 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 break; \
371 case 8: \
372 __emulate_2op_8byte(_op, _src, _dst, \
373 _eflags, _qx, _qy); \
374 break; \
375 } \
376 } while (0)
377
378#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
379 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800380 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 case 1: \
383 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400384 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400386 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800388 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400389 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390 break; \
391 default: \
392 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
393 _wx, _wy, _lx, _ly, _qx, _qy); \
394 break; \
395 } \
396 } while (0)
397
398/* Source operand is byte-sized and may be restricted to just %cl. */
399#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
400 __emulate_2op(_op, _src, _dst, _eflags, \
401 "b", "c", "b", "c", "b", "c", "b", "c")
402
403/* Source operand is byte, word, long or quad sized. */
404#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
405 __emulate_2op(_op, _src, _dst, _eflags, \
406 "b", "q", "w", "r", _LO32, "r", "", "r")
407
408/* Source operand is word, long or quad sized. */
409#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
410 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
411 "w", "r", _LO32, "r", "", "r")
412
413/* Instruction has only one explicit operand (no source operand). */
414#define emulate_1op(_op, _dst, _eflags) \
415 do { \
416 unsigned long _tmp; \
417 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400418 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 case 1: \
420 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400421 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400423 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 : "=m" (_eflags), "=m" ((_dst).val), \
425 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400426 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 break; \
428 case 2: \
429 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400430 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400432 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 : "=m" (_eflags), "=m" ((_dst).val), \
434 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400435 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 break; \
437 case 4: \
438 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400439 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400441 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 : "=m" (_eflags), "=m" ((_dst).val), \
443 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400444 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 break; \
446 case 8: \
447 __emulate_1op_8byte(_op, _dst, _eflags); \
448 break; \
449 } \
450 } while (0)
451
452/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800453#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
455 do { \
456 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400457 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400459 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400461 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 } while (0)
463
464#define __emulate_1op_8byte(_op, _dst, _eflags) \
465 do { \
466 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400467 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400469 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400471 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 } while (0)
473
474#elif defined(__i386__)
475#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
476#define __emulate_1op_8byte(_op, _dst, _eflags)
477#endif /* __i386__ */
478
479/* Fetch next part of the instruction being emulated. */
480#define insn_fetch(_type, _size, _eip) \
481({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200482 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400483 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 goto done; \
485 (_eip) += (_size); \
486 (_type)_x; \
487})
488
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800489static inline unsigned long ad_mask(struct decode_cache *c)
490{
491 return (1UL << (c->ad_bytes << 3)) - 1;
492}
493
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800495static inline unsigned long
496address_mask(struct decode_cache *c, unsigned long reg)
497{
498 if (c->ad_bytes == sizeof(unsigned long))
499 return reg;
500 else
501 return reg & ad_mask(c);
502}
503
504static inline unsigned long
505register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
506{
507 return base + address_mask(c, reg);
508}
509
Harvey Harrison7a9572752008-02-19 07:40:41 -0800510static inline void
511register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
512{
513 if (c->ad_bytes == sizeof(unsigned long))
514 *reg += inc;
515 else
516 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
517}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Harvey Harrison7a9572752008-02-19 07:40:41 -0800519static inline void jmp_rel(struct decode_cache *c, int rel)
520{
521 register_address_increment(c, &c->eip, rel);
522}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300523
Avi Kivity62266862007-11-20 13:15:52 +0200524static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
525 struct x86_emulate_ops *ops,
526 unsigned long linear, u8 *dest)
527{
528 struct fetch_cache *fc = &ctxt->decode.fetch;
529 int rc;
530 int size;
531
532 if (linear < fc->start || linear >= fc->end) {
533 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
534 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
535 if (rc)
536 return rc;
537 fc->start = linear;
538 fc->end = linear + size;
539 }
540 *dest = fc->data[linear - fc->start];
541 return 0;
542}
543
544static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
545 struct x86_emulate_ops *ops,
546 unsigned long eip, void *dest, unsigned size)
547{
548 int rc = 0;
549
550 eip += ctxt->cs_base;
551 while (size--) {
552 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
553 if (rc)
554 return rc;
555 }
556 return 0;
557}
558
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000559/*
560 * Given the 'reg' portion of a ModRM byte, and a register block, return a
561 * pointer into the block that addresses the relevant register.
562 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
563 */
564static void *decode_register(u8 modrm_reg, unsigned long *regs,
565 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566{
567 void *p;
568
569 p = &regs[modrm_reg];
570 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
571 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
572 return p;
573}
574
575static int read_descriptor(struct x86_emulate_ctxt *ctxt,
576 struct x86_emulate_ops *ops,
577 void *ptr,
578 u16 *size, unsigned long *address, int op_bytes)
579{
580 int rc;
581
582 if (op_bytes == 2)
583 op_bytes = 3;
584 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300585 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
586 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 if (rc)
588 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300589 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
590 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 return rc;
592}
593
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300594static int test_cc(unsigned int condition, unsigned int flags)
595{
596 int rc = 0;
597
598 switch ((condition & 15) >> 1) {
599 case 0: /* o */
600 rc |= (flags & EFLG_OF);
601 break;
602 case 1: /* b/c/nae */
603 rc |= (flags & EFLG_CF);
604 break;
605 case 2: /* z/e */
606 rc |= (flags & EFLG_ZF);
607 break;
608 case 3: /* be/na */
609 rc |= (flags & (EFLG_CF|EFLG_ZF));
610 break;
611 case 4: /* s */
612 rc |= (flags & EFLG_SF);
613 break;
614 case 5: /* p/pe */
615 rc |= (flags & EFLG_PF);
616 break;
617 case 7: /* le/ng */
618 rc |= (flags & EFLG_ZF);
619 /* fall through */
620 case 6: /* l/nge */
621 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
622 break;
623 }
624
625 /* Odd condition identifiers (lsb == 1) have inverted sense. */
626 return (!!rc ^ (condition & 1));
627}
628
Avi Kivity3c118e22007-10-31 10:27:04 +0200629static void decode_register_operand(struct operand *op,
630 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200631 int inhibit_bytereg)
632{
Avi Kivity33615aa2007-10-31 11:15:56 +0200633 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200634 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200635
636 if (!(c->d & ModRM))
637 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200638 op->type = OP_REG;
639 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200640 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200641 op->val = *(u8 *)op->ptr;
642 op->bytes = 1;
643 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200644 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200645 op->bytes = c->op_bytes;
646 switch (op->bytes) {
647 case 2:
648 op->val = *(u16 *)op->ptr;
649 break;
650 case 4:
651 op->val = *(u32 *)op->ptr;
652 break;
653 case 8:
654 op->val = *(u64 *) op->ptr;
655 break;
656 }
657 }
658 op->orig_val = op->val;
659}
660
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200661static int decode_modrm(struct x86_emulate_ctxt *ctxt,
662 struct x86_emulate_ops *ops)
663{
664 struct decode_cache *c = &ctxt->decode;
665 u8 sib;
666 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
667 int rc = 0;
668
669 if (c->rex_prefix) {
670 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
671 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
672 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
673 }
674
675 c->modrm = insn_fetch(u8, 1, c->eip);
676 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
677 c->modrm_reg |= (c->modrm & 0x38) >> 3;
678 c->modrm_rm |= (c->modrm & 0x07);
679 c->modrm_ea = 0;
680 c->use_modrm_ea = 1;
681
682 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300683 c->modrm_ptr = decode_register(c->modrm_rm,
684 c->regs, c->d & ByteOp);
685 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200686 return rc;
687 }
688
689 if (c->ad_bytes == 2) {
690 unsigned bx = c->regs[VCPU_REGS_RBX];
691 unsigned bp = c->regs[VCPU_REGS_RBP];
692 unsigned si = c->regs[VCPU_REGS_RSI];
693 unsigned di = c->regs[VCPU_REGS_RDI];
694
695 /* 16-bit ModR/M decode. */
696 switch (c->modrm_mod) {
697 case 0:
698 if (c->modrm_rm == 6)
699 c->modrm_ea += insn_fetch(u16, 2, c->eip);
700 break;
701 case 1:
702 c->modrm_ea += insn_fetch(s8, 1, c->eip);
703 break;
704 case 2:
705 c->modrm_ea += insn_fetch(u16, 2, c->eip);
706 break;
707 }
708 switch (c->modrm_rm) {
709 case 0:
710 c->modrm_ea += bx + si;
711 break;
712 case 1:
713 c->modrm_ea += bx + di;
714 break;
715 case 2:
716 c->modrm_ea += bp + si;
717 break;
718 case 3:
719 c->modrm_ea += bp + di;
720 break;
721 case 4:
722 c->modrm_ea += si;
723 break;
724 case 5:
725 c->modrm_ea += di;
726 break;
727 case 6:
728 if (c->modrm_mod != 0)
729 c->modrm_ea += bp;
730 break;
731 case 7:
732 c->modrm_ea += bx;
733 break;
734 }
735 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
736 (c->modrm_rm == 6 && c->modrm_mod != 0))
737 if (!c->override_base)
738 c->override_base = &ctxt->ss_base;
739 c->modrm_ea = (u16)c->modrm_ea;
740 } else {
741 /* 32/64-bit ModR/M decode. */
742 switch (c->modrm_rm) {
743 case 4:
744 case 12:
745 sib = insn_fetch(u8, 1, c->eip);
746 index_reg |= (sib >> 3) & 7;
747 base_reg |= sib & 7;
748 scale = sib >> 6;
749
750 switch (base_reg) {
751 case 5:
752 if (c->modrm_mod != 0)
753 c->modrm_ea += c->regs[base_reg];
754 else
755 c->modrm_ea +=
756 insn_fetch(s32, 4, c->eip);
757 break;
758 default:
759 c->modrm_ea += c->regs[base_reg];
760 }
761 switch (index_reg) {
762 case 4:
763 break;
764 default:
765 c->modrm_ea += c->regs[index_reg] << scale;
766 }
767 break;
768 case 5:
769 if (c->modrm_mod != 0)
770 c->modrm_ea += c->regs[c->modrm_rm];
771 else if (ctxt->mode == X86EMUL_MODE_PROT64)
772 rip_relative = 1;
773 break;
774 default:
775 c->modrm_ea += c->regs[c->modrm_rm];
776 break;
777 }
778 switch (c->modrm_mod) {
779 case 0:
780 if (c->modrm_rm == 5)
781 c->modrm_ea += insn_fetch(s32, 4, c->eip);
782 break;
783 case 1:
784 c->modrm_ea += insn_fetch(s8, 1, c->eip);
785 break;
786 case 2:
787 c->modrm_ea += insn_fetch(s32, 4, c->eip);
788 break;
789 }
790 }
791 if (rip_relative) {
792 c->modrm_ea += c->eip;
793 switch (c->d & SrcMask) {
794 case SrcImmByte:
795 c->modrm_ea += 1;
796 break;
797 case SrcImm:
798 if (c->d & ByteOp)
799 c->modrm_ea += 1;
800 else
801 if (c->op_bytes == 8)
802 c->modrm_ea += 4;
803 else
804 c->modrm_ea += c->op_bytes;
805 }
806 }
807done:
808 return rc;
809}
810
811static int decode_abs(struct x86_emulate_ctxt *ctxt,
812 struct x86_emulate_ops *ops)
813{
814 struct decode_cache *c = &ctxt->decode;
815 int rc = 0;
816
817 switch (c->ad_bytes) {
818 case 2:
819 c->modrm_ea = insn_fetch(u16, 2, c->eip);
820 break;
821 case 4:
822 c->modrm_ea = insn_fetch(u32, 4, c->eip);
823 break;
824 case 8:
825 c->modrm_ea = insn_fetch(u64, 8, c->eip);
826 break;
827 }
828done:
829 return rc;
830}
831
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200833x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200835 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200838 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
840 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
Laurent Viviere4e03de2007-09-18 11:52:50 +0200842 memset(c, 0, sizeof(struct decode_cache));
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800843 c->eip = ctxt->vcpu->arch.rip;
844 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845
846 switch (mode) {
847 case X86EMUL_MODE_REAL:
848 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200849 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 break;
851 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200852 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800854#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200856 def_op_bytes = 4;
857 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 break;
859#endif
860 default:
861 return -1;
862 }
863
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200864 c->op_bytes = def_op_bytes;
865 c->ad_bytes = def_ad_bytes;
866
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200868 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200869 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200871 /* switch between 2/4 bytes */
872 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 break;
874 case 0x67: /* address-size override */
875 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200876 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200877 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200879 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200880 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
882 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200883 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 break;
885 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200886 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 break;
888 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
891 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 break;
894 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200895 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 break;
897 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200898 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200900 case 0x40 ... 0x4f: /* REX */
901 if (mode != X86EMUL_MODE_PROT64)
902 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200903 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200904 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200906 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200908 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100909 c->rep_prefix = REPNE_PREFIX;
910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100912 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 default:
915 goto done_prefixes;
916 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200917
918 /* Any legacy prefix after a REX prefix nullifies its effect. */
919
Avi Kivity33615aa2007-10-31 11:15:56 +0200920 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 }
922
923done_prefixes:
924
925 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200927 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200928 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200931 c->d = opcode_table[c->b];
932 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200934 if (c->b == 0x0f) {
935 c->twobyte = 1;
936 c->b = insn_fetch(u8, 1, c->eip);
937 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200939 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Avi Kivitye09d0822008-01-18 12:38:59 +0200941 if (c->d & Group) {
942 group = c->d & GroupMask;
943 c->modrm = insn_fetch(u8, 1, c->eip);
944 --c->eip;
945
946 group = (group << 3) + ((c->modrm >> 3) & 7);
947 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
948 c->d = group2_table[group];
949 else
950 c->d = group_table[group];
951 }
952
953 /* Unrecognised? */
954 if (c->d == 0) {
955 DPRINTF("Cannot emulate %02x\n", c->b);
956 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 }
958
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200959 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
960 c->op_bytes = 8;
961
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200963 if (c->d & ModRM)
964 rc = decode_modrm(ctxt, ops);
965 else if (c->d & MemAbs)
966 rc = decode_abs(ctxt, ops);
967 if (rc)
968 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Avi Kivityc7e75a32007-10-28 16:34:25 +0200970 if (!c->override_base)
971 c->override_base = &ctxt->ds_base;
972 if (mode == X86EMUL_MODE_PROT64 &&
973 c->override_base != &ctxt->fs_base &&
974 c->override_base != &ctxt->gs_base)
975 c->override_base = NULL;
976
977 if (c->override_base)
978 c->modrm_ea += *c->override_base;
979
980 if (c->ad_bytes != 8)
981 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 /*
983 * Decode and fetch the source operand: register, memory
984 * or immediate.
985 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200986 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case SrcNone:
988 break;
989 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200990 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 break;
992 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200993 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 goto srcmem_common;
995 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 goto srcmem_common;
998 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200999 c->src.bytes = (c->d & ByteOp) ? 1 :
1000 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001001 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001002 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001003 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001004 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001005 /*
1006 * For instructions with a ModR/M byte, switch to register
1007 * access if Mod = 3.
1008 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 if ((c->d & ModRM) && c->modrm_mod == 3) {
1010 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001011 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001012 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001013 break;
1014 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 break;
1017 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 c->src.type = OP_IMM;
1019 c->src.ptr = (unsigned long *)c->eip;
1020 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1021 if (c->src.bytes == 8)
1022 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001024 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001026 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 break;
1028 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 break;
1031 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001032 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 break;
1034 }
1035 break;
1036 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 c->src.type = OP_IMM;
1038 c->src.ptr = (unsigned long *)c->eip;
1039 c->src.bytes = 1;
1040 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 break;
1042 }
1043
Avi Kivity038e51d2007-01-22 20:40:40 -08001044 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001045 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001046 case ImplicitOps:
1047 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001048 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001049 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001050 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001051 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001052 break;
1053 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001054 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001055 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001056 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001057 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001058 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001059 break;
1060 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001061 c->dst.type = OP_MEM;
1062 break;
1063 }
1064
1065done:
1066 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1067}
1068
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001069static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1070{
1071 struct decode_cache *c = &ctxt->decode;
1072
1073 c->dst.type = OP_MEM;
1074 c->dst.bytes = c->op_bytes;
1075 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001076 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Harvey Harrisone4706772008-02-19 07:40:38 -08001077 c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001078 c->regs[VCPU_REGS_RSP]);
1079}
1080
1081static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1082 struct x86_emulate_ops *ops)
1083{
1084 struct decode_cache *c = &ctxt->decode;
1085 int rc;
1086
Harvey Harrisone4706772008-02-19 07:40:38 -08001087 rc = ops->read_std(register_address(c, ctxt->ss_base,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001088 c->regs[VCPU_REGS_RSP]),
1089 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1090 if (rc != 0)
1091 return rc;
1092
Harvey Harrison7a9572752008-02-19 07:40:41 -08001093 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001094
1095 return 0;
1096}
1097
Laurent Vivier05f086f2007-09-24 11:10:55 +02001098static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001099{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001100 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001101 switch (c->modrm_reg) {
1102 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001103 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001104 break;
1105 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001106 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001107 break;
1108 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001109 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001110 break;
1111 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001112 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001113 break;
1114 case 4: /* sal/shl */
1115 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001116 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001117 break;
1118 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001119 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001120 break;
1121 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001122 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001123 break;
1124 }
1125}
1126
1127static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001128 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001129{
1130 struct decode_cache *c = &ctxt->decode;
1131 int rc = 0;
1132
1133 switch (c->modrm_reg) {
1134 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001135 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001136 break;
1137 case 2: /* not */
1138 c->dst.val = ~c->dst.val;
1139 break;
1140 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001141 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001142 break;
1143 default:
1144 DPRINTF("Cannot emulate %02x\n", c->b);
1145 rc = X86EMUL_UNHANDLEABLE;
1146 break;
1147 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001148 return rc;
1149}
1150
1151static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001152 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001153{
1154 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155
1156 switch (c->modrm_reg) {
1157 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001158 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001159 break;
1160 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001161 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001162 break;
1163 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001164 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165 break;
1166 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001167 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001168 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 }
1170 return 0;
1171}
1172
1173static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1174 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001175 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001176{
1177 struct decode_cache *c = &ctxt->decode;
1178 u64 old, new;
1179 int rc;
1180
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001181 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001182 if (rc != 0)
1183 return rc;
1184
1185 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1186 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1187
1188 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1189 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001190 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001191
1192 } else {
1193 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1194 (u32) c->regs[VCPU_REGS_RBX];
1195
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001196 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 if (rc != 0)
1198 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 }
1201 return 0;
1202}
1203
1204static inline int writeback(struct x86_emulate_ctxt *ctxt,
1205 struct x86_emulate_ops *ops)
1206{
1207 int rc;
1208 struct decode_cache *c = &ctxt->decode;
1209
1210 switch (c->dst.type) {
1211 case OP_REG:
1212 /* The 4-byte case *is* correct:
1213 * in 64-bit mode we zero-extend.
1214 */
1215 switch (c->dst.bytes) {
1216 case 1:
1217 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1218 break;
1219 case 2:
1220 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1221 break;
1222 case 4:
1223 *c->dst.ptr = (u32)c->dst.val;
1224 break; /* 64b: zero-ext */
1225 case 8:
1226 *c->dst.ptr = c->dst.val;
1227 break;
1228 }
1229 break;
1230 case OP_MEM:
1231 if (c->lock_prefix)
1232 rc = ops->cmpxchg_emulated(
1233 (unsigned long)c->dst.ptr,
1234 &c->dst.orig_val,
1235 &c->dst.val,
1236 c->dst.bytes,
1237 ctxt->vcpu);
1238 else
1239 rc = ops->write_emulated(
1240 (unsigned long)c->dst.ptr,
1241 &c->dst.val,
1242 c->dst.bytes,
1243 ctxt->vcpu);
1244 if (rc != 0)
1245 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001246 break;
1247 case OP_NONE:
1248 /* no writeback */
1249 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001250 default:
1251 break;
1252 }
1253 return 0;
1254}
1255
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001256int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001257x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001258{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001259 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001260 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001261 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001262 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001263 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001264
Laurent Vivier34273182007-09-18 11:27:37 +02001265 /* Shadow copy of register state. Committed on successful emulation.
1266 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1267 * modify them.
1268 */
1269
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001270 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001271 saved_eip = c->eip;
1272
Avi Kivityc7e75a32007-10-28 16:34:25 +02001273 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001274 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001275
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001276 if (c->rep_prefix && (c->d & String)) {
1277 /* All REP prefixes have the same first termination condition */
1278 if (c->regs[VCPU_REGS_RCX] == 0) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001279 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001280 goto done;
1281 }
1282 /* The second termination condition only applies for REPE
1283 * and REPNE. Test if the repeat string operation prefix is
1284 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1285 * corresponding termination condition according to:
1286 * - if REPE/REPZ and ZF = 0 then done
1287 * - if REPNE/REPNZ and ZF = 1 then done
1288 */
1289 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1290 (c->b == 0xae) || (c->b == 0xaf)) {
1291 if ((c->rep_prefix == REPE_PREFIX) &&
1292 ((ctxt->eflags & EFLG_ZF) == 0)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001293 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001294 goto done;
1295 }
1296 if ((c->rep_prefix == REPNE_PREFIX) &&
1297 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001298 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001299 goto done;
1300 }
1301 }
1302 c->regs[VCPU_REGS_RCX]--;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001303 c->eip = ctxt->vcpu->arch.rip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001304 }
1305
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001306 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001307 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001308 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001309 rc = ops->read_emulated((unsigned long)c->src.ptr,
1310 &c->src.val,
1311 c->src.bytes,
1312 ctxt->vcpu);
1313 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001314 goto done;
1315 c->src.orig_val = c->src.val;
1316 }
1317
1318 if ((c->d & DstMask) == ImplicitOps)
1319 goto special_insn;
1320
1321
1322 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001323 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001324 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1325 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001326 if (c->d & BitOp) {
1327 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001328
Laurent Viviere4e03de2007-09-18 11:52:50 +02001329 c->dst.ptr = (void *)c->dst.ptr +
1330 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001331 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001332 if (!(c->d & Mov) &&
1333 /* optimisation - avoid slow emulated read */
1334 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1335 &c->dst.val,
1336 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001337 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001338 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001339 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001340
Avi Kivity018a98d2007-11-27 19:30:56 +02001341special_insn:
1342
Laurent Viviere4e03de2007-09-18 11:52:50 +02001343 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 goto twobyte_insn;
1345
Laurent Viviere4e03de2007-09-18 11:52:50 +02001346 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 case 0x00 ... 0x05:
1348 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001349 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 break;
1351 case 0x08 ... 0x0d:
1352 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001353 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 break;
1355 case 0x10 ... 0x15:
1356 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001357 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 break;
1359 case 0x18 ... 0x1d:
1360 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001361 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001363 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001365 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001367 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001368 c->dst.type = OP_REG;
1369 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1370 c->dst.val = *(u8 *)c->dst.ptr;
1371 c->dst.bytes = 1;
1372 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001373 goto and;
1374 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001375 c->dst.type = OP_REG;
1376 c->dst.bytes = c->op_bytes;
1377 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1378 if (c->op_bytes == 2)
1379 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001380 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001381 c->dst.val = *(u32 *)c->dst.ptr;
1382 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001383 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 case 0x28 ... 0x2d:
1385 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001386 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 break;
1388 case 0x30 ... 0x35:
1389 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001390 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 break;
1392 case 0x38 ... 0x3d:
1393 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001394 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001396 case 0x40 ... 0x47: /* inc r16/r32 */
1397 emulate_1op("inc", c->dst, ctxt->eflags);
1398 break;
1399 case 0x48 ... 0x4f: /* dec r16/r32 */
1400 emulate_1op("dec", c->dst, ctxt->eflags);
1401 break;
1402 case 0x50 ... 0x57: /* push reg */
1403 c->dst.type = OP_MEM;
1404 c->dst.bytes = c->op_bytes;
1405 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001406 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001407 -c->op_bytes);
1408 c->dst.ptr = (void *) register_address(
Harvey Harrisone4706772008-02-19 07:40:38 -08001409 c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Avi Kivity33615aa2007-10-31 11:15:56 +02001410 break;
1411 case 0x58 ... 0x5f: /* pop reg */
1412 pop_instruction:
Harvey Harrisone4706772008-02-19 07:40:38 -08001413 if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
Avi Kivity33615aa2007-10-31 11:15:56 +02001414 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1415 c->op_bytes, ctxt->vcpu)) != 0)
1416 goto done;
1417
Harvey Harrison7a9572752008-02-19 07:40:41 -08001418 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001419 c->op_bytes);
1420 c->dst.type = OP_NONE; /* Disable writeback. */
1421 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001423 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001425 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001427 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001428 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001429 emulate_push(ctxt);
1430 break;
1431 case 0x6c: /* insb */
1432 case 0x6d: /* insw/insd */
1433 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1434 1,
1435 (c->d & ByteOp) ? 1 : c->op_bytes,
1436 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001437 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001438 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001439 register_address(c, ctxt->es_base,
Avi Kivity018a98d2007-11-27 19:30:56 +02001440 c->regs[VCPU_REGS_RDI]),
1441 c->rep_prefix,
1442 c->regs[VCPU_REGS_RDX]) == 0) {
1443 c->eip = saved_eip;
1444 return -1;
1445 }
1446 return 0;
1447 case 0x6e: /* outsb */
1448 case 0x6f: /* outsw/outsd */
1449 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1450 0,
1451 (c->d & ByteOp) ? 1 : c->op_bytes,
1452 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001453 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001454 (ctxt->eflags & EFLG_DF),
Harvey Harrisone4706772008-02-19 07:40:38 -08001455 register_address(c, c->override_base ?
Avi Kivity018a98d2007-11-27 19:30:56 +02001456 *c->override_base :
1457 ctxt->ds_base,
1458 c->regs[VCPU_REGS_RSI]),
1459 c->rep_prefix,
1460 c->regs[VCPU_REGS_RDX]) == 0) {
1461 c->eip = saved_eip;
1462 return -1;
1463 }
1464 return 0;
1465 case 0x70 ... 0x7f: /* jcc (short) */ {
1466 int rel = insn_fetch(s8, 1, c->eip);
1467
1468 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001469 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001470 break;
1471 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001473 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474 case 0:
1475 goto add;
1476 case 1:
1477 goto or;
1478 case 2:
1479 goto adc;
1480 case 3:
1481 goto sbb;
1482 case 4:
1483 goto and;
1484 case 5:
1485 goto sub;
1486 case 6:
1487 goto xor;
1488 case 7:
1489 goto cmp;
1490 }
1491 break;
1492 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001493 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 break;
1495 case 0x86 ... 0x87: /* xchg */
1496 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001497 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001499 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 break;
1501 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001502 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 break;
1504 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001505 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 break; /* 64b reg: zero-extend */
1507 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001508 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 break;
1510 }
1511 /*
1512 * Write back the memory destination with implicit LOCK
1513 * prefix.
1514 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001515 c->dst.val = c->src.val;
1516 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001519 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001520 case 0x8c: { /* mov r/m, sreg */
1521 struct kvm_segment segreg;
1522
1523 if (c->modrm_reg <= 5)
1524 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1525 else {
1526 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1527 c->modrm);
1528 goto cannot_emulate;
1529 }
1530 c->dst.val = segreg.selector;
1531 break;
1532 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001533 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001534 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001535 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001536 case 0x8e: { /* mov seg, r/m16 */
1537 uint16_t sel;
1538 int type_bits;
1539 int err;
1540
1541 sel = c->src.val;
1542 if (c->modrm_reg <= 5) {
1543 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1544 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1545 type_bits, c->modrm_reg);
1546 } else {
1547 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1548 c->modrm);
1549 goto cannot_emulate;
1550 }
1551
1552 if (err < 0)
1553 goto cannot_emulate;
1554
1555 c->dst.type = OP_NONE; /* Disable writeback. */
1556 break;
1557 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001559 rc = emulate_grp1a(ctxt, ops);
1560 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001563 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001564 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001565 emulate_push(ctxt);
1566 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001567 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001568 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001569 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001570 case 0xa0 ... 0xa1: /* mov */
1571 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1572 c->dst.val = c->src.val;
1573 break;
1574 case 0xa2 ... 0xa3: /* mov */
1575 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1576 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001578 c->dst.type = OP_MEM;
1579 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001580 c->dst.ptr = (unsigned long *)register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001581 ctxt->es_base,
1582 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001583 if ((rc = ops->read_emulated(register_address(c,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001584 c->override_base ? *c->override_base :
1585 ctxt->ds_base,
1586 c->regs[VCPU_REGS_RSI]),
1587 &c->dst.val,
1588 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001590 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001591 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001592 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001593 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001594 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001595 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 break;
1597 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001598 c->src.type = OP_NONE; /* Disable writeback. */
1599 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001600 c->src.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001601 c->override_base ? *c->override_base :
1602 ctxt->ds_base,
1603 c->regs[VCPU_REGS_RSI]);
1604 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1605 &c->src.val,
1606 c->src.bytes,
1607 ctxt->vcpu)) != 0)
1608 goto done;
1609
1610 c->dst.type = OP_NONE; /* Disable writeback. */
1611 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001612 c->dst.ptr = (unsigned long *)register_address(c,
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001613 ctxt->es_base,
1614 c->regs[VCPU_REGS_RDI]);
1615 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1616 &c->dst.val,
1617 c->dst.bytes,
1618 ctxt->vcpu)) != 0)
1619 goto done;
1620
1621 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1622
1623 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1624
Harvey Harrison7a9572752008-02-19 07:40:41 -08001625 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001626 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1627 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001628 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001629 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1630 : c->dst.bytes);
1631
1632 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001634 c->dst.type = OP_MEM;
1635 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001636 c->dst.ptr = (unsigned long *)register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001637 ctxt->es_base,
1638 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001639 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001640 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001641 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001642 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 break;
1644 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001645 c->dst.type = OP_REG;
1646 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1647 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001648 if ((rc = ops->read_emulated(register_address(c,
Sheng Yanga7e6c882007-11-15 14:52:28 +08001649 c->override_base ? *c->override_base :
1650 ctxt->ds_base,
1651 c->regs[VCPU_REGS_RSI]),
1652 &c->dst.val,
1653 c->dst.bytes,
1654 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001656 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001657 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001658 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 break;
1660 case 0xae ... 0xaf: /* scas */
1661 DPRINTF("Urk! I don't handle SCAS.\n");
1662 goto cannot_emulate;
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001663 case 0xb8: /* mov r, imm */
1664 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001665 case 0xc0 ... 0xc1:
1666 emulate_grp2(ctxt);
1667 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001668 case 0xc3: /* ret */
1669 c->dst.ptr = &c->eip;
1670 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001671 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1672 mov:
1673 c->dst.val = c->src.val;
1674 break;
1675 case 0xd0 ... 0xd1: /* Grp2 */
1676 c->src.val = 1;
1677 emulate_grp2(ctxt);
1678 break;
1679 case 0xd2 ... 0xd3: /* Grp2 */
1680 c->src.val = c->regs[VCPU_REGS_RCX];
1681 emulate_grp2(ctxt);
1682 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001683 case 0xe8: /* call (near) */ {
1684 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001686 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001687 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001688 break;
1689 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001690 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001691 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001692 default:
1693 DPRINTF("Call: Invalid op_bytes\n");
1694 goto cannot_emulate;
1695 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001696 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001697 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001698 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699 emulate_push(ctxt);
1700 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001701 }
1702 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001703 goto jmp;
1704 case 0xea: /* jmp far */ {
1705 uint32_t eip;
1706 uint16_t sel;
1707
1708 switch (c->op_bytes) {
1709 case 2:
1710 eip = insn_fetch(u16, 2, c->eip);
1711 break;
1712 case 4:
1713 eip = insn_fetch(u32, 4, c->eip);
1714 break;
1715 default:
1716 DPRINTF("jmp far: Invalid op_bytes\n");
1717 goto cannot_emulate;
1718 }
1719 sel = insn_fetch(u16, 2, c->eip);
1720 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1721 DPRINTF("jmp far: Failed to load CS descriptor\n");
1722 goto cannot_emulate;
1723 }
1724
1725 c->eip = eip;
1726 break;
1727 }
1728 case 0xeb:
1729 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001730 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001731 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001732 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001733 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001734 ctxt->vcpu->arch.halt_request = 1;
Avi Kivity111de5d2007-11-27 19:14:21 +02001735 goto done;
1736 case 0xf5: /* cmc */
1737 /* complement carry flag from eflags reg */
1738 ctxt->eflags ^= EFLG_CF;
1739 c->dst.type = OP_NONE; /* Disable writeback. */
1740 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001741 case 0xf6 ... 0xf7: /* Grp3 */
1742 rc = emulate_grp3(ctxt, ops);
1743 if (rc != 0)
1744 goto done;
1745 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001746 case 0xf8: /* clc */
1747 ctxt->eflags &= ~EFLG_CF;
1748 c->dst.type = OP_NONE; /* Disable writeback. */
1749 break;
1750 case 0xfa: /* cli */
1751 ctxt->eflags &= ~X86_EFLAGS_IF;
1752 c->dst.type = OP_NONE; /* Disable writeback. */
1753 break;
1754 case 0xfb: /* sti */
1755 ctxt->eflags |= X86_EFLAGS_IF;
1756 c->dst.type = OP_NONE; /* Disable writeback. */
1757 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001758 case 0xfe ... 0xff: /* Grp4/Grp5 */
1759 rc = emulate_grp45(ctxt, ops);
1760 if (rc != 0)
1761 goto done;
1762 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001764
1765writeback:
1766 rc = writeback(ctxt, ops);
1767 if (rc != 0)
1768 goto done;
1769
1770 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001771 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
1772 ctxt->vcpu->arch.rip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001773
1774done:
1775 if (rc == X86EMUL_UNHANDLEABLE) {
1776 c->eip = saved_eip;
1777 return -1;
1778 }
1779 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780
1781twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001782 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001784 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785 u16 size;
1786 unsigned long address;
1787
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001788 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001789 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001790 goto cannot_emulate;
1791
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001792 rc = kvm_fix_hypercall(ctxt->vcpu);
1793 if (rc)
1794 goto done;
1795
Avi Kivity33e38852008-05-21 15:34:25 +03001796 /* Let the processor re-execute the fixed hypercall */
1797 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity16286d02008-04-14 14:40:50 +03001798 /* Disable writeback. */
1799 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001800 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001802 rc = read_descriptor(ctxt, ops, c->src.ptr,
1803 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 if (rc)
1805 goto done;
1806 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001807 /* Disable writeback. */
1808 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001810 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001811 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001812 rc = kvm_fix_hypercall(ctxt->vcpu);
1813 if (rc)
1814 goto done;
1815 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001816 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001817 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001818 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001819 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001820 if (rc)
1821 goto done;
1822 realmode_lidt(ctxt->vcpu, size, address);
1823 }
Avi Kivity16286d02008-04-14 14:40:50 +03001824 /* Disable writeback. */
1825 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826 break;
1827 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001828 c->dst.bytes = 2;
1829 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 break;
1831 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001832 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1833 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001834 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 break;
1836 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001837 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001838 /* Disable writeback. */
1839 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 break;
1841 default:
1842 goto cannot_emulate;
1843 }
1844 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001845 case 0x06:
1846 emulate_clts(ctxt->vcpu);
1847 c->dst.type = OP_NONE;
1848 break;
1849 case 0x08: /* invd */
1850 case 0x09: /* wbinvd */
1851 case 0x0d: /* GrpP (prefetch) */
1852 case 0x18: /* Grp16 (prefetch/nop) */
1853 c->dst.type = OP_NONE;
1854 break;
1855 case 0x20: /* mov cr, reg */
1856 if (c->modrm_mod != 3)
1857 goto cannot_emulate;
1858 c->regs[c->modrm_rm] =
1859 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1860 c->dst.type = OP_NONE; /* no writeback */
1861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001863 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001865 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001866 if (rc)
1867 goto cannot_emulate;
1868 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001870 case 0x22: /* mov reg, cr */
1871 if (c->modrm_mod != 3)
1872 goto cannot_emulate;
1873 realmode_set_cr(ctxt->vcpu,
1874 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1875 c->dst.type = OP_NONE;
1876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 case 0x23: /* mov from reg to dr */
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 Viviere4e03de2007-09-18 11:52:50 +02001880 rc = emulator_set_dr(ctxt, c->modrm_reg,
1881 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001882 if (rc)
1883 goto cannot_emulate;
1884 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001886 case 0x30:
1887 /* wrmsr */
1888 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1889 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1890 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1891 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001892 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001893 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001894 }
1895 rc = X86EMUL_CONTINUE;
1896 c->dst.type = OP_NONE;
1897 break;
1898 case 0x32:
1899 /* rdmsr */
1900 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1901 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001902 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001903 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001904 } else {
1905 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1906 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1907 }
1908 rc = X86EMUL_CONTINUE;
1909 c->dst.type = OP_NONE;
1910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001913 if (!test_cc(c->b, ctxt->eflags))
1914 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001916 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1917 long int rel;
1918
1919 switch (c->op_bytes) {
1920 case 2:
1921 rel = insn_fetch(s16, 2, c->eip);
1922 break;
1923 case 4:
1924 rel = insn_fetch(s32, 4, c->eip);
1925 break;
1926 case 8:
1927 rel = insn_fetch(s64, 8, c->eip);
1928 break;
1929 default:
1930 DPRINTF("jnz: Invalid op_bytes\n");
1931 goto cannot_emulate;
1932 }
1933 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001934 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001935 c->dst.type = OP_NONE;
1936 break;
1937 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001938 case 0xa3:
1939 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001940 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001941 /* only subword offset */
1942 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001943 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001944 break;
1945 case 0xab:
1946 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001947 /* only subword offset */
1948 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001949 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001950 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951 case 0xb0 ... 0xb1: /* cmpxchg */
1952 /*
1953 * Save real source value, then compare EAX against
1954 * destination.
1955 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001956 c->src.orig_val = c->src.val;
1957 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001958 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1959 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001961 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 } else {
1963 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001964 c->dst.type = OP_REG;
1965 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 }
1967 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968 case 0xb3:
1969 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001970 /* only subword offset */
1971 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001972 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001975 c->dst.bytes = c->op_bytes;
1976 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1977 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001980 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 case 0:
1982 goto bt;
1983 case 1:
1984 goto bts;
1985 case 2:
1986 goto btr;
1987 case 3:
1988 goto btc;
1989 }
1990 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001991 case 0xbb:
1992 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001993 /* only subword offset */
1994 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001995 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001996 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001998 c->dst.bytes = c->op_bytes;
1999 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2000 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002002 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002003 c->dst.bytes = c->op_bytes;
2004 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2005 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002006 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002008 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002009 if (rc != 0)
2010 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002011 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002012 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 }
2014 goto writeback;
2015
2016cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002017 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002018 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 return -1;
2020}