blob: c191093982d8345f1f727024e3c545ab7bcfba0b [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>
26#define DPRINTF(_f, _a ...) printf( _f , ## _a )
27#else
28#include "kvm.h"
29#define DPRINTF(x...) do {} while (0)
30#endif
31#include "x86_emulate.h"
32#include <linux/module.h>
33
34/*
35 * Opcode effective-address decode tables.
36 * Note that we only emulate instructions that have at least one memory
37 * operand (excluding implicit stack references). We assume that stack
38 * references and instruction fetches will never occur in special memory
39 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
40 * not be handled.
41 */
42
43/* Operand sizes: 8-bit operands or specified/overridden size. */
44#define ByteOp (1<<0) /* 8-bit operands. */
45/* Destination operand type. */
46#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
47#define DstReg (2<<1) /* Register operand. */
48#define DstMem (3<<1) /* Memory operand. */
49#define DstMask (3<<1)
50/* Source operand type. */
51#define SrcNone (0<<3) /* No source operand. */
52#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
53#define SrcReg (1<<3) /* Register operand. */
54#define SrcMem (2<<3) /* Memory operand. */
55#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
57#define SrcImm (5<<3) /* Immediate operand. */
58#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
59#define SrcMask (7<<3)
60/* Generic ModRM decode. */
61#define ModRM (1<<6)
62/* Destination is only written; never read. */
63#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080064#define BitOp (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
66static u8 opcode_table[256] = {
67 /* 0x00 - 0x07 */
68 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
69 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
70 0, 0, 0, 0,
71 /* 0x08 - 0x0F */
72 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
73 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
74 0, 0, 0, 0,
75 /* 0x10 - 0x17 */
76 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
77 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
78 0, 0, 0, 0,
79 /* 0x18 - 0x1F */
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
82 0, 0, 0, 0,
83 /* 0x20 - 0x27 */
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030086 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080087 /* 0x28 - 0x2F */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
90 0, 0, 0, 0,
91 /* 0x30 - 0x37 */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x38 - 0x3F */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
98 0, 0, 0, 0,
99 /* 0x40 - 0x4F */
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300101 /* 0x50 - 0x57 */
Nitin A Kamble7e778162007-08-19 11:07:06 +0300102 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
103 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300104 /* 0x58 - 0x5F */
105 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
106 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700107 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700109 0, 0, 0, 0,
110 /* 0x68 - 0x6F */
111 0, 0, ImplicitOps|Mov, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300112 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
113 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300114 /* 0x70 - 0x77 */
115 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
116 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
117 /* 0x78 - 0x7F */
118 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
119 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 /* 0x80 - 0x87 */
121 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
122 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
123 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
124 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
125 /* 0x88 - 0x8F */
126 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
127 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +0300128 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129 /* 0x90 - 0x9F */
Nitin A Kamble535eabc2007-09-15 10:45:05 +0300130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 /* 0xA0 - 0xA7 */
132 ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
133 ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
134 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
135 ByteOp | ImplicitOps, ImplicitOps,
136 /* 0xA8 - 0xAF */
137 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
138 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
139 ByteOp | ImplicitOps, ImplicitOps,
140 /* 0xB0 - 0xBF */
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300143 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
144 0, ImplicitOps, 0, 0,
145 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0xC8 - 0xCF */
147 0, 0, 0, 0, 0, 0, 0, 0,
148 /* 0xD0 - 0xD7 */
149 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
150 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
151 0, 0, 0, 0,
152 /* 0xD8 - 0xDF */
153 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300154 /* 0xE0 - 0xE7 */
155 0, 0, 0, 0, 0, 0, 0, 0,
156 /* 0xE8 - 0xEF */
Nitin A Kamblef6eed392007-08-28 18:08:37 -0700157 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0xF0 - 0xF7 */
159 0, 0, 0, 0,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300160 ImplicitOps, 0,
161 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0xF8 - 0xFF */
163 0, 0, 0, 0,
164 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
165};
166
Avi Kivity038e51d2007-01-22 20:40:40 -0800167static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0x00 - 0x0F */
169 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200170 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 /* 0x10 - 0x1F */
172 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
173 /* 0x20 - 0x2F */
174 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0,
176 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300177 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0x40 - 0x47 */
179 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
180 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
181 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183 /* 0x48 - 0x4F */
184 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
185 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
186 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
188 /* 0x50 - 0x5F */
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190 /* 0x60 - 0x6F */
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 /* 0x70 - 0x7F */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300195 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
196 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
197 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
198 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0x90 - 0x9F */
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800202 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800204 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xB0 - 0xB7 */
206 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800207 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
209 DstReg | SrcMem16 | ModRM | Mov,
210 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800211 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
213 DstReg | SrcMem16 | ModRM | Mov,
214 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800215 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
216 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 /* 0xD0 - 0xDF */
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0xE0 - 0xEF */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0xF0 - 0xFF */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
223};
224
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225/* EFLAGS bit definitions. */
226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
228#define EFLG_SF (1<<7)
229#define EFLG_ZF (1<<6)
230#define EFLG_AF (1<<4)
231#define EFLG_PF (1<<2)
232#define EFLG_CF (1<<0)
233
234/*
235 * Instruction emulation:
236 * Most instructions are emulated directly via a fragment of inline assembly
237 * code. This allows us to save/restore EFLAGS and thus very easily pick up
238 * any modified flags.
239 */
240
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800241#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242#define _LO32 "k" /* force 32-bit operand */
243#define _STK "%%rsp" /* stack pointer */
244#elif defined(__i386__)
245#define _LO32 "" /* force 32-bit operand */
246#define _STK "%%esp" /* stack pointer */
247#endif
248
249/*
250 * These EFLAGS bits are restored from saved value during emulation, and
251 * any changes are written back to the saved value after emulation.
252 */
253#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
254
255/* Before executing instruction: restore necessary bits in EFLAGS. */
256#define _PRE_EFLAGS(_sav, _msk, _tmp) \
257 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
258 "push %"_sav"; " \
259 "movl %"_msk",%"_LO32 _tmp"; " \
260 "andl %"_LO32 _tmp",("_STK"); " \
261 "pushf; " \
262 "notl %"_LO32 _tmp"; " \
263 "andl %"_LO32 _tmp",("_STK"); " \
264 "pop %"_tmp"; " \
265 "orl %"_LO32 _tmp",("_STK"); " \
266 "popf; " \
267 /* _sav &= ~msk; */ \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "notl %"_LO32 _tmp"; " \
270 "andl %"_LO32 _tmp",%"_sav"; "
271
272/* After executing instruction: write-back necessary bits in EFLAGS. */
273#define _POST_EFLAGS(_sav, _msk, _tmp) \
274 /* _sav |= EFLAGS & _msk; */ \
275 "pushf; " \
276 "pop %"_tmp"; " \
277 "andl %"_msk",%"_LO32 _tmp"; " \
278 "orl %"_LO32 _tmp",%"_sav"; "
279
280/* Raw emulation: instruction has two explicit operands. */
281#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
282 do { \
283 unsigned long _tmp; \
284 \
285 switch ((_dst).bytes) { \
286 case 2: \
287 __asm__ __volatile__ ( \
288 _PRE_EFLAGS("0","4","2") \
289 _op"w %"_wx"3,%1; " \
290 _POST_EFLAGS("0","4","2") \
291 : "=m" (_eflags), "=m" ((_dst).val), \
292 "=&r" (_tmp) \
293 : _wy ((_src).val), "i" (EFLAGS_MASK) ); \
294 break; \
295 case 4: \
296 __asm__ __volatile__ ( \
297 _PRE_EFLAGS("0","4","2") \
298 _op"l %"_lx"3,%1; " \
299 _POST_EFLAGS("0","4","2") \
300 : "=m" (_eflags), "=m" ((_dst).val), \
301 "=&r" (_tmp) \
302 : _ly ((_src).val), "i" (EFLAGS_MASK) ); \
303 break; \
304 case 8: \
305 __emulate_2op_8byte(_op, _src, _dst, \
306 _eflags, _qx, _qy); \
307 break; \
308 } \
309 } while (0)
310
311#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
312 do { \
313 unsigned long _tmp; \
314 switch ( (_dst).bytes ) \
315 { \
316 case 1: \
317 __asm__ __volatile__ ( \
318 _PRE_EFLAGS("0","4","2") \
319 _op"b %"_bx"3,%1; " \
320 _POST_EFLAGS("0","4","2") \
321 : "=m" (_eflags), "=m" ((_dst).val), \
322 "=&r" (_tmp) \
323 : _by ((_src).val), "i" (EFLAGS_MASK) ); \
324 break; \
325 default: \
326 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
327 _wx, _wy, _lx, _ly, _qx, _qy); \
328 break; \
329 } \
330 } while (0)
331
332/* Source operand is byte-sized and may be restricted to just %cl. */
333#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
334 __emulate_2op(_op, _src, _dst, _eflags, \
335 "b", "c", "b", "c", "b", "c", "b", "c")
336
337/* Source operand is byte, word, long or quad sized. */
338#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
339 __emulate_2op(_op, _src, _dst, _eflags, \
340 "b", "q", "w", "r", _LO32, "r", "", "r")
341
342/* Source operand is word, long or quad sized. */
343#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
344 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
345 "w", "r", _LO32, "r", "", "r")
346
347/* Instruction has only one explicit operand (no source operand). */
348#define emulate_1op(_op, _dst, _eflags) \
349 do { \
350 unsigned long _tmp; \
351 \
352 switch ( (_dst).bytes ) \
353 { \
354 case 1: \
355 __asm__ __volatile__ ( \
356 _PRE_EFLAGS("0","3","2") \
357 _op"b %1; " \
358 _POST_EFLAGS("0","3","2") \
359 : "=m" (_eflags), "=m" ((_dst).val), \
360 "=&r" (_tmp) \
361 : "i" (EFLAGS_MASK) ); \
362 break; \
363 case 2: \
364 __asm__ __volatile__ ( \
365 _PRE_EFLAGS("0","3","2") \
366 _op"w %1; " \
367 _POST_EFLAGS("0","3","2") \
368 : "=m" (_eflags), "=m" ((_dst).val), \
369 "=&r" (_tmp) \
370 : "i" (EFLAGS_MASK) ); \
371 break; \
372 case 4: \
373 __asm__ __volatile__ ( \
374 _PRE_EFLAGS("0","3","2") \
375 _op"l %1; " \
376 _POST_EFLAGS("0","3","2") \
377 : "=m" (_eflags), "=m" ((_dst).val), \
378 "=&r" (_tmp) \
379 : "i" (EFLAGS_MASK) ); \
380 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__ ( \
392 _PRE_EFLAGS("0","4","2") \
393 _op"q %"_qx"3,%1; " \
394 _POST_EFLAGS("0","4","2") \
395 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
396 : _qy ((_src).val), "i" (EFLAGS_MASK) ); \
397 } while (0)
398
399#define __emulate_1op_8byte(_op, _dst, _eflags) \
400 do { \
401 __asm__ __volatile__ ( \
402 _PRE_EFLAGS("0","3","2") \
403 _op"q %1; " \
404 _POST_EFLAGS("0","3","2") \
405 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
406 : "i" (EFLAGS_MASK) ); \
407 } 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, \
Laurent Viviercebff022007-07-30 13:35:24 +0300418 (_size), ctxt->vcpu); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 if ( rc != 0 ) \
420 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 Kivity6aa8b732006-12-10 02:21:36 -0800519int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200520x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200522 struct decode_cache *c = &ctxt->decode;
523 u8 sib, rex_prefix = 0;
524 unsigned int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 int mode = ctxt->mode;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200527 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528
529 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
Laurent Viviere4e03de2007-09-18 11:52:50 +0200531 memset(c, 0, sizeof(struct decode_cache));
532 c->eip = ctxt->vcpu->rip;
533 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534
535 switch (mode) {
536 case X86EMUL_MODE_REAL:
537 case X86EMUL_MODE_PROT16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200538 c->op_bytes = c->ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 break;
540 case X86EMUL_MODE_PROT32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200541 c->op_bytes = c->ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800543#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 case X86EMUL_MODE_PROT64:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200545 c->op_bytes = 4;
546 c->ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 break;
548#endif
549 default:
550 return -1;
551 }
552
553 /* Legacy prefixes. */
554 for (i = 0; i < 8; i++) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200555 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 case 0x66: /* operand-size override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200557 c->op_bytes ^= 6; /* switch between 2/4 bytes */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 break;
559 case 0x67: /* address-size override */
560 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200561 /* switch between 4/8 bytes */
562 c->ad_bytes ^= 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200564 /* switch between 2/4 bytes */
565 c->ad_bytes ^= 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 break;
567 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200568 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 break;
570 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200571 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 break;
573 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200574 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 break;
576 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200577 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 break;
579 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200580 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 break;
582 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200583 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 break;
585 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200586 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200588 case 0xf2: /* REPNE/REPNZ */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 case 0xf3: /* REP/REPE/REPZ */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200590 c->rep_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 default:
593 goto done_prefixes;
594 }
595 }
596
597done_prefixes:
598
599 /* REX prefix. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200600 if ((mode == X86EMUL_MODE_PROT64) && ((c->b & 0xf0) == 0x40)) {
601 rex_prefix = c->b;
602 if (c->b & 8)
603 c->op_bytes = 8; /* REX.W */
604 c->modrm_reg = (c->b & 4) << 1; /* REX.R */
605 index_reg = (c->b & 2) << 2; /* REX.X */
606 c->modrm_rm = base_reg = (c->b & 1) << 3; /* REG.B */
607 c->b = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 }
609
610 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200611 c->d = opcode_table[c->b];
612 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200614 if (c->b == 0x0f) {
615 c->twobyte = 1;
616 c->b = insn_fetch(u8, 1, c->eip);
617 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 }
619
620 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200621 if (c->d == 0) {
622 DPRINTF("Cannot emulate %02x\n", c->b);
623 return -1;
624 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 }
626
627 /* ModRM and SIB bytes. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200628 if (c->d & ModRM) {
629 c->modrm = insn_fetch(u8, 1, c->eip);
630 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
631 c->modrm_reg |= (c->modrm & 0x38) >> 3;
632 c->modrm_rm |= (c->modrm & 0x07);
633 c->modrm_ea = 0;
634 c->use_modrm_ea = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635
Laurent Viviere4e03de2007-09-18 11:52:50 +0200636 if (c->modrm_mod == 3) {
637 c->modrm_val = *(unsigned long *)
638 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 goto modrm_done;
640 }
641
Laurent Viviere4e03de2007-09-18 11:52:50 +0200642 if (c->ad_bytes == 2) {
643 unsigned bx = c->regs[VCPU_REGS_RBX];
644 unsigned bp = c->regs[VCPU_REGS_RBP];
645 unsigned si = c->regs[VCPU_REGS_RSI];
646 unsigned di = c->regs[VCPU_REGS_RDI];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647
648 /* 16-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200649 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200651 if (c->modrm_rm == 6)
652 c->modrm_ea +=
653 insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 break;
655 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200656 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 break;
658 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200659 c->modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 break;
661 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200662 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200664 c->modrm_ea += bx + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 break;
666 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200667 c->modrm_ea += bx + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 break;
669 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200670 c->modrm_ea += bp + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 break;
672 case 3:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200673 c->modrm_ea += bp + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674 break;
675 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200676 c->modrm_ea += si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 break;
678 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200679 c->modrm_ea += di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 break;
681 case 6:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200682 if (c->modrm_mod != 0)
683 c->modrm_ea += bp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 break;
685 case 7:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200686 c->modrm_ea += bx;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 break;
688 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200689 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
690 (c->modrm_rm == 6 && c->modrm_mod != 0))
691 if (!c->override_base)
692 c->override_base = &ctxt->ss_base;
693 c->modrm_ea = (u16)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 } else {
695 /* 32/64-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200696 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 case 4:
698 case 12:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200699 sib = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 index_reg |= (sib >> 3) & 7;
701 base_reg |= sib & 7;
702 scale = sib >> 6;
703
704 switch (base_reg) {
705 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200706 if (c->modrm_mod != 0)
707 c->modrm_ea +=
708 c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200710 c->modrm_ea +=
711 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 break;
713 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200714 c->modrm_ea += c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 }
716 switch (index_reg) {
717 case 4:
718 break;
719 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200720 c->modrm_ea +=
721 c->regs[index_reg] << scale;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722
723 }
724 break;
725 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200726 if (c->modrm_mod != 0)
727 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 else if (mode == X86EMUL_MODE_PROT64)
729 rip_relative = 1;
730 break;
731 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200732 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 break;
734 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200735 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200737 if (c->modrm_rm == 5)
738 c->modrm_ea +=
739 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 break;
741 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200742 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 break;
744 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200745 c->modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 break;
747 }
748 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200749 if (!c->override_base)
750 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 if (mode == X86EMUL_MODE_PROT64 &&
Laurent Viviere4e03de2007-09-18 11:52:50 +0200752 c->override_base != &ctxt->fs_base &&
753 c->override_base != &ctxt->gs_base)
754 c->override_base = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755
Laurent Viviere4e03de2007-09-18 11:52:50 +0200756 if (c->override_base)
757 c->modrm_ea += *c->override_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758
759 if (rip_relative) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200760 c->modrm_ea += c->eip;
761 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200763 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 break;
765 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200766 if (c->d & ByteOp)
767 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200769 if (c->op_bytes == 8)
770 c->modrm_ea += 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200772 c->modrm_ea += c->op_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 }
774 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200775 if (c->ad_bytes != 8)
776 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 modrm_done:
778 ;
779 }
780
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 /*
782 * Decode and fetch the source operand: register, memory
783 * or immediate.
784 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200785 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 case SrcNone:
787 break;
788 case SrcReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200789 c->src.type = OP_REG;
790 if (c->d & ByteOp) {
791 c->src.ptr =
792 decode_register(c->modrm_reg, c->regs,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200794 c->src.val = c->src.orig_val = *(u8 *)c->src.ptr;
795 c->src.bytes = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200797 c->src.ptr =
798 decode_register(c->modrm_reg, c->regs, 0);
799 switch ((c->src.bytes = c->op_bytes)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200801 c->src.val = c->src.orig_val =
802 *(u16 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 break;
804 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200805 c->src.val = c->src.orig_val =
806 *(u32 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 break;
808 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200809 c->src.val = c->src.orig_val =
810 *(u64 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 break;
812 }
813 }
814 break;
815 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200816 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 goto srcmem_common;
818 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200819 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 goto srcmem_common;
821 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200822 c->src.bytes = (c->d & ByteOp) ? 1 :
823 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300824 /* Don't fetch the address for invlpg: it could be unmapped. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200825 if (c->twobyte && c->b == 0x01
826 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300827 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200829 /*
830 * For instructions with a ModR/M byte, switch to register
831 * access if Mod = 3.
832 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200833 if ((c->d & ModRM) && c->modrm_mod == 3) {
834 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200835 break;
836 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200837 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 break;
839 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200840 c->src.type = OP_IMM;
841 c->src.ptr = (unsigned long *)c->eip;
842 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
843 if (c->src.bytes == 8)
844 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200846 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200848 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 break;
850 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200851 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 break;
853 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200854 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 break;
856 }
857 break;
858 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200859 c->src.type = OP_IMM;
860 c->src.ptr = (unsigned long *)c->eip;
861 c->src.bytes = 1;
862 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 break;
864 }
865
Avi Kivity038e51d2007-01-22 20:40:40 -0800866 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200867 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800868 case ImplicitOps:
869 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200870 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800871 case DstReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200872 c->dst.type = OP_REG;
873 if ((c->d & ByteOp)
874 && !(c->twobyte &&
875 (c->b == 0xb6 || c->b == 0xb7))) {
876 c->dst.ptr =
877 decode_register(c->modrm_reg, c->regs,
Avi Kivity038e51d2007-01-22 20:40:40 -0800878 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200879 c->dst.val = *(u8 *) c->dst.ptr;
880 c->dst.bytes = 1;
Avi Kivity038e51d2007-01-22 20:40:40 -0800881 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200882 c->dst.ptr =
883 decode_register(c->modrm_reg, c->regs, 0);
884 switch ((c->dst.bytes = c->op_bytes)) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800885 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200886 c->dst.val = *(u16 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800887 break;
888 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 c->dst.val = *(u32 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800890 break;
891 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 c->dst.val = *(u64 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800893 break;
894 }
895 }
896 break;
897 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200898 if ((c->d & ModRM) && c->modrm_mod == 3) {
899 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200900 break;
901 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200902 c->dst.type = OP_MEM;
903 break;
904 }
905
906done:
907 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
908}
909
910int
Laurent Vivier1be3aa42007-09-18 11:27:27 +0200911x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200912{
913 unsigned long cr2 = ctxt->cr2;
914 int no_wb = 0;
915 u64 msr_data;
916 unsigned long _eflags = ctxt->eflags;
917 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +0200918 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200919
920 if ((c->d & ModRM) && (c->modrm_mod != 3))
921 cr2 = c->modrm_ea;
922
923 if (c->src.type == OP_MEM) {
924 c->src.ptr = (unsigned long *)cr2;
925 c->src.val = 0;
926 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
927 &c->src.val,
928 c->src.bytes,
929 ctxt->vcpu)) != 0)
930 goto done;
931 c->src.orig_val = c->src.val;
932 }
933
934 if ((c->d & DstMask) == ImplicitOps)
935 goto special_insn;
936
937
938 if (c->dst.type == OP_MEM) {
939 c->dst.ptr = (unsigned long *)cr2;
940 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
941 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200942 if (c->d & BitOp) {
943 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +0200944
Laurent Viviere4e03de2007-09-18 11:52:50 +0200945 c->dst.ptr = (void *)c->dst.ptr +
946 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -0800947 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200948 if (!(c->d & Mov) &&
949 /* optimisation - avoid slow emulated read */
950 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
951 &c->dst.val,
952 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -0800953 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -0800954 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200955 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -0800956
Laurent Viviere4e03de2007-09-18 11:52:50 +0200957 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 goto twobyte_insn;
959
Laurent Viviere4e03de2007-09-18 11:52:50 +0200960 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 case 0x00 ... 0x05:
962 add: /* add */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200963 emulate_2op_SrcV("add", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 break;
965 case 0x08 ... 0x0d:
966 or: /* or */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 emulate_2op_SrcV("or", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 break;
969 case 0x10 ... 0x15:
970 adc: /* adc */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200971 emulate_2op_SrcV("adc", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 break;
973 case 0x18 ... 0x1d:
974 sbb: /* sbb */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200975 emulate_2op_SrcV("sbb", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300977 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 and: /* and */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200979 emulate_2op_SrcV("and", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300981 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200982 c->dst.type = OP_REG;
983 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
984 c->dst.val = *(u8 *)c->dst.ptr;
985 c->dst.bytes = 1;
986 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300987 goto and;
988 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200989 c->dst.type = OP_REG;
990 c->dst.bytes = c->op_bytes;
991 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
992 if (c->op_bytes == 2)
993 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300994 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200995 c->dst.val = *(u32 *)c->dst.ptr;
996 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300997 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 case 0x28 ... 0x2d:
999 sub: /* sub */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001000 emulate_2op_SrcV("sub", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 break;
1002 case 0x30 ... 0x35:
1003 xor: /* xor */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001004 emulate_2op_SrcV("xor", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 break;
1006 case 0x38 ... 0x3d:
1007 cmp: /* cmp */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 emulate_2op_SrcV("cmp", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 break;
1010 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001011 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 break;
1015 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 case 0:
1018 goto add;
1019 case 1:
1020 goto or;
1021 case 2:
1022 goto adc;
1023 case 3:
1024 goto sbb;
1025 case 4:
1026 goto and;
1027 case 5:
1028 goto sub;
1029 case 6:
1030 goto xor;
1031 case 7:
1032 goto cmp;
1033 }
1034 break;
1035 case 0x84 ... 0x85:
1036 test: /* test */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 emulate_2op_SrcV("test", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 break;
1039 case 0x86 ... 0x87: /* xchg */
1040 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
1045 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 break;
1048 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001049 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 break; /* 64b reg: zero-extend */
1051 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001052 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 break;
1054 }
1055 /*
1056 * Write back the memory destination with implicit LOCK
1057 * prefix.
1058 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001059 c->dst.val = c->src.val;
1060 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001063 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001064 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001065 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001066 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 case 0x8f: /* pop (sole member of Grp1a) */
1068 /* 64-bit mode: POP always pops a 64-bit operand. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001069 if (ctxt->mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 c->dst.bytes = 8;
1071 if ((rc = ops->read_std(register_address(
1072 ctxt->ss_base,
1073 c->regs[VCPU_REGS_RSP]),
1074 &c->dst.val,
1075 c->dst.bytes,
1076 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001078 register_address_increment(c->regs[VCPU_REGS_RSP],
1079 c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001081 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001082 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1083 c->dst.val = c->src.val;
1084 /* skip src displacement */
1085 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001086 break;
1087 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001088 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1089 /* skip c->dst displacement */
1090 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001091 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 case 0xc0 ... 0xc1:
1093 grp2: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001094 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 case 0: /* rol */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001096 emulate_2op_SrcB("rol", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 break;
1098 case 1: /* ror */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001099 emulate_2op_SrcB("ror", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 break;
1101 case 2: /* rcl */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001102 emulate_2op_SrcB("rcl", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 break;
1104 case 3: /* rcr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001105 emulate_2op_SrcB("rcr", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 break;
1107 case 4: /* sal/shl */
1108 case 6: /* sal/shl */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001109 emulate_2op_SrcB("sal", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 break;
1111 case 5: /* shr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001112 emulate_2op_SrcB("shr", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 break;
1114 case 7: /* sar */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001115 emulate_2op_SrcB("sar", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 break;
1117 }
1118 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001119 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1120 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001122 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001124 c->src.val = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 goto grp2;
1126 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001127 c->src.val = c->regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 goto grp2;
1129 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 case 0 ... 1: /* test */
1132 /*
1133 * Special case in Grp3: test has an immediate
1134 * source operand.
1135 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001136 c->src.type = OP_IMM;
1137 c->src.ptr = (unsigned long *)c->eip;
1138 c->src.bytes = (c->d & ByteOp) ? 1 :
1139 c->op_bytes;
1140 if (c->src.bytes == 8)
1141 c->src.bytes = 4;
1142 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001144 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 break;
1146 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001147 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 break;
1149 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001150 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 break;
1152 }
1153 goto test;
1154 case 2: /* not */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001155 c->dst.val = ~c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 break;
1157 case 3: /* neg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001158 emulate_1op("neg", c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 break;
1160 default:
1161 goto cannot_emulate;
1162 }
1163 break;
1164 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001165 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 case 0: /* inc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001167 emulate_1op("inc", c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 break;
1169 case 1: /* dec */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001170 emulate_1op("dec", c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 break;
Nitin A Kamble26a3e982007-09-15 10:41:26 +03001172 case 4: /* jmp abs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001173 if (c->b == 0xff)
1174 c->eip = c->dst.val;
Nitin A Kamble26a3e982007-09-15 10:41:26 +03001175 else
1176 goto cannot_emulate;
1177 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 case 6: /* push */
1179 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001180 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001181 c->dst.bytes = 8;
1182 if ((rc = ops->read_std(
1183 (unsigned long)c->dst.ptr,
1184 &c->dst.val, 8,
1185 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 goto done;
1187 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001188 register_address_increment(c->regs[VCPU_REGS_RSP],
1189 -c->dst.bytes);
Amit Shah00b2ef42007-11-18 22:25:40 +05301190 if ((rc = ops->write_emulated(
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 register_address(ctxt->ss_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001192 c->regs[VCPU_REGS_RSP]),
1193 &c->dst.val,
1194 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 goto done;
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001196 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197 break;
1198 default:
1199 goto cannot_emulate;
1200 }
1201 break;
1202 }
1203
1204writeback:
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001205 if (!no_wb) {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001206 switch (c->dst.type) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207 case OP_REG:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001208 /* The 4-byte case *is* correct:
1209 * in 64-bit mode we zero-extend.
1210 */
1211 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001213 *(u8 *)c->dst.ptr = (u8)c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 break;
1215 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001216 *(u16 *)c->dst.ptr = (u16)c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 break;
1218 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001219 *c->dst.ptr = (u32)c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 break; /* 64b: zero-ext */
1221 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001222 *c->dst.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 break;
1224 }
1225 break;
1226 case OP_MEM:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001227 if (c->lock_prefix)
1228 rc = ops->cmpxchg_emulated(
1229 (unsigned long)c->dst.ptr,
1230 &c->dst.orig_val,
1231 &c->dst.val,
1232 c->dst.bytes,
1233 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001235 rc = ops->write_emulated(
1236 (unsigned long)c->dst.ptr,
1237 &c->dst.val,
1238 c->dst.bytes,
1239 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 if (rc != 0)
1241 goto done;
1242 default:
1243 break;
1244 }
1245 }
1246
1247 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001248 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 ctxt->eflags = _eflags;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001250 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251
1252done:
1253 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1254
1255special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001256 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001258 switch (c->b) {
Nitin A Kamble7e778162007-08-19 11:07:06 +03001259 case 0x50 ... 0x57: /* push reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001260 if (c->op_bytes == 2)
1261 c->src.val = (u16) c->regs[c->b & 0x7];
Nitin A Kamble7e778162007-08-19 11:07:06 +03001262 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001263 c->src.val = (u32) c->regs[c->b & 0x7];
1264 c->dst.type = OP_MEM;
1265 c->dst.bytes = c->op_bytes;
1266 c->dst.val = c->src.val;
1267 register_address_increment(c->regs[VCPU_REGS_RSP],
1268 -c->op_bytes);
1269 c->dst.ptr = (void *) register_address(
1270 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Nitin A Kamble7e778162007-08-19 11:07:06 +03001271 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001272 case 0x58 ... 0x5f: /* pop reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001273 c->dst.ptr =
1274 (unsigned long *)&c->regs[c->b & 0x7];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001275 pop_instruction:
1276 if ((rc = ops->read_std(register_address(ctxt->ss_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001277 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1278 c->op_bytes, ctxt->vcpu)) != 0)
Nitin A Kamble7de75242007-09-15 10:13:07 +03001279 goto done;
1280
Laurent Viviere4e03de2007-09-18 11:52:50 +02001281 register_address_increment(c->regs[VCPU_REGS_RSP],
1282 c->op_bytes);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001283 no_wb = 1; /* Disable writeback. */
1284 break;
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001285 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001286 c->src.val = 0L;
1287 c->src.val = insn_fetch(s8, 1, c->eip);
1288push:
1289 c->dst.type = OP_MEM;
1290 c->dst.bytes = c->op_bytes;
1291 c->dst.val = c->src.val;
1292 register_address_increment(c->regs[VCPU_REGS_RSP],
1293 -c->op_bytes);
1294 c->dst.ptr = (void *) register_address(ctxt->ss_base,
1295 c->regs[VCPU_REGS_RSP]);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001296 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001297 case 0x6c: /* insb */
1298 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001299 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001300 1,
1301 (c->d & ByteOp) ? 1 : c->op_bytes,
1302 c->rep_prefix ?
1303 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1304 (_eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001305 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001306 c->regs[VCPU_REGS_RDI]),
1307 c->rep_prefix,
1308 c->regs[VCPU_REGS_RDX]) == 0)
Laurent Viviere70669a2007-08-05 10:36:40 +03001309 return -1;
1310 return 0;
1311 case 0x6e: /* outsb */
1312 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001313 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001314 0,
1315 (c->d & ByteOp) ? 1 : c->op_bytes,
1316 c->rep_prefix ?
1317 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1318 (_eflags & EFLG_DF),
1319 register_address(c->override_base ?
1320 *c->override_base :
1321 ctxt->ds_base,
1322 c->regs[VCPU_REGS_RSI]),
1323 c->rep_prefix,
1324 c->regs[VCPU_REGS_RDX]) == 0)
Laurent Viviere70669a2007-08-05 10:36:40 +03001325 return -1;
1326 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001327 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001328 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001329
Laurent Viviere4e03de2007-09-18 11:52:50 +02001330 if (test_cc(c->b, _eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001331 JMP_REL(rel);
1332 break;
1333 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001334 case 0x9c: /* pushf */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001335 c->src.val = (unsigned long) _eflags;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001336 goto push;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001337 case 0x9d: /* popf */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001338 c->dst.ptr = (unsigned long *) &_eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001339 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001340 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001342 goto pop_instruction;
1343 case 0xf4: /* hlt */
1344 ctxt->vcpu->halt_request = 1;
1345 goto done;
Laurent Viviere70669a2007-08-05 10:36:40 +03001346 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001347 if (c->rep_prefix) {
1348 if (c->regs[VCPU_REGS_RCX] == 0) {
1349 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 goto done;
1351 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001352 c->regs[VCPU_REGS_RCX]--;
1353 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001355 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001357 c->dst.type = OP_MEM;
1358 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1359 c->dst.ptr = (unsigned long *)register_address(
1360 ctxt->es_base,
1361 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001363 c->override_base ? *c->override_base :
1364 ctxt->ds_base,
1365 c->regs[VCPU_REGS_RSI]),
1366 &c->dst.val,
1367 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001369 register_address_increment(c->regs[VCPU_REGS_RSI],
1370 (_eflags & EFLG_DF) ? -c->dst.bytes
1371 : c->dst.bytes);
1372 register_address_increment(c->regs[VCPU_REGS_RDI],
1373 (_eflags & EFLG_DF) ? -c->dst.bytes
1374 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 break;
1376 case 0xa6 ... 0xa7: /* cmps */
1377 DPRINTF("Urk! I don't handle CMPS.\n");
1378 goto cannot_emulate;
1379 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001380 c->dst.type = OP_MEM;
1381 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1382 c->dst.ptr = (unsigned long *)cr2;
1383 c->dst.val = c->regs[VCPU_REGS_RAX];
1384 register_address_increment(c->regs[VCPU_REGS_RDI],
1385 (_eflags & EFLG_DF) ? -c->dst.bytes
1386 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 break;
1388 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001389 c->dst.type = OP_REG;
1390 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1391 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1392 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1393 c->dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001394 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001396 register_address_increment(c->regs[VCPU_REGS_RSI],
1397 (_eflags & EFLG_DF) ? -c->dst.bytes
1398 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 break;
1400 case 0xae ... 0xaf: /* scas */
1401 DPRINTF("Urk! I don't handle SCAS.\n");
1402 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001403 case 0xe8: /* call (near) */ {
1404 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001405 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001406 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001407 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001408 break;
1409 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001410 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001411 break;
1412 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001413 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001414 break;
1415 default:
1416 DPRINTF("Call: Invalid op_bytes\n");
1417 goto cannot_emulate;
1418 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001419 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001420 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001421 c->op_bytes = c->ad_bytes;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001422 goto push;
1423 }
1424 case 0xe9: /* jmp rel */
1425 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001426 JMP_REL(c->src.val);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001427 no_wb = 1; /* Disable writeback. */
1428 break;
1429
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001430
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 }
1432 goto writeback;
1433
1434twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001435 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 case 0x01: /* lgdt, lidt, lmsw */
Aurelien Jarnod37c8552007-07-25 10:19:54 +02001437 /* Disable writeback. */
1438 no_wb = 1;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001439 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 u16 size;
1441 unsigned long address;
1442
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001443 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001444 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001445 goto cannot_emulate;
1446
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001447 rc = kvm_fix_hypercall(ctxt->vcpu);
1448 if (rc)
1449 goto done;
1450
1451 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001454 rc = read_descriptor(ctxt, ops, c->src.ptr,
1455 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 if (rc)
1457 goto done;
1458 realmode_lgdt(ctxt->vcpu, size, address);
1459 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001460 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001461 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001462 rc = kvm_fix_hypercall(ctxt->vcpu);
1463 if (rc)
1464 goto done;
1465 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001466 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001467 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001468 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001469 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001470 if (rc)
1471 goto done;
1472 realmode_lidt(ctxt->vcpu, size, address);
1473 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474 break;
1475 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001476 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001478 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 = realmode_get_cr(ctxt->vcpu, 0);
1480 break;
1481 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001482 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001484 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val, &_eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 break;
1486 case 7: /* invlpg*/
1487 emulate_invlpg(ctxt->vcpu, cr2);
1488 break;
1489 default:
1490 goto cannot_emulate;
1491 }
1492 break;
1493 case 0x21: /* mov from dr to reg */
Avi Kivitybac27d32007-08-05 10:16:11 +03001494 no_wb = 1;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001495 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001497 rc = emulator_get_dr(ctxt, c->modrm_reg,
1498 &c->regs[c->modrm_rm]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 break;
1500 case 0x23: /* mov from reg to dr */
Avi Kivitybac27d32007-08-05 10:16:11 +03001501 no_wb = 1;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001502 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001504 rc = emulator_set_dr(ctxt, c->modrm_reg,
1505 c->regs[c->modrm_rm]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 break;
1507 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001508 c->dst.val = c->dst.orig_val = c->src.val;
Avi Kivitye3243452007-07-20 12:30:58 +03001509 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 /*
1511 * First, assume we're decoding an even cmov opcode
1512 * (lsb == 0).
1513 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001514 switch ((c->b & 15) >> 1) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 case 0: /* cmovo */
Avi Kivitye3243452007-07-20 12:30:58 +03001516 no_wb = (_eflags & EFLG_OF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 break;
1518 case 1: /* cmovb/cmovc/cmovnae */
Avi Kivitye3243452007-07-20 12:30:58 +03001519 no_wb = (_eflags & EFLG_CF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 break;
1521 case 2: /* cmovz/cmove */
Avi Kivitye3243452007-07-20 12:30:58 +03001522 no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 break;
1524 case 3: /* cmovbe/cmovna */
Avi Kivitye3243452007-07-20 12:30:58 +03001525 no_wb = (_eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 break;
1527 case 4: /* cmovs */
Avi Kivitye3243452007-07-20 12:30:58 +03001528 no_wb = (_eflags & EFLG_SF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529 break;
1530 case 5: /* cmovp/cmovpe */
Avi Kivitye3243452007-07-20 12:30:58 +03001531 no_wb = (_eflags & EFLG_PF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 break;
1533 case 7: /* cmovle/cmovng */
Avi Kivitye3243452007-07-20 12:30:58 +03001534 no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 /* fall through */
1536 case 6: /* cmovl/cmovnge */
Avi Kivitye3243452007-07-20 12:30:58 +03001537 no_wb &= (!(_eflags & EFLG_SF) !=
1538 !(_eflags & EFLG_OF)) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539 break;
1540 }
1541 /* Odd cmov opcodes (lsb == 1) have inverted sense. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001542 no_wb ^= c->b & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001544 case 0xa3:
1545 bt: /* bt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001546 /* only subword offset */
1547 c->src.val &= (c->dst.bytes << 3) - 1;
1548 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, _eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001549 break;
1550 case 0xab:
1551 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001552 /* only subword offset */
1553 c->src.val &= (c->dst.bytes << 3) - 1;
1554 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, _eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001555 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 case 0xb0 ... 0xb1: /* cmpxchg */
1557 /*
1558 * Save real source value, then compare EAX against
1559 * destination.
1560 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001561 c->src.orig_val = c->src.val;
1562 c->src.val = c->regs[VCPU_REGS_RAX];
1563 emulate_2op_SrcV("cmp", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 if (_eflags & EFLG_ZF) {
1565 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001566 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 } else {
1568 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001569 c->dst.type = OP_REG;
1570 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 }
1572 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 case 0xb3:
1574 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 /* only subword offset */
1576 c->src.val &= (c->dst.bytes << 3) - 1;
1577 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001580 c->dst.bytes = c->op_bytes;
1581 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1582 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 case 0:
1587 goto bt;
1588 case 1:
1589 goto bts;
1590 case 2:
1591 goto btr;
1592 case 3:
1593 goto btc;
1594 }
1595 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001596 case 0xbb:
1597 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001598 /* only subword offset */
1599 c->src.val &= (c->dst.bytes << 3) - 1;
1600 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, _eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001601 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001603 c->dst.bytes = c->op_bytes;
1604 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1605 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001607 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001608 c->dst.bytes = c->op_bytes;
1609 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1610 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001611 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 }
1613 goto writeback;
1614
1615twobyte_special_insn:
1616 /* Disable writeback. */
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001617 no_wb = 1;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001618 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001619 case 0x06:
1620 emulate_clts(ctxt->vcpu);
1621 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001622 case 0x08: /* invd */
1623 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001624 case 0x09: /* wbinvd */
1625 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 case 0x0d: /* GrpP (prefetch) */
1627 case 0x18: /* Grp16 (prefetch/nop) */
1628 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 case 0x20: /* mov cr, reg */
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 Viviere4e03de2007-09-18 11:52:50 +02001632 c->regs[c->modrm_rm] =
1633 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 break;
1635 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001636 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001638 realmode_set_cr(ctxt->vcpu,
1639 c->modrm_reg, c->modrm_val, &_eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001641 case 0x30:
1642 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001643 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1644 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1645 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001646 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001647 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001648 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001649 }
1650 rc = X86EMUL_CONTINUE;
1651 break;
1652 case 0x32:
1653 /* rdmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001654 rc = kvm_get_msr(ctxt->vcpu,
1655 c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001656 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001657 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001658 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001659 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1661 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001662 }
1663 rc = X86EMUL_CONTINUE;
1664 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001665 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1666 long int rel;
1667
Laurent Viviere4e03de2007-09-18 11:52:50 +02001668 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001669 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001670 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001671 break;
1672 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001673 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001674 break;
1675 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001676 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001677 break;
1678 default:
1679 DPRINTF("jnz: Invalid op_bytes\n");
1680 goto cannot_emulate;
1681 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 if (test_cc(c->b, _eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001683 JMP_REL(rel);
1684 break;
1685 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 case 0xc7: /* Grp9 (cmpxchg8b) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687 {
Avi Kivity4c690a12007-04-22 15:28:19 +03001688 u64 old, new;
Laurent Viviercebff022007-07-30 13:35:24 +03001689 if ((rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu))
1690 != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001692 if (((u32) (old >> 0) !=
1693 (u32) c->regs[VCPU_REGS_RAX]) ||
1694 ((u32) (old >> 32) !=
1695 (u32) c->regs[VCPU_REGS_RDX])) {
1696 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1697 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 _eflags &= ~EFLG_ZF;
1699 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001700 new = ((u64)c->regs[VCPU_REGS_RCX] << 32)
1701 | (u32) c->regs[VCPU_REGS_RBX];
Avi Kivity4c690a12007-04-22 15:28:19 +03001702 if ((rc = ops->cmpxchg_emulated(cr2, &old,
Laurent Viviercebff022007-07-30 13:35:24 +03001703 &new, 8, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 goto done;
1705 _eflags |= EFLG_ZF;
1706 }
1707 break;
1708 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 }
1710 goto writeback;
1711
1712cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001713 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 return -1;
1715}