blob: e4ce34c52ba1e55182bdfccf63fa9c83d3f1e5fd [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
26#define DPRINTF(_f, _a ...) printf( _f , ## _a )
27#else
28#include "kvm.h"
29#define DPRINTF(x...) do {} while (0)
30#endif
31#include "x86_emulate.h"
32#include <linux/module.h>
33
34/*
35 * Opcode effective-address decode tables.
36 * Note that we only emulate instructions that have at least one memory
37 * operand (excluding implicit stack references). We assume that stack
38 * references and instruction fetches will never occur in special memory
39 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
40 * not be handled.
41 */
42
43/* Operand sizes: 8-bit operands or specified/overridden size. */
44#define ByteOp (1<<0) /* 8-bit operands. */
45/* Destination operand type. */
46#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
47#define DstReg (2<<1) /* Register operand. */
48#define DstMem (3<<1) /* Memory operand. */
49#define DstMask (3<<1)
50/* Source operand type. */
51#define SrcNone (0<<3) /* No source operand. */
52#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
53#define SrcReg (1<<3) /* Register operand. */
54#define SrcMem (2<<3) /* Memory operand. */
55#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
57#define SrcImm (5<<3) /* Immediate operand. */
58#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
59#define SrcMask (7<<3)
60/* Generic ModRM decode. */
61#define ModRM (1<<6)
62/* Destination is only written; never read. */
63#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080064#define BitOp (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
66static u8 opcode_table[256] = {
67 /* 0x00 - 0x07 */
68 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
69 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
70 0, 0, 0, 0,
71 /* 0x08 - 0x0F */
72 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
73 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
74 0, 0, 0, 0,
75 /* 0x10 - 0x17 */
76 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
77 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
78 0, 0, 0, 0,
79 /* 0x18 - 0x1F */
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
82 0, 0, 0, 0,
83 /* 0x20 - 0x27 */
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030086 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080087 /* 0x28 - 0x2F */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
90 0, 0, 0, 0,
91 /* 0x30 - 0x37 */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x38 - 0x3F */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
98 0, 0, 0, 0,
99 /* 0x40 - 0x4F */
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300101 /* 0x50 - 0x57 */
Nitin A Kamble7e778162007-08-19 11:07:06 +0300102 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
103 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300104 /* 0x58 - 0x5F */
105 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
106 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700107 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700109 0, 0, 0, 0,
110 /* 0x68 - 0x6F */
111 0, 0, ImplicitOps|Mov, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300112 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
113 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 /* 0x70 - 0x7F */
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 /* 0x80 - 0x87 */
117 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
118 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
119 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
120 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
121 /* 0x88 - 0x8F */
122 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
123 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
124 0, 0, 0, DstMem | SrcNone | ModRM | Mov,
125 /* 0x90 - 0x9F */
Nitin A Kamblefd2a7602007-08-28 18:22:47 -0700126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127 /* 0xA0 - 0xA7 */
128 ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
129 ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
130 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
131 ByteOp | ImplicitOps, ImplicitOps,
132 /* 0xA8 - 0xAF */
133 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
134 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
135 ByteOp | ImplicitOps, ImplicitOps,
136 /* 0xB0 - 0xBF */
137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300139 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
140 0, ImplicitOps, 0, 0,
141 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 /* 0xC8 - 0xCF */
143 0, 0, 0, 0, 0, 0, 0, 0,
144 /* 0xD0 - 0xD7 */
145 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
146 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
147 0, 0, 0, 0,
148 /* 0xD8 - 0xDF */
149 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300150 /* 0xE0 - 0xE7 */
151 0, 0, 0, 0, 0, 0, 0, 0,
152 /* 0xE8 - 0xEF */
Nitin A Kamblef6eed392007-08-28 18:08:37 -0700153 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154 /* 0xF0 - 0xF7 */
155 0, 0, 0, 0,
Avi Kivity72d6e5a2007-06-05 16:15:51 +0300156 ImplicitOps, 0,
157 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 /* 0xF8 - 0xFF */
159 0, 0, 0, 0,
160 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
161};
162
Avi Kivity038e51d2007-01-22 20:40:40 -0800163static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 /* 0x00 - 0x0F */
165 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity687fdbf2007-05-24 11:17:33 +0300166 0, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 /* 0x10 - 0x1F */
168 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
169 /* 0x20 - 0x2F */
170 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0,
172 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300173 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 /* 0x40 - 0x47 */
175 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
176 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
177 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
178 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
179 /* 0x48 - 0x4F */
180 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
181 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
184 /* 0x50 - 0x5F */
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 /* 0x60 - 0x6F */
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188 /* 0x70 - 0x7F */
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190 /* 0x80 - 0x8F */
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 /* 0x90 - 0x9F */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800195 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xA8 - 0xAF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800197 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 /* 0xB0 - 0xB7 */
199 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800200 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
202 DstReg | SrcMem16 | ModRM | Mov,
203 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800204 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
206 DstReg | SrcMem16 | ModRM | Mov,
207 /* 0xC0 - 0xCF */
208 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
209 /* 0xD0 - 0xDF */
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211 /* 0xE0 - 0xEF */
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213 /* 0xF0 - 0xFF */
214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
215};
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* Type, address-of, and value of an instruction's operand. */
218struct operand {
219 enum { OP_REG, OP_MEM, OP_IMM } type;
220 unsigned int bytes;
221 unsigned long val, orig_val, *ptr;
222};
223
224/* EFLAGS bit definitions. */
225#define EFLG_OF (1<<11)
226#define EFLG_DF (1<<10)
227#define EFLG_SF (1<<7)
228#define EFLG_ZF (1<<6)
229#define EFLG_AF (1<<4)
230#define EFLG_PF (1<<2)
231#define EFLG_CF (1<<0)
232
233/*
234 * Instruction emulation:
235 * Most instructions are emulated directly via a fragment of inline assembly
236 * code. This allows us to save/restore EFLAGS and thus very easily pick up
237 * any modified flags.
238 */
239
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800240#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define _LO32 "k" /* force 32-bit operand */
242#define _STK "%%rsp" /* stack pointer */
243#elif defined(__i386__)
244#define _LO32 "" /* force 32-bit operand */
245#define _STK "%%esp" /* stack pointer */
246#endif
247
248/*
249 * These EFLAGS bits are restored from saved value during emulation, and
250 * any changes are written back to the saved value after emulation.
251 */
252#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
253
254/* Before executing instruction: restore necessary bits in EFLAGS. */
255#define _PRE_EFLAGS(_sav, _msk, _tmp) \
256 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
257 "push %"_sav"; " \
258 "movl %"_msk",%"_LO32 _tmp"; " \
259 "andl %"_LO32 _tmp",("_STK"); " \
260 "pushf; " \
261 "notl %"_LO32 _tmp"; " \
262 "andl %"_LO32 _tmp",("_STK"); " \
263 "pop %"_tmp"; " \
264 "orl %"_LO32 _tmp",("_STK"); " \
265 "popf; " \
266 /* _sav &= ~msk; */ \
267 "movl %"_msk",%"_LO32 _tmp"; " \
268 "notl %"_LO32 _tmp"; " \
269 "andl %"_LO32 _tmp",%"_sav"; "
270
271/* After executing instruction: write-back necessary bits in EFLAGS. */
272#define _POST_EFLAGS(_sav, _msk, _tmp) \
273 /* _sav |= EFLAGS & _msk; */ \
274 "pushf; " \
275 "pop %"_tmp"; " \
276 "andl %"_msk",%"_LO32 _tmp"; " \
277 "orl %"_LO32 _tmp",%"_sav"; "
278
279/* Raw emulation: instruction has two explicit operands. */
280#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
281 do { \
282 unsigned long _tmp; \
283 \
284 switch ((_dst).bytes) { \
285 case 2: \
286 __asm__ __volatile__ ( \
287 _PRE_EFLAGS("0","4","2") \
288 _op"w %"_wx"3,%1; " \
289 _POST_EFLAGS("0","4","2") \
290 : "=m" (_eflags), "=m" ((_dst).val), \
291 "=&r" (_tmp) \
292 : _wy ((_src).val), "i" (EFLAGS_MASK) ); \
293 break; \
294 case 4: \
295 __asm__ __volatile__ ( \
296 _PRE_EFLAGS("0","4","2") \
297 _op"l %"_lx"3,%1; " \
298 _POST_EFLAGS("0","4","2") \
299 : "=m" (_eflags), "=m" ((_dst).val), \
300 "=&r" (_tmp) \
301 : _ly ((_src).val), "i" (EFLAGS_MASK) ); \
302 break; \
303 case 8: \
304 __emulate_2op_8byte(_op, _src, _dst, \
305 _eflags, _qx, _qy); \
306 break; \
307 } \
308 } while (0)
309
310#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
311 do { \
312 unsigned long _tmp; \
313 switch ( (_dst).bytes ) \
314 { \
315 case 1: \
316 __asm__ __volatile__ ( \
317 _PRE_EFLAGS("0","4","2") \
318 _op"b %"_bx"3,%1; " \
319 _POST_EFLAGS("0","4","2") \
320 : "=m" (_eflags), "=m" ((_dst).val), \
321 "=&r" (_tmp) \
322 : _by ((_src).val), "i" (EFLAGS_MASK) ); \
323 break; \
324 default: \
325 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
326 _wx, _wy, _lx, _ly, _qx, _qy); \
327 break; \
328 } \
329 } while (0)
330
331/* Source operand is byte-sized and may be restricted to just %cl. */
332#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
333 __emulate_2op(_op, _src, _dst, _eflags, \
334 "b", "c", "b", "c", "b", "c", "b", "c")
335
336/* Source operand is byte, word, long or quad sized. */
337#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
338 __emulate_2op(_op, _src, _dst, _eflags, \
339 "b", "q", "w", "r", _LO32, "r", "", "r")
340
341/* Source operand is word, long or quad sized. */
342#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
343 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
344 "w", "r", _LO32, "r", "", "r")
345
346/* Instruction has only one explicit operand (no source operand). */
347#define emulate_1op(_op, _dst, _eflags) \
348 do { \
349 unsigned long _tmp; \
350 \
351 switch ( (_dst).bytes ) \
352 { \
353 case 1: \
354 __asm__ __volatile__ ( \
355 _PRE_EFLAGS("0","3","2") \
356 _op"b %1; " \
357 _POST_EFLAGS("0","3","2") \
358 : "=m" (_eflags), "=m" ((_dst).val), \
359 "=&r" (_tmp) \
360 : "i" (EFLAGS_MASK) ); \
361 break; \
362 case 2: \
363 __asm__ __volatile__ ( \
364 _PRE_EFLAGS("0","3","2") \
365 _op"w %1; " \
366 _POST_EFLAGS("0","3","2") \
367 : "=m" (_eflags), "=m" ((_dst).val), \
368 "=&r" (_tmp) \
369 : "i" (EFLAGS_MASK) ); \
370 break; \
371 case 4: \
372 __asm__ __volatile__ ( \
373 _PRE_EFLAGS("0","3","2") \
374 _op"l %1; " \
375 _POST_EFLAGS("0","3","2") \
376 : "=m" (_eflags), "=m" ((_dst).val), \
377 "=&r" (_tmp) \
378 : "i" (EFLAGS_MASK) ); \
379 break; \
380 case 8: \
381 __emulate_1op_8byte(_op, _dst, _eflags); \
382 break; \
383 } \
384 } while (0)
385
386/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800387#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
389 do { \
390 __asm__ __volatile__ ( \
391 _PRE_EFLAGS("0","4","2") \
392 _op"q %"_qx"3,%1; " \
393 _POST_EFLAGS("0","4","2") \
394 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
395 : _qy ((_src).val), "i" (EFLAGS_MASK) ); \
396 } while (0)
397
398#define __emulate_1op_8byte(_op, _dst, _eflags) \
399 do { \
400 __asm__ __volatile__ ( \
401 _PRE_EFLAGS("0","3","2") \
402 _op"q %1; " \
403 _POST_EFLAGS("0","3","2") \
404 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
405 : "i" (EFLAGS_MASK) ); \
406 } while (0)
407
408#elif defined(__i386__)
409#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
410#define __emulate_1op_8byte(_op, _dst, _eflags)
411#endif /* __i386__ */
412
413/* Fetch next part of the instruction being emulated. */
414#define insn_fetch(_type, _size, _eip) \
415({ unsigned long _x; \
416 rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
Laurent Viviercebff022007-07-30 13:35:24 +0300417 (_size), ctxt->vcpu); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 if ( rc != 0 ) \
419 goto done; \
420 (_eip) += (_size); \
421 (_type)_x; \
422})
423
424/* Access/update address held in a register, based on addressing mode. */
Laurent Viviere70669a2007-08-05 10:36:40 +0300425#define address_mask(reg) \
426 ((ad_bytes == sizeof(unsigned long)) ? \
427 (reg) : ((reg) & ((1UL << (ad_bytes << 3)) - 1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428#define register_address(base, reg) \
Laurent Viviere70669a2007-08-05 10:36:40 +0300429 ((base) + address_mask(reg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430#define register_address_increment(reg, inc) \
431 do { \
432 /* signed type ensures sign extension to long */ \
433 int _inc = (inc); \
434 if ( ad_bytes == sizeof(unsigned long) ) \
435 (reg) += _inc; \
436 else \
437 (reg) = ((reg) & ~((1UL << (ad_bytes << 3)) - 1)) | \
438 (((reg) + _inc) & ((1UL << (ad_bytes << 3)) - 1)); \
439 } while (0)
440
Nitin A Kamble098c9372007-08-19 11:00:36 +0300441#define JMP_REL(rel) \
442 do { \
443 _eip += (int)(rel); \
444 _eip = ((op_bytes == 2) ? (uint16_t)_eip : (uint32_t)_eip); \
445 } while (0)
446
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000447/*
448 * Given the 'reg' portion of a ModRM byte, and a register block, return a
449 * pointer into the block that addresses the relevant register.
450 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
451 */
452static void *decode_register(u8 modrm_reg, unsigned long *regs,
453 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454{
455 void *p;
456
457 p = &regs[modrm_reg];
458 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
459 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
460 return p;
461}
462
463static int read_descriptor(struct x86_emulate_ctxt *ctxt,
464 struct x86_emulate_ops *ops,
465 void *ptr,
466 u16 *size, unsigned long *address, int op_bytes)
467{
468 int rc;
469
470 if (op_bytes == 2)
471 op_bytes = 3;
472 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300473 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
474 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 if (rc)
476 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300477 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
478 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 return rc;
480}
481
482int
483x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
484{
Avi Kivity038e51d2007-01-22 20:40:40 -0800485 unsigned d;
486 u8 b, sib, twobyte = 0, rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 u8 modrm, modrm_mod = 0, modrm_reg = 0, modrm_rm = 0;
488 unsigned long *override_base = NULL;
489 unsigned int op_bytes, ad_bytes, lock_prefix = 0, rep_prefix = 0, i;
490 int rc = 0;
491 struct operand src, dst;
492 unsigned long cr2 = ctxt->cr2;
493 int mode = ctxt->mode;
494 unsigned long modrm_ea;
495 int use_modrm_ea, index_reg = 0, base_reg = 0, scale, rip_relative = 0;
Luca Tettamanti02c03a32007-06-19 22:41:20 +0200496 int no_wb = 0;
Avi Kivity35f3f282007-07-17 14:20:30 +0300497 u64 msr_data;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498
499 /* Shadow copy of register state. Committed on successful emulation. */
500 unsigned long _regs[NR_VCPU_REGS];
501 unsigned long _eip = ctxt->vcpu->rip, _eflags = ctxt->eflags;
502 unsigned long modrm_val = 0;
503
504 memcpy(_regs, ctxt->vcpu->regs, sizeof _regs);
505
506 switch (mode) {
507 case X86EMUL_MODE_REAL:
508 case X86EMUL_MODE_PROT16:
509 op_bytes = ad_bytes = 2;
510 break;
511 case X86EMUL_MODE_PROT32:
512 op_bytes = ad_bytes = 4;
513 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800514#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 case X86EMUL_MODE_PROT64:
516 op_bytes = 4;
517 ad_bytes = 8;
518 break;
519#endif
520 default:
521 return -1;
522 }
523
524 /* Legacy prefixes. */
525 for (i = 0; i < 8; i++) {
526 switch (b = insn_fetch(u8, 1, _eip)) {
527 case 0x66: /* operand-size override */
528 op_bytes ^= 6; /* switch between 2/4 bytes */
529 break;
530 case 0x67: /* address-size override */
531 if (mode == X86EMUL_MODE_PROT64)
532 ad_bytes ^= 12; /* switch between 4/8 bytes */
533 else
534 ad_bytes ^= 6; /* switch between 2/4 bytes */
535 break;
536 case 0x2e: /* CS override */
537 override_base = &ctxt->cs_base;
538 break;
539 case 0x3e: /* DS override */
540 override_base = &ctxt->ds_base;
541 break;
542 case 0x26: /* ES override */
543 override_base = &ctxt->es_base;
544 break;
545 case 0x64: /* FS override */
546 override_base = &ctxt->fs_base;
547 break;
548 case 0x65: /* GS override */
549 override_base = &ctxt->gs_base;
550 break;
551 case 0x36: /* SS override */
552 override_base = &ctxt->ss_base;
553 break;
554 case 0xf0: /* LOCK */
555 lock_prefix = 1;
556 break;
557 case 0xf3: /* REP/REPE/REPZ */
558 rep_prefix = 1;
559 break;
560 case 0xf2: /* REPNE/REPNZ */
561 break;
562 default:
563 goto done_prefixes;
564 }
565 }
566
567done_prefixes:
568
569 /* REX prefix. */
570 if ((mode == X86EMUL_MODE_PROT64) && ((b & 0xf0) == 0x40)) {
571 rex_prefix = b;
572 if (b & 8)
573 op_bytes = 8; /* REX.W */
574 modrm_reg = (b & 4) << 1; /* REX.R */
575 index_reg = (b & 2) << 2; /* REX.X */
576 modrm_rm = base_reg = (b & 1) << 3; /* REG.B */
577 b = insn_fetch(u8, 1, _eip);
578 }
579
580 /* Opcode byte(s). */
581 d = opcode_table[b];
582 if (d == 0) {
583 /* Two-byte opcode? */
584 if (b == 0x0f) {
585 twobyte = 1;
586 b = insn_fetch(u8, 1, _eip);
587 d = twobyte_table[b];
588 }
589
590 /* Unrecognised? */
591 if (d == 0)
592 goto cannot_emulate;
593 }
594
595 /* ModRM and SIB bytes. */
596 if (d & ModRM) {
597 modrm = insn_fetch(u8, 1, _eip);
598 modrm_mod |= (modrm & 0xc0) >> 6;
599 modrm_reg |= (modrm & 0x38) >> 3;
600 modrm_rm |= (modrm & 0x07);
601 modrm_ea = 0;
602 use_modrm_ea = 1;
603
604 if (modrm_mod == 3) {
605 modrm_val = *(unsigned long *)
606 decode_register(modrm_rm, _regs, d & ByteOp);
607 goto modrm_done;
608 }
609
610 if (ad_bytes == 2) {
611 unsigned bx = _regs[VCPU_REGS_RBX];
612 unsigned bp = _regs[VCPU_REGS_RBP];
613 unsigned si = _regs[VCPU_REGS_RSI];
614 unsigned di = _regs[VCPU_REGS_RDI];
615
616 /* 16-bit ModR/M decode. */
617 switch (modrm_mod) {
618 case 0:
619 if (modrm_rm == 6)
620 modrm_ea += insn_fetch(u16, 2, _eip);
621 break;
622 case 1:
623 modrm_ea += insn_fetch(s8, 1, _eip);
624 break;
625 case 2:
626 modrm_ea += insn_fetch(u16, 2, _eip);
627 break;
628 }
629 switch (modrm_rm) {
630 case 0:
631 modrm_ea += bx + si;
632 break;
633 case 1:
634 modrm_ea += bx + di;
635 break;
636 case 2:
637 modrm_ea += bp + si;
638 break;
639 case 3:
640 modrm_ea += bp + di;
641 break;
642 case 4:
643 modrm_ea += si;
644 break;
645 case 5:
646 modrm_ea += di;
647 break;
648 case 6:
649 if (modrm_mod != 0)
650 modrm_ea += bp;
651 break;
652 case 7:
653 modrm_ea += bx;
654 break;
655 }
656 if (modrm_rm == 2 || modrm_rm == 3 ||
657 (modrm_rm == 6 && modrm_mod != 0))
658 if (!override_base)
659 override_base = &ctxt->ss_base;
660 modrm_ea = (u16)modrm_ea;
661 } else {
662 /* 32/64-bit ModR/M decode. */
663 switch (modrm_rm) {
664 case 4:
665 case 12:
666 sib = insn_fetch(u8, 1, _eip);
667 index_reg |= (sib >> 3) & 7;
668 base_reg |= sib & 7;
669 scale = sib >> 6;
670
671 switch (base_reg) {
672 case 5:
673 if (modrm_mod != 0)
674 modrm_ea += _regs[base_reg];
675 else
676 modrm_ea += insn_fetch(s32, 4, _eip);
677 break;
678 default:
679 modrm_ea += _regs[base_reg];
680 }
681 switch (index_reg) {
682 case 4:
683 break;
684 default:
685 modrm_ea += _regs[index_reg] << scale;
686
687 }
688 break;
689 case 5:
690 if (modrm_mod != 0)
691 modrm_ea += _regs[modrm_rm];
692 else if (mode == X86EMUL_MODE_PROT64)
693 rip_relative = 1;
694 break;
695 default:
696 modrm_ea += _regs[modrm_rm];
697 break;
698 }
699 switch (modrm_mod) {
700 case 0:
701 if (modrm_rm == 5)
702 modrm_ea += insn_fetch(s32, 4, _eip);
703 break;
704 case 1:
705 modrm_ea += insn_fetch(s8, 1, _eip);
706 break;
707 case 2:
708 modrm_ea += insn_fetch(s32, 4, _eip);
709 break;
710 }
711 }
712 if (!override_base)
713 override_base = &ctxt->ds_base;
714 if (mode == X86EMUL_MODE_PROT64 &&
715 override_base != &ctxt->fs_base &&
716 override_base != &ctxt->gs_base)
717 override_base = NULL;
718
719 if (override_base)
720 modrm_ea += *override_base;
721
722 if (rip_relative) {
723 modrm_ea += _eip;
724 switch (d & SrcMask) {
725 case SrcImmByte:
726 modrm_ea += 1;
727 break;
728 case SrcImm:
729 if (d & ByteOp)
730 modrm_ea += 1;
731 else
732 if (op_bytes == 8)
733 modrm_ea += 4;
734 else
735 modrm_ea += op_bytes;
736 }
737 }
738 if (ad_bytes != 8)
739 modrm_ea = (u32)modrm_ea;
740 cr2 = modrm_ea;
741 modrm_done:
742 ;
743 }
744
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 /*
746 * Decode and fetch the source operand: register, memory
747 * or immediate.
748 */
749 switch (d & SrcMask) {
750 case SrcNone:
751 break;
752 case SrcReg:
753 src.type = OP_REG;
754 if (d & ByteOp) {
755 src.ptr = decode_register(modrm_reg, _regs,
756 (rex_prefix == 0));
757 src.val = src.orig_val = *(u8 *) src.ptr;
758 src.bytes = 1;
759 } else {
760 src.ptr = decode_register(modrm_reg, _regs, 0);
761 switch ((src.bytes = op_bytes)) {
762 case 2:
763 src.val = src.orig_val = *(u16 *) src.ptr;
764 break;
765 case 4:
766 src.val = src.orig_val = *(u32 *) src.ptr;
767 break;
768 case 8:
769 src.val = src.orig_val = *(u64 *) src.ptr;
770 break;
771 }
772 }
773 break;
774 case SrcMem16:
775 src.bytes = 2;
776 goto srcmem_common;
777 case SrcMem32:
778 src.bytes = 4;
779 goto srcmem_common;
780 case SrcMem:
781 src.bytes = (d & ByteOp) ? 1 : op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300782 /* Don't fetch the address for invlpg: it could be unmapped. */
783 if (twobyte && b == 0x01 && modrm_reg == 7)
784 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 srcmem_common:
786 src.type = OP_MEM;
787 src.ptr = (unsigned long *)cr2;
788 if ((rc = ops->read_emulated((unsigned long)src.ptr,
Laurent Viviercebff022007-07-30 13:35:24 +0300789 &src.val, src.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 goto done;
791 src.orig_val = src.val;
792 break;
793 case SrcImm:
794 src.type = OP_IMM;
795 src.ptr = (unsigned long *)_eip;
796 src.bytes = (d & ByteOp) ? 1 : op_bytes;
797 if (src.bytes == 8)
798 src.bytes = 4;
799 /* NB. Immediates are sign-extended as necessary. */
800 switch (src.bytes) {
801 case 1:
802 src.val = insn_fetch(s8, 1, _eip);
803 break;
804 case 2:
805 src.val = insn_fetch(s16, 2, _eip);
806 break;
807 case 4:
808 src.val = insn_fetch(s32, 4, _eip);
809 break;
810 }
811 break;
812 case SrcImmByte:
813 src.type = OP_IMM;
814 src.ptr = (unsigned long *)_eip;
815 src.bytes = 1;
816 src.val = insn_fetch(s8, 1, _eip);
817 break;
818 }
819
Avi Kivity038e51d2007-01-22 20:40:40 -0800820 /* Decode and fetch the destination operand: register or memory. */
821 switch (d & DstMask) {
822 case ImplicitOps:
823 /* Special instructions do their own operand decoding. */
824 goto special_insn;
825 case DstReg:
826 dst.type = OP_REG;
827 if ((d & ByteOp)
Avi Kivity394b6e52007-07-22 15:51:58 +0300828 && !(twobyte && (b == 0xb6 || b == 0xb7))) {
Avi Kivity038e51d2007-01-22 20:40:40 -0800829 dst.ptr = decode_register(modrm_reg, _regs,
830 (rex_prefix == 0));
831 dst.val = *(u8 *) dst.ptr;
832 dst.bytes = 1;
833 } else {
834 dst.ptr = decode_register(modrm_reg, _regs, 0);
835 switch ((dst.bytes = op_bytes)) {
836 case 2:
837 dst.val = *(u16 *)dst.ptr;
838 break;
839 case 4:
840 dst.val = *(u32 *)dst.ptr;
841 break;
842 case 8:
843 dst.val = *(u64 *)dst.ptr;
844 break;
845 }
846 }
847 break;
848 case DstMem:
849 dst.type = OP_MEM;
850 dst.ptr = (unsigned long *)cr2;
851 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
852 if (d & BitOp) {
Avi Kivitydf513e22007-03-28 20:04:16 +0200853 unsigned long mask = ~(dst.bytes * 8 - 1);
854
855 dst.ptr = (void *)dst.ptr + (src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -0800856 }
857 if (!(d & Mov) && /* optimisation - avoid slow emulated read */
858 ((rc = ops->read_emulated((unsigned long)dst.ptr,
Laurent Viviercebff022007-07-30 13:35:24 +0300859 &dst.val, dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -0800860 goto done;
861 break;
862 }
863 dst.orig_val = dst.val;
864
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 if (twobyte)
866 goto twobyte_insn;
867
868 switch (b) {
869 case 0x00 ... 0x05:
870 add: /* add */
871 emulate_2op_SrcV("add", src, dst, _eflags);
872 break;
873 case 0x08 ... 0x0d:
874 or: /* or */
875 emulate_2op_SrcV("or", src, dst, _eflags);
876 break;
877 case 0x10 ... 0x15:
878 adc: /* adc */
879 emulate_2op_SrcV("adc", src, dst, _eflags);
880 break;
881 case 0x18 ... 0x1d:
882 sbb: /* sbb */
883 emulate_2op_SrcV("sbb", src, dst, _eflags);
884 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300885 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 and: /* and */
887 emulate_2op_SrcV("and", src, dst, _eflags);
888 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +0300889 case 0x24: /* and al imm8 */
890 dst.type = OP_REG;
891 dst.ptr = &_regs[VCPU_REGS_RAX];
892 dst.val = *(u8 *)dst.ptr;
893 dst.bytes = 1;
894 dst.orig_val = dst.val;
895 goto and;
896 case 0x25: /* and ax imm16, or eax imm32 */
897 dst.type = OP_REG;
898 dst.bytes = op_bytes;
899 dst.ptr = &_regs[VCPU_REGS_RAX];
900 if (op_bytes == 2)
901 dst.val = *(u16 *)dst.ptr;
902 else
903 dst.val = *(u32 *)dst.ptr;
904 dst.orig_val = dst.val;
905 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 case 0x28 ... 0x2d:
907 sub: /* sub */
908 emulate_2op_SrcV("sub", src, dst, _eflags);
909 break;
910 case 0x30 ... 0x35:
911 xor: /* xor */
912 emulate_2op_SrcV("xor", src, dst, _eflags);
913 break;
914 case 0x38 ... 0x3d:
915 cmp: /* cmp */
916 emulate_2op_SrcV("cmp", src, dst, _eflags);
917 break;
918 case 0x63: /* movsxd */
919 if (mode != X86EMUL_MODE_PROT64)
920 goto cannot_emulate;
921 dst.val = (s32) src.val;
922 break;
Nitin A Kamble7d316912007-08-28 17:58:52 -0700923 case 0x6a: /* push imm8 */
924 src.val = 0L;
925 src.val = insn_fetch(s8, 1, _eip);
926push:
927 dst.type = OP_MEM;
928 dst.bytes = op_bytes;
929 dst.val = src.val;
930 register_address_increment(_regs[VCPU_REGS_RSP], -op_bytes);
Nitin A Kamblefd2a7602007-08-28 18:22:47 -0700931 dst.ptr = (void *) register_address(ctxt->ss_base,
932 _regs[VCPU_REGS_RSP]);
Nitin A Kamble7d316912007-08-28 17:58:52 -0700933 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 case 0x80 ... 0x83: /* Grp1 */
935 switch (modrm_reg) {
936 case 0:
937 goto add;
938 case 1:
939 goto or;
940 case 2:
941 goto adc;
942 case 3:
943 goto sbb;
944 case 4:
945 goto and;
946 case 5:
947 goto sub;
948 case 6:
949 goto xor;
950 case 7:
951 goto cmp;
952 }
953 break;
954 case 0x84 ... 0x85:
955 test: /* test */
956 emulate_2op_SrcV("test", src, dst, _eflags);
957 break;
958 case 0x86 ... 0x87: /* xchg */
959 /* Write back the register source. */
960 switch (dst.bytes) {
961 case 1:
962 *(u8 *) src.ptr = (u8) dst.val;
963 break;
964 case 2:
965 *(u16 *) src.ptr = (u16) dst.val;
966 break;
967 case 4:
968 *src.ptr = (u32) dst.val;
969 break; /* 64b reg: zero-extend */
970 case 8:
971 *src.ptr = dst.val;
972 break;
973 }
974 /*
975 * Write back the memory destination with implicit LOCK
976 * prefix.
977 */
978 dst.val = src.val;
979 lock_prefix = 1;
980 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +0300982 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 case 0x8f: /* pop (sole member of Grp1a) */
984 /* 64-bit mode: POP always pops a 64-bit operand. */
985 if (mode == X86EMUL_MODE_PROT64)
986 dst.bytes = 8;
987 if ((rc = ops->read_std(register_address(ctxt->ss_base,
988 _regs[VCPU_REGS_RSP]),
Laurent Viviercebff022007-07-30 13:35:24 +0300989 &dst.val, dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 goto done;
991 register_address_increment(_regs[VCPU_REGS_RSP], dst.bytes);
992 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +0300993 case 0xa0 ... 0xa1: /* mov */
994 dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
995 dst.val = src.val;
996 _eip += ad_bytes; /* skip src displacement */
997 break;
998 case 0xa2 ... 0xa3: /* mov */
999 dst.val = (unsigned long)_regs[VCPU_REGS_RAX];
1000 _eip += ad_bytes; /* skip dst displacement */
1001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 case 0xc0 ... 0xc1:
1003 grp2: /* Grp2 */
1004 switch (modrm_reg) {
1005 case 0: /* rol */
1006 emulate_2op_SrcB("rol", src, dst, _eflags);
1007 break;
1008 case 1: /* ror */
1009 emulate_2op_SrcB("ror", src, dst, _eflags);
1010 break;
1011 case 2: /* rcl */
1012 emulate_2op_SrcB("rcl", src, dst, _eflags);
1013 break;
1014 case 3: /* rcr */
1015 emulate_2op_SrcB("rcr", src, dst, _eflags);
1016 break;
1017 case 4: /* sal/shl */
1018 case 6: /* sal/shl */
1019 emulate_2op_SrcB("sal", src, dst, _eflags);
1020 break;
1021 case 5: /* shr */
1022 emulate_2op_SrcB("shr", src, dst, _eflags);
1023 break;
1024 case 7: /* sar */
1025 emulate_2op_SrcB("sar", src, dst, _eflags);
1026 break;
1027 }
1028 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001029 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1030 mov:
1031 dst.val = src.val;
1032 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 case 0xd0 ... 0xd1: /* Grp2 */
1034 src.val = 1;
1035 goto grp2;
1036 case 0xd2 ... 0xd3: /* Grp2 */
1037 src.val = _regs[VCPU_REGS_RCX];
1038 goto grp2;
Nitin A Kamblef6eed392007-08-28 18:08:37 -07001039 case 0xe8: /* call (near) */ {
1040 long int rel;
1041 switch (op_bytes) {
1042 case 2:
1043 rel = insn_fetch(s16, 2, _eip);
1044 break;
1045 case 4:
1046 rel = insn_fetch(s32, 4, _eip);
1047 break;
1048 case 8:
1049 rel = insn_fetch(s64, 8, _eip);
1050 break;
1051 default:
1052 DPRINTF("Call: Invalid op_bytes\n");
1053 goto cannot_emulate;
1054 }
1055 src.val = (unsigned long) _eip;
1056 JMP_REL(rel);
1057 goto push;
1058 }
Nitin A Kamble098c9372007-08-19 11:00:36 +03001059 case 0xe9: /* jmp rel */
Nitin A Kamblec53ce172007-08-19 11:03:13 +03001060 case 0xeb: /* jmp rel short */
Nitin A Kamble098c9372007-08-19 11:00:36 +03001061 JMP_REL(src.val);
1062 no_wb = 1; /* Disable writeback. */
1063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 case 0xf6 ... 0xf7: /* Grp3 */
1065 switch (modrm_reg) {
1066 case 0 ... 1: /* test */
1067 /*
1068 * Special case in Grp3: test has an immediate
1069 * source operand.
1070 */
1071 src.type = OP_IMM;
1072 src.ptr = (unsigned long *)_eip;
1073 src.bytes = (d & ByteOp) ? 1 : op_bytes;
1074 if (src.bytes == 8)
1075 src.bytes = 4;
1076 switch (src.bytes) {
1077 case 1:
1078 src.val = insn_fetch(s8, 1, _eip);
1079 break;
1080 case 2:
1081 src.val = insn_fetch(s16, 2, _eip);
1082 break;
1083 case 4:
1084 src.val = insn_fetch(s32, 4, _eip);
1085 break;
1086 }
1087 goto test;
1088 case 2: /* not */
1089 dst.val = ~dst.val;
1090 break;
1091 case 3: /* neg */
1092 emulate_1op("neg", dst, _eflags);
1093 break;
1094 default:
1095 goto cannot_emulate;
1096 }
1097 break;
1098 case 0xfe ... 0xff: /* Grp4/Grp5 */
1099 switch (modrm_reg) {
1100 case 0: /* inc */
1101 emulate_1op("inc", dst, _eflags);
1102 break;
1103 case 1: /* dec */
1104 emulate_1op("dec", dst, _eflags);
1105 break;
1106 case 6: /* push */
1107 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1108 if (mode == X86EMUL_MODE_PROT64) {
1109 dst.bytes = 8;
1110 if ((rc = ops->read_std((unsigned long)dst.ptr,
1111 &dst.val, 8,
Laurent Viviercebff022007-07-30 13:35:24 +03001112 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 goto done;
1114 }
1115 register_address_increment(_regs[VCPU_REGS_RSP],
1116 -dst.bytes);
1117 if ((rc = ops->write_std(
1118 register_address(ctxt->ss_base,
1119 _regs[VCPU_REGS_RSP]),
Laurent Viviercebff022007-07-30 13:35:24 +03001120 &dst.val, dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 goto done;
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001122 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 break;
1124 default:
1125 goto cannot_emulate;
1126 }
1127 break;
1128 }
1129
1130writeback:
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001131 if (!no_wb) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 switch (dst.type) {
1133 case OP_REG:
1134 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1135 switch (dst.bytes) {
1136 case 1:
1137 *(u8 *)dst.ptr = (u8)dst.val;
1138 break;
1139 case 2:
1140 *(u16 *)dst.ptr = (u16)dst.val;
1141 break;
1142 case 4:
1143 *dst.ptr = (u32)dst.val;
1144 break; /* 64b: zero-ext */
1145 case 8:
1146 *dst.ptr = dst.val;
1147 break;
1148 }
1149 break;
1150 case OP_MEM:
1151 if (lock_prefix)
1152 rc = ops->cmpxchg_emulated((unsigned long)dst.
Avi Kivity4c690a12007-04-22 15:28:19 +03001153 ptr, &dst.orig_val,
1154 &dst.val, dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001155 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 else
1157 rc = ops->write_emulated((unsigned long)dst.ptr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001158 &dst.val, dst.bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001159 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 if (rc != 0)
1161 goto done;
1162 default:
1163 break;
1164 }
1165 }
1166
1167 /* Commit shadow register state. */
1168 memcpy(ctxt->vcpu->regs, _regs, sizeof _regs);
1169 ctxt->eflags = _eflags;
1170 ctxt->vcpu->rip = _eip;
1171
1172done:
1173 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1174
1175special_insn:
1176 if (twobyte)
1177 goto twobyte_special_insn;
Laurent Viviere70669a2007-08-05 10:36:40 +03001178 switch(b) {
Nitin A Kamble7e778162007-08-19 11:07:06 +03001179 case 0x50 ... 0x57: /* push reg */
1180 if (op_bytes == 2)
1181 src.val = (u16) _regs[b & 0x7];
1182 else
1183 src.val = (u32) _regs[b & 0x7];
1184 dst.type = OP_MEM;
1185 dst.bytes = op_bytes;
1186 dst.val = src.val;
1187 register_address_increment(_regs[VCPU_REGS_RSP], -op_bytes);
1188 dst.ptr = (void *) register_address(
1189 ctxt->ss_base, _regs[VCPU_REGS_RSP]);
Nitin A Kamble7e778162007-08-19 11:07:06 +03001190 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001191 case 0x58 ... 0x5f: /* pop reg */
1192 dst.ptr = (unsigned long *)&_regs[b & 0x7];
1193 pop_instruction:
1194 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1195 _regs[VCPU_REGS_RSP]), dst.ptr, op_bytes, ctxt->vcpu))
1196 != 0)
1197 goto done;
1198
1199 register_address_increment(_regs[VCPU_REGS_RSP], op_bytes);
1200 no_wb = 1; /* Disable writeback. */
1201 break;
Laurent Viviere70669a2007-08-05 10:36:40 +03001202 case 0x6c: /* insb */
1203 case 0x6d: /* insw/insd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001204 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere70669a2007-08-05 10:36:40 +03001205 1, /* in */
1206 (d & ByteOp) ? 1 : op_bytes, /* size */
1207 rep_prefix ?
1208 address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
Laurent Viviere70669a2007-08-05 10:36:40 +03001209 (_eflags & EFLG_DF), /* down */
1210 register_address(ctxt->es_base,
1211 _regs[VCPU_REGS_RDI]), /* address */
1212 rep_prefix,
1213 _regs[VCPU_REGS_RDX] /* port */
1214 ) == 0)
1215 return -1;
1216 return 0;
1217 case 0x6e: /* outsb */
1218 case 0x6f: /* outsw/outsd */
Laurent Vivier3090dd72007-08-05 10:43:32 +03001219 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
Laurent Viviere70669a2007-08-05 10:36:40 +03001220 0, /* in */
1221 (d & ByteOp) ? 1 : op_bytes, /* size */
1222 rep_prefix ?
1223 address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
Laurent Viviere70669a2007-08-05 10:36:40 +03001224 (_eflags & EFLG_DF), /* down */
1225 register_address(override_base ?
1226 *override_base : ctxt->ds_base,
1227 _regs[VCPU_REGS_RSI]), /* address */
1228 rep_prefix,
1229 _regs[VCPU_REGS_RDX] /* port */
1230 ) == 0)
1231 return -1;
1232 return 0;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001233 case 0x9c: /* pushf */
1234 src.val = (unsigned long) _eflags;
1235 goto push;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001236 case 0xc3: /* ret */
1237 dst.ptr = &_eip;
1238 goto pop_instruction;
1239 case 0xf4: /* hlt */
1240 ctxt->vcpu->halt_request = 1;
1241 goto done;
Laurent Viviere70669a2007-08-05 10:36:40 +03001242 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 if (rep_prefix) {
1244 if (_regs[VCPU_REGS_RCX] == 0) {
1245 ctxt->vcpu->rip = _eip;
1246 goto done;
1247 }
1248 _regs[VCPU_REGS_RCX]--;
1249 _eip = ctxt->vcpu->rip;
1250 }
1251 switch (b) {
1252 case 0xa4 ... 0xa5: /* movs */
1253 dst.type = OP_MEM;
1254 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1255 dst.ptr = (unsigned long *)register_address(ctxt->es_base,
1256 _regs[VCPU_REGS_RDI]);
1257 if ((rc = ops->read_emulated(register_address(
1258 override_base ? *override_base : ctxt->ds_base,
Laurent Viviercebff022007-07-30 13:35:24 +03001259 _regs[VCPU_REGS_RSI]), &dst.val, dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 goto done;
1261 register_address_increment(_regs[VCPU_REGS_RSI],
1262 (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1263 register_address_increment(_regs[VCPU_REGS_RDI],
1264 (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1265 break;
1266 case 0xa6 ... 0xa7: /* cmps */
1267 DPRINTF("Urk! I don't handle CMPS.\n");
1268 goto cannot_emulate;
1269 case 0xaa ... 0xab: /* stos */
1270 dst.type = OP_MEM;
1271 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1272 dst.ptr = (unsigned long *)cr2;
1273 dst.val = _regs[VCPU_REGS_RAX];
1274 register_address_increment(_regs[VCPU_REGS_RDI],
1275 (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1276 break;
1277 case 0xac ... 0xad: /* lods */
1278 dst.type = OP_REG;
1279 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1280 dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
Laurent Viviercebff022007-07-30 13:35:24 +03001281 if ((rc = ops->read_emulated(cr2, &dst.val, dst.bytes,
1282 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 goto done;
1284 register_address_increment(_regs[VCPU_REGS_RSI],
1285 (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1286 break;
1287 case 0xae ... 0xaf: /* scas */
1288 DPRINTF("Urk! I don't handle SCAS.\n");
1289 goto cannot_emulate;
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +03001290
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 }
1292 goto writeback;
1293
1294twobyte_insn:
1295 switch (b) {
1296 case 0x01: /* lgdt, lidt, lmsw */
Aurelien Jarnod37c8552007-07-25 10:19:54 +02001297 /* Disable writeback. */
1298 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 switch (modrm_reg) {
1300 u16 size;
1301 unsigned long address;
1302
1303 case 2: /* lgdt */
1304 rc = read_descriptor(ctxt, ops, src.ptr,
1305 &size, &address, op_bytes);
1306 if (rc)
1307 goto done;
1308 realmode_lgdt(ctxt->vcpu, size, address);
1309 break;
1310 case 3: /* lidt */
1311 rc = read_descriptor(ctxt, ops, src.ptr,
1312 &size, &address, op_bytes);
1313 if (rc)
1314 goto done;
1315 realmode_lidt(ctxt->vcpu, size, address);
1316 break;
1317 case 4: /* smsw */
1318 if (modrm_mod != 3)
1319 goto cannot_emulate;
1320 *(u16 *)&_regs[modrm_rm]
1321 = realmode_get_cr(ctxt->vcpu, 0);
1322 break;
1323 case 6: /* lmsw */
1324 if (modrm_mod != 3)
1325 goto cannot_emulate;
1326 realmode_lmsw(ctxt->vcpu, (u16)modrm_val, &_eflags);
1327 break;
1328 case 7: /* invlpg*/
1329 emulate_invlpg(ctxt->vcpu, cr2);
1330 break;
1331 default:
1332 goto cannot_emulate;
1333 }
1334 break;
1335 case 0x21: /* mov from dr to reg */
Avi Kivitybac27d32007-08-05 10:16:11 +03001336 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 if (modrm_mod != 3)
1338 goto cannot_emulate;
1339 rc = emulator_get_dr(ctxt, modrm_reg, &_regs[modrm_rm]);
1340 break;
1341 case 0x23: /* mov from reg to dr */
Avi Kivitybac27d32007-08-05 10:16:11 +03001342 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 if (modrm_mod != 3)
1344 goto cannot_emulate;
1345 rc = emulator_set_dr(ctxt, modrm_reg, _regs[modrm_rm]);
1346 break;
1347 case 0x40 ... 0x4f: /* cmov */
1348 dst.val = dst.orig_val = src.val;
Avi Kivitye3243452007-07-20 12:30:58 +03001349 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 /*
1351 * First, assume we're decoding an even cmov opcode
1352 * (lsb == 0).
1353 */
1354 switch ((b & 15) >> 1) {
1355 case 0: /* cmovo */
Avi Kivitye3243452007-07-20 12:30:58 +03001356 no_wb = (_eflags & EFLG_OF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 break;
1358 case 1: /* cmovb/cmovc/cmovnae */
Avi Kivitye3243452007-07-20 12:30:58 +03001359 no_wb = (_eflags & EFLG_CF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 break;
1361 case 2: /* cmovz/cmove */
Avi Kivitye3243452007-07-20 12:30:58 +03001362 no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 break;
1364 case 3: /* cmovbe/cmovna */
Avi Kivitye3243452007-07-20 12:30:58 +03001365 no_wb = (_eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 break;
1367 case 4: /* cmovs */
Avi Kivitye3243452007-07-20 12:30:58 +03001368 no_wb = (_eflags & EFLG_SF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 break;
1370 case 5: /* cmovp/cmovpe */
Avi Kivitye3243452007-07-20 12:30:58 +03001371 no_wb = (_eflags & EFLG_PF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 break;
1373 case 7: /* cmovle/cmovng */
Avi Kivitye3243452007-07-20 12:30:58 +03001374 no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 /* fall through */
1376 case 6: /* cmovl/cmovnge */
Avi Kivitye3243452007-07-20 12:30:58 +03001377 no_wb &= (!(_eflags & EFLG_SF) !=
1378 !(_eflags & EFLG_OF)) ? 0 : 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 break;
1380 }
1381 /* Odd cmov opcodes (lsb == 1) have inverted sense. */
Avi Kivitye3243452007-07-20 12:30:58 +03001382 no_wb ^= b & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001384 case 0xa3:
1385 bt: /* bt */
1386 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1387 emulate_2op_SrcV_nobyte("bt", src, dst, _eflags);
1388 break;
1389 case 0xab:
1390 bts: /* bts */
1391 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1392 emulate_2op_SrcV_nobyte("bts", src, dst, _eflags);
1393 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 case 0xb0 ... 0xb1: /* cmpxchg */
1395 /*
1396 * Save real source value, then compare EAX against
1397 * destination.
1398 */
1399 src.orig_val = src.val;
1400 src.val = _regs[VCPU_REGS_RAX];
1401 emulate_2op_SrcV("cmp", src, dst, _eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 if (_eflags & EFLG_ZF) {
1403 /* Success: write back to memory. */
1404 dst.val = src.orig_val;
1405 } else {
1406 /* Failure: write the value we saw to EAX. */
1407 dst.type = OP_REG;
1408 dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
1409 }
1410 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 case 0xb3:
1412 btr: /* btr */
1413 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1414 emulate_2op_SrcV_nobyte("btr", src, dst, _eflags);
1415 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 case 0xb6 ... 0xb7: /* movzx */
1417 dst.bytes = op_bytes;
1418 dst.val = (d & ByteOp) ? (u8) src.val : (u16) src.val;
1419 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 case 0xba: /* Grp8 */
1421 switch (modrm_reg & 3) {
1422 case 0:
1423 goto bt;
1424 case 1:
1425 goto bts;
1426 case 2:
1427 goto btr;
1428 case 3:
1429 goto btc;
1430 }
1431 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03001432 case 0xbb:
1433 btc: /* btc */
1434 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1435 emulate_2op_SrcV_nobyte("btc", src, dst, _eflags);
1436 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 case 0xbe ... 0xbf: /* movsx */
1438 dst.bytes = op_bytes;
1439 dst.val = (d & ByteOp) ? (s8) src.val : (s16) src.val;
1440 break;
1441 }
1442 goto writeback;
1443
1444twobyte_special_insn:
1445 /* Disable writeback. */
Luca Tettamanti02c03a32007-06-19 22:41:20 +02001446 no_wb = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 switch (b) {
Nitin A Kamble7de75242007-09-15 10:13:07 +03001448 case 0x06:
1449 emulate_clts(ctxt->vcpu);
1450 break;
Avi Kivity687fdbf2007-05-24 11:17:33 +03001451 case 0x09: /* wbinvd */
1452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 case 0x0d: /* GrpP (prefetch) */
1454 case 0x18: /* Grp16 (prefetch/nop) */
1455 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 case 0x20: /* mov cr, reg */
1457 if (modrm_mod != 3)
1458 goto cannot_emulate;
1459 _regs[modrm_rm] = realmode_get_cr(ctxt->vcpu, modrm_reg);
1460 break;
1461 case 0x22: /* mov reg, cr */
1462 if (modrm_mod != 3)
1463 goto cannot_emulate;
1464 realmode_set_cr(ctxt->vcpu, modrm_reg, modrm_val, &_eflags);
1465 break;
Avi Kivity35f3f282007-07-17 14:20:30 +03001466 case 0x30:
1467 /* wrmsr */
1468 msr_data = (u32)_regs[VCPU_REGS_RAX]
1469 | ((u64)_regs[VCPU_REGS_RDX] << 32);
1470 rc = kvm_set_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], msr_data);
1471 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001472 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Avi Kivity35f3f282007-07-17 14:20:30 +03001473 _eip = ctxt->vcpu->rip;
1474 }
1475 rc = X86EMUL_CONTINUE;
1476 break;
1477 case 0x32:
1478 /* rdmsr */
1479 rc = kvm_get_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], &msr_data);
1480 if (rc) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001481 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
Avi Kivity35f3f282007-07-17 14:20:30 +03001482 _eip = ctxt->vcpu->rip;
1483 } else {
1484 _regs[VCPU_REGS_RAX] = (u32)msr_data;
1485 _regs[VCPU_REGS_RDX] = msr_data >> 32;
1486 }
1487 rc = X86EMUL_CONTINUE;
1488 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 case 0xc7: /* Grp9 (cmpxchg8b) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490 {
Avi Kivity4c690a12007-04-22 15:28:19 +03001491 u64 old, new;
Laurent Viviercebff022007-07-30 13:35:24 +03001492 if ((rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu))
1493 != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 goto done;
1495 if (((u32) (old >> 0) != (u32) _regs[VCPU_REGS_RAX]) ||
1496 ((u32) (old >> 32) != (u32) _regs[VCPU_REGS_RDX])) {
1497 _regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1498 _regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1499 _eflags &= ~EFLG_ZF;
1500 } else {
Avi Kivity4c690a12007-04-22 15:28:19 +03001501 new = ((u64)_regs[VCPU_REGS_RCX] << 32)
1502 | (u32) _regs[VCPU_REGS_RBX];
1503 if ((rc = ops->cmpxchg_emulated(cr2, &old,
Laurent Viviercebff022007-07-30 13:35:24 +03001504 &new, 8, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 goto done;
1506 _eflags |= EFLG_ZF;
1507 }
1508 break;
1509 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 }
1511 goto writeback;
1512
1513cannot_emulate:
1514 DPRINTF("Cannot emulate %02x\n", b);
1515 return -1;
1516}
1517
1518#ifdef __XEN__
1519
1520#include <asm/mm.h>
1521#include <asm/uaccess.h>
1522
1523int
1524x86_emulate_read_std(unsigned long addr,
1525 unsigned long *val,
1526 unsigned int bytes, struct x86_emulate_ctxt *ctxt)
1527{
1528 unsigned int rc;
1529
1530 *val = 0;
1531
1532 if ((rc = copy_from_user((void *)val, (void *)addr, bytes)) != 0) {
1533 propagate_page_fault(addr + bytes - rc, 0); /* read fault */
1534 return X86EMUL_PROPAGATE_FAULT;
1535 }
1536
1537 return X86EMUL_CONTINUE;
1538}
1539
1540int
1541x86_emulate_write_std(unsigned long addr,
1542 unsigned long val,
1543 unsigned int bytes, struct x86_emulate_ctxt *ctxt)
1544{
1545 unsigned int rc;
1546
1547 if ((rc = copy_to_user((void *)addr, (void *)&val, bytes)) != 0) {
1548 propagate_page_fault(addr + bytes - rc, PGERR_write_access);
1549 return X86EMUL_PROPAGATE_FAULT;
1550 }
1551
1552 return X86EMUL_CONTINUE;
1553}
1554
1555#endif