blob: b352a6c4d4a2a20f56aaaae2e24760423c8cd33f [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"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080029#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
32#include "x86_emulate.h"
33#include <linux/module.h>
34
35/*
36 * Opcode effective-address decode tables.
37 * Note that we only emulate instructions that have at least one memory
38 * operand (excluding implicit stack references). We assume that stack
39 * references and instruction fetches will never occur in special memory
40 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
41 * not be handled.
42 */
43
44/* Operand sizes: 8-bit operands or specified/overridden size. */
45#define ByteOp (1<<0) /* 8-bit operands. */
46/* Destination operand type. */
47#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
48#define DstReg (2<<1) /* Register operand. */
49#define DstMem (3<<1) /* Memory operand. */
50#define DstMask (3<<1)
51/* Source operand type. */
52#define SrcNone (0<<3) /* No source operand. */
53#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
54#define SrcReg (1<<3) /* Register operand. */
55#define SrcMem (2<<3) /* Memory operand. */
56#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
57#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
58#define SrcImm (5<<3) /* Immediate operand. */
59#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
60#define SrcMask (7<<3)
61/* Generic ModRM decode. */
62#define ModRM (1<<6)
63/* Destination is only written; never read. */
64#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080065#define BitOp (1<<8)
Avi Kivityc7e75a32007-10-28 16:34:25 +020066#define MemAbs (1<<9) /* Memory operand is absolute displacement */
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
Avi Kivityc7e75a32007-10-28 16:34:25 +020068static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080069 /* 0x00 - 0x07 */
70 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
71 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
72 0, 0, 0, 0,
73 /* 0x08 - 0x0F */
74 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
75 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
76 0, 0, 0, 0,
77 /* 0x10 - 0x17 */
78 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
79 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
80 0, 0, 0, 0,
81 /* 0x18 - 0x1F */
82 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
83 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
84 0, 0, 0, 0,
85 /* 0x20 - 0x27 */
86 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
87 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030088 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080089 /* 0x28 - 0x2F */
90 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
91 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
92 0, 0, 0, 0,
93 /* 0x30 - 0x37 */
94 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
95 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
96 0, 0, 0, 0,
97 /* 0x38 - 0x3F */
98 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
99 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
100 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700101 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200102 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700103 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200104 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300105 /* 0x50 - 0x57 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200106 SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300107 /* 0x58 - 0x5F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200108 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700109 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700111 0, 0, 0, 0,
112 /* 0x68 - 0x6F */
113 0, 0, ImplicitOps|Mov, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300114 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
115 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300116 /* 0x70 - 0x77 */
117 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
118 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
119 /* 0x78 - 0x7F */
120 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
121 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 /* 0x80 - 0x87 */
123 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
124 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
125 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
126 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
127 /* 0x88 - 0x8F */
128 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
129 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +0300130 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 /* 0x90 - 0x9F */
Nitin A Kamble535eabc2007-09-15 10:45:05 +0300132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200134 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
135 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
137 ByteOp | ImplicitOps, ImplicitOps,
138 /* 0xA8 - 0xAF */
139 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
140 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
141 ByteOp | ImplicitOps, ImplicitOps,
142 /* 0xB0 - 0xBF */
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300145 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
146 0, ImplicitOps, 0, 0,
147 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 /* 0xC8 - 0xCF */
149 0, 0, 0, 0, 0, 0, 0, 0,
150 /* 0xD0 - 0xD7 */
151 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
152 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
153 0, 0, 0, 0,
154 /* 0xD8 - 0xDF */
155 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300156 /* 0xE0 - 0xE7 */
157 0, 0, 0, 0, 0, 0, 0, 0,
158 /* 0xE8 - 0xEF */
Nitin A Kamblef6eed392007-08-28 18:08:37 -0700159 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160 /* 0xF0 - 0xF7 */
161 0, 0, 0, 0,
Nitin A Kambleb284be52007-10-16 18:23:27 -0700162 ImplicitOps, ImplicitOps,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300163 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700165 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
167};
168
Avi Kivity038e51d2007-01-22 20:40:40 -0800169static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170 /* 0x00 - 0x0F */
171 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200172 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0x10 - 0x1F */
174 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
175 /* 0x20 - 0x2F */
176 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
177 0, 0, 0, 0, 0, 0, 0, 0,
178 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300179 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0x40 - 0x47 */
181 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
184 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
185 /* 0x48 - 0x4F */
186 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
188 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
189 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
190 /* 0x50 - 0x5F */
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 /* 0x60 - 0x6F */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0x70 - 0x7F */
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300197 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
198 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
199 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
200 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 /* 0x90 - 0x9F */
202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 /* 0xA0 - 0xA7 */
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 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800206 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 /* 0xB0 - 0xB7 */
208 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800209 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
211 DstReg | SrcMem16 | ModRM | Mov,
212 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800213 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
215 DstReg | SrcMem16 | ModRM | Mov,
216 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800217 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
218 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 /* 0xD0 - 0xDF */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0xE0 - 0xEF */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 /* 0xF0 - 0xFF */
224 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
225};
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227/* EFLAGS bit definitions. */
228#define EFLG_OF (1<<11)
229#define EFLG_DF (1<<10)
230#define EFLG_SF (1<<7)
231#define EFLG_ZF (1<<6)
232#define EFLG_AF (1<<4)
233#define EFLG_PF (1<<2)
234#define EFLG_CF (1<<0)
235
236/*
237 * Instruction emulation:
238 * Most instructions are emulated directly via a fragment of inline assembly
239 * code. This allows us to save/restore EFLAGS and thus very easily pick up
240 * any modified flags.
241 */
242
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800243#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244#define _LO32 "k" /* force 32-bit operand */
245#define _STK "%%rsp" /* stack pointer */
246#elif defined(__i386__)
247#define _LO32 "" /* force 32-bit operand */
248#define _STK "%%esp" /* stack pointer */
249#endif
250
251/*
252 * These EFLAGS bits are restored from saved value during emulation, and
253 * any changes are written back to the saved value after emulation.
254 */
255#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
256
257/* Before executing instruction: restore necessary bits in EFLAGS. */
258#define _PRE_EFLAGS(_sav, _msk, _tmp) \
259 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
260 "push %"_sav"; " \
261 "movl %"_msk",%"_LO32 _tmp"; " \
262 "andl %"_LO32 _tmp",("_STK"); " \
263 "pushf; " \
264 "notl %"_LO32 _tmp"; " \
265 "andl %"_LO32 _tmp",("_STK"); " \
266 "pop %"_tmp"; " \
267 "orl %"_LO32 _tmp",("_STK"); " \
268 "popf; " \
269 /* _sav &= ~msk; */ \
270 "movl %"_msk",%"_LO32 _tmp"; " \
271 "notl %"_LO32 _tmp"; " \
272 "andl %"_LO32 _tmp",%"_sav"; "
273
274/* After executing instruction: write-back necessary bits in EFLAGS. */
275#define _POST_EFLAGS(_sav, _msk, _tmp) \
276 /* _sav |= EFLAGS & _msk; */ \
277 "pushf; " \
278 "pop %"_tmp"; " \
279 "andl %"_msk",%"_LO32 _tmp"; " \
280 "orl %"_LO32 _tmp",%"_sav"; "
281
282/* Raw emulation: instruction has two explicit operands. */
283#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
284 do { \
285 unsigned long _tmp; \
286 \
287 switch ((_dst).bytes) { \
288 case 2: \
289 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400290 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400292 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 : "=m" (_eflags), "=m" ((_dst).val), \
294 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400295 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 break; \
297 case 4: \
298 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400299 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400301 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 : "=m" (_eflags), "=m" ((_dst).val), \
303 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400304 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 break; \
306 case 8: \
307 __emulate_2op_8byte(_op, _src, _dst, \
308 _eflags, _qx, _qy); \
309 break; \
310 } \
311 } while (0)
312
313#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
314 do { \
315 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400316 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 case 1: \
318 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400319 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400321 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 : "=m" (_eflags), "=m" ((_dst).val), \
323 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400324 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 break; \
326 default: \
327 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
328 _wx, _wy, _lx, _ly, _qx, _qy); \
329 break; \
330 } \
331 } while (0)
332
333/* Source operand is byte-sized and may be restricted to just %cl. */
334#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
335 __emulate_2op(_op, _src, _dst, _eflags, \
336 "b", "c", "b", "c", "b", "c", "b", "c")
337
338/* Source operand is byte, word, long or quad sized. */
339#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
340 __emulate_2op(_op, _src, _dst, _eflags, \
341 "b", "q", "w", "r", _LO32, "r", "", "r")
342
343/* Source operand is word, long or quad sized. */
344#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
345 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
346 "w", "r", _LO32, "r", "", "r")
347
348/* Instruction has only one explicit operand (no source operand). */
349#define emulate_1op(_op, _dst, _eflags) \
350 do { \
351 unsigned long _tmp; \
352 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400353 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 case 1: \
355 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400356 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400358 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 : "=m" (_eflags), "=m" ((_dst).val), \
360 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400361 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 break; \
363 case 2: \
364 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400365 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400367 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 : "=m" (_eflags), "=m" ((_dst).val), \
369 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400370 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 break; \
372 case 4: \
373 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400374 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400376 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 : "=m" (_eflags), "=m" ((_dst).val), \
378 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400379 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 break; \
381 case 8: \
382 __emulate_1op_8byte(_op, _dst, _eflags); \
383 break; \
384 } \
385 } while (0)
386
387/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800388#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
390 do { \
391 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400392 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400394 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400396 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 } while (0)
398
399#define __emulate_1op_8byte(_op, _dst, _eflags) \
400 do { \
401 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400402 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400404 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400406 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 } while (0)
408
409#elif defined(__i386__)
410#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
411#define __emulate_1op_8byte(_op, _dst, _eflags)
412#endif /* __i386__ */
413
414/* Fetch next part of the instruction being emulated. */
415#define insn_fetch(_type, _size, _eip) \
416({ unsigned long _x; \
417 rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
Mike Dayd77c26f2007-10-08 09:02:08 -0400418 (_size), ctxt->vcpu); \
419 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 goto done; \
421 (_eip) += (_size); \
422 (_type)_x; \
423})
424
425/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300426#define address_mask(reg) \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200427 ((c->ad_bytes == sizeof(unsigned long)) ? \
428 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300430 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431#define register_address_increment(reg, inc) \
432 do { \
433 /* signed type ensures sign extension to long */ \
434 int _inc = (inc); \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200435 if (c->ad_bytes == sizeof(unsigned long)) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 (reg) += _inc; \
437 else \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200438 (reg) = ((reg) & \
439 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
440 (((reg) + _inc) & \
441 ((1UL << (c->ad_bytes << 3)) - 1)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 } while (0)
443
Nitin A Kamble098c9372007-08-19 11:00:36 +0300444#define JMP_REL(rel) \
445 do { \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200446 register_address_increment(c->eip, rel); \
Nitin A Kamble098c9372007-08-19 11:00:36 +0300447 } while (0)
448
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000449/*
450 * Given the 'reg' portion of a ModRM byte, and a register block, return a
451 * pointer into the block that addresses the relevant register.
452 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
453 */
454static void *decode_register(u8 modrm_reg, unsigned long *regs,
455 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456{
457 void *p;
458
459 p = &regs[modrm_reg];
460 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
461 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
462 return p;
463}
464
465static int read_descriptor(struct x86_emulate_ctxt *ctxt,
466 struct x86_emulate_ops *ops,
467 void *ptr,
468 u16 *size, unsigned long *address, int op_bytes)
469{
470 int rc;
471
472 if (op_bytes == 2)
473 op_bytes = 3;
474 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300475 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
476 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 if (rc)
478 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300479 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
480 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 return rc;
482}
483
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300484static int test_cc(unsigned int condition, unsigned int flags)
485{
486 int rc = 0;
487
488 switch ((condition & 15) >> 1) {
489 case 0: /* o */
490 rc |= (flags & EFLG_OF);
491 break;
492 case 1: /* b/c/nae */
493 rc |= (flags & EFLG_CF);
494 break;
495 case 2: /* z/e */
496 rc |= (flags & EFLG_ZF);
497 break;
498 case 3: /* be/na */
499 rc |= (flags & (EFLG_CF|EFLG_ZF));
500 break;
501 case 4: /* s */
502 rc |= (flags & EFLG_SF);
503 break;
504 case 5: /* p/pe */
505 rc |= (flags & EFLG_PF);
506 break;
507 case 7: /* le/ng */
508 rc |= (flags & EFLG_ZF);
509 /* fall through */
510 case 6: /* l/nge */
511 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
512 break;
513 }
514
515 /* Odd condition identifiers (lsb == 1) have inverted sense. */
516 return (!!rc ^ (condition & 1));
517}
518
Avi Kivity3c118e22007-10-31 10:27:04 +0200519static void decode_register_operand(struct operand *op,
520 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200521 int inhibit_bytereg)
522{
Avi Kivity33615aa2007-10-31 11:15:56 +0200523 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200524 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200525
526 if (!(c->d & ModRM))
527 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200528 op->type = OP_REG;
529 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200530 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200531 op->val = *(u8 *)op->ptr;
532 op->bytes = 1;
533 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200534 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200535 op->bytes = c->op_bytes;
536 switch (op->bytes) {
537 case 2:
538 op->val = *(u16 *)op->ptr;
539 break;
540 case 4:
541 op->val = *(u32 *)op->ptr;
542 break;
543 case 8:
544 op->val = *(u64 *) op->ptr;
545 break;
546 }
547 }
548 op->orig_val = op->val;
549}
550
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200552x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200554 struct decode_cache *c = &ctxt->decode;
Avi Kivity33615aa2007-10-31 11:15:56 +0200555 u8 sib;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 int mode = ctxt->mode;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200558 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559
560 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
Laurent Viviere4e03de2007-09-18 11:52:50 +0200562 memset(c, 0, sizeof(struct decode_cache));
563 c->eip = ctxt->vcpu->rip;
564 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565
566 switch (mode) {
567 case X86EMUL_MODE_REAL:
568 case X86EMUL_MODE_PROT16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200569 c->op_bytes = c->ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 break;
571 case X86EMUL_MODE_PROT32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200572 c->op_bytes = c->ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800574#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 case X86EMUL_MODE_PROT64:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200576 c->op_bytes = 4;
577 c->ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 break;
579#endif
580 default:
581 return -1;
582 }
583
584 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200585 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200586 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 case 0x66: /* operand-size override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200588 c->op_bytes ^= 6; /* switch between 2/4 bytes */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 break;
590 case 0x67: /* address-size override */
591 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200592 /* switch between 4/8 bytes */
593 c->ad_bytes ^= 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200595 /* switch between 2/4 bytes */
596 c->ad_bytes ^= 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597 break;
598 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200599 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 break;
601 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200602 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603 break;
604 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200605 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 break;
607 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200608 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 break;
610 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200611 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 break;
613 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200614 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200616 case 0x40 ... 0x4f: /* REX */
617 if (mode != X86EMUL_MODE_PROT64)
618 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200619 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200620 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200622 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200624 case 0xf2: /* REPNE/REPNZ */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 case 0xf3: /* REP/REPE/REPZ */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200626 c->rep_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628 default:
629 goto done_prefixes;
630 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200631
632 /* Any legacy prefix after a REX prefix nullifies its effect. */
633
Avi Kivity33615aa2007-10-31 11:15:56 +0200634 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 }
636
637done_prefixes:
638
639 /* REX prefix. */
Avi Kivity33615aa2007-10-31 11:15:56 +0200640 if (c->rex_prefix) {
641 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200642 c->op_bytes = 8; /* REX.W */
Avi Kivity33615aa2007-10-31 11:15:56 +0200643 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
644 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
645 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 }
647
648 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200649 c->d = opcode_table[c->b];
650 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200652 if (c->b == 0x0f) {
653 c->twobyte = 1;
654 c->b = insn_fetch(u8, 1, c->eip);
655 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 }
657
658 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200659 if (c->d == 0) {
660 DPRINTF("Cannot emulate %02x\n", c->b);
661 return -1;
662 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 }
664
665 /* ModRM and SIB bytes. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200666 if (c->d & ModRM) {
667 c->modrm = insn_fetch(u8, 1, c->eip);
668 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
669 c->modrm_reg |= (c->modrm & 0x38) >> 3;
670 c->modrm_rm |= (c->modrm & 0x07);
671 c->modrm_ea = 0;
672 c->use_modrm_ea = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673
Laurent Viviere4e03de2007-09-18 11:52:50 +0200674 if (c->modrm_mod == 3) {
675 c->modrm_val = *(unsigned long *)
676 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 goto modrm_done;
678 }
679
Laurent Viviere4e03de2007-09-18 11:52:50 +0200680 if (c->ad_bytes == 2) {
681 unsigned bx = c->regs[VCPU_REGS_RBX];
682 unsigned bp = c->regs[VCPU_REGS_RBP];
683 unsigned si = c->regs[VCPU_REGS_RSI];
684 unsigned di = c->regs[VCPU_REGS_RDI];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685
686 /* 16-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200687 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200689 if (c->modrm_rm == 6)
690 c->modrm_ea +=
691 insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 break;
693 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200694 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 break;
696 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200697 c->modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 break;
699 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200700 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200702 c->modrm_ea += bx + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703 break;
704 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200705 c->modrm_ea += bx + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 break;
707 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200708 c->modrm_ea += bp + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 break;
710 case 3:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200711 c->modrm_ea += bp + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 break;
713 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200714 c->modrm_ea += si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 break;
716 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200717 c->modrm_ea += di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 break;
719 case 6:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200720 if (c->modrm_mod != 0)
721 c->modrm_ea += bp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 break;
723 case 7:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200724 c->modrm_ea += bx;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 break;
726 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200727 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
728 (c->modrm_rm == 6 && c->modrm_mod != 0))
729 if (!c->override_base)
730 c->override_base = &ctxt->ss_base;
731 c->modrm_ea = (u16)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 } else {
733 /* 32/64-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200734 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 case 4:
736 case 12:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200737 sib = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 index_reg |= (sib >> 3) & 7;
739 base_reg |= sib & 7;
740 scale = sib >> 6;
741
742 switch (base_reg) {
743 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200744 if (c->modrm_mod != 0)
745 c->modrm_ea +=
746 c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200748 c->modrm_ea +=
749 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 break;
751 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200752 c->modrm_ea += c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 }
754 switch (index_reg) {
755 case 4:
756 break;
757 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200758 c->modrm_ea +=
759 c->regs[index_reg] << scale;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760
761 }
762 break;
763 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200764 if (c->modrm_mod != 0)
765 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 else if (mode == X86EMUL_MODE_PROT64)
767 rip_relative = 1;
768 break;
769 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200770 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 break;
772 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200773 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200775 if (c->modrm_rm == 5)
776 c->modrm_ea +=
777 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 break;
779 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200780 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 break;
782 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200783 c->modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 break;
785 }
786 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 if (rip_relative) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200788 c->modrm_ea += c->eip;
789 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200791 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 break;
793 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200794 if (c->d & ByteOp)
795 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200797 if (c->op_bytes == 8)
798 c->modrm_ea += 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200800 c->modrm_ea += c->op_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 }
802 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400803modrm_done:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 ;
Avi Kivityc7e75a32007-10-28 16:34:25 +0200805 } else if (c->d & MemAbs) {
806 switch (c->ad_bytes) {
807 case 2:
808 c->modrm_ea = insn_fetch(u16, 2, c->eip);
809 break;
810 case 4:
811 c->modrm_ea = insn_fetch(u32, 4, c->eip);
812 break;
813 case 8:
814 c->modrm_ea = insn_fetch(u64, 8, c->eip);
815 break;
816 }
817
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 }
819
Avi Kivityc7e75a32007-10-28 16:34:25 +0200820 if (!c->override_base)
821 c->override_base = &ctxt->ds_base;
822 if (mode == X86EMUL_MODE_PROT64 &&
823 c->override_base != &ctxt->fs_base &&
824 c->override_base != &ctxt->gs_base)
825 c->override_base = NULL;
826
827 if (c->override_base)
828 c->modrm_ea += *c->override_base;
829
830 if (c->ad_bytes != 8)
831 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 /*
833 * Decode and fetch the source operand: register, memory
834 * or immediate.
835 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200836 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 case SrcNone:
838 break;
839 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200840 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 break;
842 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200843 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 goto srcmem_common;
845 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200846 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 goto srcmem_common;
848 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200849 c->src.bytes = (c->d & ByteOp) ? 1 :
850 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300851 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400852 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300853 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400854 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200855 /*
856 * For instructions with a ModR/M byte, switch to register
857 * access if Mod = 3.
858 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200859 if ((c->d & ModRM) && c->modrm_mod == 3) {
860 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200861 break;
862 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200863 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 break;
865 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200866 c->src.type = OP_IMM;
867 c->src.ptr = (unsigned long *)c->eip;
868 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
869 if (c->src.bytes == 8)
870 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200872 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200874 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
876 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200877 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 break;
879 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200880 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
882 }
883 break;
884 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200885 c->src.type = OP_IMM;
886 c->src.ptr = (unsigned long *)c->eip;
887 c->src.bytes = 1;
888 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 break;
890 }
891
Avi Kivity038e51d2007-01-22 20:40:40 -0800892 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800894 case ImplicitOps:
895 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200896 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800897 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200898 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200899 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -0800900 break;
901 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200902 if ((c->d & ModRM) && c->modrm_mod == 3) {
903 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200904 break;
905 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200906 c->dst.type = OP_MEM;
907 break;
908 }
909
910done:
911 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
912}
913
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200914static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
915{
916 struct decode_cache *c = &ctxt->decode;
917
918 c->dst.type = OP_MEM;
919 c->dst.bytes = c->op_bytes;
920 c->dst.val = c->src.val;
921 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
922 c->dst.ptr = (void *) register_address(ctxt->ss_base,
923 c->regs[VCPU_REGS_RSP]);
924}
925
926static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
927 struct x86_emulate_ops *ops)
928{
929 struct decode_cache *c = &ctxt->decode;
930 int rc;
931
932 /* 64-bit mode: POP always pops a 64-bit operand. */
933
934 if (ctxt->mode == X86EMUL_MODE_PROT64)
935 c->dst.bytes = 8;
936
937 rc = ops->read_std(register_address(ctxt->ss_base,
938 c->regs[VCPU_REGS_RSP]),
939 &c->dst.val, c->dst.bytes, ctxt->vcpu);
940 if (rc != 0)
941 return rc;
942
943 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
944
945 return 0;
946}
947
Laurent Vivier05f086f2007-09-24 11:10:55 +0200948static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200949{
Laurent Vivier05f086f2007-09-24 11:10:55 +0200950 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200951 switch (c->modrm_reg) {
952 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200953 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200954 break;
955 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200956 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200957 break;
958 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200959 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200960 break;
961 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200962 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200963 break;
964 case 4: /* sal/shl */
965 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200966 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200967 break;
968 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200969 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200970 break;
971 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200972 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200973 break;
974 }
975}
976
977static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +0200978 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200979{
980 struct decode_cache *c = &ctxt->decode;
981 int rc = 0;
982
983 switch (c->modrm_reg) {
984 case 0 ... 1: /* test */
985 /*
986 * Special case in Grp3: test has an immediate
987 * source operand.
988 */
989 c->src.type = OP_IMM;
990 c->src.ptr = (unsigned long *)c->eip;
991 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
992 if (c->src.bytes == 8)
993 c->src.bytes = 4;
994 switch (c->src.bytes) {
995 case 1:
996 c->src.val = insn_fetch(s8, 1, c->eip);
997 break;
998 case 2:
999 c->src.val = insn_fetch(s16, 2, c->eip);
1000 break;
1001 case 4:
1002 c->src.val = insn_fetch(s32, 4, c->eip);
1003 break;
1004 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001005 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001006 break;
1007 case 2: /* not */
1008 c->dst.val = ~c->dst.val;
1009 break;
1010 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001011 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001012 break;
1013 default:
1014 DPRINTF("Cannot emulate %02x\n", c->b);
1015 rc = X86EMUL_UNHANDLEABLE;
1016 break;
1017 }
1018done:
1019 return rc;
1020}
1021
1022static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001023 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001024{
1025 struct decode_cache *c = &ctxt->decode;
1026 int rc;
1027
1028 switch (c->modrm_reg) {
1029 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001030 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001031 break;
1032 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001033 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001034 break;
1035 case 4: /* jmp abs */
1036 if (c->b == 0xff)
1037 c->eip = c->dst.val;
1038 else {
1039 DPRINTF("Cannot emulate %02x\n", c->b);
1040 return X86EMUL_UNHANDLEABLE;
1041 }
1042 break;
1043 case 6: /* push */
1044
1045 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1046
1047 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1048 c->dst.bytes = 8;
1049 rc = ops->read_std((unsigned long)c->dst.ptr,
1050 &c->dst.val, 8, ctxt->vcpu);
1051 if (rc != 0)
1052 return rc;
1053 }
1054 register_address_increment(c->regs[VCPU_REGS_RSP],
1055 -c->dst.bytes);
1056 rc = ops->write_emulated(register_address(ctxt->ss_base,
1057 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1058 c->dst.bytes, ctxt->vcpu);
1059 if (rc != 0)
1060 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001061 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001062 break;
1063 default:
1064 DPRINTF("Cannot emulate %02x\n", c->b);
1065 return X86EMUL_UNHANDLEABLE;
1066 }
1067 return 0;
1068}
1069
1070static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1071 struct x86_emulate_ops *ops,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001072 unsigned long cr2)
1073{
1074 struct decode_cache *c = &ctxt->decode;
1075 u64 old, new;
1076 int rc;
1077
1078 rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu);
1079 if (rc != 0)
1080 return rc;
1081
1082 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1083 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1084
1085 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1086 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001087 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001088
1089 } else {
1090 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1091 (u32) c->regs[VCPU_REGS_RBX];
1092
1093 rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu);
1094 if (rc != 0)
1095 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001096 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001097 }
1098 return 0;
1099}
1100
1101static inline int writeback(struct x86_emulate_ctxt *ctxt,
1102 struct x86_emulate_ops *ops)
1103{
1104 int rc;
1105 struct decode_cache *c = &ctxt->decode;
1106
1107 switch (c->dst.type) {
1108 case OP_REG:
1109 /* The 4-byte case *is* correct:
1110 * in 64-bit mode we zero-extend.
1111 */
1112 switch (c->dst.bytes) {
1113 case 1:
1114 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1115 break;
1116 case 2:
1117 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1118 break;
1119 case 4:
1120 *c->dst.ptr = (u32)c->dst.val;
1121 break; /* 64b: zero-ext */
1122 case 8:
1123 *c->dst.ptr = c->dst.val;
1124 break;
1125 }
1126 break;
1127 case OP_MEM:
1128 if (c->lock_prefix)
1129 rc = ops->cmpxchg_emulated(
1130 (unsigned long)c->dst.ptr,
1131 &c->dst.orig_val,
1132 &c->dst.val,
1133 c->dst.bytes,
1134 ctxt->vcpu);
1135 else
1136 rc = ops->write_emulated(
1137 (unsigned long)c->dst.ptr,
1138 &c->dst.val,
1139 c->dst.bytes,
1140 ctxt->vcpu);
1141 if (rc != 0)
1142 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001143 break;
1144 case OP_NONE:
1145 /* no writeback */
1146 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001147 default:
1148 break;
1149 }
1150 return 0;
1151}
1152
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001153int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001154x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001155{
1156 unsigned long cr2 = ctxt->cr2;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001157 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001158 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001159 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001160 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001161
Laurent Vivier34273182007-09-18 11:27:37 +02001162 /* Shadow copy of register state. Committed on successful emulation.
1163 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1164 * modify them.
1165 */
1166
1167 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1168 saved_eip = c->eip;
1169
Avi Kivityc7e75a32007-10-28 16:34:25 +02001170 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001171 cr2 = c->modrm_ea;
1172
1173 if (c->src.type == OP_MEM) {
1174 c->src.ptr = (unsigned long *)cr2;
1175 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001176 rc = ops->read_emulated((unsigned long)c->src.ptr,
1177 &c->src.val,
1178 c->src.bytes,
1179 ctxt->vcpu);
1180 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001181 goto done;
1182 c->src.orig_val = c->src.val;
1183 }
1184
1185 if ((c->d & DstMask) == ImplicitOps)
1186 goto special_insn;
1187
1188
1189 if (c->dst.type == OP_MEM) {
1190 c->dst.ptr = (unsigned long *)cr2;
1191 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1192 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001193 if (c->d & BitOp) {
1194 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001195
Laurent Viviere4e03de2007-09-18 11:52:50 +02001196 c->dst.ptr = (void *)c->dst.ptr +
1197 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001198 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001199 if (!(c->d & Mov) &&
1200 /* optimisation - avoid slow emulated read */
1201 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1202 &c->dst.val,
1203 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001204 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001205 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001206 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001207
Laurent Viviere4e03de2007-09-18 11:52:50 +02001208 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 goto twobyte_insn;
1210
Laurent Viviere4e03de2007-09-18 11:52:50 +02001211 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 case 0x00 ... 0x05:
1213 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001214 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 break;
1216 case 0x08 ... 0x0d:
1217 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001218 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 break;
1220 case 0x10 ... 0x15:
1221 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001222 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 break;
1224 case 0x18 ... 0x1d:
1225 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001226 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001228 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001230 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001232 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001233 c->dst.type = OP_REG;
1234 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1235 c->dst.val = *(u8 *)c->dst.ptr;
1236 c->dst.bytes = 1;
1237 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001238 goto and;
1239 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001240 c->dst.type = OP_REG;
1241 c->dst.bytes = c->op_bytes;
1242 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1243 if (c->op_bytes == 2)
1244 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001245 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001246 c->dst.val = *(u32 *)c->dst.ptr;
1247 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001248 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 case 0x28 ... 0x2d:
1250 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001251 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 break;
1253 case 0x30 ... 0x35:
1254 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001255 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 break;
1257 case 0x38 ... 0x3d:
1258 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001259 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001261 case 0x40 ... 0x47: /* inc r16/r32 */
1262 emulate_1op("inc", c->dst, ctxt->eflags);
1263 break;
1264 case 0x48 ... 0x4f: /* dec r16/r32 */
1265 emulate_1op("dec", c->dst, ctxt->eflags);
1266 break;
1267 case 0x50 ... 0x57: /* push reg */
1268 c->dst.type = OP_MEM;
1269 c->dst.bytes = c->op_bytes;
1270 c->dst.val = c->src.val;
1271 register_address_increment(c->regs[VCPU_REGS_RSP],
1272 -c->op_bytes);
1273 c->dst.ptr = (void *) register_address(
1274 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1275 break;
1276 case 0x58 ... 0x5f: /* pop reg */
1277 pop_instruction:
1278 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1279 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1280 c->op_bytes, ctxt->vcpu)) != 0)
1281 goto done;
1282
1283 register_address_increment(c->regs[VCPU_REGS_RSP],
1284 c->op_bytes);
1285 c->dst.type = OP_NONE; /* Disable writeback. */
1286 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001288 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001290 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 break;
1292 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001293 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 case 0:
1295 goto add;
1296 case 1:
1297 goto or;
1298 case 2:
1299 goto adc;
1300 case 3:
1301 goto sbb;
1302 case 4:
1303 goto and;
1304 case 5:
1305 goto sub;
1306 case 6:
1307 goto xor;
1308 case 7:
1309 goto cmp;
1310 }
1311 break;
1312 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001313 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 break;
1315 case 0x86 ... 0x87: /* xchg */
1316 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001317 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001319 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 break;
1321 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001322 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 break;
1324 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001325 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 break; /* 64b reg: zero-extend */
1327 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001328 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329 break;
1330 }
1331 /*
1332 * Write back the memory destination with implicit LOCK
1333 * prefix.
1334 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001335 c->dst.val = c->src.val;
1336 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001339 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001340 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001344 rc = emulate_grp1a(ctxt, ops);
1345 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001348 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001349 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1350 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001351 break;
1352 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001353 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001354 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 case 0xc0 ... 0xc1:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001356 emulate_grp2(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001358 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1359 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001360 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001361 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001363 c->src.val = 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001364 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001367 c->src.val = c->regs[VCPU_REGS_RCX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001368 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001369 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001371 rc = emulate_grp3(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001372 if (rc != 0)
1373 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 break;
1375 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviera01af5e2007-09-24 11:10:56 +02001376 rc = emulate_grp45(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377 if (rc != 0)
1378 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 break;
1380 }
1381
1382writeback:
Laurent Viviera01af5e2007-09-24 11:10:56 +02001383 rc = writeback(ctxt, ops);
1384 if (rc != 0)
1385 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386
1387 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001388 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001389 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390
1391done:
Laurent Vivier34273182007-09-18 11:27:37 +02001392 if (rc == X86EMUL_UNHANDLEABLE) {
1393 c->eip = saved_eip;
1394 return -1;
1395 }
1396 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397
1398special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001399 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001401 switch (c->b) {
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001402 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001403 c->src.val = 0L;
1404 c->src.val = insn_fetch(s8, 1, c->eip);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001405 emulate_push(ctxt);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001406 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001407 case 0x6c: /* insb */
1408 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001409 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001410 1,
1411 (c->d & ByteOp) ? 1 : c->op_bytes,
1412 c->rep_prefix ?
1413 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001414 (ctxt->eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001415 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001416 c->regs[VCPU_REGS_RDI]),
1417 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001418 c->regs[VCPU_REGS_RDX]) == 0) {
1419 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001420 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001421 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001422 return 0;
1423 case 0x6e: /* outsb */
1424 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001425 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001426 0,
1427 (c->d & ByteOp) ? 1 : c->op_bytes,
1428 c->rep_prefix ?
1429 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001430 (ctxt->eflags & EFLG_DF),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001431 register_address(c->override_base ?
1432 *c->override_base :
1433 ctxt->ds_base,
1434 c->regs[VCPU_REGS_RSI]),
1435 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001436 c->regs[VCPU_REGS_RDX]) == 0) {
1437 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001438 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001439 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001440 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001441 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001442 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001443
Laurent Vivier05f086f2007-09-24 11:10:55 +02001444 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001445 JMP_REL(rel);
1446 break;
1447 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001448 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001449 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450 emulate_push(ctxt);
1451 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001452 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001453 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001454 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001455 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001456 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001457 goto pop_instruction;
1458 case 0xf4: /* hlt */
1459 ctxt->vcpu->halt_request = 1;
1460 goto done;
Nitin A Kambleb284be52007-10-16 18:23:27 -07001461 case 0xf5: /* cmc */
1462 /* complement carry flag from eflags reg */
1463 ctxt->eflags ^= EFLG_CF;
1464 c->dst.type = OP_NONE; /* Disable writeback. */
1465 break;
1466 case 0xf8: /* clc */
1467 ctxt->eflags &= ~EFLG_CF;
1468 c->dst.type = OP_NONE; /* Disable writeback. */
1469 break;
1470 case 0xfa: /* cli */
1471 ctxt->eflags &= ~X86_EFLAGS_IF;
1472 c->dst.type = OP_NONE; /* Disable writeback. */
1473 break;
1474 case 0xfb: /* sti */
1475 ctxt->eflags |= X86_EFLAGS_IF;
1476 c->dst.type = OP_NONE; /* Disable writeback. */
1477 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001478 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001479 if (c->rep_prefix) {
1480 if (c->regs[VCPU_REGS_RCX] == 0) {
1481 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 goto done;
1483 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001484 c->regs[VCPU_REGS_RCX]--;
1485 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001487 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001489 c->dst.type = OP_MEM;
1490 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1491 c->dst.ptr = (unsigned long *)register_address(
1492 ctxt->es_base,
1493 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001495 c->override_base ? *c->override_base :
1496 ctxt->ds_base,
1497 c->regs[VCPU_REGS_RSI]),
1498 &c->dst.val,
1499 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001501 register_address_increment(c->regs[VCPU_REGS_RSI],
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);
1504 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001505 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 break;
1508 case 0xa6 ... 0xa7: /* cmps */
1509 DPRINTF("Urk! I don't handle CMPS.\n");
1510 goto cannot_emulate;
1511 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001512 c->dst.type = OP_MEM;
1513 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1514 c->dst.ptr = (unsigned long *)cr2;
1515 c->dst.val = c->regs[VCPU_REGS_RAX];
1516 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001517 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001518 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 break;
1520 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001521 c->dst.type = OP_REG;
1522 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1523 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1524 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1525 c->dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001526 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001528 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001529 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001530 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 break;
1532 case 0xae ... 0xaf: /* scas */
1533 DPRINTF("Urk! I don't handle SCAS.\n");
1534 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001535 case 0xe8: /* call (near) */ {
1536 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001537 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001538 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001539 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001540 break;
1541 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001542 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001543 break;
1544 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001545 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001546 break;
1547 default:
1548 DPRINTF("Call: Invalid op_bytes\n");
1549 goto cannot_emulate;
1550 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001551 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001552 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001553 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001554 emulate_push(ctxt);
1555 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001556 }
1557 case 0xe9: /* jmp rel */
1558 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001559 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001560 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001561 break;
1562
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001563
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 }
1565 goto writeback;
1566
1567twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001568 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001570 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 u16 size;
1572 unsigned long address;
1573
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001574 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001576 goto cannot_emulate;
1577
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001578 rc = kvm_fix_hypercall(ctxt->vcpu);
1579 if (rc)
1580 goto done;
1581
1582 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 rc = read_descriptor(ctxt, ops, c->src.ptr,
1586 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 if (rc)
1588 goto done;
1589 realmode_lgdt(ctxt->vcpu, size, address);
1590 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001591 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001592 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001593 rc = kvm_fix_hypercall(ctxt->vcpu);
1594 if (rc)
1595 goto done;
1596 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001597 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001598 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001599 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001600 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001601 if (rc)
1602 goto done;
1603 realmode_lidt(ctxt->vcpu, size, address);
1604 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605 break;
1606 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001607 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 = realmode_get_cr(ctxt->vcpu, 0);
1611 break;
1612 case 6: /* lmsw */
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 Vivier05f086f2007-09-24 11:10:55 +02001615 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1616 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 break;
1618 case 7: /* invlpg*/
1619 emulate_invlpg(ctxt->vcpu, cr2);
1620 break;
1621 default:
1622 goto cannot_emulate;
1623 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001624 /* Disable writeback. */
1625 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 break;
1627 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001628 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001630 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001631 if (rc)
1632 goto cannot_emulate;
1633 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 break;
1635 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001636 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001638 rc = emulator_set_dr(ctxt, c->modrm_reg,
1639 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001640 if (rc)
1641 goto cannot_emulate;
1642 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 break;
1644 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001645 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001646 if (!test_cc(c->b, ctxt->eflags))
1647 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001649 case 0xa3:
1650 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001651 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001652 /* only subword offset */
1653 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001654 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001655 break;
1656 case 0xab:
1657 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001658 /* only subword offset */
1659 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001660 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001661 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 case 0xb0 ... 0xb1: /* cmpxchg */
1663 /*
1664 * Save real source value, then compare EAX against
1665 * destination.
1666 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001667 c->src.orig_val = c->src.val;
1668 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001669 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1670 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001672 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 } else {
1674 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001675 c->dst.type = OP_REG;
1676 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 }
1678 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 case 0xb3:
1680 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001681 /* only subword offset */
1682 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001683 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001686 c->dst.bytes = c->op_bytes;
1687 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1688 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001691 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 case 0:
1693 goto bt;
1694 case 1:
1695 goto bts;
1696 case 2:
1697 goto btr;
1698 case 3:
1699 goto btc;
1700 }
1701 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001702 case 0xbb:
1703 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001704 /* only subword offset */
1705 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001706 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001707 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001709 c->dst.bytes = c->op_bytes;
1710 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1711 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001713 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001714 c->dst.bytes = c->op_bytes;
1715 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1716 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001717 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 }
1719 goto writeback;
1720
1721twobyte_special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001722 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001723 case 0x06:
1724 emulate_clts(ctxt->vcpu);
1725 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001726 case 0x08: /* invd */
1727 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001728 case 0x09: /* wbinvd */
1729 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 case 0x0d: /* GrpP (prefetch) */
1731 case 0x18: /* Grp16 (prefetch/nop) */
1732 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 case 0x20: /* mov cr, reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001734 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001736 c->regs[c->modrm_rm] =
1737 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001738 break;
1739 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001740 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001742 realmode_set_cr(ctxt->vcpu,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001743 c->modrm_reg, c->modrm_val, &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001745 case 0x30:
1746 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001747 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1748 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1749 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001750 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001751 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001752 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001753 }
1754 rc = X86EMUL_CONTINUE;
1755 break;
1756 case 0x32:
1757 /* rdmsr */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001758 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001759 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001760 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001761 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001762 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001763 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1764 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001765 }
1766 rc = X86EMUL_CONTINUE;
1767 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001768 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1769 long int rel;
1770
Laurent Viviere4e03de2007-09-18 11:52:50 +02001771 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001772 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001773 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001774 break;
1775 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001776 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001777 break;
1778 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001779 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001780 break;
1781 default:
1782 DPRINTF("jnz: Invalid op_bytes\n");
1783 goto cannot_emulate;
1784 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001785 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001786 JMP_REL(rel);
1787 break;
1788 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 case 0xc7: /* Grp9 (cmpxchg8b) */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001790 rc = emulate_grp9(ctxt, ops, cr2);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 if (rc != 0)
1792 goto done;
1793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001795 /* Disable writeback. */
1796 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 goto writeback;
1798
1799cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001800 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001801 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 return -1;
1803}