blob: 79586003397aa7c90bc171f020710ce1fa2054e0 [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
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#define DPRINTF(x...) do {} while (0)
30#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/module.h>
Avi Kivityedf88412007-12-16 11:02:48 +020032#include <asm/kvm_x86_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
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 Kivityc7e75a32007-10-28 16:34:25 +020065#define MemAbs (1<<9) /* Memory operand is absolute displacement */
Avi Kivityb9fa9d62007-11-27 19:05:37 +020066#define String (1<<10) /* String instruction (rep capable) */
Avi Kivity6e3d5df2007-12-06 18:14:14 +020067#define Stack (1<<11) /* Stack instruction (push/pop) */
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivityc7e75a32007-10-28 16:34:25 +020069static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080070 /* 0x00 - 0x07 */
71 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
72 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
73 0, 0, 0, 0,
74 /* 0x08 - 0x0F */
75 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
76 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
77 0, 0, 0, 0,
78 /* 0x10 - 0x17 */
79 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
80 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
81 0, 0, 0, 0,
82 /* 0x18 - 0x1F */
83 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
84 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
85 0, 0, 0, 0,
86 /* 0x20 - 0x27 */
87 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
88 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030089 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080090 /* 0x28 - 0x2F */
91 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
93 0, 0, 0, 0,
94 /* 0x30 - 0x37 */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
97 0, 0, 0, 0,
98 /* 0x38 - 0x3F */
99 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
101 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700102 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200103 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700104 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200105 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300106 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200107 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
108 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300109 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200110 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
111 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700112 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700114 0, 0, 0, 0,
115 /* 0x68 - 0x6F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200116 0, 0, ImplicitOps | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300117 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
118 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300119 /* 0x70 - 0x77 */
120 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
121 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
122 /* 0x78 - 0x7F */
123 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
124 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 /* 0x80 - 0x87 */
126 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
127 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
128 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
129 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
130 /* 0x88 - 0x8F */
131 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
132 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200133 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov | Stack,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 /* 0x90 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200135 0, 0, 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200138 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
139 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200140 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
141 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200143 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
144 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
145 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0xB0 - 0xBF */
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300149 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200150 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300151 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152 /* 0xC8 - 0xCF */
153 0, 0, 0, 0, 0, 0, 0, 0,
154 /* 0xD0 - 0xD7 */
155 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
156 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
157 0, 0, 0, 0,
158 /* 0xD8 - 0xDF */
159 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300160 /* 0xE0 - 0xE7 */
161 0, 0, 0, 0, 0, 0, 0, 0,
162 /* 0xE8 - 0xEF */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200163 ImplicitOps | Stack, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps,
164 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 /* 0xF0 - 0xF7 */
166 0, 0, 0, 0,
Nitin A Kambleb284be52007-10-16 18:23:27 -0700167 ImplicitOps, ImplicitOps,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300168 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700170 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
172};
173
Avi Kivity038e51d2007-01-22 20:40:40 -0800174static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175 /* 0x00 - 0x0F */
176 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200177 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0x10 - 0x1F */
179 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
180 /* 0x20 - 0x2F */
181 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
182 0, 0, 0, 0, 0, 0, 0, 0,
183 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300184 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0x40 - 0x47 */
186 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
188 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
189 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
190 /* 0x48 - 0x4F */
191 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
192 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
193 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
194 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
195 /* 0x50 - 0x5F */
196 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
197 /* 0x60 - 0x6F */
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
199 /* 0x70 - 0x7F */
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300202 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
203 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
204 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
205 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206 /* 0x90 - 0x9F */
207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
208 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800209 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800211 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 /* 0xB0 - 0xB7 */
213 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800214 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
216 DstReg | SrcMem16 | ModRM | Mov,
217 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800218 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
220 DstReg | SrcMem16 | ModRM | Mov,
221 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800222 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
223 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 /* 0xD0 - 0xDF */
225 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
226 /* 0xE0 - 0xEF */
227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228 /* 0xF0 - 0xFF */
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
230};
231
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232/* EFLAGS bit definitions. */
233#define EFLG_OF (1<<11)
234#define EFLG_DF (1<<10)
235#define EFLG_SF (1<<7)
236#define EFLG_ZF (1<<6)
237#define EFLG_AF (1<<4)
238#define EFLG_PF (1<<2)
239#define EFLG_CF (1<<0)
240
241/*
242 * Instruction emulation:
243 * Most instructions are emulated directly via a fragment of inline assembly
244 * code. This allows us to save/restore EFLAGS and thus very easily pick up
245 * any modified flags.
246 */
247
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800248#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249#define _LO32 "k" /* force 32-bit operand */
250#define _STK "%%rsp" /* stack pointer */
251#elif defined(__i386__)
252#define _LO32 "" /* force 32-bit operand */
253#define _STK "%%esp" /* stack pointer */
254#endif
255
256/*
257 * These EFLAGS bits are restored from saved value during emulation, and
258 * any changes are written back to the saved value after emulation.
259 */
260#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
261
262/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200263#define _PRE_EFLAGS(_sav, _msk, _tmp) \
264 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
265 "movl %"_sav",%"_LO32 _tmp"; " \
266 "push %"_tmp"; " \
267 "push %"_tmp"; " \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "andl %"_LO32 _tmp",("_STK"); " \
270 "pushf; " \
271 "notl %"_LO32 _tmp"; " \
272 "andl %"_LO32 _tmp",("_STK"); " \
273 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
274 "pop %"_tmp"; " \
275 "orl %"_LO32 _tmp",("_STK"); " \
276 "popf; " \
277 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278
279/* After executing instruction: write-back necessary bits in EFLAGS. */
280#define _POST_EFLAGS(_sav, _msk, _tmp) \
281 /* _sav |= EFLAGS & _msk; */ \
282 "pushf; " \
283 "pop %"_tmp"; " \
284 "andl %"_msk",%"_LO32 _tmp"; " \
285 "orl %"_LO32 _tmp",%"_sav"; "
286
287/* Raw emulation: instruction has two explicit operands. */
288#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
289 do { \
290 unsigned long _tmp; \
291 \
292 switch ((_dst).bytes) { \
293 case 2: \
294 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400295 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400297 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 : "=m" (_eflags), "=m" ((_dst).val), \
299 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400300 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 break; \
302 case 4: \
303 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400304 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400306 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 : "=m" (_eflags), "=m" ((_dst).val), \
308 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400309 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310 break; \
311 case 8: \
312 __emulate_2op_8byte(_op, _src, _dst, \
313 _eflags, _qx, _qy); \
314 break; \
315 } \
316 } while (0)
317
318#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
319 do { \
320 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400321 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 case 1: \
323 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400324 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400326 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 : "=m" (_eflags), "=m" ((_dst).val), \
328 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400329 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 break; \
331 default: \
332 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
333 _wx, _wy, _lx, _ly, _qx, _qy); \
334 break; \
335 } \
336 } while (0)
337
338/* Source operand is byte-sized and may be restricted to just %cl. */
339#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
340 __emulate_2op(_op, _src, _dst, _eflags, \
341 "b", "c", "b", "c", "b", "c", "b", "c")
342
343/* Source operand is byte, word, long or quad sized. */
344#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
345 __emulate_2op(_op, _src, _dst, _eflags, \
346 "b", "q", "w", "r", _LO32, "r", "", "r")
347
348/* Source operand is word, long or quad sized. */
349#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
350 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
351 "w", "r", _LO32, "r", "", "r")
352
353/* Instruction has only one explicit operand (no source operand). */
354#define emulate_1op(_op, _dst, _eflags) \
355 do { \
356 unsigned long _tmp; \
357 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400358 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 case 1: \
360 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400361 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400363 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 : "=m" (_eflags), "=m" ((_dst).val), \
365 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400366 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 break; \
368 case 2: \
369 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400370 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400372 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 : "=m" (_eflags), "=m" ((_dst).val), \
374 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400375 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376 break; \
377 case 4: \
378 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400379 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 : "=m" (_eflags), "=m" ((_dst).val), \
383 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400384 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 break; \
386 case 8: \
387 __emulate_1op_8byte(_op, _dst, _eflags); \
388 break; \
389 } \
390 } while (0)
391
392/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800393#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
395 do { \
396 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400397 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400399 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400401 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 } while (0)
403
404#define __emulate_1op_8byte(_op, _dst, _eflags) \
405 do { \
406 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400407 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400409 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400411 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 } while (0)
413
414#elif defined(__i386__)
415#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
416#define __emulate_1op_8byte(_op, _dst, _eflags)
417#endif /* __i386__ */
418
419/* Fetch next part of the instruction being emulated. */
420#define insn_fetch(_type, _size, _eip) \
421({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200422 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400423 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 goto done; \
425 (_eip) += (_size); \
426 (_type)_x; \
427})
428
429/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300430#define address_mask(reg) \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200431 ((c->ad_bytes == sizeof(unsigned long)) ? \
432 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300434 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435#define register_address_increment(reg, inc) \
436 do { \
437 /* signed type ensures sign extension to long */ \
438 int _inc = (inc); \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200439 if (c->ad_bytes == sizeof(unsigned long)) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 (reg) += _inc; \
441 else \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200442 (reg) = ((reg) & \
443 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
444 (((reg) + _inc) & \
445 ((1UL << (c->ad_bytes << 3)) - 1)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 } while (0)
447
Nitin A Kamble098c9372007-08-19 11:00:36 +0300448#define JMP_REL(rel) \
449 do { \
Laurent Viviere4e03de2007-09-18 11:52:50 +0200450 register_address_increment(c->eip, rel); \
Nitin A Kamble098c9372007-08-19 11:00:36 +0300451 } while (0)
452
Avi Kivity62266862007-11-20 13:15:52 +0200453static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
454 struct x86_emulate_ops *ops,
455 unsigned long linear, u8 *dest)
456{
457 struct fetch_cache *fc = &ctxt->decode.fetch;
458 int rc;
459 int size;
460
461 if (linear < fc->start || linear >= fc->end) {
462 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
463 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
464 if (rc)
465 return rc;
466 fc->start = linear;
467 fc->end = linear + size;
468 }
469 *dest = fc->data[linear - fc->start];
470 return 0;
471}
472
473static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
474 struct x86_emulate_ops *ops,
475 unsigned long eip, void *dest, unsigned size)
476{
477 int rc = 0;
478
479 eip += ctxt->cs_base;
480 while (size--) {
481 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
482 if (rc)
483 return rc;
484 }
485 return 0;
486}
487
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000488/*
489 * Given the 'reg' portion of a ModRM byte, and a register block, return a
490 * pointer into the block that addresses the relevant register.
491 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
492 */
493static void *decode_register(u8 modrm_reg, unsigned long *regs,
494 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495{
496 void *p;
497
498 p = &regs[modrm_reg];
499 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
500 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
501 return p;
502}
503
504static int read_descriptor(struct x86_emulate_ctxt *ctxt,
505 struct x86_emulate_ops *ops,
506 void *ptr,
507 u16 *size, unsigned long *address, int op_bytes)
508{
509 int rc;
510
511 if (op_bytes == 2)
512 op_bytes = 3;
513 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300514 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
515 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 if (rc)
517 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300518 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
519 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 return rc;
521}
522
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300523static int test_cc(unsigned int condition, unsigned int flags)
524{
525 int rc = 0;
526
527 switch ((condition & 15) >> 1) {
528 case 0: /* o */
529 rc |= (flags & EFLG_OF);
530 break;
531 case 1: /* b/c/nae */
532 rc |= (flags & EFLG_CF);
533 break;
534 case 2: /* z/e */
535 rc |= (flags & EFLG_ZF);
536 break;
537 case 3: /* be/na */
538 rc |= (flags & (EFLG_CF|EFLG_ZF));
539 break;
540 case 4: /* s */
541 rc |= (flags & EFLG_SF);
542 break;
543 case 5: /* p/pe */
544 rc |= (flags & EFLG_PF);
545 break;
546 case 7: /* le/ng */
547 rc |= (flags & EFLG_ZF);
548 /* fall through */
549 case 6: /* l/nge */
550 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
551 break;
552 }
553
554 /* Odd condition identifiers (lsb == 1) have inverted sense. */
555 return (!!rc ^ (condition & 1));
556}
557
Avi Kivity3c118e22007-10-31 10:27:04 +0200558static void decode_register_operand(struct operand *op,
559 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200560 int inhibit_bytereg)
561{
Avi Kivity33615aa2007-10-31 11:15:56 +0200562 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200563 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200564
565 if (!(c->d & ModRM))
566 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200567 op->type = OP_REG;
568 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200569 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200570 op->val = *(u8 *)op->ptr;
571 op->bytes = 1;
572 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200573 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200574 op->bytes = c->op_bytes;
575 switch (op->bytes) {
576 case 2:
577 op->val = *(u16 *)op->ptr;
578 break;
579 case 4:
580 op->val = *(u32 *)op->ptr;
581 break;
582 case 8:
583 op->val = *(u64 *) op->ptr;
584 break;
585 }
586 }
587 op->orig_val = op->val;
588}
589
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200590static int decode_modrm(struct x86_emulate_ctxt *ctxt,
591 struct x86_emulate_ops *ops)
592{
593 struct decode_cache *c = &ctxt->decode;
594 u8 sib;
595 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
596 int rc = 0;
597
598 if (c->rex_prefix) {
599 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
600 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
601 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
602 }
603
604 c->modrm = insn_fetch(u8, 1, c->eip);
605 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
606 c->modrm_reg |= (c->modrm & 0x38) >> 3;
607 c->modrm_rm |= (c->modrm & 0x07);
608 c->modrm_ea = 0;
609 c->use_modrm_ea = 1;
610
611 if (c->modrm_mod == 3) {
612 c->modrm_val = *(unsigned long *)
613 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
614 return rc;
615 }
616
617 if (c->ad_bytes == 2) {
618 unsigned bx = c->regs[VCPU_REGS_RBX];
619 unsigned bp = c->regs[VCPU_REGS_RBP];
620 unsigned si = c->regs[VCPU_REGS_RSI];
621 unsigned di = c->regs[VCPU_REGS_RDI];
622
623 /* 16-bit ModR/M decode. */
624 switch (c->modrm_mod) {
625 case 0:
626 if (c->modrm_rm == 6)
627 c->modrm_ea += insn_fetch(u16, 2, c->eip);
628 break;
629 case 1:
630 c->modrm_ea += insn_fetch(s8, 1, c->eip);
631 break;
632 case 2:
633 c->modrm_ea += insn_fetch(u16, 2, c->eip);
634 break;
635 }
636 switch (c->modrm_rm) {
637 case 0:
638 c->modrm_ea += bx + si;
639 break;
640 case 1:
641 c->modrm_ea += bx + di;
642 break;
643 case 2:
644 c->modrm_ea += bp + si;
645 break;
646 case 3:
647 c->modrm_ea += bp + di;
648 break;
649 case 4:
650 c->modrm_ea += si;
651 break;
652 case 5:
653 c->modrm_ea += di;
654 break;
655 case 6:
656 if (c->modrm_mod != 0)
657 c->modrm_ea += bp;
658 break;
659 case 7:
660 c->modrm_ea += bx;
661 break;
662 }
663 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
664 (c->modrm_rm == 6 && c->modrm_mod != 0))
665 if (!c->override_base)
666 c->override_base = &ctxt->ss_base;
667 c->modrm_ea = (u16)c->modrm_ea;
668 } else {
669 /* 32/64-bit ModR/M decode. */
670 switch (c->modrm_rm) {
671 case 4:
672 case 12:
673 sib = insn_fetch(u8, 1, c->eip);
674 index_reg |= (sib >> 3) & 7;
675 base_reg |= sib & 7;
676 scale = sib >> 6;
677
678 switch (base_reg) {
679 case 5:
680 if (c->modrm_mod != 0)
681 c->modrm_ea += c->regs[base_reg];
682 else
683 c->modrm_ea +=
684 insn_fetch(s32, 4, c->eip);
685 break;
686 default:
687 c->modrm_ea += c->regs[base_reg];
688 }
689 switch (index_reg) {
690 case 4:
691 break;
692 default:
693 c->modrm_ea += c->regs[index_reg] << scale;
694 }
695 break;
696 case 5:
697 if (c->modrm_mod != 0)
698 c->modrm_ea += c->regs[c->modrm_rm];
699 else if (ctxt->mode == X86EMUL_MODE_PROT64)
700 rip_relative = 1;
701 break;
702 default:
703 c->modrm_ea += c->regs[c->modrm_rm];
704 break;
705 }
706 switch (c->modrm_mod) {
707 case 0:
708 if (c->modrm_rm == 5)
709 c->modrm_ea += insn_fetch(s32, 4, c->eip);
710 break;
711 case 1:
712 c->modrm_ea += insn_fetch(s8, 1, c->eip);
713 break;
714 case 2:
715 c->modrm_ea += insn_fetch(s32, 4, c->eip);
716 break;
717 }
718 }
719 if (rip_relative) {
720 c->modrm_ea += c->eip;
721 switch (c->d & SrcMask) {
722 case SrcImmByte:
723 c->modrm_ea += 1;
724 break;
725 case SrcImm:
726 if (c->d & ByteOp)
727 c->modrm_ea += 1;
728 else
729 if (c->op_bytes == 8)
730 c->modrm_ea += 4;
731 else
732 c->modrm_ea += c->op_bytes;
733 }
734 }
735done:
736 return rc;
737}
738
739static int decode_abs(struct x86_emulate_ctxt *ctxt,
740 struct x86_emulate_ops *ops)
741{
742 struct decode_cache *c = &ctxt->decode;
743 int rc = 0;
744
745 switch (c->ad_bytes) {
746 case 2:
747 c->modrm_ea = insn_fetch(u16, 2, c->eip);
748 break;
749 case 4:
750 c->modrm_ea = insn_fetch(u32, 4, c->eip);
751 break;
752 case 8:
753 c->modrm_ea = insn_fetch(u64, 8, c->eip);
754 break;
755 }
756done:
757 return rc;
758}
759
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200761x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200763 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 int mode = ctxt->mode;
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200766 int def_op_bytes, def_ad_bytes;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767
768 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769
Laurent Viviere4e03de2007-09-18 11:52:50 +0200770 memset(c, 0, sizeof(struct decode_cache));
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800771 c->eip = ctxt->vcpu->arch.rip;
772 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
774 switch (mode) {
775 case X86EMUL_MODE_REAL:
776 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200777 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 break;
779 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200780 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800782#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200784 def_op_bytes = 4;
785 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 break;
787#endif
788 default:
789 return -1;
790 }
791
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200792 c->op_bytes = def_op_bytes;
793 c->ad_bytes = def_ad_bytes;
794
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200796 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200797 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200799 /* switch between 2/4 bytes */
800 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 break;
802 case 0x67: /* address-size override */
803 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200804 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200805 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200807 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200808 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 break;
810 case 0x2e: /* CS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200811 c->override_base = &ctxt->cs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 break;
813 case 0x3e: /* DS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200814 c->override_base = &ctxt->ds_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 break;
816 case 0x26: /* ES override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200817 c->override_base = &ctxt->es_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 break;
819 case 0x64: /* FS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200820 c->override_base = &ctxt->fs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 break;
822 case 0x65: /* GS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200823 c->override_base = &ctxt->gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 break;
825 case 0x36: /* SS override */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200826 c->override_base = &ctxt->ss_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200828 case 0x40 ... 0x4f: /* REX */
829 if (mode != X86EMUL_MODE_PROT64)
830 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200831 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200832 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200834 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200836 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100837 c->rep_prefix = REPNE_PREFIX;
838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100840 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 default:
843 goto done_prefixes;
844 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200845
846 /* Any legacy prefix after a REX prefix nullifies its effect. */
847
Avi Kivity33615aa2007-10-31 11:15:56 +0200848 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 }
850
851done_prefixes:
852
853 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200854 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200855 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200856 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857
858 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200859 c->d = opcode_table[c->b];
860 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200862 if (c->b == 0x0f) {
863 c->twobyte = 1;
864 c->b = insn_fetch(u8, 1, c->eip);
865 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 }
867
868 /* Unrecognised? */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200869 if (c->d == 0) {
870 DPRINTF("Cannot emulate %02x\n", c->b);
871 return -1;
872 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 }
874
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200875 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
876 c->op_bytes = 8;
877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200879 if (c->d & ModRM)
880 rc = decode_modrm(ctxt, ops);
881 else if (c->d & MemAbs)
882 rc = decode_abs(ctxt, ops);
883 if (rc)
884 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885
Avi Kivityc7e75a32007-10-28 16:34:25 +0200886 if (!c->override_base)
887 c->override_base = &ctxt->ds_base;
888 if (mode == X86EMUL_MODE_PROT64 &&
889 c->override_base != &ctxt->fs_base &&
890 c->override_base != &ctxt->gs_base)
891 c->override_base = NULL;
892
893 if (c->override_base)
894 c->modrm_ea += *c->override_base;
895
896 if (c->ad_bytes != 8)
897 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 /*
899 * Decode and fetch the source operand: register, memory
900 * or immediate.
901 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200902 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 case SrcNone:
904 break;
905 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200906 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 break;
908 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200909 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 goto srcmem_common;
911 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200912 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 goto srcmem_common;
914 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200915 c->src.bytes = (c->d & ByteOp) ? 1 :
916 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300917 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400918 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300919 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400920 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200921 /*
922 * For instructions with a ModR/M byte, switch to register
923 * access if Mod = 3.
924 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200925 if ((c->d & ModRM) && c->modrm_mod == 3) {
926 c->src.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200927 break;
928 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200929 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 break;
931 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200932 c->src.type = OP_IMM;
933 c->src.ptr = (unsigned long *)c->eip;
934 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
935 if (c->src.bytes == 8)
936 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200938 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200940 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 break;
942 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200943 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 break;
945 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200946 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 break;
948 }
949 break;
950 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200951 c->src.type = OP_IMM;
952 c->src.ptr = (unsigned long *)c->eip;
953 c->src.bytes = 1;
954 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 break;
956 }
957
Avi Kivity038e51d2007-01-22 20:40:40 -0800958 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200959 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800960 case ImplicitOps:
961 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200962 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -0800963 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200964 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200965 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -0800966 break;
967 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 if ((c->d & ModRM) && c->modrm_mod == 3) {
969 c->dst.type = OP_REG;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200970 break;
971 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200972 c->dst.type = OP_MEM;
973 break;
974 }
975
976done:
977 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
978}
979
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200980static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
981{
982 struct decode_cache *c = &ctxt->decode;
983
984 c->dst.type = OP_MEM;
985 c->dst.bytes = c->op_bytes;
986 c->dst.val = c->src.val;
987 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
988 c->dst.ptr = (void *) register_address(ctxt->ss_base,
989 c->regs[VCPU_REGS_RSP]);
990}
991
992static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
993 struct x86_emulate_ops *ops)
994{
995 struct decode_cache *c = &ctxt->decode;
996 int rc;
997
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +0200998 rc = ops->read_std(register_address(ctxt->ss_base,
999 c->regs[VCPU_REGS_RSP]),
1000 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1001 if (rc != 0)
1002 return rc;
1003
1004 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
1005
1006 return 0;
1007}
1008
Laurent Vivier05f086f2007-09-24 11:10:55 +02001009static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001010{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001011 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001012 switch (c->modrm_reg) {
1013 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001014 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001015 break;
1016 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001017 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001018 break;
1019 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001020 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001021 break;
1022 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001023 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001024 break;
1025 case 4: /* sal/shl */
1026 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001027 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001028 break;
1029 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001030 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001031 break;
1032 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001033 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001034 break;
1035 }
1036}
1037
1038static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001039 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001040{
1041 struct decode_cache *c = &ctxt->decode;
1042 int rc = 0;
1043
1044 switch (c->modrm_reg) {
1045 case 0 ... 1: /* test */
1046 /*
1047 * Special case in Grp3: test has an immediate
1048 * source operand.
1049 */
1050 c->src.type = OP_IMM;
1051 c->src.ptr = (unsigned long *)c->eip;
1052 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1053 if (c->src.bytes == 8)
1054 c->src.bytes = 4;
1055 switch (c->src.bytes) {
1056 case 1:
1057 c->src.val = insn_fetch(s8, 1, c->eip);
1058 break;
1059 case 2:
1060 c->src.val = insn_fetch(s16, 2, c->eip);
1061 break;
1062 case 4:
1063 c->src.val = insn_fetch(s32, 4, c->eip);
1064 break;
1065 }
Laurent Vivier05f086f2007-09-24 11:10:55 +02001066 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001067 break;
1068 case 2: /* not */
1069 c->dst.val = ~c->dst.val;
1070 break;
1071 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001072 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001073 break;
1074 default:
1075 DPRINTF("Cannot emulate %02x\n", c->b);
1076 rc = X86EMUL_UNHANDLEABLE;
1077 break;
1078 }
1079done:
1080 return rc;
1081}
1082
1083static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001084 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001085{
1086 struct decode_cache *c = &ctxt->decode;
1087 int rc;
1088
1089 switch (c->modrm_reg) {
1090 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001091 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001092 break;
1093 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001094 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001095 break;
1096 case 4: /* jmp abs */
1097 if (c->b == 0xff)
1098 c->eip = c->dst.val;
1099 else {
1100 DPRINTF("Cannot emulate %02x\n", c->b);
1101 return X86EMUL_UNHANDLEABLE;
1102 }
1103 break;
1104 case 6: /* push */
1105
1106 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1107
1108 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1109 c->dst.bytes = 8;
1110 rc = ops->read_std((unsigned long)c->dst.ptr,
1111 &c->dst.val, 8, ctxt->vcpu);
1112 if (rc != 0)
1113 return rc;
1114 }
1115 register_address_increment(c->regs[VCPU_REGS_RSP],
1116 -c->dst.bytes);
1117 rc = ops->write_emulated(register_address(ctxt->ss_base,
1118 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1119 c->dst.bytes, ctxt->vcpu);
1120 if (rc != 0)
1121 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001122 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001123 break;
1124 default:
1125 DPRINTF("Cannot emulate %02x\n", c->b);
1126 return X86EMUL_UNHANDLEABLE;
1127 }
1128 return 0;
1129}
1130
1131static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1132 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001133 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134{
1135 struct decode_cache *c = &ctxt->decode;
1136 u64 old, new;
1137 int rc;
1138
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001139 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001140 if (rc != 0)
1141 return rc;
1142
1143 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1144 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1145
1146 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1147 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001148 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001149
1150 } else {
1151 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1152 (u32) c->regs[VCPU_REGS_RBX];
1153
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001154 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155 if (rc != 0)
1156 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001157 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001158 }
1159 return 0;
1160}
1161
1162static inline int writeback(struct x86_emulate_ctxt *ctxt,
1163 struct x86_emulate_ops *ops)
1164{
1165 int rc;
1166 struct decode_cache *c = &ctxt->decode;
1167
1168 switch (c->dst.type) {
1169 case OP_REG:
1170 /* The 4-byte case *is* correct:
1171 * in 64-bit mode we zero-extend.
1172 */
1173 switch (c->dst.bytes) {
1174 case 1:
1175 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1176 break;
1177 case 2:
1178 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1179 break;
1180 case 4:
1181 *c->dst.ptr = (u32)c->dst.val;
1182 break; /* 64b: zero-ext */
1183 case 8:
1184 *c->dst.ptr = c->dst.val;
1185 break;
1186 }
1187 break;
1188 case OP_MEM:
1189 if (c->lock_prefix)
1190 rc = ops->cmpxchg_emulated(
1191 (unsigned long)c->dst.ptr,
1192 &c->dst.orig_val,
1193 &c->dst.val,
1194 c->dst.bytes,
1195 ctxt->vcpu);
1196 else
1197 rc = ops->write_emulated(
1198 (unsigned long)c->dst.ptr,
1199 &c->dst.val,
1200 c->dst.bytes,
1201 ctxt->vcpu);
1202 if (rc != 0)
1203 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001204 break;
1205 case OP_NONE:
1206 /* no writeback */
1207 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001208 default:
1209 break;
1210 }
1211 return 0;
1212}
1213
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001214int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001215x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001216{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001217 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001218 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001219 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001220 struct decode_cache *c = &ctxt->decode;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001221 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001222
Laurent Vivier34273182007-09-18 11:27:37 +02001223 /* Shadow copy of register state. Committed on successful emulation.
1224 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1225 * modify them.
1226 */
1227
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001228 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001229 saved_eip = c->eip;
1230
Avi Kivityc7e75a32007-10-28 16:34:25 +02001231 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001232 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001233
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001234 if (c->rep_prefix && (c->d & String)) {
1235 /* All REP prefixes have the same first termination condition */
1236 if (c->regs[VCPU_REGS_RCX] == 0) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001237 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001238 goto done;
1239 }
1240 /* The second termination condition only applies for REPE
1241 * and REPNE. Test if the repeat string operation prefix is
1242 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1243 * corresponding termination condition according to:
1244 * - if REPE/REPZ and ZF = 0 then done
1245 * - if REPNE/REPNZ and ZF = 1 then done
1246 */
1247 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1248 (c->b == 0xae) || (c->b == 0xaf)) {
1249 if ((c->rep_prefix == REPE_PREFIX) &&
1250 ((ctxt->eflags & EFLG_ZF) == 0)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001251 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001252 goto done;
1253 }
1254 if ((c->rep_prefix == REPNE_PREFIX) &&
1255 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001256 ctxt->vcpu->arch.rip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001257 goto done;
1258 }
1259 }
1260 c->regs[VCPU_REGS_RCX]--;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001261 c->eip = ctxt->vcpu->arch.rip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001262 }
1263
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001264 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001265 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001266 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001267 rc = ops->read_emulated((unsigned long)c->src.ptr,
1268 &c->src.val,
1269 c->src.bytes,
1270 ctxt->vcpu);
1271 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001272 goto done;
1273 c->src.orig_val = c->src.val;
1274 }
1275
1276 if ((c->d & DstMask) == ImplicitOps)
1277 goto special_insn;
1278
1279
1280 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001281 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001282 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1283 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001284 if (c->d & BitOp) {
1285 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001286
Laurent Viviere4e03de2007-09-18 11:52:50 +02001287 c->dst.ptr = (void *)c->dst.ptr +
1288 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001289 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001290 if (!(c->d & Mov) &&
1291 /* optimisation - avoid slow emulated read */
1292 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1293 &c->dst.val,
1294 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001295 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001296 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001297 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001298
Avi Kivity018a98d2007-11-27 19:30:56 +02001299special_insn:
1300
Laurent Viviere4e03de2007-09-18 11:52:50 +02001301 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 goto twobyte_insn;
1303
Laurent Viviere4e03de2007-09-18 11:52:50 +02001304 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 case 0x00 ... 0x05:
1306 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001307 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 break;
1309 case 0x08 ... 0x0d:
1310 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001311 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 break;
1313 case 0x10 ... 0x15:
1314 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001315 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 break;
1317 case 0x18 ... 0x1d:
1318 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001319 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001321 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001323 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001325 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001326 c->dst.type = OP_REG;
1327 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1328 c->dst.val = *(u8 *)c->dst.ptr;
1329 c->dst.bytes = 1;
1330 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001331 goto and;
1332 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001333 c->dst.type = OP_REG;
1334 c->dst.bytes = c->op_bytes;
1335 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1336 if (c->op_bytes == 2)
1337 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001338 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001339 c->dst.val = *(u32 *)c->dst.ptr;
1340 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001341 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342 case 0x28 ... 0x2d:
1343 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001344 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 break;
1346 case 0x30 ... 0x35:
1347 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001348 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 break;
1350 case 0x38 ... 0x3d:
1351 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001352 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001354 case 0x40 ... 0x47: /* inc r16/r32 */
1355 emulate_1op("inc", c->dst, ctxt->eflags);
1356 break;
1357 case 0x48 ... 0x4f: /* dec r16/r32 */
1358 emulate_1op("dec", c->dst, ctxt->eflags);
1359 break;
1360 case 0x50 ... 0x57: /* push reg */
1361 c->dst.type = OP_MEM;
1362 c->dst.bytes = c->op_bytes;
1363 c->dst.val = c->src.val;
1364 register_address_increment(c->regs[VCPU_REGS_RSP],
1365 -c->op_bytes);
1366 c->dst.ptr = (void *) register_address(
1367 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1368 break;
1369 case 0x58 ... 0x5f: /* pop reg */
1370 pop_instruction:
1371 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1372 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1373 c->op_bytes, ctxt->vcpu)) != 0)
1374 goto done;
1375
1376 register_address_increment(c->regs[VCPU_REGS_RSP],
1377 c->op_bytes);
1378 c->dst.type = OP_NONE; /* Disable writeback. */
1379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001381 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001383 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001385 case 0x6a: /* push imm8 */
1386 c->src.val = 0L;
1387 c->src.val = insn_fetch(s8, 1, c->eip);
1388 emulate_push(ctxt);
1389 break;
1390 case 0x6c: /* insb */
1391 case 0x6d: /* insw/insd */
1392 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1393 1,
1394 (c->d & ByteOp) ? 1 : c->op_bytes,
1395 c->rep_prefix ?
1396 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1397 (ctxt->eflags & EFLG_DF),
1398 register_address(ctxt->es_base,
1399 c->regs[VCPU_REGS_RDI]),
1400 c->rep_prefix,
1401 c->regs[VCPU_REGS_RDX]) == 0) {
1402 c->eip = saved_eip;
1403 return -1;
1404 }
1405 return 0;
1406 case 0x6e: /* outsb */
1407 case 0x6f: /* outsw/outsd */
1408 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1409 0,
1410 (c->d & ByteOp) ? 1 : c->op_bytes,
1411 c->rep_prefix ?
1412 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1413 (ctxt->eflags & EFLG_DF),
1414 register_address(c->override_base ?
1415 *c->override_base :
1416 ctxt->ds_base,
1417 c->regs[VCPU_REGS_RSI]),
1418 c->rep_prefix,
1419 c->regs[VCPU_REGS_RDX]) == 0) {
1420 c->eip = saved_eip;
1421 return -1;
1422 }
1423 return 0;
1424 case 0x70 ... 0x7f: /* jcc (short) */ {
1425 int rel = insn_fetch(s8, 1, c->eip);
1426
1427 if (test_cc(c->b, ctxt->eflags))
1428 JMP_REL(rel);
1429 break;
1430 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001432 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 case 0:
1434 goto add;
1435 case 1:
1436 goto or;
1437 case 2:
1438 goto adc;
1439 case 3:
1440 goto sbb;
1441 case 4:
1442 goto and;
1443 case 5:
1444 goto sub;
1445 case 6:
1446 goto xor;
1447 case 7:
1448 goto cmp;
1449 }
1450 break;
1451 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001452 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 break;
1454 case 0x86 ... 0x87: /* xchg */
1455 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001456 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001458 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 break;
1460 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001461 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 break;
1463 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001464 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 break; /* 64b reg: zero-extend */
1466 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001467 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 break;
1469 }
1470 /*
1471 * Write back the memory destination with implicit LOCK
1472 * prefix.
1473 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 c->dst.val = c->src.val;
1475 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001478 goto mov;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001479 case 0x8d: /* lea r16/r32, m */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001480 c->dst.val = c->modrm_val;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001481 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001483 rc = emulate_grp1a(ctxt, ops);
1484 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001487 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001488 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001489 emulate_push(ctxt);
1490 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001491 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001492 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001493 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001494 case 0xa0 ... 0xa1: /* mov */
1495 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1496 c->dst.val = c->src.val;
1497 break;
1498 case 0xa2 ... 0xa3: /* mov */
1499 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1500 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001502 c->dst.type = OP_MEM;
1503 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1504 c->dst.ptr = (unsigned long *)register_address(
1505 ctxt->es_base,
1506 c->regs[VCPU_REGS_RDI]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 if ((rc = ops->read_emulated(register_address(
Laurent Viviere4e03de2007-09-18 11:52:50 +02001508 c->override_base ? *c->override_base :
1509 ctxt->ds_base,
1510 c->regs[VCPU_REGS_RSI]),
1511 &c->dst.val,
1512 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001514 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001515 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001516 : c->dst.bytes);
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 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001522 c->src.type = OP_NONE; /* Disable writeback. */
1523 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1524 c->src.ptr = (unsigned long *)register_address(
1525 c->override_base ? *c->override_base :
1526 ctxt->ds_base,
1527 c->regs[VCPU_REGS_RSI]);
1528 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1529 &c->src.val,
1530 c->src.bytes,
1531 ctxt->vcpu)) != 0)
1532 goto done;
1533
1534 c->dst.type = OP_NONE; /* Disable writeback. */
1535 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1536 c->dst.ptr = (unsigned long *)register_address(
1537 ctxt->es_base,
1538 c->regs[VCPU_REGS_RDI]);
1539 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1540 &c->dst.val,
1541 c->dst.bytes,
1542 ctxt->vcpu)) != 0)
1543 goto done;
1544
1545 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1546
1547 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1548
1549 register_address_increment(c->regs[VCPU_REGS_RSI],
1550 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1551 : c->src.bytes);
1552 register_address_increment(c->regs[VCPU_REGS_RDI],
1553 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1554 : c->dst.bytes);
1555
1556 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001558 c->dst.type = OP_MEM;
1559 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Sheng Yanga7e6c882007-11-15 14:52:28 +08001560 c->dst.ptr = (unsigned long *)register_address(
1561 ctxt->es_base,
1562 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001563 c->dst.val = c->regs[VCPU_REGS_RAX];
1564 register_address_increment(c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001565 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001566 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 break;
1568 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001569 c->dst.type = OP_REG;
1570 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1571 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Sheng Yanga7e6c882007-11-15 14:52:28 +08001572 if ((rc = ops->read_emulated(register_address(
1573 c->override_base ? *c->override_base :
1574 ctxt->ds_base,
1575 c->regs[VCPU_REGS_RSI]),
1576 &c->dst.val,
1577 c->dst.bytes,
1578 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 goto done;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001580 register_address_increment(c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001581 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001582 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 break;
1584 case 0xae ... 0xaf: /* scas */
1585 DPRINTF("Urk! I don't handle SCAS.\n");
1586 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02001587 case 0xc0 ... 0xc1:
1588 emulate_grp2(ctxt);
1589 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001590 case 0xc3: /* ret */
1591 c->dst.ptr = &c->eip;
1592 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001593 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1594 mov:
1595 c->dst.val = c->src.val;
1596 break;
1597 case 0xd0 ... 0xd1: /* Grp2 */
1598 c->src.val = 1;
1599 emulate_grp2(ctxt);
1600 break;
1601 case 0xd2 ... 0xd3: /* Grp2 */
1602 c->src.val = c->regs[VCPU_REGS_RCX];
1603 emulate_grp2(ctxt);
1604 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001605 case 0xe8: /* call (near) */ {
1606 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001607 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001608 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001610 break;
1611 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001612 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001613 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001614 default:
1615 DPRINTF("Call: Invalid op_bytes\n");
1616 goto cannot_emulate;
1617 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001618 c->src.val = (unsigned long) c->eip;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001619 JMP_REL(rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001620 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001621 emulate_push(ctxt);
1622 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001623 }
1624 case 0xe9: /* jmp rel */
1625 case 0xeb: /* jmp rel short */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001626 JMP_REL(c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001627 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001628 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001629 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001630 ctxt->vcpu->arch.halt_request = 1;
Avi Kivity111de5d2007-11-27 19:14:21 +02001631 goto done;
1632 case 0xf5: /* cmc */
1633 /* complement carry flag from eflags reg */
1634 ctxt->eflags ^= EFLG_CF;
1635 c->dst.type = OP_NONE; /* Disable writeback. */
1636 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001637 case 0xf6 ... 0xf7: /* Grp3 */
1638 rc = emulate_grp3(ctxt, ops);
1639 if (rc != 0)
1640 goto done;
1641 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001642 case 0xf8: /* clc */
1643 ctxt->eflags &= ~EFLG_CF;
1644 c->dst.type = OP_NONE; /* Disable writeback. */
1645 break;
1646 case 0xfa: /* cli */
1647 ctxt->eflags &= ~X86_EFLAGS_IF;
1648 c->dst.type = OP_NONE; /* Disable writeback. */
1649 break;
1650 case 0xfb: /* sti */
1651 ctxt->eflags |= X86_EFLAGS_IF;
1652 c->dst.type = OP_NONE; /* Disable writeback. */
1653 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001654 case 0xfe ... 0xff: /* Grp4/Grp5 */
1655 rc = emulate_grp45(ctxt, ops);
1656 if (rc != 0)
1657 goto done;
1658 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001660
1661writeback:
1662 rc = writeback(ctxt, ops);
1663 if (rc != 0)
1664 goto done;
1665
1666 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001667 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
1668 ctxt->vcpu->arch.rip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001669
1670done:
1671 if (rc == X86EMUL_UNHANDLEABLE) {
1672 c->eip = saved_eip;
1673 return -1;
1674 }
1675 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676
1677twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001678 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001680 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681 u16 size;
1682 unsigned long address;
1683
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001684 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001686 goto cannot_emulate;
1687
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001688 rc = kvm_fix_hypercall(ctxt->vcpu);
1689 if (rc)
1690 goto done;
1691
1692 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001693 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001695 rc = read_descriptor(ctxt, ops, c->src.ptr,
1696 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 if (rc)
1698 goto done;
1699 realmode_lgdt(ctxt->vcpu, size, address);
1700 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001701 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001702 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001703 rc = kvm_fix_hypercall(ctxt->vcpu);
1704 if (rc)
1705 goto done;
1706 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001707 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001708 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001709 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001710 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001711 if (rc)
1712 goto done;
1713 realmode_lidt(ctxt->vcpu, size, address);
1714 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715 break;
1716 case 4: /* smsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001717 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001719 *(u16 *)&c->regs[c->modrm_rm]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 = realmode_get_cr(ctxt->vcpu, 0);
1721 break;
1722 case 6: /* lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 goto cannot_emulate;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001725 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1726 &ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 break;
1728 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001729 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 break;
1731 default:
1732 goto cannot_emulate;
1733 }
Laurent Viviera01af5e2007-09-24 11:10:56 +02001734 /* Disable writeback. */
1735 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001737 case 0x06:
1738 emulate_clts(ctxt->vcpu);
1739 c->dst.type = OP_NONE;
1740 break;
1741 case 0x08: /* invd */
1742 case 0x09: /* wbinvd */
1743 case 0x0d: /* GrpP (prefetch) */
1744 case 0x18: /* Grp16 (prefetch/nop) */
1745 c->dst.type = OP_NONE;
1746 break;
1747 case 0x20: /* mov cr, reg */
1748 if (c->modrm_mod != 3)
1749 goto cannot_emulate;
1750 c->regs[c->modrm_rm] =
1751 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1752 c->dst.type = OP_NONE; /* no writeback */
1753 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001755 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001758 if (rc)
1759 goto cannot_emulate;
1760 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001762 case 0x22: /* mov reg, cr */
1763 if (c->modrm_mod != 3)
1764 goto cannot_emulate;
1765 realmode_set_cr(ctxt->vcpu,
1766 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1767 c->dst.type = OP_NONE;
1768 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001770 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001772 rc = emulator_set_dr(ctxt, c->modrm_reg,
1773 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001774 if (rc)
1775 goto cannot_emulate;
1776 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001778 case 0x30:
1779 /* wrmsr */
1780 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1781 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1782 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1783 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001784 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001785 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001786 }
1787 rc = X86EMUL_CONTINUE;
1788 c->dst.type = OP_NONE;
1789 break;
1790 case 0x32:
1791 /* rdmsr */
1792 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1793 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001794 kvm_inject_gp(ctxt->vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001795 c->eip = ctxt->vcpu->arch.rip;
Avi Kivity018a98d2007-11-27 19:30:56 +02001796 } else {
1797 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1798 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1799 }
1800 rc = X86EMUL_CONTINUE;
1801 c->dst.type = OP_NONE;
1802 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001804 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001805 if (!test_cc(c->b, ctxt->eflags))
1806 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001808 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1809 long int rel;
1810
1811 switch (c->op_bytes) {
1812 case 2:
1813 rel = insn_fetch(s16, 2, c->eip);
1814 break;
1815 case 4:
1816 rel = insn_fetch(s32, 4, c->eip);
1817 break;
1818 case 8:
1819 rel = insn_fetch(s64, 8, c->eip);
1820 break;
1821 default:
1822 DPRINTF("jnz: Invalid op_bytes\n");
1823 goto cannot_emulate;
1824 }
1825 if (test_cc(c->b, ctxt->eflags))
1826 JMP_REL(rel);
1827 c->dst.type = OP_NONE;
1828 break;
1829 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001830 case 0xa3:
1831 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001832 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001833 /* only subword offset */
1834 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001835 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001836 break;
1837 case 0xab:
1838 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001839 /* only subword offset */
1840 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001841 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001842 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 case 0xb0 ... 0xb1: /* cmpxchg */
1844 /*
1845 * Save real source value, then compare EAX against
1846 * destination.
1847 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001848 c->src.orig_val = c->src.val;
1849 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001850 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1851 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001853 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 } else {
1855 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001856 c->dst.type = OP_REG;
1857 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 }
1859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 case 0xb3:
1861 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001862 /* only subword offset */
1863 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001864 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001867 c->dst.bytes = c->op_bytes;
1868 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1869 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001872 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873 case 0:
1874 goto bt;
1875 case 1:
1876 goto bts;
1877 case 2:
1878 goto btr;
1879 case 3:
1880 goto btc;
1881 }
1882 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001883 case 0xbb:
1884 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001885 /* only subword offset */
1886 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001887 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001888 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001890 c->dst.bytes = c->op_bytes;
1891 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1892 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 break;
Sheng Yanga012e652007-10-15 14:24:20 +08001894 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001895 c->dst.bytes = c->op_bytes;
1896 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1897 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08001898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001900 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901 if (rc != 0)
1902 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02001903 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001904 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 }
1906 goto writeback;
1907
1908cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001909 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02001910 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 return -1;
1912}