blob: 988c6498640f6d276af5fb819f47bc068c74e76d [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
28#include "kvm.h"
29#define DPRINTF(x...) do {} while (0)
30#endif
31#include "x86_emulate.h"
32#include <linux/module.h>
33
34/*
35 * Opcode effective-address decode tables.
36 * Note that we only emulate instructions that have at least one memory
37 * operand (excluding implicit stack references). We assume that stack
38 * references and instruction fetches will never occur in special memory
39 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
40 * not be handled.
41 */
42
43/* Operand sizes: 8-bit operands or specified/overridden size. */
44#define ByteOp (1<<0) /* 8-bit operands. */
45/* Destination operand type. */
46#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
47#define DstReg (2<<1) /* Register operand. */
48#define DstMem (3<<1) /* Memory operand. */
49#define DstMask (3<<1)
50/* Source operand type. */
51#define SrcNone (0<<3) /* No source operand. */
52#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
53#define SrcReg (1<<3) /* Register operand. */
54#define SrcMem (2<<3) /* Memory operand. */
55#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
57#define SrcImm (5<<3) /* Immediate operand. */
58#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
59#define SrcMask (7<<3)
60/* Generic ModRM decode. */
61#define ModRM (1<<6)
62/* Destination is only written; never read. */
63#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080064#define BitOp (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
66static u8 opcode_table[256] = {
67 /* 0x00 - 0x07 */
68 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
69 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
70 0, 0, 0, 0,
71 /* 0x08 - 0x0F */
72 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
73 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
74 0, 0, 0, 0,
75 /* 0x10 - 0x17 */
76 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
77 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
78 0, 0, 0, 0,
79 /* 0x18 - 0x1F */
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
82 0, 0, 0, 0,
83 /* 0x20 - 0x27 */
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030086 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080087 /* 0x28 - 0x2F */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
90 0, 0, 0, 0,
91 /* 0x30 - 0x37 */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x38 - 0x3F */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
98 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -070099 /* 0x40 - 0x47 */
100 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
101 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
102 /* 0x48 - 0x4F */
103 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
104 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300105 /* 0x50 - 0x57 */
Nitin A Kamble7e778162007-08-19 11:07:06 +0300106 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
107 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300108 /* 0x58 - 0x5F */
109 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
110 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700111 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700113 0, 0, 0, 0,
114 /* 0x68 - 0x6F */
115 0, 0, ImplicitOps|Mov, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300116 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
117 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300118 /* 0x70 - 0x77 */
119 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
120 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
121 /* 0x78 - 0x7F */
122 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
123 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 /* 0x80 - 0x87 */
125 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
126 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
127 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
128 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
129 /* 0x88 - 0x8F */
130 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
131 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +0300132 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 /* 0x90 - 0x9F */
Nitin A Kamble535eabc2007-09-15 10:45:05 +0300134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135 /* 0xA0 - 0xA7 */
136 ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
137 ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
138 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
139 ByteOp | ImplicitOps, ImplicitOps,
140 /* 0xA8 - 0xAF */
141 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
142 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
143 ByteOp | ImplicitOps, ImplicitOps,
144 /* 0xB0 - 0xBF */
145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300147 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
148 0, ImplicitOps, 0, 0,
149 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150 /* 0xC8 - 0xCF */
151 0, 0, 0, 0, 0, 0, 0, 0,
152 /* 0xD0 - 0xD7 */
153 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
154 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
155 0, 0, 0, 0,
156 /* 0xD8 - 0xDF */
157 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300158 /* 0xE0 - 0xE7 */
159 0, 0, 0, 0, 0, 0, 0, 0,
160 /* 0xE8 - 0xEF */
Nitin A Kamblef6eed392007-08-28 18:08:37 -0700161 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0xF0 - 0xF7 */
163 0, 0, 0, 0,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300164 ImplicitOps, 0,
165 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0xF8 - 0xFF */
167 0, 0, 0, 0,
168 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
169};
170
Avi Kivity038e51d2007-01-22 20:40:40 -0800171static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 /* 0x00 - 0x0F */
173 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200174 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175 /* 0x10 - 0x1F */
176 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
177 /* 0x20 - 0x2F */
178 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
179 0, 0, 0, 0, 0, 0, 0, 0,
180 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300181 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0x40 - 0x47 */
183 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
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 /* 0x48 - 0x4F */
188 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
189 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
190 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
191 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
192 /* 0x50 - 0x5F */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0x60 - 0x6F */
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196 /* 0x70 - 0x7F */
197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300199 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
200 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
201 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
202 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0x90 - 0x9F */
204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205 /* 0xA0 - 0xA7 */
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 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800208 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0xB0 - 0xB7 */
210 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800211 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 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800215 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
217 DstReg | SrcMem16 | ModRM | Mov,
218 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800219 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
220 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0xD0 - 0xDF */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 /* 0xE0 - 0xEF */
224 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225 /* 0xF0 - 0xFF */
226 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
227};
228
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229/* EFLAGS bit definitions. */
230#define EFLG_OF (1<<11)
231#define EFLG_DF (1<<10)
232#define EFLG_SF (1<<7)
233#define EFLG_ZF (1<<6)
234#define EFLG_AF (1<<4)
235#define EFLG_PF (1<<2)
236#define EFLG_CF (1<<0)
237
238/*
239 * Instruction emulation:
240 * Most instructions are emulated directly via a fragment of inline assembly
241 * code. This allows us to save/restore EFLAGS and thus very easily pick up
242 * any modified flags.
243 */
244
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800245#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246#define _LO32 "k" /* force 32-bit operand */
247#define _STK "%%rsp" /* stack pointer */
248#elif defined(__i386__)
249#define _LO32 "" /* force 32-bit operand */
250#define _STK "%%esp" /* stack pointer */
251#endif
252
253/*
254 * These EFLAGS bits are restored from saved value during emulation, and
255 * any changes are written back to the saved value after emulation.
256 */
257#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
258
259/* Before executing instruction: restore necessary bits in EFLAGS. */
260#define _PRE_EFLAGS(_sav, _msk, _tmp) \
261 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
262 "push %"_sav"; " \
263 "movl %"_msk",%"_LO32 _tmp"; " \
264 "andl %"_LO32 _tmp",("_STK"); " \
265 "pushf; " \
266 "notl %"_LO32 _tmp"; " \
267 "andl %"_LO32 _tmp",("_STK"); " \
268 "pop %"_tmp"; " \
269 "orl %"_LO32 _tmp",("_STK"); " \
270 "popf; " \
271 /* _sav &= ~msk; */ \
272 "movl %"_msk",%"_LO32 _tmp"; " \
273 "notl %"_LO32 _tmp"; " \
274 "andl %"_LO32 _tmp",%"_sav"; "
275
276/* After executing instruction: write-back necessary bits in EFLAGS. */
277#define _POST_EFLAGS(_sav, _msk, _tmp) \
278 /* _sav |= EFLAGS & _msk; */ \
279 "pushf; " \
280 "pop %"_tmp"; " \
281 "andl %"_msk",%"_LO32 _tmp"; " \
282 "orl %"_LO32 _tmp",%"_sav"; "
283
284/* Raw emulation: instruction has two explicit operands. */
285#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
286 do { \
287 unsigned long _tmp; \
288 \
289 switch ((_dst).bytes) { \
290 case 2: \
291 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400292 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400294 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 : "=m" (_eflags), "=m" ((_dst).val), \
296 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400297 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 break; \
299 case 4: \
300 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400301 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400303 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 : "=m" (_eflags), "=m" ((_dst).val), \
305 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400306 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 break; \
308 case 8: \
309 __emulate_2op_8byte(_op, _src, _dst, \
310 _eflags, _qx, _qy); \
311 break; \
312 } \
313 } while (0)
314
315#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
316 do { \
317 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400318 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319 case 1: \
320 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400321 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400323 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 : "=m" (_eflags), "=m" ((_dst).val), \
325 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400326 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 break; \
328 default: \
329 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
330 _wx, _wy, _lx, _ly, _qx, _qy); \
331 break; \
332 } \
333 } while (0)
334
335/* Source operand is byte-sized and may be restricted to just %cl. */
336#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
337 __emulate_2op(_op, _src, _dst, _eflags, \
338 "b", "c", "b", "c", "b", "c", "b", "c")
339
340/* Source operand is byte, word, long or quad sized. */
341#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
342 __emulate_2op(_op, _src, _dst, _eflags, \
343 "b", "q", "w", "r", _LO32, "r", "", "r")
344
345/* Source operand is word, long or quad sized. */
346#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
347 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
348 "w", "r", _LO32, "r", "", "r")
349
350/* Instruction has only one explicit operand (no source operand). */
351#define emulate_1op(_op, _dst, _eflags) \
352 do { \
353 unsigned long _tmp; \
354 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400355 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 case 1: \
357 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400358 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400360 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 : "=m" (_eflags), "=m" ((_dst).val), \
362 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400363 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 break; \
365 case 2: \
366 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400367 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400369 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 : "=m" (_eflags), "=m" ((_dst).val), \
371 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400372 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 break; \
374 case 4: \
375 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400376 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400378 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 : "=m" (_eflags), "=m" ((_dst).val), \
380 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 break; \
383 case 8: \
384 __emulate_1op_8byte(_op, _dst, _eflags); \
385 break; \
386 } \
387 } while (0)
388
389/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800390#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
392 do { \
393 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400394 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400396 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400398 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 } while (0)
400
401#define __emulate_1op_8byte(_op, _dst, _eflags) \
402 do { \
403 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400404 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400406 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400408 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 } while (0)
410
411#elif defined(__i386__)
412#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
413#define __emulate_1op_8byte(_op, _dst, _eflags)
414#endif /* __i386__ */
415
416/* Fetch next part of the instruction being emulated. */
417#define insn_fetch(_type, _size, _eip) \
418({ unsigned long _x; \
419 rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
Mike Dayd77c26f2007-10-08 09:02:08 -0400420 (_size), ctxt->vcpu); \
421 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 goto done; \
423 (_eip) += (_size); \
424 (_type)_x; \
425})
426
427/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300428#define address_mask(reg) \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200429 ((c->ad_bytes == sizeof(unsigned long)) ? \
430 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300432 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433#define register_address_increment(reg, inc) \
434 do { \
435 /* signed type ensures sign extension to long */ \
436 int _inc = (inc); \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200437 if (c->ad_bytes == sizeof(unsigned long)) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 (reg) += _inc; \
439 else \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200440 (reg) = ((reg) & \
441 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
442 (((reg) + _inc) & \
443 ((1UL << (c->ad_bytes << 3)) - 1)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 } while (0)
445
Nitin A Kamble098c9372007-08-19 11:00:36 +0300446#define JMP_REL(rel) \
447 do { \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200448 register_address_increment(c->eip, rel); \
Nitin A Kamble098c9372007-08-19 11:00:36 +0300449 } while (0)
450
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000451/*
452 * Given the 'reg' portion of a ModRM byte, and a register block, return a
453 * pointer into the block that addresses the relevant register.
454 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
455 */
456static void *decode_register(u8 modrm_reg, unsigned long *regs,
457 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458{
459 void *p;
460
461 p = &regs[modrm_reg];
462 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
463 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
464 return p;
465}
466
467static int read_descriptor(struct x86_emulate_ctxt *ctxt,
468 struct x86_emulate_ops *ops,
469 void *ptr,
470 u16 *size, unsigned long *address, int op_bytes)
471{
472 int rc;
473
474 if (op_bytes == 2)
475 op_bytes = 3;
476 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300477 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
478 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 if (rc)
480 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300481 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
482 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 return rc;
484}
485
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300486static int test_cc(unsigned int condition, unsigned int flags)
487{
488 int rc = 0;
489
490 switch ((condition & 15) >> 1) {
491 case 0: /* o */
492 rc |= (flags & EFLG_OF);
493 break;
494 case 1: /* b/c/nae */
495 rc |= (flags & EFLG_CF);
496 break;
497 case 2: /* z/e */
498 rc |= (flags & EFLG_ZF);
499 break;
500 case 3: /* be/na */
501 rc |= (flags & (EFLG_CF|EFLG_ZF));
502 break;
503 case 4: /* s */
504 rc |= (flags & EFLG_SF);
505 break;
506 case 5: /* p/pe */
507 rc |= (flags & EFLG_PF);
508 break;
509 case 7: /* le/ng */
510 rc |= (flags & EFLG_ZF);
511 /* fall through */
512 case 6: /* l/nge */
513 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
514 break;
515 }
516
517 /* Odd condition identifiers (lsb == 1) have inverted sense. */
518 return (!!rc ^ (condition & 1));
519}
520
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200522x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200524 struct decode_cache *c = &ctxt->decode;
525 u8 sib, rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 int mode = ctxt->mode;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200528 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
530 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531
Laurent Viviere4e03de2007-09-18 11:52:50 +0200532 memset(c, 0, sizeof(struct decode_cache));
533 c->eip = ctxt->vcpu->rip;
534 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535
536 switch (mode) {
537 case X86EMUL_MODE_REAL:
538 case X86EMUL_MODE_PROT16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200539 c->op_bytes = c->ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 break;
541 case X86EMUL_MODE_PROT32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200542 c->op_bytes = c->ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800544#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 case X86EMUL_MODE_PROT64:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200546 c->op_bytes = 4;
547 c->ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 break;
549#endif
550 default:
551 return -1;
552 }
553
554 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200555 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200556 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 case 0x66: /* operand-size override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200558 c->op_bytes ^= 6; /* switch between 2/4 bytes */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 break;
560 case 0x67: /* address-size override */
561 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200562 /* switch between 4/8 bytes */
563 c->ad_bytes ^= 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200565 /* switch between 2/4 bytes */
566 c->ad_bytes ^= 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 break;
568 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200569 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 break;
571 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200572 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 break;
574 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200575 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576 break;
577 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200578 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 break;
580 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200581 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582 break;
583 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200584 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200586 case 0x40 ... 0x4f: /* REX */
587 if (mode != X86EMUL_MODE_PROT64)
588 goto done_prefixes;
589 rex_prefix = c->b;
590 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200592 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200594 case 0xf2: /* REPNE/REPNZ */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595 case 0xf3: /* REP/REPE/REPZ */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200596 c->rep_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 default:
599 goto done_prefixes;
600 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200601
602 /* Any legacy prefix after a REX prefix nullifies its effect. */
603
604 rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 }
606
607done_prefixes:
608
609 /* REX prefix. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200610 if (rex_prefix) {
611 if (rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200612 c->op_bytes = 8; /* REX.W */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200613 c->modrm_reg = (rex_prefix & 4) << 1; /* REX.R */
614 index_reg = (rex_prefix & 2) << 2; /* REX.X */
615 c->modrm_rm = base_reg = (rex_prefix & 1) << 3; /* REG.B */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 }
617
618 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200619 c->d = opcode_table[c->b];
620 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200622 if (c->b == 0x0f) {
623 c->twobyte = 1;
624 c->b = insn_fetch(u8, 1, c->eip);
625 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 }
627
628 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200629 if (c->d == 0) {
630 DPRINTF("Cannot emulate %02x\n", c->b);
631 return -1;
632 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 }
634
635 /* ModRM and SIB bytes. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200636 if (c->d & ModRM) {
637 c->modrm = insn_fetch(u8, 1, c->eip);
638 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
639 c->modrm_reg |= (c->modrm & 0x38) >> 3;
640 c->modrm_rm |= (c->modrm & 0x07);
641 c->modrm_ea = 0;
642 c->use_modrm_ea = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643
Laurent Viviere4e03de2007-09-18 11:52:50 +0200644 if (c->modrm_mod == 3) {
645 c->modrm_val = *(unsigned long *)
646 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 goto modrm_done;
648 }
649
Laurent Viviere4e03de2007-09-18 11:52:50 +0200650 if (c->ad_bytes == 2) {
651 unsigned bx = c->regs[VCPU_REGS_RBX];
652 unsigned bp = c->regs[VCPU_REGS_RBP];
653 unsigned si = c->regs[VCPU_REGS_RSI];
654 unsigned di = c->regs[VCPU_REGS_RDI];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655
656 /* 16-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200657 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200659 if (c->modrm_rm == 6)
660 c->modrm_ea +=
661 insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 break;
663 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200664 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 break;
666 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200667 c->modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 break;
669 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200670 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200672 c->modrm_ea += bx + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 break;
674 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200675 c->modrm_ea += bx + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 break;
677 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200678 c->modrm_ea += bp + si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679 break;
680 case 3:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200681 c->modrm_ea += bp + di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682 break;
683 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200684 c->modrm_ea += si;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 break;
686 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200687 c->modrm_ea += di;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 break;
689 case 6:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200690 if (c->modrm_mod != 0)
691 c->modrm_ea += bp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 break;
693 case 7:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200694 c->modrm_ea += bx;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 break;
696 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200697 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
698 (c->modrm_rm == 6 && c->modrm_mod != 0))
699 if (!c->override_base)
700 c->override_base = &ctxt->ss_base;
701 c->modrm_ea = (u16)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 } else {
703 /* 32/64-bit ModR/M decode. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200704 switch (c->modrm_rm) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 case 4:
706 case 12:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200707 sib = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 index_reg |= (sib >> 3) & 7;
709 base_reg |= sib & 7;
710 scale = sib >> 6;
711
712 switch (base_reg) {
713 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200714 if (c->modrm_mod != 0)
715 c->modrm_ea +=
716 c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200718 c->modrm_ea +=
719 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 break;
721 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200722 c->modrm_ea += c->regs[base_reg];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 }
724 switch (index_reg) {
725 case 4:
726 break;
727 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200728 c->modrm_ea +=
729 c->regs[index_reg] << scale;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
731 }
732 break;
733 case 5:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200734 if (c->modrm_mod != 0)
735 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 else if (mode == X86EMUL_MODE_PROT64)
737 rip_relative = 1;
738 break;
739 default:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200740 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 break;
742 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200743 switch (c->modrm_mod) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 case 0:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200745 if (c->modrm_rm == 5)
746 c->modrm_ea +=
747 insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 break;
749 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200750 c->modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 break;
752 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200753 c->modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 break;
755 }
756 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200757 if (!c->override_base)
758 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759 if (mode == X86EMUL_MODE_PROT64 &&
Laurent Viviere4e03de2007-09-18 11:52:50 +0200760 c->override_base != &ctxt->fs_base &&
761 c->override_base != &ctxt->gs_base)
762 c->override_base = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Laurent Viviere4e03de2007-09-18 11:52:50 +0200764 if (c->override_base)
765 c->modrm_ea += *c->override_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
767 if (rip_relative) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200768 c->modrm_ea += c->eip;
769 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200771 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 break;
773 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200774 if (c->d & ByteOp)
775 c->modrm_ea += 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200777 if (c->op_bytes == 8)
778 c->modrm_ea += 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200780 c->modrm_ea += c->op_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 }
782 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200783 if (c->ad_bytes != 8)
784 c->modrm_ea = (u32)c->modrm_ea;
Mike Dayd77c26f2007-10-08 09:02:08 -0400785modrm_done:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 ;
787 }
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 /*
790 * Decode and fetch the source operand: register, memory
791 * or immediate.
792 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200793 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 case SrcNone:
795 break;
796 case SrcReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200797 c->src.type = OP_REG;
798 if (c->d & ByteOp) {
799 c->src.ptr =
800 decode_register(c->modrm_reg, c->regs,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200802 c->src.val = c->src.orig_val = *(u8 *)c->src.ptr;
803 c->src.bytes = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200805 c->src.ptr =
806 decode_register(c->modrm_reg, c->regs, 0);
807 switch ((c->src.bytes = c->op_bytes)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200809 c->src.val = c->src.orig_val =
810 *(u16 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 break;
812 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200813 c->src.val = c->src.orig_val =
814 *(u32 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 break;
816 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200817 c->src.val = c->src.orig_val =
818 *(u64 *) c->src.ptr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 break;
820 }
821 }
822 break;
823 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200824 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 goto srcmem_common;
826 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200827 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 goto srcmem_common;
829 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200830 c->src.bytes = (c->d & ByteOp) ? 1 :
831 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300832 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400833 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300834 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400835 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200836 /*
837 * For instructions with a ModR/M byte, switch to register
838 * access if Mod = 3.
839 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200840 if ((c->d & ModRM) && c->modrm_mod == 3) {
841 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200842 break;
843 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200844 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 break;
846 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200847 c->src.type = OP_IMM;
848 c->src.ptr = (unsigned long *)c->eip;
849 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
850 if (c->src.bytes == 8)
851 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200853 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200855 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 break;
857 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200858 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 break;
860 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200861 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 break;
863 }
864 break;
865 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200866 c->src.type = OP_IMM;
867 c->src.ptr = (unsigned long *)c->eip;
868 c->src.bytes = 1;
869 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 break;
871 }
872
Avi Kivity038e51d2007-01-22 20:40:40 -0800873 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200874 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800875 case ImplicitOps:
876 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200877 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800878 case DstReg:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200879 c->dst.type = OP_REG;
880 if ((c->d & ByteOp)
881 && !(c->twobyte &&
882 (c->b == 0xb6 || c->b == 0xb7))) {
883 c->dst.ptr =
884 decode_register(c->modrm_reg, c->regs,
Avi Kivity038e51d2007-01-22 20:40:40 -0800885 (rex_prefix == 0));
Laurent Viviere4e03de2007-09-18 11:52:50 +0200886 c->dst.val = *(u8 *) c->dst.ptr;
887 c->dst.bytes = 1;
Avi Kivity038e51d2007-01-22 20:40:40 -0800888 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 c->dst.ptr =
890 decode_register(c->modrm_reg, c->regs, 0);
891 switch ((c->dst.bytes = c->op_bytes)) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800892 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 c->dst.val = *(u16 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800894 break;
895 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200896 c->dst.val = *(u32 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800897 break;
898 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200899 c->dst.val = *(u64 *)c->dst.ptr;
Avi Kivity038e51d2007-01-22 20:40:40 -0800900 break;
901 }
902 }
903 break;
904 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200905 if ((c->d & ModRM) && c->modrm_mod == 3) {
906 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200907 break;
908 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200909 c->dst.type = OP_MEM;
910 break;
911 }
912
913done:
914 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
915}
916
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200917static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
918{
919 struct decode_cache *c = &ctxt->decode;
920
921 c->dst.type = OP_MEM;
922 c->dst.bytes = c->op_bytes;
923 c->dst.val = c->src.val;
924 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
925 c->dst.ptr = (void *) register_address(ctxt->ss_base,
926 c->regs[VCPU_REGS_RSP]);
927}
928
929static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
930 struct x86_emulate_ops *ops)
931{
932 struct decode_cache *c = &ctxt->decode;
933 int rc;
934
935 /* 64-bit mode: POP always pops a 64-bit operand. */
936
937 if (ctxt->mode == X86EMUL_MODE_PROT64)
938 c->dst.bytes = 8;
939
940 rc = ops->read_std(register_address(ctxt->ss_base,
941 c->regs[VCPU_REGS_RSP]),
942 &c->dst.val, c->dst.bytes, ctxt->vcpu);
943 if (rc != 0)
944 return rc;
945
946 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
947
948 return 0;
949}
950
Laurent Vivier05f086f2007-09-24 11:10:55 +0200951static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200952{
Laurent Vivier05f086f2007-09-24 11:10:55 +0200953 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200954 switch (c->modrm_reg) {
955 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200956 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200957 break;
958 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200959 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200960 break;
961 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200962 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200963 break;
964 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200965 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200966 break;
967 case 4: /* sal/shl */
968 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200969 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200970 break;
971 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200972 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200973 break;
974 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +0200975 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200976 break;
977 }
978}
979
980static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +0200981 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200982{
983 struct decode_cache *c = &ctxt->decode;
984 int rc = 0;
985
986 switch (c->modrm_reg) {
987 case 0 ... 1: /* test */
988 /*
989 * Special case in Grp3: test has an immediate
990 * source operand.
991 */
992 c->src.type = OP_IMM;
993 c->src.ptr = (unsigned long *)c->eip;
994 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
995 if (c->src.bytes == 8)
996 c->src.bytes = 4;
997 switch (c->src.bytes) {
998 case 1:
999 c->src.val = insn_fetch(s8, 1, c->eip);
1000 break;
1001 case 2:
1002 c->src.val = insn_fetch(s16, 2, c->eip);
1003 break;
1004 case 4:
1005 c->src.val = insn_fetch(s32, 4, c->eip);
1006 break;
1007 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001008 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001009 break;
1010 case 2: /* not */
1011 c->dst.val = ~c->dst.val;
1012 break;
1013 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001014 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001015 break;
1016 default:
1017 DPRINTF("Cannot emulate %02x\n", c->b);
1018 rc = X86EMUL_UNHANDLEABLE;
1019 break;
1020 }
1021done:
1022 return rc;
1023}
1024
1025static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001026 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001027{
1028 struct decode_cache *c = &ctxt->decode;
1029 int rc;
1030
1031 switch (c->modrm_reg) {
1032 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001033 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001034 break;
1035 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001036 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001037 break;
1038 case 4: /* jmp abs */
1039 if (c->b == 0xff)
1040 c->eip = c->dst.val;
1041 else {
1042 DPRINTF("Cannot emulate %02x\n", c->b);
1043 return X86EMUL_UNHANDLEABLE;
1044 }
1045 break;
1046 case 6: /* push */
1047
1048 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1049
1050 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1051 c->dst.bytes = 8;
1052 rc = ops->read_std((unsigned long)c->dst.ptr,
1053 &c->dst.val, 8, ctxt->vcpu);
1054 if (rc != 0)
1055 return rc;
1056 }
1057 register_address_increment(c->regs[VCPU_REGS_RSP],
1058 -c->dst.bytes);
1059 rc = ops->write_emulated(register_address(ctxt->ss_base,
1060 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1061 c->dst.bytes, ctxt->vcpu);
1062 if (rc != 0)
1063 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001064 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001065 break;
1066 default:
1067 DPRINTF("Cannot emulate %02x\n", c->b);
1068 return X86EMUL_UNHANDLEABLE;
1069 }
1070 return 0;
1071}
1072
1073static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1074 struct x86_emulate_ops *ops,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001075 unsigned long cr2)
1076{
1077 struct decode_cache *c = &ctxt->decode;
1078 u64 old, new;
1079 int rc;
1080
1081 rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu);
1082 if (rc != 0)
1083 return rc;
1084
1085 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1086 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1087
1088 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1089 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001090 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001091
1092 } else {
1093 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1094 (u32) c->regs[VCPU_REGS_RBX];
1095
1096 rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu);
1097 if (rc != 0)
1098 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001099 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001100 }
1101 return 0;
1102}
1103
1104static inline int writeback(struct x86_emulate_ctxt *ctxt,
1105 struct x86_emulate_ops *ops)
1106{
1107 int rc;
1108 struct decode_cache *c = &ctxt->decode;
1109
1110 switch (c->dst.type) {
1111 case OP_REG:
1112 /* The 4-byte case *is* correct:
1113 * in 64-bit mode we zero-extend.
1114 */
1115 switch (c->dst.bytes) {
1116 case 1:
1117 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1118 break;
1119 case 2:
1120 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1121 break;
1122 case 4:
1123 *c->dst.ptr = (u32)c->dst.val;
1124 break; /* 64b: zero-ext */
1125 case 8:
1126 *c->dst.ptr = c->dst.val;
1127 break;
1128 }
1129 break;
1130 case OP_MEM:
1131 if (c->lock_prefix)
1132 rc = ops->cmpxchg_emulated(
1133 (unsigned long)c->dst.ptr,
1134 &c->dst.orig_val,
1135 &c->dst.val,
1136 c->dst.bytes,
1137 ctxt->vcpu);
1138 else
1139 rc = ops->write_emulated(
1140 (unsigned long)c->dst.ptr,
1141 &c->dst.val,
1142 c->dst.bytes,
1143 ctxt->vcpu);
1144 if (rc != 0)
1145 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001146 break;
1147 case OP_NONE:
1148 /* no writeback */
1149 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150 default:
1151 break;
1152 }
1153 return 0;
1154}
1155
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001156int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001157x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001158{
1159 unsigned long cr2 = ctxt->cr2;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001160 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001161 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001162 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001163 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001164
Laurent Vivier34273182007-09-18 11:27:37 +02001165 /* Shadow copy of register state. Committed on successful emulation.
1166 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1167 * modify them.
1168 */
1169
1170 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1171 saved_eip = c->eip;
1172
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001173 if ((c->d & ModRM) && (c->modrm_mod != 3))
1174 cr2 = c->modrm_ea;
1175
1176 if (c->src.type == OP_MEM) {
1177 c->src.ptr = (unsigned long *)cr2;
1178 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001179 rc = ops->read_emulated((unsigned long)c->src.ptr,
1180 &c->src.val,
1181 c->src.bytes,
1182 ctxt->vcpu);
1183 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001184 goto done;
1185 c->src.orig_val = c->src.val;
1186 }
1187
1188 if ((c->d & DstMask) == ImplicitOps)
1189 goto special_insn;
1190
1191
1192 if (c->dst.type == OP_MEM) {
1193 c->dst.ptr = (unsigned long *)cr2;
1194 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1195 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001196 if (c->d & BitOp) {
1197 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001198
Laurent Viviere4e03de2007-09-18 11:52:50 +02001199 c->dst.ptr = (void *)c->dst.ptr +
1200 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001201 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001202 if (!(c->d & Mov) &&
1203 /* optimisation - avoid slow emulated read */
1204 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1205 &c->dst.val,
1206 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001207 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001208 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001209 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001210
Laurent Viviere4e03de2007-09-18 11:52:50 +02001211 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 goto twobyte_insn;
1213
Laurent Viviere4e03de2007-09-18 11:52:50 +02001214 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 case 0x00 ... 0x05:
1216 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001217 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 break;
1219 case 0x08 ... 0x0d:
1220 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001221 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 break;
1223 case 0x10 ... 0x15:
1224 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001225 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 break;
1227 case 0x18 ... 0x1d:
1228 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001229 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001231 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001233 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001235 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001236 c->dst.type = OP_REG;
1237 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1238 c->dst.val = *(u8 *)c->dst.ptr;
1239 c->dst.bytes = 1;
1240 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001241 goto and;
1242 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001243 c->dst.type = OP_REG;
1244 c->dst.bytes = c->op_bytes;
1245 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1246 if (c->op_bytes == 2)
1247 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001248 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001249 c->dst.val = *(u32 *)c->dst.ptr;
1250 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001251 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 case 0x28 ... 0x2d:
1253 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001254 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 break;
1256 case 0x30 ... 0x35:
1257 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001258 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 break;
1260 case 0x38 ... 0x3d:
1261 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001262 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 break;
1264 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001265 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001267 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 break;
1269 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001270 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 case 0:
1272 goto add;
1273 case 1:
1274 goto or;
1275 case 2:
1276 goto adc;
1277 case 3:
1278 goto sbb;
1279 case 4:
1280 goto and;
1281 case 5:
1282 goto sub;
1283 case 6:
1284 goto xor;
1285 case 7:
1286 goto cmp;
1287 }
1288 break;
1289 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001290 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 break;
1292 case 0x86 ... 0x87: /* xchg */
1293 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001294 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001296 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 break;
1298 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001299 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 break;
1301 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001302 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 break; /* 64b reg: zero-extend */
1304 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001305 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 break;
1307 }
1308 /*
1309 * Write back the memory destination with implicit LOCK
1310 * prefix.
1311 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001312 c->dst.val = c->src.val;
1313 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001316 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001317 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001318 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001319 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001321 rc = emulate_grp1a(ctxt, ops);
1322 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001325 case 0xa0 ... 0xa1: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001326 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1327 c->dst.val = c->src.val;
1328 /* skip src displacement */
1329 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001330 break;
1331 case 0xa2 ... 0xa3: /* mov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001332 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1333 /* skip c->dst displacement */
1334 c->eip += c->ad_bytes;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001335 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 case 0xc0 ... 0xc1:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001337 emulate_grp2(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001339 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1340 mov:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 c->dst.val = c->src.val;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 case 0xd0 ... 0xd1: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001344 c->src.val = 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001345 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 case 0xd2 ... 0xd3: /* Grp2 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001348 c->src.val = c->regs[VCPU_REGS_RCX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001349 emulate_grp2(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001350 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 case 0xf6 ... 0xf7: /* Grp3 */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001352 rc = emulate_grp3(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001353 if (rc != 0)
1354 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 break;
1356 case 0xfe ... 0xff: /* Grp4/Grp5 */
Laurent Viviera01af5e2007-09-24 11:10:56 +02001357 rc = emulate_grp45(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001358 if (rc != 0)
1359 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 break;
1361 }
1362
1363writeback:
Laurent Viviera01af5e2007-09-24 11:10:56 +02001364 rc = writeback(ctxt, ops);
1365 if (rc != 0)
1366 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
1368 /* Commit shadow register state. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001369 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001370 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371
1372done:
Laurent Vivier34273182007-09-18 11:27:37 +02001373 if (rc == X86EMUL_UNHANDLEABLE) {
1374 c->eip = saved_eip;
1375 return -1;
1376 }
1377 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
1379special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001380 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 goto twobyte_special_insn;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001382 switch (c->b) {
Nitin A Kambled77a2502007-10-12 17:40:33 -07001383 case 0x40 ... 0x47: /* inc r16/r32 */
1384 c->dst.bytes = c->op_bytes;
1385 c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
1386 c->dst.val = *c->dst.ptr;
1387 emulate_1op("inc", c->dst, ctxt->eflags);
1388 break;
1389 case 0x48 ... 0x4f: /* dec r16/r32 */
1390 c->dst.bytes = c->op_bytes;
1391 c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
1392 c->dst.val = *c->dst.ptr;
1393 emulate_1op("dec", c->dst, ctxt->eflags);
1394 break;
Nitin A Kamble7e778162007-08-19 11:07:06 +03001395 case 0x50 ... 0x57: /* push reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001396 if (c->op_bytes == 2)
1397 c->src.val = (u16) c->regs[c->b & 0x7];
Nitin A Kamble7e778162007-08-19 11:07:06 +03001398 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001399 c->src.val = (u32) c->regs[c->b & 0x7];
1400 c->dst.type = OP_MEM;
1401 c->dst.bytes = c->op_bytes;
1402 c->dst.val = c->src.val;
1403 register_address_increment(c->regs[VCPU_REGS_RSP],
1404 -c->op_bytes);
1405 c->dst.ptr = (void *) register_address(
1406 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
Nitin A Kamble7e778162007-08-19 11:07:06 +03001407 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001408 case 0x58 ... 0x5f: /* pop reg */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001409 c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
Nitin A Kamble7de75242007-09-15 10:13:07 +03001410 pop_instruction:
1411 if ((rc = ops->read_std(register_address(ctxt->ss_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001412 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1413 c->op_bytes, ctxt->vcpu)) != 0)
Nitin A Kamble7de75242007-09-15 10:13:07 +03001414 goto done;
1415
Laurent Viviere4e03de2007-09-18 11:52:50 +02001416 register_address_increment(c->regs[VCPU_REGS_RSP],
1417 c->op_bytes);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001418 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001419 break;
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001420 case 0x6a: /* push imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001421 c->src.val = 0L;
1422 c->src.val = insn_fetch(s8, 1, c->eip);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001423 emulate_push(ctxt);
Avi Kivity1e35d3c2007-10-26 14:16:56 +02001424 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001425 case 0x6c: /* insb */
1426 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001427 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001428 1,
1429 (c->d & ByteOp) ? 1 : c->op_bytes,
1430 c->rep_prefix ?
1431 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432 (ctxt->eflags & EFLG_DF),
Laurent Viviere70669a2007-08-05 10:36:40 +03001433 register_address(ctxt->es_base,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001434 c->regs[VCPU_REGS_RDI]),
1435 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001436 c->regs[VCPU_REGS_RDX]) == 0) {
1437 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001438 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001439 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001440 return 0;
1441 case 0x6e: /* outsb */
1442 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001443 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001444 0,
1445 (c->d & ByteOp) ? 1 : c->op_bytes,
1446 c->rep_prefix ?
1447 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001448 (ctxt->eflags & EFLG_DF),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001449 register_address(c->override_base ?
1450 *c->override_base :
1451 ctxt->ds_base,
1452 c->regs[VCPU_REGS_RSI]),
1453 c->rep_prefix,
Laurent Vivier34273182007-09-18 11:27:37 +02001454 c->regs[VCPU_REGS_RDX]) == 0) {
1455 c->eip = saved_eip;
Laurent Viviere70669a2007-08-05 10:36:40 +03001456 return -1;
Laurent Vivier34273182007-09-18 11:27:37 +02001457 }
Laurent Viviere70669a2007-08-05 10:36:40 +03001458 return 0;
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001459 case 0x70 ... 0x7f: /* jcc (short) */ {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001460 int rel = insn_fetch(s8, 1, c->eip);
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001461
Laurent Vivier05f086f2007-09-24 11:10:55 +02001462 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamble55bebde2007-09-15 10:25:41 +03001463 JMP_REL(rel);
1464 break;
1465 }
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001466 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001467 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468 emulate_push(ctxt);
1469 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001470 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001471 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001472 goto pop_instruction;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001473 case 0xc3: /* ret */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 c->dst.ptr = &c->eip;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001475 goto pop_instruction;
1476 case 0xf4: /* hlt */
1477 ctxt->vcpu->halt_request = 1;
1478 goto done;
Laurent Viviere70669a2007-08-05 10:36:40 +03001479 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001480 if (c->rep_prefix) {
1481 if (c->regs[VCPU_REGS_RCX] == 0) {
1482 ctxt->vcpu->rip = c->eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 goto done;
1484 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001485 c->regs[VCPU_REGS_RCX]--;
1486 c->eip = ctxt->vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001488 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001490 c->dst.type = OP_MEM;
1491 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1492 c->dst.ptr = (unsigned long *)register_address(
1493 ctxt->es_base,
1494 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001496 c->override_base ? *c->override_base :
1497 ctxt->ds_base,
1498 c->regs[VCPU_REGS_RSI]),
1499 &c->dst.val,
1500 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001502 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001503 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001504 : c->dst.bytes);
1505 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001506 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001507 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 break;
1509 case 0xa6 ... 0xa7: /* cmps */
1510 DPRINTF("Urk! I don't handle CMPS.\n");
1511 goto cannot_emulate;
1512 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001513 c->dst.type = OP_MEM;
1514 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1515 c->dst.ptr = (unsigned long *)cr2;
1516 c->dst.val = c->regs[VCPU_REGS_RAX];
1517 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001518 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001519 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 break;
1521 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001522 c->dst.type = OP_REG;
1523 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1524 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1525 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1526 c->dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001527 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001529 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001530 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001531 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 break;
1533 case 0xae ... 0xaf: /* scas */
1534 DPRINTF("Urk! I don't handle SCAS.\n");
1535 goto cannot_emulate;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001536 case 0xe8: /* call (near) */ {
1537 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001538 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001539 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001540 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001541 break;
1542 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001543 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001544 break;
1545 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001546 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001547 break;
1548 default:
1549 DPRINTF("Call: Invalid op_bytes\n");
1550 goto cannot_emulate;
1551 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001552 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001553 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001554 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001555 emulate_push(ctxt);
1556 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001557 }
1558 case 0xe9: /* jmp rel */
1559 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001560 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001561 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001562 break;
1563
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001564
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 }
1566 goto writeback;
1567
1568twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001569 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001571 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572 u16 size;
1573 unsigned long address;
1574
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001575 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001576 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001577 goto cannot_emulate;
1578
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001579 rc = kvm_fix_hypercall(ctxt->vcpu);
1580 if (rc)
1581 goto done;
1582
1583 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001584 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001586 rc = read_descriptor(ctxt, ops, c->src.ptr,
1587 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 if (rc)
1589 goto done;
1590 realmode_lgdt(ctxt->vcpu, size, address);
1591 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001592 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001593 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001594 rc = kvm_fix_hypercall(ctxt->vcpu);
1595 if (rc)
1596 goto done;
1597 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001598 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001599 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001600 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001601 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001602 if (rc)
1603 goto done;
1604 realmode_lidt(ctxt->vcpu, size, address);
1605 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 break;
1607 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001608 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001610 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 = realmode_get_cr(ctxt->vcpu, 0);
1612 break;
1613 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001614 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 goto cannot_emulate;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001616 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1617 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 break;
1619 case 7: /* invlpg*/
1620 emulate_invlpg(ctxt->vcpu, cr2);
1621 break;
1622 default:
1623 goto cannot_emulate;
1624 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001625 /* Disable writeback. */
1626 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 break;
1628 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001629 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001631 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001632 if (rc)
1633 goto cannot_emulate;
1634 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 break;
1636 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001637 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001639 rc = emulator_set_dr(ctxt, c->modrm_reg,
1640 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001641 if (rc)
1642 goto cannot_emulate;
1643 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 break;
1645 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001646 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001647 if (!test_cc(c->b, ctxt->eflags))
1648 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001650 case 0xa3:
1651 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001652 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001653 /* only subword offset */
1654 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001655 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001656 break;
1657 case 0xab:
1658 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001659 /* only subword offset */
1660 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001661 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001662 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 case 0xb0 ... 0xb1: /* cmpxchg */
1664 /*
1665 * Save real source value, then compare EAX against
1666 * destination.
1667 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001668 c->src.orig_val = c->src.val;
1669 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001670 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1671 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001673 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 } else {
1675 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001676 c->dst.type = OP_REG;
1677 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 }
1679 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 case 0xb3:
1681 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 /* only subword offset */
1683 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001684 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001687 c->dst.bytes = c->op_bytes;
1688 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1689 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001692 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 case 0:
1694 goto bt;
1695 case 1:
1696 goto bts;
1697 case 2:
1698 goto btr;
1699 case 3:
1700 goto btc;
1701 }
1702 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001703 case 0xbb:
1704 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001705 /* only subword offset */
1706 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001707 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001708 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001710 c->dst.bytes = c->op_bytes;
1711 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1712 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001714 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001715 c->dst.bytes = c->op_bytes;
1716 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1717 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001718 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719 }
1720 goto writeback;
1721
1722twobyte_special_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 switch (c->b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001724 case 0x06:
1725 emulate_clts(ctxt->vcpu);
1726 break;
Avi Kivity651a3e22007-10-28 16:09:18 +02001727 case 0x08: /* invd */
1728 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001729 case 0x09: /* wbinvd */
1730 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 case 0x0d: /* GrpP (prefetch) */
1732 case 0x18: /* Grp16 (prefetch/nop) */
1733 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 case 0x20: /* mov cr, reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001735 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001737 c->regs[c->modrm_rm] =
1738 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 break;
1740 case 0x22: /* mov reg, cr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001741 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001743 realmode_set_cr(ctxt->vcpu,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001744 c->modrm_reg, c->modrm_val, &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001746 case 0x30:
1747 /* wrmsr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001748 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1749 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1750 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001751 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001752 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001753 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001754 }
1755 rc = X86EMUL_CONTINUE;
1756 break;
1757 case 0x32:
1758 /* rdmsr */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
Avi Kivity35f3f282007-07-17 14:20:30 +03001760 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001761 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001762 c->eip = ctxt->vcpu->rip;
Avi Kivity35f3f282007-07-17 14:20:30 +03001763 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001764 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1765 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity35f3f282007-07-17 14:20:30 +03001766 }
1767 rc = X86EMUL_CONTINUE;
1768 break;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001769 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1770 long int rel;
1771
Laurent Viviere4e03de2007-09-18 11:52:50 +02001772 switch (c->op_bytes) {
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001773 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001774 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001775 break;
1776 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001777 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001778 break;
1779 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001780 rel = insn_fetch(s64, 8, c->eip);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001781 break;
1782 default:
1783 DPRINTF("jnz: Invalid op_bytes\n");
1784 goto cannot_emulate;
1785 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001786 if (test_cc(c->b, ctxt->eflags))
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001787 JMP_REL(rel);
1788 break;
1789 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 case 0xc7: /* Grp9 (cmpxchg8b) */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001791 rc = emulate_grp9(ctxt, ops, cr2);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792 if (rc != 0)
1793 goto done;
1794 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001796 /* Disable writeback. */
1797 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 goto writeback;
1799
1800cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001801 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001802 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 return -1;
1804}