blob: 84e536603f5ae109621c488950a87b6149a93e65 [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; \
Avi Kivity62266862007-11-20 13:15:52 +0200417 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400418 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 goto done; \
420 (_eip) += (_size); \
421 (_type)_x; \
422})
423
424/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300425#define address_mask(reg) \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200426 ((c->ad_bytes == sizeof(unsigned long)) ? \
427 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300429 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430#define register_address_increment(reg, inc) \
431 do { \
432 /* signed type ensures sign extension to long */ \
433 int _inc = (inc); \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200434 if (c->ad_bytes == sizeof(unsigned long)) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 (reg) += _inc; \
436 else \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200437 (reg) = ((reg) & \
438 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
439 (((reg) + _inc) & \
440 ((1UL << (c->ad_bytes << 3)) - 1)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 } while (0)
442
Nitin A Kamble098c9372007-08-19 11:00:36 +0300443#define JMP_REL(rel) \
444 do { \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200445 register_address_increment(c->eip, rel); \
Nitin A Kamble098c9372007-08-19 11:00:36 +0300446 } while (0)
447
Avi Kivity62266862007-11-20 13:15:52 +0200448static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
449 struct x86_emulate_ops *ops,
450 unsigned long linear, u8 *dest)
451{
452 struct fetch_cache *fc = &ctxt->decode.fetch;
453 int rc;
454 int size;
455
456 if (linear < fc->start || linear >= fc->end) {
457 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
458 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
459 if (rc)
460 return rc;
461 fc->start = linear;
462 fc->end = linear + size;
463 }
464 *dest = fc->data[linear - fc->start];
465 return 0;
466}
467
468static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
469 struct x86_emulate_ops *ops,
470 unsigned long eip, void *dest, unsigned size)
471{
472 int rc = 0;
473
474 eip += ctxt->cs_base;
475 while (size--) {
476 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
477 if (rc)
478 return rc;
479 }
480 return 0;
481}
482
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000483/*
484 * Given the 'reg' portion of a ModRM byte, and a register block, return a
485 * pointer into the block that addresses the relevant register.
486 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
487 */
488static void *decode_register(u8 modrm_reg, unsigned long *regs,
489 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490{
491 void *p;
492
493 p = &regs[modrm_reg];
494 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
495 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
496 return p;
497}
498
499static int read_descriptor(struct x86_emulate_ctxt *ctxt,
500 struct x86_emulate_ops *ops,
501 void *ptr,
502 u16 *size, unsigned long *address, int op_bytes)
503{
504 int rc;
505
506 if (op_bytes == 2)
507 op_bytes = 3;
508 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300509 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
510 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 if (rc)
512 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300513 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
514 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 return rc;
516}
517
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300518static int test_cc(unsigned int condition, unsigned int flags)
519{
520 int rc = 0;
521
522 switch ((condition & 15) >> 1) {
523 case 0: /* o */
524 rc |= (flags & EFLG_OF);
525 break;
526 case 1: /* b/c/nae */
527 rc |= (flags & EFLG_CF);
528 break;
529 case 2: /* z/e */
530 rc |= (flags & EFLG_ZF);
531 break;
532 case 3: /* be/na */
533 rc |= (flags & (EFLG_CF|EFLG_ZF));
534 break;
535 case 4: /* s */
536 rc |= (flags & EFLG_SF);
537 break;
538 case 5: /* p/pe */
539 rc |= (flags & EFLG_PF);
540 break;
541 case 7: /* le/ng */
542 rc |= (flags & EFLG_ZF);
543 /* fall through */
544 case 6: /* l/nge */
545 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
546 break;
547 }
548
549 /* Odd condition identifiers (lsb == 1) have inverted sense. */
550 return (!!rc ^ (condition & 1));
551}
552
Avi Kivity3c118e22007-10-31 10:27:04 +0200553static void decode_register_operand(struct operand *op,
554 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200555 int inhibit_bytereg)
556{
Avi Kivity33615aa2007-10-31 11:15:56 +0200557 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200558 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200559
560 if (!(c->d & ModRM))
561 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200562 op->type = OP_REG;
563 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200564 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200565 op->val = *(u8 *)op->ptr;
566 op->bytes = 1;
567 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200568 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200569 op->bytes = c->op_bytes;
570 switch (op->bytes) {
571 case 2:
572 op->val = *(u16 *)op->ptr;
573 break;
574 case 4:
575 op->val = *(u32 *)op->ptr;
576 break;
577 case 8:
578 op->val = *(u64 *) op->ptr;
579 break;
580 }
581 }
582 op->orig_val = op->val;
583}
584
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200585static int decode_modrm(struct x86_emulate_ctxt *ctxt,
586 struct x86_emulate_ops *ops)
587{
588 struct decode_cache *c = &ctxt->decode;
589 u8 sib;
590 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
591 int rc = 0;
592
593 if (c->rex_prefix) {
594 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
595 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
596 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
597 }
598
599 c->modrm = insn_fetch(u8, 1, c->eip);
600 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
601 c->modrm_reg |= (c->modrm & 0x38) >> 3;
602 c->modrm_rm |= (c->modrm & 0x07);
603 c->modrm_ea = 0;
604 c->use_modrm_ea = 1;
605
606 if (c->modrm_mod == 3) {
607 c->modrm_val = *(unsigned long *)
608 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
609 return rc;
610 }
611
612 if (c->ad_bytes == 2) {
613 unsigned bx = c->regs[VCPU_REGS_RBX];
614 unsigned bp = c->regs[VCPU_REGS_RBP];
615 unsigned si = c->regs[VCPU_REGS_RSI];
616 unsigned di = c->regs[VCPU_REGS_RDI];
617
618 /* 16-bit ModR/M decode. */
619 switch (c->modrm_mod) {
620 case 0:
621 if (c->modrm_rm == 6)
622 c->modrm_ea += insn_fetch(u16, 2, c->eip);
623 break;
624 case 1:
625 c->modrm_ea += insn_fetch(s8, 1, c->eip);
626 break;
627 case 2:
628 c->modrm_ea += insn_fetch(u16, 2, c->eip);
629 break;
630 }
631 switch (c->modrm_rm) {
632 case 0:
633 c->modrm_ea += bx + si;
634 break;
635 case 1:
636 c->modrm_ea += bx + di;
637 break;
638 case 2:
639 c->modrm_ea += bp + si;
640 break;
641 case 3:
642 c->modrm_ea += bp + di;
643 break;
644 case 4:
645 c->modrm_ea += si;
646 break;
647 case 5:
648 c->modrm_ea += di;
649 break;
650 case 6:
651 if (c->modrm_mod != 0)
652 c->modrm_ea += bp;
653 break;
654 case 7:
655 c->modrm_ea += bx;
656 break;
657 }
658 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
659 (c->modrm_rm == 6 && c->modrm_mod != 0))
660 if (!c->override_base)
661 c->override_base = &ctxt->ss_base;
662 c->modrm_ea = (u16)c->modrm_ea;
663 } else {
664 /* 32/64-bit ModR/M decode. */
665 switch (c->modrm_rm) {
666 case 4:
667 case 12:
668 sib = insn_fetch(u8, 1, c->eip);
669 index_reg |= (sib >> 3) & 7;
670 base_reg |= sib & 7;
671 scale = sib >> 6;
672
673 switch (base_reg) {
674 case 5:
675 if (c->modrm_mod != 0)
676 c->modrm_ea += c->regs[base_reg];
677 else
678 c->modrm_ea +=
679 insn_fetch(s32, 4, c->eip);
680 break;
681 default:
682 c->modrm_ea += c->regs[base_reg];
683 }
684 switch (index_reg) {
685 case 4:
686 break;
687 default:
688 c->modrm_ea += c->regs[index_reg] << scale;
689 }
690 break;
691 case 5:
692 if (c->modrm_mod != 0)
693 c->modrm_ea += c->regs[c->modrm_rm];
694 else if (ctxt->mode == X86EMUL_MODE_PROT64)
695 rip_relative = 1;
696 break;
697 default:
698 c->modrm_ea += c->regs[c->modrm_rm];
699 break;
700 }
701 switch (c->modrm_mod) {
702 case 0:
703 if (c->modrm_rm == 5)
704 c->modrm_ea += insn_fetch(s32, 4, c->eip);
705 break;
706 case 1:
707 c->modrm_ea += insn_fetch(s8, 1, c->eip);
708 break;
709 case 2:
710 c->modrm_ea += insn_fetch(s32, 4, c->eip);
711 break;
712 }
713 }
714 if (rip_relative) {
715 c->modrm_ea += c->eip;
716 switch (c->d & SrcMask) {
717 case SrcImmByte:
718 c->modrm_ea += 1;
719 break;
720 case SrcImm:
721 if (c->d & ByteOp)
722 c->modrm_ea += 1;
723 else
724 if (c->op_bytes == 8)
725 c->modrm_ea += 4;
726 else
727 c->modrm_ea += c->op_bytes;
728 }
729 }
730done:
731 return rc;
732}
733
734static int decode_abs(struct x86_emulate_ctxt *ctxt,
735 struct x86_emulate_ops *ops)
736{
737 struct decode_cache *c = &ctxt->decode;
738 int rc = 0;
739
740 switch (c->ad_bytes) {
741 case 2:
742 c->modrm_ea = insn_fetch(u16, 2, c->eip);
743 break;
744 case 4:
745 c->modrm_ea = insn_fetch(u32, 4, c->eip);
746 break;
747 case 8:
748 c->modrm_ea = insn_fetch(u64, 8, c->eip);
749 break;
750 }
751done:
752 return rc;
753}
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200756x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200758 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 int mode = ctxt->mode;
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200761 int def_op_bytes, def_ad_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762
763 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
Laurent Viviere4e03de2007-09-18 11:52:50 +0200765 memset(c, 0, sizeof(struct decode_cache));
766 c->eip = ctxt->vcpu->rip;
767 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
769 switch (mode) {
770 case X86EMUL_MODE_REAL:
771 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200772 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 break;
774 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200775 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800777#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200779 def_op_bytes = 4;
780 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 break;
782#endif
783 default:
784 return -1;
785 }
786
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200787 c->op_bytes = def_op_bytes;
788 c->ad_bytes = def_ad_bytes;
789
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200791 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200792 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200794 /* switch between 2/4 bytes */
795 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 break;
797 case 0x67: /* address-size override */
798 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200799 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200800 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200802 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200803 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 break;
805 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200806 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 break;
808 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200809 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 break;
811 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200812 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 break;
814 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200815 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 break;
817 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200818 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 break;
820 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200821 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200823 case 0x40 ... 0x4f: /* REX */
824 if (mode != X86EMUL_MODE_PROT64)
825 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200826 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200827 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200829 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200831 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100832 c->rep_prefix = REPNE_PREFIX;
833 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100835 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 default:
838 goto done_prefixes;
839 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200840
841 /* Any legacy prefix after a REX prefix nullifies its effect. */
842
Avi Kivity33615aa2007-10-31 11:15:56 +0200843 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 }
845
846done_prefixes:
847
848 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200849 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200850 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200851 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852
853 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200854 c->d = opcode_table[c->b];
855 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200857 if (c->b == 0x0f) {
858 c->twobyte = 1;
859 c->b = insn_fetch(u8, 1, c->eip);
860 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 }
862
863 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200864 if (c->d == 0) {
865 DPRINTF("Cannot emulate %02x\n", c->b);
866 return -1;
867 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 }
869
870 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 if (c->d & ModRM)
872 rc = decode_modrm(ctxt, ops);
873 else if (c->d & MemAbs)
874 rc = decode_abs(ctxt, ops);
875 if (rc)
876 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
Avi Kivityc7e75a32007-10-28 16:34:25 +0200878 if (!c->override_base)
879 c->override_base = &ctxt->ds_base;
880 if (mode == X86EMUL_MODE_PROT64 &&
881 c->override_base != &ctxt->fs_base &&
882 c->override_base != &ctxt->gs_base)
883 c->override_base = NULL;
884
885 if (c->override_base)
886 c->modrm_ea += *c->override_base;
887
888 if (c->ad_bytes != 8)
889 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 /*
891 * Decode and fetch the source operand: register, memory
892 * or immediate.
893 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200894 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 case SrcNone:
896 break;
897 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200898 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 break;
900 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 goto srcmem_common;
903 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200904 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 goto srcmem_common;
906 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200907 c->src.bytes = (c->d & ByteOp) ? 1 :
908 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300909 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400910 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300911 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400912 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200913 /*
914 * For instructions with a ModR/M byte, switch to register
915 * access if Mod = 3.
916 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200917 if ((c->d & ModRM) && c->modrm_mod == 3) {
918 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200919 break;
920 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200921 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 break;
923 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200924 c->src.type = OP_IMM;
925 c->src.ptr = (unsigned long *)c->eip;
926 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
927 if (c->src.bytes == 8)
928 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200932 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 break;
934 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200935 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 break;
937 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200938 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 break;
940 }
941 break;
942 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200943 c->src.type = OP_IMM;
944 c->src.ptr = (unsigned long *)c->eip;
945 c->src.bytes = 1;
946 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 break;
948 }
949
Avi Kivity038e51d2007-01-22 20:40:40 -0800950 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200951 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800952 case ImplicitOps:
953 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200954 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800955 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200956 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200957 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -0800958 break;
959 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200960 if ((c->d & ModRM) && c->modrm_mod == 3) {
961 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200962 break;
963 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200964 c->dst.type = OP_MEM;
965 break;
966 }
967
968done:
969 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
970}
971
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200972static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
973{
974 struct decode_cache *c = &ctxt->decode;
975
976 c->dst.type = OP_MEM;
977 c->dst.bytes = c->op_bytes;
978 c->dst.val = c->src.val;
979 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
980 c->dst.ptr = (void *) register_address(ctxt->ss_base,
981 c->regs[VCPU_REGS_RSP]);
982}
983
984static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
985 struct x86_emulate_ops *ops)
986{
987 struct decode_cache *c = &ctxt->decode;
988 int rc;
989
990 /* 64-bit mode: POP always pops a 64-bit operand. */
991
992 if (ctxt->mode == X86EMUL_MODE_PROT64)
993 c->dst.bytes = 8;
994
995 rc = ops->read_std(register_address(ctxt->ss_base,
996 c->regs[VCPU_REGS_RSP]),
997 &c->dst.val, c->dst.bytes, ctxt->vcpu);
998 if (rc != 0)
999 return rc;
1000
1001 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
1002
1003 return 0;
1004}
1005
Laurent Vivier05f086f2007-09-24 11:10:55 +02001006static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001007{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001008 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001009 switch (c->modrm_reg) {
1010 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001011 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001012 break;
1013 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001014 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001015 break;
1016 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001017 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001018 break;
1019 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001020 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001021 break;
1022 case 4: /* sal/shl */
1023 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001024 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001025 break;
1026 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001027 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001028 break;
1029 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001030 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001031 break;
1032 }
1033}
1034
1035static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001036 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001037{
1038 struct decode_cache *c = &ctxt->decode;
1039 int rc = 0;
1040
1041 switch (c->modrm_reg) {
1042 case 0 ... 1: /* test */
1043 /*
1044 * Special case in Grp3: test has an immediate
1045 * source operand.
1046 */
1047 c->src.type = OP_IMM;
1048 c->src.ptr = (unsigned long *)c->eip;
1049 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1050 if (c->src.bytes == 8)
1051 c->src.bytes = 4;
1052 switch (c->src.bytes) {
1053 case 1:
1054 c->src.val = insn_fetch(s8, 1, c->eip);
1055 break;
1056 case 2:
1057 c->src.val = insn_fetch(s16, 2, c->eip);
1058 break;
1059 case 4:
1060 c->src.val = insn_fetch(s32, 4, c->eip);
1061 break;
1062 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001063 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001064 break;
1065 case 2: /* not */
1066 c->dst.val = ~c->dst.val;
1067 break;
1068 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001069 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001070 break;
1071 default:
1072 DPRINTF("Cannot emulate %02x\n", c->b);
1073 rc = X86EMUL_UNHANDLEABLE;
1074 break;
1075 }
1076done:
1077 return rc;
1078}
1079
1080static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001081 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001082{
1083 struct decode_cache *c = &ctxt->decode;
1084 int rc;
1085
1086 switch (c->modrm_reg) {
1087 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001088 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001089 break;
1090 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001091 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001092 break;
1093 case 4: /* jmp abs */
1094 if (c->b == 0xff)
1095 c->eip = c->dst.val;
1096 else {
1097 DPRINTF("Cannot emulate %02x\n", c->b);
1098 return X86EMUL_UNHANDLEABLE;
1099 }
1100 break;
1101 case 6: /* push */
1102
1103 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1104
1105 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1106 c->dst.bytes = 8;
1107 rc = ops->read_std((unsigned long)c->dst.ptr,
1108 &c->dst.val, 8, ctxt->vcpu);
1109 if (rc != 0)
1110 return rc;
1111 }
1112 register_address_increment(c->regs[VCPU_REGS_RSP],
1113 -c->dst.bytes);
1114 rc = ops->write_emulated(register_address(ctxt->ss_base,
1115 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1116 c->dst.bytes, ctxt->vcpu);
1117 if (rc != 0)
1118 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001119 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001120 break;
1121 default:
1122 DPRINTF("Cannot emulate %02x\n", c->b);
1123 return X86EMUL_UNHANDLEABLE;
1124 }
1125 return 0;
1126}
1127
1128static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1129 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001130 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001131{
1132 struct decode_cache *c = &ctxt->decode;
1133 u64 old, new;
1134 int rc;
1135
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001136 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001137 if (rc != 0)
1138 return rc;
1139
1140 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1141 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1142
1143 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1144 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001145 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001146
1147 } else {
1148 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1149 (u32) c->regs[VCPU_REGS_RBX];
1150
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001151 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001152 if (rc != 0)
1153 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001154 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155 }
1156 return 0;
1157}
1158
1159static inline int writeback(struct x86_emulate_ctxt *ctxt,
1160 struct x86_emulate_ops *ops)
1161{
1162 int rc;
1163 struct decode_cache *c = &ctxt->decode;
1164
1165 switch (c->dst.type) {
1166 case OP_REG:
1167 /* The 4-byte case *is* correct:
1168 * in 64-bit mode we zero-extend.
1169 */
1170 switch (c->dst.bytes) {
1171 case 1:
1172 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1173 break;
1174 case 2:
1175 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1176 break;
1177 case 4:
1178 *c->dst.ptr = (u32)c->dst.val;
1179 break; /* 64b: zero-ext */
1180 case 8:
1181 *c->dst.ptr = c->dst.val;
1182 break;
1183 }
1184 break;
1185 case OP_MEM:
1186 if (c->lock_prefix)
1187 rc = ops->cmpxchg_emulated(
1188 (unsigned long)c->dst.ptr,
1189 &c->dst.orig_val,
1190 &c->dst.val,
1191 c->dst.bytes,
1192 ctxt->vcpu);
1193 else
1194 rc = ops->write_emulated(
1195 (unsigned long)c->dst.ptr,
1196 &c->dst.val,
1197 c->dst.bytes,
1198 ctxt->vcpu);
1199 if (rc != 0)
1200 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001201 break;
1202 case OP_NONE:
1203 /* no writeback */
1204 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001205 default:
1206 break;
1207 }
1208 return 0;
1209}
1210
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001211int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001212x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001213{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001214 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001215 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001216 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001217 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001218 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001219
Laurent Vivier34273182007-09-18 11:27:37 +02001220 /* Shadow copy of register state. Committed on successful emulation.
1221 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1222 * modify them.
1223 */
1224
1225 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1226 saved_eip = c->eip;
1227
Avi Kivityc7e75a32007-10-28 16:34:25 +02001228 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001229 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001230
1231 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001232 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001233 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001234 rc = ops->read_emulated((unsigned long)c->src.ptr,
1235 &c->src.val,
1236 c->src.bytes,
1237 ctxt->vcpu);
1238 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001239 goto done;
1240 c->src.orig_val = c->src.val;
1241 }
1242
1243 if ((c->d & DstMask) == ImplicitOps)
1244 goto special_insn;
1245
1246
1247 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001248 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001249 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1250 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001251 if (c->d & BitOp) {
1252 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001253
Laurent Viviere4e03de2007-09-18 11:52:50 +02001254 c->dst.ptr = (void *)c->dst.ptr +
1255 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001256 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001257 if (!(c->d & Mov) &&
1258 /* optimisation - avoid slow emulated read */
1259 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1260 &c->dst.val,
1261 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001262 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001263 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001264 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001265
Laurent Viviere4e03de2007-09-18 11:52:50 +02001266 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 goto twobyte_insn;
1268
Laurent Viviere4e03de2007-09-18 11:52:50 +02001269 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 case 0x00 ... 0x05:
1271 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001272 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 break;
1274 case 0x08 ... 0x0d:
1275 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001276 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 break;
1278 case 0x10 ... 0x15:
1279 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001280 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 break;
1282 case 0x18 ... 0x1d:
1283 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001284 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001286 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001288 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001290 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001291 c->dst.type = OP_REG;
1292 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1293 c->dst.val = *(u8 *)c->dst.ptr;
1294 c->dst.bytes = 1;
1295 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001296 goto and;
1297 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001298 c->dst.type = OP_REG;
1299 c->dst.bytes = c->op_bytes;
1300 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1301 if (c->op_bytes == 2)
1302 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001303 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001304 c->dst.val = *(u32 *)c->dst.ptr;
1305 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001306 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 case 0x28 ... 0x2d:
1308 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001309 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 break;
1311 case 0x30 ... 0x35:
1312 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001313 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 break;
1315 case 0x38 ... 0x3d:
1316 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001317 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001319 case 0x40 ... 0x47: /* inc r16/r32 */
1320 emulate_1op("inc", c->dst, ctxt->eflags);
1321 break;
1322 case 0x48 ... 0x4f: /* dec r16/r32 */
1323 emulate_1op("dec", c->dst, ctxt->eflags);
1324 break;
1325 case 0x50 ... 0x57: /* push reg */
1326 c->dst.type = OP_MEM;
1327 c->dst.bytes = c->op_bytes;
1328 c->dst.val = c->src.val;
1329 register_address_increment(c->regs[VCPU_REGS_RSP],
1330 -c->op_bytes);
1331 c->dst.ptr = (void *) register_address(
1332 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1333 break;
1334 case 0x58 ... 0x5f: /* pop reg */
1335 pop_instruction:
1336 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1337 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1338 c->op_bytes, ctxt->vcpu)) != 0)
1339 goto done;
1340
1341 register_address_increment(c->regs[VCPU_REGS_RSP],
1342 c->op_bytes);
1343 c->dst.type = OP_NONE; /* Disable writeback. */
1344 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001346 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001348 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 break;
1350 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001351 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 case 0:
1353 goto add;
1354 case 1:
1355 goto or;
1356 case 2:
1357 goto adc;
1358 case 3:
1359 goto sbb;
1360 case 4:
1361 goto and;
1362 case 5:
1363 goto sub;
1364 case 6:
1365 goto xor;
1366 case 7:
1367 goto cmp;
1368 }
1369 break;
1370 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001371 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 break;
1373 case 0x86 ... 0x87: /* xchg */
1374 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001375 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001377 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 break;
1379 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001380 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 break;
1382 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001383 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 break; /* 64b reg: zero-extend */
1385 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001386 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 break;
1388 }
1389 /*
1390 * Write back the memory destination with implicit LOCK
1391 * prefix.
1392 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001393 c->dst.val = c->src.val;
1394 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001397 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001398 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001399 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001400 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001402 rc = emulate_grp1a(ctxt, ops);
1403 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001406 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001407 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1408 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001409 break;
1410 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001411 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001412 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 case 0xc0 ... 0xc1:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001414 emulate_grp2(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001416 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1417 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001418 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001419 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001421 c->src.val = 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001422 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001423 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001425 c->src.val = c->regs[VCPU_REGS_RCX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001426 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001427 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001429 rc = emulate_grp3(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001430 if (rc != 0)
1431 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 break;
1433 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviera01af5e2007-09-24 11:10:56 +02001434 rc = emulate_grp45(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001435 if (rc != 0)
1436 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 break;
1438 }
1439
1440writeback:
Laurent Viviera01af5e2007-09-24 11:10:56 +02001441 rc = writeback(ctxt, ops);
1442 if (rc != 0)
1443 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444
1445 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001446 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001447 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
1449done:
Laurent Vivier34273182007-09-18 11:27:37 +02001450 if (rc == X86EMUL_UNHANDLEABLE) {
1451 c->eip = saved_eip;
1452 return -1;
1453 }
1454 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455
1456special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001457 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001459 switch (c->b) {
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001460 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001461 c->src.val = 0L;
1462 c->src.val = insn_fetch(s8, 1, c->eip);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001463 emulate_push(ctxt);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001464 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001465 case 0x6c: /* insb */
1466 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001467 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001468 1,
1469 (c->d & ByteOp) ? 1 : c->op_bytes,
1470 c->rep_prefix ?
1471 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001472 (ctxt->eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001473 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 c->regs[VCPU_REGS_RDI]),
1475 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001476 c->regs[VCPU_REGS_RDX]) == 0) {
1477 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001478 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001479 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001480 return 0;
1481 case 0x6e: /* outsb */
1482 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001483 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001484 0,
1485 (c->d & ByteOp) ? 1 : c->op_bytes,
1486 c->rep_prefix ?
1487 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001488 (ctxt->eflags & EFLG_DF),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001489 register_address(c->override_base ?
1490 *c->override_base :
1491 ctxt->ds_base,
1492 c->regs[VCPU_REGS_RSI]),
1493 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001494 c->regs[VCPU_REGS_RDX]) == 0) {
1495 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001496 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001497 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001498 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001499 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001500 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001501
Laurent Vivier05f086f2007-09-24 11:10:55 +02001502 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001503 JMP_REL(rel);
1504 break;
1505 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001506 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001507 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001508 emulate_push(ctxt);
1509 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001510 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001511 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001512 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001513 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001514 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001515 goto pop_instruction;
1516 case 0xf4: /* hlt */
1517 ctxt->vcpu->halt_request = 1;
1518 goto done;
Nitin A Kambleb284be52007-10-16 18:23:27 -07001519 case 0xf5: /* cmc */
1520 /* complement carry flag from eflags reg */
1521 ctxt->eflags ^= EFLG_CF;
1522 c->dst.type = OP_NONE; /* Disable writeback. */
1523 break;
1524 case 0xf8: /* clc */
1525 ctxt->eflags &= ~EFLG_CF;
1526 c->dst.type = OP_NONE; /* Disable writeback. */
1527 break;
1528 case 0xfa: /* cli */
1529 ctxt->eflags &= ~X86_EFLAGS_IF;
1530 c->dst.type = OP_NONE; /* Disable writeback. */
1531 break;
1532 case 0xfb: /* sti */
1533 ctxt->eflags |= X86_EFLAGS_IF;
1534 c->dst.type = OP_NONE; /* Disable writeback. */
1535 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001536 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001537 if (c->rep_prefix) {
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001538 /* All REP prefixes have the same first termination condition */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001539 if (c->regs[VCPU_REGS_RCX] == 0) {
1540 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 goto done;
1542 }
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001543 /* The second termination condition only applies for REPE
1544 * and REPNE. Test if the repeat string operation prefix is
1545 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1546 * corresponding termination condition according to:
1547 * - if REPE/REPZ and ZF = 0 then done
1548 * - if REPNE/REPNZ and ZF = 1 then done
1549 */
1550 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1551 (c->b == 0xae) || (c->b == 0xaf)) {
1552 if ((c->rep_prefix == REPE_PREFIX) &&
1553 ((ctxt->eflags & EFLG_ZF) == 0)) {
1554 ctxt->vcpu->rip = c->eip;
1555 goto done;
1556 }
1557 if ((c->rep_prefix == REPNE_PREFIX) &&
1558 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
1559 ctxt->vcpu->rip = c->eip;
1560 goto done;
1561 }
1562 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001563 c->regs[VCPU_REGS_RCX]--;
1564 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001566 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001568 c->dst.type = OP_MEM;
1569 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1570 c->dst.ptr = (unsigned long *)register_address(
1571 ctxt->es_base,
1572 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001574 c->override_base ? *c->override_base :
1575 ctxt->ds_base,
1576 c->regs[VCPU_REGS_RSI]),
1577 &c->dst.val,
1578 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001580 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001581 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001582 : c->dst.bytes);
1583 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001584 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 break;
1587 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001588 c->src.type = OP_NONE; /* Disable writeback. */
1589 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1590 c->src.ptr = (unsigned long *)register_address(
1591 c->override_base ? *c->override_base :
1592 ctxt->ds_base,
1593 c->regs[VCPU_REGS_RSI]);
1594 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1595 &c->src.val,
1596 c->src.bytes,
1597 ctxt->vcpu)) != 0)
1598 goto done;
1599
1600 c->dst.type = OP_NONE; /* Disable writeback. */
1601 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1602 c->dst.ptr = (unsigned long *)register_address(
1603 ctxt->es_base,
1604 c->regs[VCPU_REGS_RDI]);
1605 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1606 &c->dst.val,
1607 c->dst.bytes,
1608 ctxt->vcpu)) != 0)
1609 goto done;
1610
1611 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1612
1613 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1614
1615 register_address_increment(c->regs[VCPU_REGS_RSI],
1616 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1617 : c->src.bytes);
1618 register_address_increment(c->regs[VCPU_REGS_RDI],
1619 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1620 : c->dst.bytes);
1621
1622 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001624 c->dst.type = OP_MEM;
1625 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Sheng Yanga7e6c882007-11-15 14:52:28 +08001626 c->dst.ptr = (unsigned long *)register_address(
1627 ctxt->es_base,
1628 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001629 c->dst.val = c->regs[VCPU_REGS_RAX];
1630 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001631 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001632 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 break;
1634 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001635 c->dst.type = OP_REG;
1636 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1637 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Sheng Yanga7e6c882007-11-15 14:52:28 +08001638 if ((rc = ops->read_emulated(register_address(
1639 c->override_base ? *c->override_base :
1640 ctxt->ds_base,
1641 c->regs[VCPU_REGS_RSI]),
1642 &c->dst.val,
1643 c->dst.bytes,
1644 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001646 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001647 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001648 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 break;
1650 case 0xae ... 0xaf: /* scas */
1651 DPRINTF("Urk! I don't handle SCAS.\n");
1652 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001653 case 0xe8: /* call (near) */ {
1654 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001655 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001656 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001657 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001658 break;
1659 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001661 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001662 default:
1663 DPRINTF("Call: Invalid op_bytes\n");
1664 goto cannot_emulate;
1665 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001666 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001667 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001668 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001669 emulate_push(ctxt);
1670 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001671 }
1672 case 0xe9: /* jmp rel */
1673 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001674 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001675 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001676 break;
1677
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001678
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 }
1680 goto writeback;
1681
1682twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001683 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 u16 size;
1687 unsigned long address;
1688
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001689 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001690 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001691 goto cannot_emulate;
1692
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001693 rc = kvm_fix_hypercall(ctxt->vcpu);
1694 if (rc)
1695 goto done;
1696
1697 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001698 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001700 rc = read_descriptor(ctxt, ops, c->src.ptr,
1701 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 if (rc)
1703 goto done;
1704 realmode_lgdt(ctxt->vcpu, size, address);
1705 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001706 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001707 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001708 rc = kvm_fix_hypercall(ctxt->vcpu);
1709 if (rc)
1710 goto done;
1711 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001712 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001713 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001714 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001715 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001716 if (rc)
1717 goto done;
1718 realmode_lidt(ctxt->vcpu, size, address);
1719 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 break;
1721 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001722 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001724 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 = realmode_get_cr(ctxt->vcpu, 0);
1726 break;
1727 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001728 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 goto cannot_emulate;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001730 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1731 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 break;
1733 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001734 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 break;
1736 default:
1737 goto cannot_emulate;
1738 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001739 /* Disable writeback. */
1740 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741 break;
1742 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001743 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001746 if (rc)
1747 goto cannot_emulate;
1748 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749 break;
1750 case 0x23: /* mov from reg to dr */
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 rc = emulator_set_dr(ctxt, c->modrm_reg,
1754 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001755 if (rc)
1756 goto cannot_emulate;
1757 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 break;
1759 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001760 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001761 if (!test_cc(c->b, ctxt->eflags))
1762 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001764 case 0xa3:
1765 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001766 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001767 /* only subword offset */
1768 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001769 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001770 break;
1771 case 0xab:
1772 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001773 /* only subword offset */
1774 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001775 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001776 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777 case 0xb0 ... 0xb1: /* cmpxchg */
1778 /*
1779 * Save real source value, then compare EAX against
1780 * destination.
1781 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001782 c->src.orig_val = c->src.val;
1783 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001784 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1785 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001787 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 } else {
1789 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001790 c->dst.type = OP_REG;
1791 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792 }
1793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 case 0xb3:
1795 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001796 /* only subword offset */
1797 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001798 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001801 c->dst.bytes = c->op_bytes;
1802 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1803 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001806 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 case 0:
1808 goto bt;
1809 case 1:
1810 goto bts;
1811 case 2:
1812 goto btr;
1813 case 3:
1814 goto btc;
1815 }
1816 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001817 case 0xbb:
1818 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001819 /* only subword offset */
1820 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001821 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001822 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001824 c->dst.bytes = c->op_bytes;
1825 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1826 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001828 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001829 c->dst.bytes = c->op_bytes;
1830 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1831 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001832 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833 }
1834 goto writeback;
1835
1836twobyte_special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001837 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001838 case 0x06:
1839 emulate_clts(ctxt->vcpu);
1840 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001841 case 0x08: /* invd */
1842 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001843 case 0x09: /* wbinvd */
1844 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 case 0x0d: /* GrpP (prefetch) */
1846 case 0x18: /* Grp16 (prefetch/nop) */
1847 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 case 0x20: /* mov cr, reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001849 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001851 c->regs[c->modrm_rm] =
1852 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853 break;
1854 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001855 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001857 realmode_set_cr(ctxt->vcpu,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001858 c->modrm_reg, c->modrm_val, &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001860 case 0x30:
1861 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001862 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1863 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1864 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001865 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001866 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001867 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001868 }
1869 rc = X86EMUL_CONTINUE;
1870 break;
1871 case 0x32:
1872 /* rdmsr */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001874 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001875 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001876 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001877 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001878 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1879 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001880 }
1881 rc = X86EMUL_CONTINUE;
1882 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001883 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1884 long int rel;
1885
Laurent Viviere4e03de2007-09-18 11:52:50 +02001886 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001887 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001888 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001889 break;
1890 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001891 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001892 break;
1893 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001894 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001895 break;
1896 default:
1897 DPRINTF("jnz: Invalid op_bytes\n");
1898 goto cannot_emulate;
1899 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001900 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001901 JMP_REL(rel);
1902 break;
1903 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001905 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906 if (rc != 0)
1907 goto done;
1908 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001910 /* Disable writeback. */
1911 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 goto writeback;
1913
1914cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001915 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001916 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 return -1;
1918}