blob: 75fd23bade9c682f062011b773a222b46d18e379 [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
28#include "kvm.h"
29#define DPRINTF(x...) do {} while (0)
30#endif
31#include "x86_emulate.h"
32#include <linux/module.h>
33
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 Kivity6aa8b732006-12-10 02:21:36 -080065
66static u8 opcode_table[256] = {
67 /* 0x00 - 0x07 */
68 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
69 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
70 0, 0, 0, 0,
71 /* 0x08 - 0x0F */
72 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
73 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
74 0, 0, 0, 0,
75 /* 0x10 - 0x17 */
76 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
77 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
78 0, 0, 0, 0,
79 /* 0x18 - 0x1F */
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
82 0, 0, 0, 0,
83 /* 0x20 - 0x27 */
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030086 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080087 /* 0x28 - 0x2F */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
90 0, 0, 0, 0,
91 /* 0x30 - 0x37 */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x38 - 0x3F */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
98 0, 0, 0, 0,
99 /* 0x40 - 0x4F */
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300101 /* 0x50 - 0x57 */
Nitin A Kamble7e778162007-08-19 11:07:06 +0300102 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
103 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300104 /* 0x58 - 0x5F */
105 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
106 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700107 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700109 0, 0, 0, 0,
110 /* 0x68 - 0x6F */
111 0, 0, ImplicitOps|Mov, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300112 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
113 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300114 /* 0x70 - 0x77 */
115 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
116 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
117 /* 0x78 - 0x7F */
118 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
119 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 /* 0x80 - 0x87 */
121 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
122 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
123 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
124 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
125 /* 0x88 - 0x8F */
126 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
127 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +0300128 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 /* 0x90 - 0x9F */
Nitin A Kamble535eabc2007-09-15 10:45:05 +0300130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 /* 0xA0 - 0xA7 */
132 ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
133 ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
134 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
135 ByteOp | ImplicitOps, ImplicitOps,
136 /* 0xA8 - 0xAF */
137 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
138 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
139 ByteOp | ImplicitOps, ImplicitOps,
140 /* 0xB0 - 0xBF */
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300143 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
144 0, ImplicitOps, 0, 0,
145 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0xC8 - 0xCF */
147 0, 0, 0, 0, 0, 0, 0, 0,
148 /* 0xD0 - 0xD7 */
149 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
150 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
151 0, 0, 0, 0,
152 /* 0xD8 - 0xDF */
153 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300154 /* 0xE0 - 0xE7 */
155 0, 0, 0, 0, 0, 0, 0, 0,
156 /* 0xE8 - 0xEF */
Nitin A Kamblef6eed392007-08-28 18:08:37 -0700157 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0xF0 - 0xF7 */
159 0, 0, 0, 0,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300160 ImplicitOps, 0,
161 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0xF8 - 0xFF */
163 0, 0, 0, 0,
164 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
165};
166
Avi Kivity038e51d2007-01-22 20:40:40 -0800167static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0x00 - 0x0F */
169 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200170 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 /* 0x10 - 0x1F */
172 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
173 /* 0x20 - 0x2F */
174 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0,
176 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300177 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0x40 - 0x47 */
179 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
180 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
181 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183 /* 0x48 - 0x4F */
184 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
185 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
186 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
188 /* 0x50 - 0x5F */
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190 /* 0x60 - 0x6F */
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 /* 0x70 - 0x7F */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300195 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
196 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
197 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
198 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0x90 - 0x9F */
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800202 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800204 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xB0 - 0xB7 */
206 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800207 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
209 DstReg | SrcMem16 | ModRM | Mov,
210 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800211 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
213 DstReg | SrcMem16 | ModRM | Mov,
214 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800215 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
216 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xD0 - 0xDF */
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0xE0 - 0xEF */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0xF0 - 0xFF */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
223};
224
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225/* EFLAGS bit definitions. */
226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
228#define EFLG_SF (1<<7)
229#define EFLG_ZF (1<<6)
230#define EFLG_AF (1<<4)
231#define EFLG_PF (1<<2)
232#define EFLG_CF (1<<0)
233
234/*
235 * Instruction emulation:
236 * Most instructions are emulated directly via a fragment of inline assembly
237 * code. This allows us to save/restore EFLAGS and thus very easily pick up
238 * any modified flags.
239 */
240
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800241#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242#define _LO32 "k" /* force 32-bit operand */
243#define _STK "%%rsp" /* stack pointer */
244#elif defined(__i386__)
245#define _LO32 "" /* force 32-bit operand */
246#define _STK "%%esp" /* stack pointer */
247#endif
248
249/*
250 * These EFLAGS bits are restored from saved value during emulation, and
251 * any changes are written back to the saved value after emulation.
252 */
253#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
254
255/* Before executing instruction: restore necessary bits in EFLAGS. */
256#define _PRE_EFLAGS(_sav, _msk, _tmp) \
257 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
258 "push %"_sav"; " \
259 "movl %"_msk",%"_LO32 _tmp"; " \
260 "andl %"_LO32 _tmp",("_STK"); " \
261 "pushf; " \
262 "notl %"_LO32 _tmp"; " \
263 "andl %"_LO32 _tmp",("_STK"); " \
264 "pop %"_tmp"; " \
265 "orl %"_LO32 _tmp",("_STK"); " \
266 "popf; " \
267 /* _sav &= ~msk; */ \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "notl %"_LO32 _tmp"; " \
270 "andl %"_LO32 _tmp",%"_sav"; "
271
272/* After executing instruction: write-back necessary bits in EFLAGS. */
273#define _POST_EFLAGS(_sav, _msk, _tmp) \
274 /* _sav |= EFLAGS & _msk; */ \
275 "pushf; " \
276 "pop %"_tmp"; " \
277 "andl %"_msk",%"_LO32 _tmp"; " \
278 "orl %"_LO32 _tmp",%"_sav"; "
279
280/* Raw emulation: instruction has two explicit operands. */
281#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
282 do { \
283 unsigned long _tmp; \
284 \
285 switch ((_dst).bytes) { \
286 case 2: \
287 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400288 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400290 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 : "=m" (_eflags), "=m" ((_dst).val), \
292 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400293 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 break; \
295 case 4: \
296 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400297 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400299 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300 : "=m" (_eflags), "=m" ((_dst).val), \
301 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400302 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 break; \
304 case 8: \
305 __emulate_2op_8byte(_op, _src, _dst, \
306 _eflags, _qx, _qy); \
307 break; \
308 } \
309 } while (0)
310
311#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
312 do { \
313 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400314 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 case 1: \
316 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400317 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400319 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320 : "=m" (_eflags), "=m" ((_dst).val), \
321 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400322 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 break; \
324 default: \
325 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
326 _wx, _wy, _lx, _ly, _qx, _qy); \
327 break; \
328 } \
329 } while (0)
330
331/* Source operand is byte-sized and may be restricted to just %cl. */
332#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
333 __emulate_2op(_op, _src, _dst, _eflags, \
334 "b", "c", "b", "c", "b", "c", "b", "c")
335
336/* Source operand is byte, word, long or quad sized. */
337#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
338 __emulate_2op(_op, _src, _dst, _eflags, \
339 "b", "q", "w", "r", _LO32, "r", "", "r")
340
341/* Source operand is word, long or quad sized. */
342#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
343 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
344 "w", "r", _LO32, "r", "", "r")
345
346/* Instruction has only one explicit operand (no source operand). */
347#define emulate_1op(_op, _dst, _eflags) \
348 do { \
349 unsigned long _tmp; \
350 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400351 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352 case 1: \
353 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400354 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400356 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 : "=m" (_eflags), "=m" ((_dst).val), \
358 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400359 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 break; \
361 case 2: \
362 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400363 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400365 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 : "=m" (_eflags), "=m" ((_dst).val), \
367 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400368 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369 break; \
370 case 4: \
371 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400372 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400374 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 : "=m" (_eflags), "=m" ((_dst).val), \
376 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400377 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 break; \
379 case 8: \
380 __emulate_1op_8byte(_op, _dst, _eflags); \
381 break; \
382 } \
383 } while (0)
384
385/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800386#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
388 do { \
389 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400390 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400392 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400394 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 } while (0)
396
397#define __emulate_1op_8byte(_op, _dst, _eflags) \
398 do { \
399 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400400 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400402 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400404 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 } while (0)
406
407#elif defined(__i386__)
408#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
409#define __emulate_1op_8byte(_op, _dst, _eflags)
410#endif /* __i386__ */
411
412/* Fetch next part of the instruction being emulated. */
413#define insn_fetch(_type, _size, _eip) \
414({ unsigned long _x; \
415 rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
Mike Dayd77c26f2007-10-08 09:02:08 -0400416 (_size), ctxt->vcpu); \
417 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 goto done; \
419 (_eip) += (_size); \
420 (_type)_x; \
421})
422
423/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300424#define address_mask(reg) \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200425 ((c->ad_bytes == sizeof(unsigned long)) ? \
426 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300428 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429#define register_address_increment(reg, inc) \
430 do { \
431 /* signed type ensures sign extension to long */ \
432 int _inc = (inc); \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200433 if (c->ad_bytes == sizeof(unsigned long)) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 (reg) += _inc; \
435 else \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200436 (reg) = ((reg) & \
437 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
438 (((reg) + _inc) & \
439 ((1UL << (c->ad_bytes << 3)) - 1)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 } while (0)
441
Nitin A Kamble098c9372007-08-19 11:00:36 +0300442#define JMP_REL(rel) \
443 do { \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200444 register_address_increment(c->eip, rel); \
Nitin A Kamble098c9372007-08-19 11:00:36 +0300445 } while (0)
446
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000447/*
448 * Given the 'reg' portion of a ModRM byte, and a register block, return a
449 * pointer into the block that addresses the relevant register.
450 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
451 */
452static void *decode_register(u8 modrm_reg, unsigned long *regs,
453 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454{
455 void *p;
456
457 p = &regs[modrm_reg];
458 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
459 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
460 return p;
461}
462
463static int read_descriptor(struct x86_emulate_ctxt *ctxt,
464 struct x86_emulate_ops *ops,
465 void *ptr,
466 u16 *size, unsigned long *address, int op_bytes)
467{
468 int rc;
469
470 if (op_bytes == 2)
471 op_bytes = 3;
472 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300473 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
474 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 if (rc)
476 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300477 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
478 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 return rc;
480}
481
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300482static int test_cc(unsigned int condition, unsigned int flags)
483{
484 int rc = 0;
485
486 switch ((condition & 15) >> 1) {
487 case 0: /* o */
488 rc |= (flags & EFLG_OF);
489 break;
490 case 1: /* b/c/nae */
491 rc |= (flags & EFLG_CF);
492 break;
493 case 2: /* z/e */
494 rc |= (flags & EFLG_ZF);
495 break;
496 case 3: /* be/na */
497 rc |= (flags & (EFLG_CF|EFLG_ZF));
498 break;
499 case 4: /* s */
500 rc |= (flags & EFLG_SF);
501 break;
502 case 5: /* p/pe */
503 rc |= (flags & EFLG_PF);
504 break;
505 case 7: /* le/ng */
506 rc |= (flags & EFLG_ZF);
507 /* fall through */
508 case 6: /* l/nge */
509 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
510 break;
511 }
512
513 /* Odd condition identifiers (lsb == 1) have inverted sense. */
514 return (!!rc ^ (condition & 1));
515}
516
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200518x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200520 struct decode_cache *c = &ctxt->decode;
521 u8 sib, rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523 int mode = ctxt->mode;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200524 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
526 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527
Laurent Viviere4e03de2007-09-18 11:52:50 +0200528 memset(c, 0, sizeof(struct decode_cache));
529 c->eip = ctxt->vcpu->rip;
530 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531
532 switch (mode) {
533 case X86EMUL_MODE_REAL:
534 case X86EMUL_MODE_PROT16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200535 c->op_bytes = c->ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 break;
537 case X86EMUL_MODE_PROT32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200538 c->op_bytes = c->ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800540#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 case X86EMUL_MODE_PROT64:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200542 c->op_bytes = 4;
543 c->ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 break;
545#endif
546 default:
547 return -1;
548 }
549
550 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200551 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200552 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 case 0x66: /* operand-size override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200554 c->op_bytes ^= 6; /* switch between 2/4 bytes */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 break;
556 case 0x67: /* address-size override */
557 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200558 /* switch between 4/8 bytes */
559 c->ad_bytes ^= 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200561 /* switch between 2/4 bytes */
562 c->ad_bytes ^= 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 break;
564 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200565 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 break;
567 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200568 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 break;
570 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200571 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 break;
573 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200574 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 break;
576 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200577 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 break;
579 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200580 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200582 case 0x40 ... 0x4f: /* REX */
583 if (mode != X86EMUL_MODE_PROT64)
584 goto done_prefixes;
585 rex_prefix = c->b;
586 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200588 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200590 case 0xf2: /* REPNE/REPNZ */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 case 0xf3: /* REP/REPE/REPZ */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200592 c->rep_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 default:
595 goto done_prefixes;
596 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200597
598 /* Any legacy prefix after a REX prefix nullifies its effect. */
599
600 rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601 }
602
603done_prefixes:
604
605 /* REX prefix. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200606 if (rex_prefix) {
607 if (rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200608 c->op_bytes = 8; /* REX.W */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200609 c->modrm_reg = (rex_prefix & 4) << 1; /* REX.R */
610 index_reg = (rex_prefix & 2) << 2; /* REX.X */
611 c->modrm_rm = base_reg = (rex_prefix & 1) << 3; /* REG.B */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 }
613
614 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200615 c->d = opcode_table[c->b];
616 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200618 if (c->b == 0x0f) {
619 c->twobyte = 1;
620 c->b = insn_fetch(u8, 1, c->eip);
621 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 }
623
624 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200625 if (c->d == 0) {
626 DPRINTF("Cannot emulate %02x\n", c->b);
627 return -1;
628 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 }
630
631 /* ModRM and SIB bytes. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200632 if (c->d & ModRM) {
633 c->modrm = insn_fetch(u8, 1, c->eip);
634 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
635 c->modrm_reg |= (c->modrm & 0x38) >> 3;
636 c->modrm_rm |= (c->modrm & 0x07);
637 c->modrm_ea = 0;
638 c->use_modrm_ea = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639
Laurent Viviere4e03de2007-09-18 11:52:50 +0200640 if (c->modrm_mod == 3) {
641 c->modrm_val = *(unsigned long *)
642 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 goto modrm_done;
644 }
645
Laurent Viviere4e03de2007-09-18 11:52:50 +0200646 if (c->ad_bytes == 2) {
647 unsigned bx = c->regs[VCPU_REGS_RBX];
648 unsigned bp = c->regs[VCPU_REGS_RBP];
649 unsigned si = c->regs[VCPU_REGS_RSI];
650 unsigned di = c->regs[VCPU_REGS_RDI];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
652 /* 16-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200653 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200655 if (c->modrm_rm == 6)
656 c->modrm_ea +=
657 insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 break;
659 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200660 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 break;
662 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200663 c->modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 break;
665 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200666 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200668 c->modrm_ea += bx + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 break;
670 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200671 c->modrm_ea += bx + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 break;
673 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200674 c->modrm_ea += bp + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 break;
676 case 3:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200677 c->modrm_ea += bp + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 break;
679 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200680 c->modrm_ea += si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 break;
682 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200683 c->modrm_ea += di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 break;
685 case 6:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200686 if (c->modrm_mod != 0)
687 c->modrm_ea += bp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 break;
689 case 7:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200690 c->modrm_ea += bx;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 break;
692 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200693 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
694 (c->modrm_rm == 6 && c->modrm_mod != 0))
695 if (!c->override_base)
696 c->override_base = &ctxt->ss_base;
697 c->modrm_ea = (u16)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 } else {
699 /* 32/64-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200700 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 case 4:
702 case 12:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200703 sib = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 index_reg |= (sib >> 3) & 7;
705 base_reg |= sib & 7;
706 scale = sib >> 6;
707
708 switch (base_reg) {
709 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200710 if (c->modrm_mod != 0)
711 c->modrm_ea +=
712 c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200714 c->modrm_ea +=
715 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 break;
717 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200718 c->modrm_ea += c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 }
720 switch (index_reg) {
721 case 4:
722 break;
723 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200724 c->modrm_ea +=
725 c->regs[index_reg] << scale;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726
727 }
728 break;
729 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200730 if (c->modrm_mod != 0)
731 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 else if (mode == X86EMUL_MODE_PROT64)
733 rip_relative = 1;
734 break;
735 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200736 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 break;
738 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200739 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200741 if (c->modrm_rm == 5)
742 c->modrm_ea +=
743 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 break;
745 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200746 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 break;
748 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200749 c->modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 break;
751 }
752 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200753 if (!c->override_base)
754 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 if (mode == X86EMUL_MODE_PROT64 &&
Laurent Viviere4e03de2007-09-18 11:52:50 +0200756 c->override_base != &ctxt->fs_base &&
757 c->override_base != &ctxt->gs_base)
758 c->override_base = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759
Laurent Viviere4e03de2007-09-18 11:52:50 +0200760 if (c->override_base)
761 c->modrm_ea += *c->override_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762
763 if (rip_relative) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200764 c->modrm_ea += c->eip;
765 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200767 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 break;
769 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200770 if (c->d & ByteOp)
771 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200773 if (c->op_bytes == 8)
774 c->modrm_ea += 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200776 c->modrm_ea += c->op_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 }
778 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200779 if (c->ad_bytes != 8)
780 c->modrm_ea = (u32)c->modrm_ea;
Mike Dayd77c26f2007-10-08 09:02:08 -0400781modrm_done:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 ;
783 }
784
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 /*
786 * Decode and fetch the source operand: register, memory
787 * or immediate.
788 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200789 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 case SrcNone:
791 break;
792 case SrcReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200793 c->src.type = OP_REG;
794 if (c->d & ByteOp) {
795 c->src.ptr =
796 decode_register(c->modrm_reg, c->regs,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200798 c->src.val = c->src.orig_val = *(u8 *)c->src.ptr;
799 c->src.bytes = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200801 c->src.ptr =
802 decode_register(c->modrm_reg, c->regs, 0);
803 switch ((c->src.bytes = c->op_bytes)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200805 c->src.val = c->src.orig_val =
806 *(u16 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 break;
808 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200809 c->src.val = c->src.orig_val =
810 *(u32 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 break;
812 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200813 c->src.val = c->src.orig_val =
814 *(u64 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 break;
816 }
817 }
818 break;
819 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200820 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 goto srcmem_common;
822 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200823 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 goto srcmem_common;
825 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200826 c->src.bytes = (c->d & ByteOp) ? 1 :
827 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300828 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400829 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300830 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400831 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200832 /*
833 * For instructions with a ModR/M byte, switch to register
834 * access if Mod = 3.
835 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200836 if ((c->d & ModRM) && c->modrm_mod == 3) {
837 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200838 break;
839 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200840 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 break;
842 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200843 c->src.type = OP_IMM;
844 c->src.ptr = (unsigned long *)c->eip;
845 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
846 if (c->src.bytes == 8)
847 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200849 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200851 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 break;
853 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200854 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 break;
856 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200857 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 break;
859 }
860 break;
861 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200862 c->src.type = OP_IMM;
863 c->src.ptr = (unsigned long *)c->eip;
864 c->src.bytes = 1;
865 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 break;
867 }
868
Avi Kivity038e51d2007-01-22 20:40:40 -0800869 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200870 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800871 case ImplicitOps:
872 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200873 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800874 case DstReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200875 c->dst.type = OP_REG;
876 if ((c->d & ByteOp)
877 && !(c->twobyte &&
878 (c->b == 0xb6 || c->b == 0xb7))) {
879 c->dst.ptr =
880 decode_register(c->modrm_reg, c->regs,
Avi Kivity038e51d2007-01-22 20:40:40 -0800881 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200882 c->dst.val = *(u8 *) c->dst.ptr;
883 c->dst.bytes = 1;
Avi Kivity038e51d2007-01-22 20:40:40 -0800884 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200885 c->dst.ptr =
886 decode_register(c->modrm_reg, c->regs, 0);
887 switch ((c->dst.bytes = c->op_bytes)) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800888 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 c->dst.val = *(u16 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800890 break;
891 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 c->dst.val = *(u32 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800893 break;
894 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200895 c->dst.val = *(u64 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800896 break;
897 }
898 }
899 break;
900 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 if ((c->d & ModRM) && c->modrm_mod == 3) {
902 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200903 break;
904 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200905 c->dst.type = OP_MEM;
906 break;
907 }
908
909done:
910 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
911}
912
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200913static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
914{
915 struct decode_cache *c = &ctxt->decode;
916
917 c->dst.type = OP_MEM;
918 c->dst.bytes = c->op_bytes;
919 c->dst.val = c->src.val;
920 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
921 c->dst.ptr = (void *) register_address(ctxt->ss_base,
922 c->regs[VCPU_REGS_RSP]);
923}
924
925static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
926 struct x86_emulate_ops *ops)
927{
928 struct decode_cache *c = &ctxt->decode;
929 int rc;
930
931 /* 64-bit mode: POP always pops a 64-bit operand. */
932
933 if (ctxt->mode == X86EMUL_MODE_PROT64)
934 c->dst.bytes = 8;
935
936 rc = ops->read_std(register_address(ctxt->ss_base,
937 c->regs[VCPU_REGS_RSP]),
938 &c->dst.val, c->dst.bytes, ctxt->vcpu);
939 if (rc != 0)
940 return rc;
941
942 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
943
944 return 0;
945}
946
Laurent Vivier05f086f2007-09-24 11:10:55 +0200947static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200948{
Laurent Vivier05f086f2007-09-24 11:10:55 +0200949 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200950 switch (c->modrm_reg) {
951 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200952 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200953 break;
954 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200955 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200956 break;
957 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200958 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200959 break;
960 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200961 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200962 break;
963 case 4: /* sal/shl */
964 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200965 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200966 break;
967 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200968 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200969 break;
970 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200971 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200972 break;
973 }
974}
975
976static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +0200977 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200978{
979 struct decode_cache *c = &ctxt->decode;
980 int rc = 0;
981
982 switch (c->modrm_reg) {
983 case 0 ... 1: /* test */
984 /*
985 * Special case in Grp3: test has an immediate
986 * source operand.
987 */
988 c->src.type = OP_IMM;
989 c->src.ptr = (unsigned long *)c->eip;
990 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
991 if (c->src.bytes == 8)
992 c->src.bytes = 4;
993 switch (c->src.bytes) {
994 case 1:
995 c->src.val = insn_fetch(s8, 1, c->eip);
996 break;
997 case 2:
998 c->src.val = insn_fetch(s16, 2, c->eip);
999 break;
1000 case 4:
1001 c->src.val = insn_fetch(s32, 4, c->eip);
1002 break;
1003 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001004 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001005 break;
1006 case 2: /* not */
1007 c->dst.val = ~c->dst.val;
1008 break;
1009 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001010 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001011 break;
1012 default:
1013 DPRINTF("Cannot emulate %02x\n", c->b);
1014 rc = X86EMUL_UNHANDLEABLE;
1015 break;
1016 }
1017done:
1018 return rc;
1019}
1020
1021static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001022 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001023{
1024 struct decode_cache *c = &ctxt->decode;
1025 int rc;
1026
1027 switch (c->modrm_reg) {
1028 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001029 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001030 break;
1031 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001032 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001033 break;
1034 case 4: /* jmp abs */
1035 if (c->b == 0xff)
1036 c->eip = c->dst.val;
1037 else {
1038 DPRINTF("Cannot emulate %02x\n", c->b);
1039 return X86EMUL_UNHANDLEABLE;
1040 }
1041 break;
1042 case 6: /* push */
1043
1044 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1045
1046 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1047 c->dst.bytes = 8;
1048 rc = ops->read_std((unsigned long)c->dst.ptr,
1049 &c->dst.val, 8, ctxt->vcpu);
1050 if (rc != 0)
1051 return rc;
1052 }
1053 register_address_increment(c->regs[VCPU_REGS_RSP],
1054 -c->dst.bytes);
1055 rc = ops->write_emulated(register_address(ctxt->ss_base,
1056 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1057 c->dst.bytes, ctxt->vcpu);
1058 if (rc != 0)
1059 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001060 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001061 break;
1062 default:
1063 DPRINTF("Cannot emulate %02x\n", c->b);
1064 return X86EMUL_UNHANDLEABLE;
1065 }
1066 return 0;
1067}
1068
1069static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1070 struct x86_emulate_ops *ops,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001071 unsigned long cr2)
1072{
1073 struct decode_cache *c = &ctxt->decode;
1074 u64 old, new;
1075 int rc;
1076
1077 rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu);
1078 if (rc != 0)
1079 return rc;
1080
1081 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1082 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1083
1084 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1085 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001086 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001087
1088 } else {
1089 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1090 (u32) c->regs[VCPU_REGS_RBX];
1091
1092 rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu);
1093 if (rc != 0)
1094 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001095 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096 }
1097 return 0;
1098}
1099
1100static inline int writeback(struct x86_emulate_ctxt *ctxt,
1101 struct x86_emulate_ops *ops)
1102{
1103 int rc;
1104 struct decode_cache *c = &ctxt->decode;
1105
1106 switch (c->dst.type) {
1107 case OP_REG:
1108 /* The 4-byte case *is* correct:
1109 * in 64-bit mode we zero-extend.
1110 */
1111 switch (c->dst.bytes) {
1112 case 1:
1113 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1114 break;
1115 case 2:
1116 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1117 break;
1118 case 4:
1119 *c->dst.ptr = (u32)c->dst.val;
1120 break; /* 64b: zero-ext */
1121 case 8:
1122 *c->dst.ptr = c->dst.val;
1123 break;
1124 }
1125 break;
1126 case OP_MEM:
1127 if (c->lock_prefix)
1128 rc = ops->cmpxchg_emulated(
1129 (unsigned long)c->dst.ptr,
1130 &c->dst.orig_val,
1131 &c->dst.val,
1132 c->dst.bytes,
1133 ctxt->vcpu);
1134 else
1135 rc = ops->write_emulated(
1136 (unsigned long)c->dst.ptr,
1137 &c->dst.val,
1138 c->dst.bytes,
1139 ctxt->vcpu);
1140 if (rc != 0)
1141 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001142 break;
1143 case OP_NONE:
1144 /* no writeback */
1145 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001146 default:
1147 break;
1148 }
1149 return 0;
1150}
1151
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001152int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001153x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001154{
1155 unsigned long cr2 = ctxt->cr2;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001156 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001157 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001158 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001159 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001160
Laurent Vivier34273182007-09-18 11:27:37 +02001161 /* Shadow copy of register state. Committed on successful emulation.
1162 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1163 * modify them.
1164 */
1165
1166 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1167 saved_eip = c->eip;
1168
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001169 if ((c->d & ModRM) && (c->modrm_mod != 3))
1170 cr2 = c->modrm_ea;
1171
1172 if (c->src.type == OP_MEM) {
1173 c->src.ptr = (unsigned long *)cr2;
1174 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001175 rc = ops->read_emulated((unsigned long)c->src.ptr,
1176 &c->src.val,
1177 c->src.bytes,
1178 ctxt->vcpu);
1179 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001180 goto done;
1181 c->src.orig_val = c->src.val;
1182 }
1183
1184 if ((c->d & DstMask) == ImplicitOps)
1185 goto special_insn;
1186
1187
1188 if (c->dst.type == OP_MEM) {
1189 c->dst.ptr = (unsigned long *)cr2;
1190 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1191 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001192 if (c->d & BitOp) {
1193 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001194
Laurent Viviere4e03de2007-09-18 11:52:50 +02001195 c->dst.ptr = (void *)c->dst.ptr +
1196 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001197 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001198 if (!(c->d & Mov) &&
1199 /* optimisation - avoid slow emulated read */
1200 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1201 &c->dst.val,
1202 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001203 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001204 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001205 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001206
Laurent Viviere4e03de2007-09-18 11:52:50 +02001207 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 goto twobyte_insn;
1209
Laurent Viviere4e03de2007-09-18 11:52:50 +02001210 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 case 0x00 ... 0x05:
1212 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001213 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 break;
1215 case 0x08 ... 0x0d:
1216 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001217 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 break;
1219 case 0x10 ... 0x15:
1220 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001221 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 break;
1223 case 0x18 ... 0x1d:
1224 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001225 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001227 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001229 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001231 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001232 c->dst.type = OP_REG;
1233 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1234 c->dst.val = *(u8 *)c->dst.ptr;
1235 c->dst.bytes = 1;
1236 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001237 goto and;
1238 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001239 c->dst.type = OP_REG;
1240 c->dst.bytes = c->op_bytes;
1241 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1242 if (c->op_bytes == 2)
1243 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001244 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001245 c->dst.val = *(u32 *)c->dst.ptr;
1246 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001247 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 case 0x28 ... 0x2d:
1249 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001250 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 break;
1252 case 0x30 ... 0x35:
1253 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001254 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 break;
1256 case 0x38 ... 0x3d:
1257 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001258 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 break;
1260 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001261 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001263 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 break;
1265 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001266 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 case 0:
1268 goto add;
1269 case 1:
1270 goto or;
1271 case 2:
1272 goto adc;
1273 case 3:
1274 goto sbb;
1275 case 4:
1276 goto and;
1277 case 5:
1278 goto sub;
1279 case 6:
1280 goto xor;
1281 case 7:
1282 goto cmp;
1283 }
1284 break;
1285 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001286 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 break;
1288 case 0x86 ... 0x87: /* xchg */
1289 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001290 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001292 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 break;
1294 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001295 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 break;
1297 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001298 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 break; /* 64b reg: zero-extend */
1300 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001301 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 break;
1303 }
1304 /*
1305 * Write back the memory destination with implicit LOCK
1306 * prefix.
1307 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001308 c->dst.val = c->src.val;
1309 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001312 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001313 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001314 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001315 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001317 rc = emulate_grp1a(ctxt, ops);
1318 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001321 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001322 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1323 c->dst.val = c->src.val;
1324 /* skip src displacement */
1325 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001326 break;
1327 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001328 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1329 /* skip c->dst displacement */
1330 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001331 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 case 0xc0 ... 0xc1:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001333 emulate_grp2(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001335 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1336 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001337 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001338 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001340 c->src.val = 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001341 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001344 c->src.val = c->regs[VCPU_REGS_RCX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001345 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001348 rc = emulate_grp3(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001349 if (rc != 0)
1350 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 break;
1352 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviera01af5e2007-09-24 11:10:56 +02001353 rc = emulate_grp45(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001354 if (rc != 0)
1355 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 break;
1357 }
1358
1359writeback:
Laurent Viviera01af5e2007-09-24 11:10:56 +02001360 rc = writeback(ctxt, ops);
1361 if (rc != 0)
1362 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363
1364 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001365 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001366 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
1368done:
Laurent Vivier34273182007-09-18 11:27:37 +02001369 if (rc == X86EMUL_UNHANDLEABLE) {
1370 c->eip = saved_eip;
1371 return -1;
1372 }
1373 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374
1375special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001376 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001378 switch (c->b) {
Nitin A Kamble7e778162007-08-19 11:07:06 +03001379 case 0x50 ... 0x57: /* push reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001380 if (c->op_bytes == 2)
1381 c->src.val = (u16) c->regs[c->b & 0x7];
Nitin A Kamble7e778162007-08-19 11:07:06 +03001382 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001383 c->src.val = (u32) c->regs[c->b & 0x7];
1384 c->dst.type = OP_MEM;
1385 c->dst.bytes = c->op_bytes;
1386 c->dst.val = c->src.val;
1387 register_address_increment(c->regs[VCPU_REGS_RSP],
1388 -c->op_bytes);
1389 c->dst.ptr = (void *) register_address(
1390 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Nitin A Kamble7e778162007-08-19 11:07:06 +03001391 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001392 case 0x58 ... 0x5f: /* pop reg */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393 c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001394 pop_instruction:
1395 if ((rc = ops->read_std(register_address(ctxt->ss_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001396 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1397 c->op_bytes, ctxt->vcpu)) != 0)
Nitin A Kamble7de75242007-09-15 10:13:07 +03001398 goto done;
1399
Laurent Viviere4e03de2007-09-18 11:52:50 +02001400 register_address_increment(c->regs[VCPU_REGS_RSP],
1401 c->op_bytes);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001402 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001403 break;
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001404 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001405 c->src.val = 0L;
1406 c->src.val = insn_fetch(s8, 1, c->eip);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001407 emulate_push(ctxt);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001408 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001409 case 0x6c: /* insb */
1410 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001411 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001412 1,
1413 (c->d & ByteOp) ? 1 : c->op_bytes,
1414 c->rep_prefix ?
1415 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001416 (ctxt->eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001417 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001418 c->regs[VCPU_REGS_RDI]),
1419 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001420 c->regs[VCPU_REGS_RDX]) == 0) {
1421 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001422 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001423 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001424 return 0;
1425 case 0x6e: /* outsb */
1426 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001427 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001428 0,
1429 (c->d & ByteOp) ? 1 : c->op_bytes,
1430 c->rep_prefix ?
1431 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432 (ctxt->eflags & EFLG_DF),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001433 register_address(c->override_base ?
1434 *c->override_base :
1435 ctxt->ds_base,
1436 c->regs[VCPU_REGS_RSI]),
1437 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001438 c->regs[VCPU_REGS_RDX]) == 0) {
1439 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001440 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001441 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001442 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001443 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001444 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001445
Laurent Vivier05f086f2007-09-24 11:10:55 +02001446 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001447 JMP_REL(rel);
1448 break;
1449 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001450 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001451 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001452 emulate_push(ctxt);
1453 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001454 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001455 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001456 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001457 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001458 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001459 goto pop_instruction;
1460 case 0xf4: /* hlt */
1461 ctxt->vcpu->halt_request = 1;
1462 goto done;
Laurent Viviere70669a2007-08-05 10:36:40 +03001463 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001464 if (c->rep_prefix) {
1465 if (c->regs[VCPU_REGS_RCX] == 0) {
1466 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 goto done;
1468 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001469 c->regs[VCPU_REGS_RCX]--;
1470 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001472 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 c->dst.type = OP_MEM;
1475 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1476 c->dst.ptr = (unsigned long *)register_address(
1477 ctxt->es_base,
1478 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001480 c->override_base ? *c->override_base :
1481 ctxt->ds_base,
1482 c->regs[VCPU_REGS_RSI]),
1483 &c->dst.val,
1484 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001486 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001487 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001488 : c->dst.bytes);
1489 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001490 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001491 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 break;
1493 case 0xa6 ... 0xa7: /* cmps */
1494 DPRINTF("Urk! I don't handle CMPS.\n");
1495 goto cannot_emulate;
1496 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001497 c->dst.type = OP_MEM;
1498 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1499 c->dst.ptr = (unsigned long *)cr2;
1500 c->dst.val = c->regs[VCPU_REGS_RAX];
1501 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001502 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001503 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 break;
1505 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 c->dst.type = OP_REG;
1507 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1508 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1509 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1510 c->dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001511 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001513 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001514 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001515 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 break;
1517 case 0xae ... 0xaf: /* scas */
1518 DPRINTF("Urk! I don't handle SCAS.\n");
1519 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001520 case 0xe8: /* call (near) */ {
1521 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001522 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001523 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001524 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001525 break;
1526 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001527 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001528 break;
1529 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001530 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001531 break;
1532 default:
1533 DPRINTF("Call: Invalid op_bytes\n");
1534 goto cannot_emulate;
1535 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001536 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001537 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001538 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539 emulate_push(ctxt);
1540 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001541 }
1542 case 0xe9: /* jmp rel */
1543 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001544 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001545 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001546 break;
1547
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001548
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 }
1550 goto writeback;
1551
1552twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001553 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001555 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 u16 size;
1557 unsigned long address;
1558
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001559 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001560 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001561 goto cannot_emulate;
1562
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001563 rc = kvm_fix_hypercall(ctxt->vcpu);
1564 if (rc)
1565 goto done;
1566
1567 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001568 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001570 rc = read_descriptor(ctxt, ops, c->src.ptr,
1571 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572 if (rc)
1573 goto done;
1574 realmode_lgdt(ctxt->vcpu, size, address);
1575 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001576 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001577 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001578 rc = kvm_fix_hypercall(ctxt->vcpu);
1579 if (rc)
1580 goto done;
1581 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001582 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001583 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001584 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001586 if (rc)
1587 goto done;
1588 realmode_lidt(ctxt->vcpu, size, address);
1589 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 break;
1591 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001592 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001594 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 = realmode_get_cr(ctxt->vcpu, 0);
1596 break;
1597 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001598 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 goto cannot_emulate;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001600 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1601 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 break;
1603 case 7: /* invlpg*/
1604 emulate_invlpg(ctxt->vcpu, cr2);
1605 break;
1606 default:
1607 goto cannot_emulate;
1608 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001609 /* Disable writeback. */
1610 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 break;
1612 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001613 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001615 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001616 if (rc)
1617 goto cannot_emulate;
1618 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 break;
1620 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001621 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001623 rc = emulator_set_dr(ctxt, c->modrm_reg,
1624 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001625 if (rc)
1626 goto cannot_emulate;
1627 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 break;
1629 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001630 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001631 if (!test_cc(c->b, ctxt->eflags))
1632 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001634 case 0xa3:
1635 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001636 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001637 /* only subword offset */
1638 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001639 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001640 break;
1641 case 0xab:
1642 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001643 /* only subword offset */
1644 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001645 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001646 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 case 0xb0 ... 0xb1: /* cmpxchg */
1648 /*
1649 * Save real source value, then compare EAX against
1650 * destination.
1651 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001652 c->src.orig_val = c->src.val;
1653 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001654 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1655 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001657 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 } else {
1659 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 c->dst.type = OP_REG;
1661 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 }
1663 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664 case 0xb3:
1665 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001666 /* only subword offset */
1667 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001668 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001671 c->dst.bytes = c->op_bytes;
1672 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1673 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001676 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 case 0:
1678 goto bt;
1679 case 1:
1680 goto bts;
1681 case 2:
1682 goto btr;
1683 case 3:
1684 goto btc;
1685 }
1686 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001687 case 0xbb:
1688 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001689 /* only subword offset */
1690 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001691 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001694 c->dst.bytes = c->op_bytes;
1695 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1696 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001698 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001699 c->dst.bytes = c->op_bytes;
1700 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1701 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001702 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 }
1704 goto writeback;
1705
1706twobyte_special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001707 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001708 case 0x06:
1709 emulate_clts(ctxt->vcpu);
1710 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001711 case 0x08: /* invd */
1712 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001713 case 0x09: /* wbinvd */
1714 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715 case 0x0d: /* GrpP (prefetch) */
1716 case 0x18: /* Grp16 (prefetch/nop) */
1717 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 case 0x20: /* mov cr, reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001719 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001721 c->regs[c->modrm_rm] =
1722 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 break;
1724 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001725 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001727 realmode_set_cr(ctxt->vcpu,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001728 c->modrm_reg, c->modrm_val, &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001730 case 0x30:
1731 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001732 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1733 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1734 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001735 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001736 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001737 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001738 }
1739 rc = X86EMUL_CONTINUE;
1740 break;
1741 case 0x32:
1742 /* rdmsr */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001744 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001745 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001746 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001747 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001748 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1749 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001750 }
1751 rc = X86EMUL_CONTINUE;
1752 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001753 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1754 long int rel;
1755
Laurent Viviere4e03de2007-09-18 11:52:50 +02001756 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001757 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001758 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001759 break;
1760 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001761 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001762 break;
1763 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001764 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001765 break;
1766 default:
1767 DPRINTF("jnz: Invalid op_bytes\n");
1768 goto cannot_emulate;
1769 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001770 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001771 JMP_REL(rel);
1772 break;
1773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 case 0xc7: /* Grp9 (cmpxchg8b) */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001775 rc = emulate_grp9(ctxt, ops, cr2);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776 if (rc != 0)
1777 goto done;
1778 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001780 /* Disable writeback. */
1781 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782 goto writeback;
1783
1784cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001785 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001786 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 return -1;
1788}