blob: 64888a6edc275e9011ad09d739b5ad008b9b9b26 [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 Kivity1c73ef6652007-11-01 06:31:28 +0200551static int decode_modrm(struct x86_emulate_ctxt *ctxt,
552 struct x86_emulate_ops *ops)
553{
554 struct decode_cache *c = &ctxt->decode;
555 u8 sib;
556 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
557 int rc = 0;
558
559 if (c->rex_prefix) {
560 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
561 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
562 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
563 }
564
565 c->modrm = insn_fetch(u8, 1, c->eip);
566 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
567 c->modrm_reg |= (c->modrm & 0x38) >> 3;
568 c->modrm_rm |= (c->modrm & 0x07);
569 c->modrm_ea = 0;
570 c->use_modrm_ea = 1;
571
572 if (c->modrm_mod == 3) {
573 c->modrm_val = *(unsigned long *)
574 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
575 return rc;
576 }
577
578 if (c->ad_bytes == 2) {
579 unsigned bx = c->regs[VCPU_REGS_RBX];
580 unsigned bp = c->regs[VCPU_REGS_RBP];
581 unsigned si = c->regs[VCPU_REGS_RSI];
582 unsigned di = c->regs[VCPU_REGS_RDI];
583
584 /* 16-bit ModR/M decode. */
585 switch (c->modrm_mod) {
586 case 0:
587 if (c->modrm_rm == 6)
588 c->modrm_ea += insn_fetch(u16, 2, c->eip);
589 break;
590 case 1:
591 c->modrm_ea += insn_fetch(s8, 1, c->eip);
592 break;
593 case 2:
594 c->modrm_ea += insn_fetch(u16, 2, c->eip);
595 break;
596 }
597 switch (c->modrm_rm) {
598 case 0:
599 c->modrm_ea += bx + si;
600 break;
601 case 1:
602 c->modrm_ea += bx + di;
603 break;
604 case 2:
605 c->modrm_ea += bp + si;
606 break;
607 case 3:
608 c->modrm_ea += bp + di;
609 break;
610 case 4:
611 c->modrm_ea += si;
612 break;
613 case 5:
614 c->modrm_ea += di;
615 break;
616 case 6:
617 if (c->modrm_mod != 0)
618 c->modrm_ea += bp;
619 break;
620 case 7:
621 c->modrm_ea += bx;
622 break;
623 }
624 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
625 (c->modrm_rm == 6 && c->modrm_mod != 0))
626 if (!c->override_base)
627 c->override_base = &ctxt->ss_base;
628 c->modrm_ea = (u16)c->modrm_ea;
629 } else {
630 /* 32/64-bit ModR/M decode. */
631 switch (c->modrm_rm) {
632 case 4:
633 case 12:
634 sib = insn_fetch(u8, 1, c->eip);
635 index_reg |= (sib >> 3) & 7;
636 base_reg |= sib & 7;
637 scale = sib >> 6;
638
639 switch (base_reg) {
640 case 5:
641 if (c->modrm_mod != 0)
642 c->modrm_ea += c->regs[base_reg];
643 else
644 c->modrm_ea +=
645 insn_fetch(s32, 4, c->eip);
646 break;
647 default:
648 c->modrm_ea += c->regs[base_reg];
649 }
650 switch (index_reg) {
651 case 4:
652 break;
653 default:
654 c->modrm_ea += c->regs[index_reg] << scale;
655 }
656 break;
657 case 5:
658 if (c->modrm_mod != 0)
659 c->modrm_ea += c->regs[c->modrm_rm];
660 else if (ctxt->mode == X86EMUL_MODE_PROT64)
661 rip_relative = 1;
662 break;
663 default:
664 c->modrm_ea += c->regs[c->modrm_rm];
665 break;
666 }
667 switch (c->modrm_mod) {
668 case 0:
669 if (c->modrm_rm == 5)
670 c->modrm_ea += insn_fetch(s32, 4, c->eip);
671 break;
672 case 1:
673 c->modrm_ea += insn_fetch(s8, 1, c->eip);
674 break;
675 case 2:
676 c->modrm_ea += insn_fetch(s32, 4, c->eip);
677 break;
678 }
679 }
680 if (rip_relative) {
681 c->modrm_ea += c->eip;
682 switch (c->d & SrcMask) {
683 case SrcImmByte:
684 c->modrm_ea += 1;
685 break;
686 case SrcImm:
687 if (c->d & ByteOp)
688 c->modrm_ea += 1;
689 else
690 if (c->op_bytes == 8)
691 c->modrm_ea += 4;
692 else
693 c->modrm_ea += c->op_bytes;
694 }
695 }
696done:
697 return rc;
698}
699
700static int decode_abs(struct x86_emulate_ctxt *ctxt,
701 struct x86_emulate_ops *ops)
702{
703 struct decode_cache *c = &ctxt->decode;
704 int rc = 0;
705
706 switch (c->ad_bytes) {
707 case 2:
708 c->modrm_ea = insn_fetch(u16, 2, c->eip);
709 break;
710 case 4:
711 c->modrm_ea = insn_fetch(u32, 4, c->eip);
712 break;
713 case 8:
714 c->modrm_ea = insn_fetch(u64, 8, c->eip);
715 break;
716 }
717done:
718 return rc;
719}
720
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200722x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200724 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 int mode = ctxt->mode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727
728 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Laurent Viviere4e03de2007-09-18 11:52:50 +0200730 memset(c, 0, sizeof(struct decode_cache));
731 c->eip = ctxt->vcpu->rip;
732 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
734 switch (mode) {
735 case X86EMUL_MODE_REAL:
736 case X86EMUL_MODE_PROT16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200737 c->op_bytes = c->ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 break;
739 case X86EMUL_MODE_PROT32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200740 c->op_bytes = c->ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800742#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 case X86EMUL_MODE_PROT64:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200744 c->op_bytes = 4;
745 c->ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 break;
747#endif
748 default:
749 return -1;
750 }
751
752 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200753 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200754 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 case 0x66: /* operand-size override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200756 c->op_bytes ^= 6; /* switch between 2/4 bytes */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 break;
758 case 0x67: /* address-size override */
759 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200760 /* switch between 4/8 bytes */
761 c->ad_bytes ^= 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200763 /* switch between 2/4 bytes */
764 c->ad_bytes ^= 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 break;
766 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200767 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 break;
769 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200770 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 break;
772 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200773 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 break;
775 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200776 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 break;
778 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200779 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 break;
781 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200782 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200784 case 0x40 ... 0x4f: /* REX */
785 if (mode != X86EMUL_MODE_PROT64)
786 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200787 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200788 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200790 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200792 case 0xf2: /* REPNE/REPNZ */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 case 0xf3: /* REP/REPE/REPZ */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200794 c->rep_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 default:
797 goto done_prefixes;
798 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200799
800 /* Any legacy prefix after a REX prefix nullifies its effect. */
801
Avi Kivity33615aa2007-10-31 11:15:56 +0200802 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 }
804
805done_prefixes:
806
807 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200809 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200810 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
812 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200813 c->d = opcode_table[c->b];
814 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200816 if (c->b == 0x0f) {
817 c->twobyte = 1;
818 c->b = insn_fetch(u8, 1, c->eip);
819 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 }
821
822 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200823 if (c->d == 0) {
824 DPRINTF("Cannot emulate %02x\n", c->b);
825 return -1;
826 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 }
828
829 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200830 if (c->d & ModRM)
831 rc = decode_modrm(ctxt, ops);
832 else if (c->d & MemAbs)
833 rc = decode_abs(ctxt, ops);
834 if (rc)
835 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836
Avi Kivityc7e75a32007-10-28 16:34:25 +0200837 if (!c->override_base)
838 c->override_base = &ctxt->ds_base;
839 if (mode == X86EMUL_MODE_PROT64 &&
840 c->override_base != &ctxt->fs_base &&
841 c->override_base != &ctxt->gs_base)
842 c->override_base = NULL;
843
844 if (c->override_base)
845 c->modrm_ea += *c->override_base;
846
847 if (c->ad_bytes != 8)
848 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 /*
850 * Decode and fetch the source operand: register, memory
851 * or immediate.
852 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200853 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 case SrcNone:
855 break;
856 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200857 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 break;
859 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200860 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 goto srcmem_common;
862 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200863 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 goto srcmem_common;
865 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200866 c->src.bytes = (c->d & ByteOp) ? 1 :
867 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300868 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400869 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300870 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400871 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200872 /*
873 * For instructions with a ModR/M byte, switch to register
874 * access if Mod = 3.
875 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200876 if ((c->d & ModRM) && c->modrm_mod == 3) {
877 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200878 break;
879 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200880 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
882 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200883 c->src.type = OP_IMM;
884 c->src.ptr = (unsigned long *)c->eip;
885 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
886 if (c->src.bytes == 8)
887 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200891 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 break;
893 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200894 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 break;
896 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200897 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 break;
899 }
900 break;
901 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200902 c->src.type = OP_IMM;
903 c->src.ptr = (unsigned long *)c->eip;
904 c->src.bytes = 1;
905 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 break;
907 }
908
Avi Kivity038e51d2007-01-22 20:40:40 -0800909 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200910 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800911 case ImplicitOps:
912 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200913 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800914 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200915 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200916 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -0800917 break;
918 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200919 if ((c->d & ModRM) && c->modrm_mod == 3) {
920 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200921 break;
922 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200923 c->dst.type = OP_MEM;
924 break;
925 }
926
927done:
928 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
929}
930
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200931static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
932{
933 struct decode_cache *c = &ctxt->decode;
934
935 c->dst.type = OP_MEM;
936 c->dst.bytes = c->op_bytes;
937 c->dst.val = c->src.val;
938 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
939 c->dst.ptr = (void *) register_address(ctxt->ss_base,
940 c->regs[VCPU_REGS_RSP]);
941}
942
943static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
944 struct x86_emulate_ops *ops)
945{
946 struct decode_cache *c = &ctxt->decode;
947 int rc;
948
949 /* 64-bit mode: POP always pops a 64-bit operand. */
950
951 if (ctxt->mode == X86EMUL_MODE_PROT64)
952 c->dst.bytes = 8;
953
954 rc = ops->read_std(register_address(ctxt->ss_base,
955 c->regs[VCPU_REGS_RSP]),
956 &c->dst.val, c->dst.bytes, ctxt->vcpu);
957 if (rc != 0)
958 return rc;
959
960 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
961
962 return 0;
963}
964
Laurent Vivier05f086f2007-09-24 11:10:55 +0200965static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200966{
Laurent Vivier05f086f2007-09-24 11:10:55 +0200967 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200968 switch (c->modrm_reg) {
969 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200970 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200971 break;
972 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200973 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200974 break;
975 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200976 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200977 break;
978 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200979 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200980 break;
981 case 4: /* sal/shl */
982 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200983 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200984 break;
985 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200986 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200987 break;
988 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200989 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200990 break;
991 }
992}
993
994static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +0200995 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200996{
997 struct decode_cache *c = &ctxt->decode;
998 int rc = 0;
999
1000 switch (c->modrm_reg) {
1001 case 0 ... 1: /* test */
1002 /*
1003 * Special case in Grp3: test has an immediate
1004 * source operand.
1005 */
1006 c->src.type = OP_IMM;
1007 c->src.ptr = (unsigned long *)c->eip;
1008 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1009 if (c->src.bytes == 8)
1010 c->src.bytes = 4;
1011 switch (c->src.bytes) {
1012 case 1:
1013 c->src.val = insn_fetch(s8, 1, c->eip);
1014 break;
1015 case 2:
1016 c->src.val = insn_fetch(s16, 2, c->eip);
1017 break;
1018 case 4:
1019 c->src.val = insn_fetch(s32, 4, c->eip);
1020 break;
1021 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001022 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001023 break;
1024 case 2: /* not */
1025 c->dst.val = ~c->dst.val;
1026 break;
1027 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001028 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001029 break;
1030 default:
1031 DPRINTF("Cannot emulate %02x\n", c->b);
1032 rc = X86EMUL_UNHANDLEABLE;
1033 break;
1034 }
1035done:
1036 return rc;
1037}
1038
1039static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001040 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001041{
1042 struct decode_cache *c = &ctxt->decode;
1043 int rc;
1044
1045 switch (c->modrm_reg) {
1046 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001047 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001048 break;
1049 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001050 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001051 break;
1052 case 4: /* jmp abs */
1053 if (c->b == 0xff)
1054 c->eip = c->dst.val;
1055 else {
1056 DPRINTF("Cannot emulate %02x\n", c->b);
1057 return X86EMUL_UNHANDLEABLE;
1058 }
1059 break;
1060 case 6: /* push */
1061
1062 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1063
1064 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1065 c->dst.bytes = 8;
1066 rc = ops->read_std((unsigned long)c->dst.ptr,
1067 &c->dst.val, 8, ctxt->vcpu);
1068 if (rc != 0)
1069 return rc;
1070 }
1071 register_address_increment(c->regs[VCPU_REGS_RSP],
1072 -c->dst.bytes);
1073 rc = ops->write_emulated(register_address(ctxt->ss_base,
1074 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1075 c->dst.bytes, ctxt->vcpu);
1076 if (rc != 0)
1077 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001078 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001079 break;
1080 default:
1081 DPRINTF("Cannot emulate %02x\n", c->b);
1082 return X86EMUL_UNHANDLEABLE;
1083 }
1084 return 0;
1085}
1086
1087static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1088 struct x86_emulate_ops *ops,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001089 unsigned long cr2)
1090{
1091 struct decode_cache *c = &ctxt->decode;
1092 u64 old, new;
1093 int rc;
1094
1095 rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu);
1096 if (rc != 0)
1097 return rc;
1098
1099 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1100 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1101
1102 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1103 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001104 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001105
1106 } else {
1107 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1108 (u32) c->regs[VCPU_REGS_RBX];
1109
1110 rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu);
1111 if (rc != 0)
1112 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001113 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001114 }
1115 return 0;
1116}
1117
1118static inline int writeback(struct x86_emulate_ctxt *ctxt,
1119 struct x86_emulate_ops *ops)
1120{
1121 int rc;
1122 struct decode_cache *c = &ctxt->decode;
1123
1124 switch (c->dst.type) {
1125 case OP_REG:
1126 /* The 4-byte case *is* correct:
1127 * in 64-bit mode we zero-extend.
1128 */
1129 switch (c->dst.bytes) {
1130 case 1:
1131 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1132 break;
1133 case 2:
1134 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1135 break;
1136 case 4:
1137 *c->dst.ptr = (u32)c->dst.val;
1138 break; /* 64b: zero-ext */
1139 case 8:
1140 *c->dst.ptr = c->dst.val;
1141 break;
1142 }
1143 break;
1144 case OP_MEM:
1145 if (c->lock_prefix)
1146 rc = ops->cmpxchg_emulated(
1147 (unsigned long)c->dst.ptr,
1148 &c->dst.orig_val,
1149 &c->dst.val,
1150 c->dst.bytes,
1151 ctxt->vcpu);
1152 else
1153 rc = ops->write_emulated(
1154 (unsigned long)c->dst.ptr,
1155 &c->dst.val,
1156 c->dst.bytes,
1157 ctxt->vcpu);
1158 if (rc != 0)
1159 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001160 break;
1161 case OP_NONE:
1162 /* no writeback */
1163 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164 default:
1165 break;
1166 }
1167 return 0;
1168}
1169
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001170int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001171x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001172{
1173 unsigned long cr2 = ctxt->cr2;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001174 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001175 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001176 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001177 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001178
Laurent Vivier34273182007-09-18 11:27:37 +02001179 /* Shadow copy of register state. Committed on successful emulation.
1180 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1181 * modify them.
1182 */
1183
1184 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1185 saved_eip = c->eip;
1186
Avi Kivityc7e75a32007-10-28 16:34:25 +02001187 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001188 cr2 = c->modrm_ea;
1189
1190 if (c->src.type == OP_MEM) {
1191 c->src.ptr = (unsigned long *)cr2;
1192 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001193 rc = ops->read_emulated((unsigned long)c->src.ptr,
1194 &c->src.val,
1195 c->src.bytes,
1196 ctxt->vcpu);
1197 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001198 goto done;
1199 c->src.orig_val = c->src.val;
1200 }
1201
1202 if ((c->d & DstMask) == ImplicitOps)
1203 goto special_insn;
1204
1205
1206 if (c->dst.type == OP_MEM) {
1207 c->dst.ptr = (unsigned long *)cr2;
1208 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1209 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001210 if (c->d & BitOp) {
1211 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001212
Laurent Viviere4e03de2007-09-18 11:52:50 +02001213 c->dst.ptr = (void *)c->dst.ptr +
1214 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001215 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001216 if (!(c->d & Mov) &&
1217 /* optimisation - avoid slow emulated read */
1218 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1219 &c->dst.val,
1220 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001221 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001222 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001223 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001224
Laurent Viviere4e03de2007-09-18 11:52:50 +02001225 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 goto twobyte_insn;
1227
Laurent Viviere4e03de2007-09-18 11:52:50 +02001228 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 case 0x00 ... 0x05:
1230 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001231 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 break;
1233 case 0x08 ... 0x0d:
1234 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001235 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 break;
1237 case 0x10 ... 0x15:
1238 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001239 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 break;
1241 case 0x18 ... 0x1d:
1242 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001243 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001245 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001247 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001249 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001250 c->dst.type = OP_REG;
1251 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1252 c->dst.val = *(u8 *)c->dst.ptr;
1253 c->dst.bytes = 1;
1254 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001255 goto and;
1256 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001257 c->dst.type = OP_REG;
1258 c->dst.bytes = c->op_bytes;
1259 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1260 if (c->op_bytes == 2)
1261 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001262 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001263 c->dst.val = *(u32 *)c->dst.ptr;
1264 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001265 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 case 0x28 ... 0x2d:
1267 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001268 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 break;
1270 case 0x30 ... 0x35:
1271 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001272 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 break;
1274 case 0x38 ... 0x3d:
1275 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001276 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001278 case 0x40 ... 0x47: /* inc r16/r32 */
1279 emulate_1op("inc", c->dst, ctxt->eflags);
1280 break;
1281 case 0x48 ... 0x4f: /* dec r16/r32 */
1282 emulate_1op("dec", c->dst, ctxt->eflags);
1283 break;
1284 case 0x50 ... 0x57: /* push reg */
1285 c->dst.type = OP_MEM;
1286 c->dst.bytes = c->op_bytes;
1287 c->dst.val = c->src.val;
1288 register_address_increment(c->regs[VCPU_REGS_RSP],
1289 -c->op_bytes);
1290 c->dst.ptr = (void *) register_address(
1291 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1292 break;
1293 case 0x58 ... 0x5f: /* pop reg */
1294 pop_instruction:
1295 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1296 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1297 c->op_bytes, ctxt->vcpu)) != 0)
1298 goto done;
1299
1300 register_address_increment(c->regs[VCPU_REGS_RSP],
1301 c->op_bytes);
1302 c->dst.type = OP_NONE; /* Disable writeback. */
1303 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001305 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001307 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 break;
1309 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001310 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 case 0:
1312 goto add;
1313 case 1:
1314 goto or;
1315 case 2:
1316 goto adc;
1317 case 3:
1318 goto sbb;
1319 case 4:
1320 goto and;
1321 case 5:
1322 goto sub;
1323 case 6:
1324 goto xor;
1325 case 7:
1326 goto cmp;
1327 }
1328 break;
1329 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001330 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 break;
1332 case 0x86 ... 0x87: /* xchg */
1333 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001334 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001336 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 break;
1338 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001339 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 break;
1341 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001342 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 break; /* 64b reg: zero-extend */
1344 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001345 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 break;
1347 }
1348 /*
1349 * Write back the memory destination with implicit LOCK
1350 * prefix.
1351 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001352 c->dst.val = c->src.val;
1353 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001356 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001357 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001358 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001359 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001361 rc = emulate_grp1a(ctxt, ops);
1362 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001365 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001366 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1367 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001368 break;
1369 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001370 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001371 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 case 0xc0 ... 0xc1:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001373 emulate_grp2(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001375 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1376 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001377 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001378 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001380 c->src.val = 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001384 c->src.val = c->regs[VCPU_REGS_RCX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 rc = emulate_grp3(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389 if (rc != 0)
1390 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 break;
1392 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviera01af5e2007-09-24 11:10:56 +02001393 rc = emulate_grp45(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394 if (rc != 0)
1395 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 break;
1397 }
1398
1399writeback:
Laurent Viviera01af5e2007-09-24 11:10:56 +02001400 rc = writeback(ctxt, ops);
1401 if (rc != 0)
1402 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403
1404 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001405 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001406 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407
1408done:
Laurent Vivier34273182007-09-18 11:27:37 +02001409 if (rc == X86EMUL_UNHANDLEABLE) {
1410 c->eip = saved_eip;
1411 return -1;
1412 }
1413 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414
1415special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001416 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001418 switch (c->b) {
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001419 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001420 c->src.val = 0L;
1421 c->src.val = insn_fetch(s8, 1, c->eip);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422 emulate_push(ctxt);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001423 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001424 case 0x6c: /* insb */
1425 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001426 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001427 1,
1428 (c->d & ByteOp) ? 1 : c->op_bytes,
1429 c->rep_prefix ?
1430 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001431 (ctxt->eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001432 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001433 c->regs[VCPU_REGS_RDI]),
1434 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001435 c->regs[VCPU_REGS_RDX]) == 0) {
1436 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001437 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001438 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001439 return 0;
1440 case 0x6e: /* outsb */
1441 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001442 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001443 0,
1444 (c->d & ByteOp) ? 1 : c->op_bytes,
1445 c->rep_prefix ?
1446 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001447 (ctxt->eflags & EFLG_DF),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001448 register_address(c->override_base ?
1449 *c->override_base :
1450 ctxt->ds_base,
1451 c->regs[VCPU_REGS_RSI]),
1452 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001453 c->regs[VCPU_REGS_RDX]) == 0) {
1454 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001455 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001456 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001457 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001458 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001459 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001460
Laurent Vivier05f086f2007-09-24 11:10:55 +02001461 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001462 JMP_REL(rel);
1463 break;
1464 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001465 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001466 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001467 emulate_push(ctxt);
1468 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001469 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001470 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001471 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001472 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001473 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001474 goto pop_instruction;
1475 case 0xf4: /* hlt */
1476 ctxt->vcpu->halt_request = 1;
1477 goto done;
Nitin A Kambleb284be52007-10-16 18:23:27 -07001478 case 0xf5: /* cmc */
1479 /* complement carry flag from eflags reg */
1480 ctxt->eflags ^= EFLG_CF;
1481 c->dst.type = OP_NONE; /* Disable writeback. */
1482 break;
1483 case 0xf8: /* clc */
1484 ctxt->eflags &= ~EFLG_CF;
1485 c->dst.type = OP_NONE; /* Disable writeback. */
1486 break;
1487 case 0xfa: /* cli */
1488 ctxt->eflags &= ~X86_EFLAGS_IF;
1489 c->dst.type = OP_NONE; /* Disable writeback. */
1490 break;
1491 case 0xfb: /* sti */
1492 ctxt->eflags |= X86_EFLAGS_IF;
1493 c->dst.type = OP_NONE; /* Disable writeback. */
1494 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001495 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001496 if (c->rep_prefix) {
1497 if (c->regs[VCPU_REGS_RCX] == 0) {
1498 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 goto done;
1500 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001501 c->regs[VCPU_REGS_RCX]--;
1502 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001504 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 c->dst.type = OP_MEM;
1507 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1508 c->dst.ptr = (unsigned long *)register_address(
1509 ctxt->es_base,
1510 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001512 c->override_base ? *c->override_base :
1513 ctxt->ds_base,
1514 c->regs[VCPU_REGS_RSI]),
1515 &c->dst.val,
1516 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001518 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001519 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001520 : c->dst.bytes);
1521 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001522 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001523 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 break;
1525 case 0xa6 ... 0xa7: /* cmps */
1526 DPRINTF("Urk! I don't handle CMPS.\n");
1527 goto cannot_emulate;
1528 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001529 c->dst.type = OP_MEM;
1530 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1531 c->dst.ptr = (unsigned long *)cr2;
1532 c->dst.val = c->regs[VCPU_REGS_RAX];
1533 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001534 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001535 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536 break;
1537 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001538 c->dst.type = OP_REG;
1539 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1540 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1541 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1542 c->dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001543 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001545 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001546 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001547 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 break;
1549 case 0xae ... 0xaf: /* scas */
1550 DPRINTF("Urk! I don't handle SCAS.\n");
1551 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001552 case 0xe8: /* call (near) */ {
1553 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001554 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001555 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001556 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001557 break;
1558 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001559 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001560 break;
1561 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001562 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001563 break;
1564 default:
1565 DPRINTF("Call: Invalid op_bytes\n");
1566 goto cannot_emulate;
1567 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001568 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001569 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001570 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001571 emulate_push(ctxt);
1572 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001573 }
1574 case 0xe9: /* jmp rel */
1575 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001576 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001577 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001578 break;
1579
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001580
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 }
1582 goto writeback;
1583
1584twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001587 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 u16 size;
1589 unsigned long address;
1590
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001591 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001592 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001593 goto cannot_emulate;
1594
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001595 rc = kvm_fix_hypercall(ctxt->vcpu);
1596 if (rc)
1597 goto done;
1598
1599 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001600 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001602 rc = read_descriptor(ctxt, ops, c->src.ptr,
1603 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604 if (rc)
1605 goto done;
1606 realmode_lgdt(ctxt->vcpu, size, address);
1607 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001608 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001610 rc = kvm_fix_hypercall(ctxt->vcpu);
1611 if (rc)
1612 goto done;
1613 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001614 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001615 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001616 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001617 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001618 if (rc)
1619 goto done;
1620 realmode_lidt(ctxt->vcpu, size, address);
1621 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 break;
1623 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001624 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001626 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 = realmode_get_cr(ctxt->vcpu, 0);
1628 break;
1629 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001630 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 goto cannot_emulate;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001632 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1633 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 break;
1635 case 7: /* invlpg*/
1636 emulate_invlpg(ctxt->vcpu, cr2);
1637 break;
1638 default:
1639 goto cannot_emulate;
1640 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001641 /* Disable writeback. */
1642 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 break;
1644 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001645 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001648 if (rc)
1649 goto cannot_emulate;
1650 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651 break;
1652 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001653 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001655 rc = emulator_set_dr(ctxt, c->modrm_reg,
1656 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001657 if (rc)
1658 goto cannot_emulate;
1659 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 break;
1661 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001662 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001663 if (!test_cc(c->b, ctxt->eflags))
1664 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001666 case 0xa3:
1667 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001668 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001669 /* only subword offset */
1670 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001671 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001672 break;
1673 case 0xab:
1674 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001675 /* only subword offset */
1676 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001677 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001678 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 case 0xb0 ... 0xb1: /* cmpxchg */
1680 /*
1681 * Save real source value, then compare EAX against
1682 * destination.
1683 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001684 c->src.orig_val = c->src.val;
1685 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001686 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1687 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001689 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 } else {
1691 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001692 c->dst.type = OP_REG;
1693 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 }
1695 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 case 0xb3:
1697 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001698 /* only subword offset */
1699 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001700 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001703 c->dst.bytes = c->op_bytes;
1704 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1705 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001708 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 case 0:
1710 goto bt;
1711 case 1:
1712 goto bts;
1713 case 2:
1714 goto btr;
1715 case 3:
1716 goto btc;
1717 }
1718 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001719 case 0xbb:
1720 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001721 /* only subword offset */
1722 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001723 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001724 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001726 c->dst.bytes = c->op_bytes;
1727 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1728 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001730 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001731 c->dst.bytes = c->op_bytes;
1732 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1733 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001734 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 }
1736 goto writeback;
1737
1738twobyte_special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001739 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001740 case 0x06:
1741 emulate_clts(ctxt->vcpu);
1742 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001743 case 0x08: /* invd */
1744 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001745 case 0x09: /* wbinvd */
1746 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 case 0x0d: /* GrpP (prefetch) */
1748 case 0x18: /* Grp16 (prefetch/nop) */
1749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 case 0x20: /* mov cr, reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001751 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001753 c->regs[c->modrm_rm] =
1754 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755 break;
1756 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001757 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001759 realmode_set_cr(ctxt->vcpu,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001760 c->modrm_reg, c->modrm_val, &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001762 case 0x30:
1763 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001764 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1765 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1766 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001767 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001768 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001769 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001770 }
1771 rc = X86EMUL_CONTINUE;
1772 break;
1773 case 0x32:
1774 /* rdmsr */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001776 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001777 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001778 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001779 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001780 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1781 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001782 }
1783 rc = X86EMUL_CONTINUE;
1784 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001785 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1786 long int rel;
1787
Laurent Viviere4e03de2007-09-18 11:52:50 +02001788 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001789 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001790 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001791 break;
1792 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001793 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001794 break;
1795 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001796 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001797 break;
1798 default:
1799 DPRINTF("jnz: Invalid op_bytes\n");
1800 goto cannot_emulate;
1801 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001802 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001803 JMP_REL(rel);
1804 break;
1805 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 case 0xc7: /* Grp9 (cmpxchg8b) */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001807 rc = emulate_grp9(ctxt, ops, cr2);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001808 if (rc != 0)
1809 goto done;
1810 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001812 /* Disable writeback. */
1813 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 goto writeback;
1815
1816cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001817 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001818 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819 return -1;
1820}