KVM: X86 emulator: fix 'push reg' writeback
[pandora-kernel.git] / drivers / kvm / x86_emulate.c
1 /******************************************************************************
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
9  * privileged instructions:
10  *
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)
64 #define BitOp       (1<<8)
65
66 static 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,
86         SrcImmByte, SrcImm, 0, 0,
87         /* 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,
101         /* 0x50 - 0x57 */
102         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
103         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
104         /* 0x58 - 0x5F */
105         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
106         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
107         /* 0x60 - 0x6B */
108         0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
109         0, 0, 0, 0, 0, 0, 0, 0,
110         /* 0x6C - 0x6F */
111         SrcNone  | ByteOp  | ImplicitOps, SrcNone  | ImplicitOps, /* insb, insw/insd */
112         SrcNone  | ByteOp  | ImplicitOps, SrcNone  | ImplicitOps, /* outsb, outsw/outsd */
113         /* 0x70 - 0x7F */
114         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115         /* 0x80 - 0x87 */
116         ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
117         ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
118         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
119         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
120         /* 0x88 - 0x8F */
121         ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
122         ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
123         0, 0, 0, DstMem | SrcNone | ModRM | Mov,
124         /* 0x90 - 0x9F */
125         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126         /* 0xA0 - 0xA7 */
127         ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
128         ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
129         ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
130         ByteOp | ImplicitOps, ImplicitOps,
131         /* 0xA8 - 0xAF */
132         0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
133         ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
134         ByteOp | ImplicitOps, ImplicitOps,
135         /* 0xB0 - 0xBF */
136         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137         /* 0xC0 - 0xC7 */
138         ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
139         0, ImplicitOps, 0, 0,
140         ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
141         /* 0xC8 - 0xCF */
142         0, 0, 0, 0, 0, 0, 0, 0,
143         /* 0xD0 - 0xD7 */
144         ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
145         ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
146         0, 0, 0, 0,
147         /* 0xD8 - 0xDF */
148         0, 0, 0, 0, 0, 0, 0, 0,
149         /* 0xE0 - 0xE7 */
150         0, 0, 0, 0, 0, 0, 0, 0,
151         /* 0xE8 - 0xEF */
152         0, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
153         /* 0xF0 - 0xF7 */
154         0, 0, 0, 0,
155         ImplicitOps, 0,
156         ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
157         /* 0xF8 - 0xFF */
158         0, 0, 0, 0,
159         0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
160 };
161
162 static u16 twobyte_table[256] = {
163         /* 0x00 - 0x0F */
164         0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
165         0, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
166         /* 0x10 - 0x1F */
167         0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
168         /* 0x20 - 0x2F */
169         ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
170         0, 0, 0, 0, 0, 0, 0, 0,
171         /* 0x30 - 0x3F */
172         ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173         /* 0x40 - 0x47 */
174         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
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         /* 0x48 - 0x4F */
179         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
180         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
181         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183         /* 0x50 - 0x5F */
184         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185         /* 0x60 - 0x6F */
186         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187         /* 0x70 - 0x7F */
188         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189         /* 0x80 - 0x8F */
190         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191         /* 0x90 - 0x9F */
192         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193         /* 0xA0 - 0xA7 */
194         0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
195         /* 0xA8 - 0xAF */
196         0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
197         /* 0xB0 - 0xB7 */
198         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
199             DstMem | SrcReg | ModRM | BitOp,
200         0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
201             DstReg | SrcMem16 | ModRM | Mov,
202         /* 0xB8 - 0xBF */
203         0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
204         0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
205             DstReg | SrcMem16 | ModRM | Mov,
206         /* 0xC0 - 0xCF */
207         0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
208         /* 0xD0 - 0xDF */
209         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210         /* 0xE0 - 0xEF */
211         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212         /* 0xF0 - 0xFF */
213         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
214 };
215
216 /*
217  * Tell the emulator that of the Group 7 instructions (sgdt, lidt, etc.) we
218  * are interested only in invlpg and not in any of the rest.
219  *
220  * invlpg is a special instruction in that the data it references may not
221  * be mapped.
222  */
223 void kvm_emulator_want_group7_invlpg(void)
224 {
225         twobyte_table[1] &= ~SrcMem;
226 }
227 EXPORT_SYMBOL_GPL(kvm_emulator_want_group7_invlpg);
228
229 /* Type, address-of, and value of an instruction's operand. */
230 struct operand {
231         enum { OP_REG, OP_MEM, OP_IMM } type;
232         unsigned int bytes;
233         unsigned long val, orig_val, *ptr;
234 };
235
236 /* EFLAGS bit definitions. */
237 #define EFLG_OF (1<<11)
238 #define EFLG_DF (1<<10)
239 #define EFLG_SF (1<<7)
240 #define EFLG_ZF (1<<6)
241 #define EFLG_AF (1<<4)
242 #define EFLG_PF (1<<2)
243 #define EFLG_CF (1<<0)
244
245 /*
246  * Instruction emulation:
247  * Most instructions are emulated directly via a fragment of inline assembly
248  * code. This allows us to save/restore EFLAGS and thus very easily pick up
249  * any modified flags.
250  */
251
252 #if defined(CONFIG_X86_64)
253 #define _LO32 "k"               /* force 32-bit operand */
254 #define _STK  "%%rsp"           /* stack pointer */
255 #elif defined(__i386__)
256 #define _LO32 ""                /* force 32-bit operand */
257 #define _STK  "%%esp"           /* stack pointer */
258 #endif
259
260 /*
261  * These EFLAGS bits are restored from saved value during emulation, and
262  * any changes are written back to the saved value after emulation.
263  */
264 #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
265
266 /* Before executing instruction: restore necessary bits in EFLAGS. */
267 #define _PRE_EFLAGS(_sav, _msk, _tmp) \
268         /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */        \
269         "push %"_sav"; "                                        \
270         "movl %"_msk",%"_LO32 _tmp"; "                          \
271         "andl %"_LO32 _tmp",("_STK"); "                         \
272         "pushf; "                                               \
273         "notl %"_LO32 _tmp"; "                                  \
274         "andl %"_LO32 _tmp",("_STK"); "                         \
275         "pop  %"_tmp"; "                                        \
276         "orl  %"_LO32 _tmp",("_STK"); "                         \
277         "popf; "                                                \
278         /* _sav &= ~msk; */                                     \
279         "movl %"_msk",%"_LO32 _tmp"; "                          \
280         "notl %"_LO32 _tmp"; "                                  \
281         "andl %"_LO32 _tmp",%"_sav"; "
282
283 /* After executing instruction: write-back necessary bits in EFLAGS. */
284 #define _POST_EFLAGS(_sav, _msk, _tmp) \
285         /* _sav |= EFLAGS & _msk; */            \
286         "pushf; "                               \
287         "pop  %"_tmp"; "                        \
288         "andl %"_msk",%"_LO32 _tmp"; "          \
289         "orl  %"_LO32 _tmp",%"_sav"; "
290
291 /* Raw emulation: instruction has two explicit operands. */
292 #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
293         do {                                                                \
294                 unsigned long _tmp;                                         \
295                                                                             \
296                 switch ((_dst).bytes) {                                     \
297                 case 2:                                                     \
298                         __asm__ __volatile__ (                              \
299                                 _PRE_EFLAGS("0","4","2")                    \
300                                 _op"w %"_wx"3,%1; "                         \
301                                 _POST_EFLAGS("0","4","2")                   \
302                                 : "=m" (_eflags), "=m" ((_dst).val),        \
303                                   "=&r" (_tmp)                              \
304                                 : _wy ((_src).val), "i" (EFLAGS_MASK) );    \
305                         break;                                              \
306                 case 4:                                                     \
307                         __asm__ __volatile__ (                              \
308                                 _PRE_EFLAGS("0","4","2")                    \
309                                 _op"l %"_lx"3,%1; "                         \
310                                 _POST_EFLAGS("0","4","2")                   \
311                                 : "=m" (_eflags), "=m" ((_dst).val),        \
312                                   "=&r" (_tmp)                              \
313                                 : _ly ((_src).val), "i" (EFLAGS_MASK) );    \
314                         break;                                              \
315                 case 8:                                                     \
316                         __emulate_2op_8byte(_op, _src, _dst,                \
317                                             _eflags, _qx, _qy);             \
318                         break;                                              \
319                 }                                                           \
320         } while (0)
321
322 #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
323         do {                                                                 \
324                 unsigned long _tmp;                                          \
325                 switch ( (_dst).bytes )                                      \
326                 {                                                            \
327                 case 1:                                                      \
328                         __asm__ __volatile__ (                               \
329                                 _PRE_EFLAGS("0","4","2")                     \
330                                 _op"b %"_bx"3,%1; "                          \
331                                 _POST_EFLAGS("0","4","2")                    \
332                                 : "=m" (_eflags), "=m" ((_dst).val),         \
333                                   "=&r" (_tmp)                               \
334                                 : _by ((_src).val), "i" (EFLAGS_MASK) );     \
335                         break;                                               \
336                 default:                                                     \
337                         __emulate_2op_nobyte(_op, _src, _dst, _eflags,       \
338                                              _wx, _wy, _lx, _ly, _qx, _qy);  \
339                         break;                                               \
340                 }                                                            \
341         } while (0)
342
343 /* Source operand is byte-sized and may be restricted to just %cl. */
344 #define emulate_2op_SrcB(_op, _src, _dst, _eflags)                      \
345         __emulate_2op(_op, _src, _dst, _eflags,                         \
346                       "b", "c", "b", "c", "b", "c", "b", "c")
347
348 /* Source operand is byte, word, long or quad sized. */
349 #define emulate_2op_SrcV(_op, _src, _dst, _eflags)                      \
350         __emulate_2op(_op, _src, _dst, _eflags,                         \
351                       "b", "q", "w", "r", _LO32, "r", "", "r")
352
353 /* Source operand is word, long or quad sized. */
354 #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags)               \
355         __emulate_2op_nobyte(_op, _src, _dst, _eflags,                  \
356                              "w", "r", _LO32, "r", "", "r")
357
358 /* Instruction has only one explicit operand (no source operand). */
359 #define emulate_1op(_op, _dst, _eflags)                                    \
360         do {                                                            \
361                 unsigned long _tmp;                                     \
362                                                                         \
363                 switch ( (_dst).bytes )                                 \
364                 {                                                       \
365                 case 1:                                                 \
366                         __asm__ __volatile__ (                          \
367                                 _PRE_EFLAGS("0","3","2")                \
368                                 _op"b %1; "                             \
369                                 _POST_EFLAGS("0","3","2")               \
370                                 : "=m" (_eflags), "=m" ((_dst).val),    \
371                                   "=&r" (_tmp)                          \
372                                 : "i" (EFLAGS_MASK) );                  \
373                         break;                                          \
374                 case 2:                                                 \
375                         __asm__ __volatile__ (                          \
376                                 _PRE_EFLAGS("0","3","2")                \
377                                 _op"w %1; "                             \
378                                 _POST_EFLAGS("0","3","2")               \
379                                 : "=m" (_eflags), "=m" ((_dst).val),    \
380                                   "=&r" (_tmp)                          \
381                                 : "i" (EFLAGS_MASK) );                  \
382                         break;                                          \
383                 case 4:                                                 \
384                         __asm__ __volatile__ (                          \
385                                 _PRE_EFLAGS("0","3","2")                \
386                                 _op"l %1; "                             \
387                                 _POST_EFLAGS("0","3","2")               \
388                                 : "=m" (_eflags), "=m" ((_dst).val),    \
389                                   "=&r" (_tmp)                          \
390                                 : "i" (EFLAGS_MASK) );                  \
391                         break;                                          \
392                 case 8:                                                 \
393                         __emulate_1op_8byte(_op, _dst, _eflags);        \
394                         break;                                          \
395                 }                                                       \
396         } while (0)
397
398 /* Emulate an instruction with quadword operands (x86/64 only). */
399 #if defined(CONFIG_X86_64)
400 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)           \
401         do {                                                              \
402                 __asm__ __volatile__ (                                    \
403                         _PRE_EFLAGS("0","4","2")                          \
404                         _op"q %"_qx"3,%1; "                               \
405                         _POST_EFLAGS("0","4","2")                         \
406                         : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
407                         : _qy ((_src).val), "i" (EFLAGS_MASK) );          \
408         } while (0)
409
410 #define __emulate_1op_8byte(_op, _dst, _eflags)                           \
411         do {                                                              \
412                 __asm__ __volatile__ (                                    \
413                         _PRE_EFLAGS("0","3","2")                          \
414                         _op"q %1; "                                       \
415                         _POST_EFLAGS("0","3","2")                         \
416                         : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
417                         : "i" (EFLAGS_MASK) );                            \
418         } while (0)
419
420 #elif defined(__i386__)
421 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
422 #define __emulate_1op_8byte(_op, _dst, _eflags)
423 #endif                          /* __i386__ */
424
425 /* Fetch next part of the instruction being emulated. */
426 #define insn_fetch(_type, _size, _eip)                                  \
427 ({      unsigned long _x;                                               \
428         rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x,  \
429                                                   (_size), ctxt->vcpu); \
430         if ( rc != 0 )                                                  \
431                 goto done;                                              \
432         (_eip) += (_size);                                              \
433         (_type)_x;                                                      \
434 })
435
436 /* Access/update address held in a register, based on addressing mode. */
437 #define address_mask(reg)                                               \
438         ((ad_bytes == sizeof(unsigned long)) ?                          \
439                 (reg) : ((reg) & ((1UL << (ad_bytes << 3)) - 1)))
440 #define register_address(base, reg)                                     \
441         ((base) + address_mask(reg))
442 #define register_address_increment(reg, inc)                            \
443         do {                                                            \
444                 /* signed type ensures sign extension to long */        \
445                 int _inc = (inc);                                       \
446                 if ( ad_bytes == sizeof(unsigned long) )                \
447                         (reg) += _inc;                                  \
448                 else                                                    \
449                         (reg) = ((reg) & ~((1UL << (ad_bytes << 3)) - 1)) | \
450                            (((reg) + _inc) & ((1UL << (ad_bytes << 3)) - 1)); \
451         } while (0)
452
453 #define JMP_REL(rel)                                                    \
454         do {                                                            \
455                 _eip += (int)(rel);                                     \
456                 _eip = ((op_bytes == 2) ? (uint16_t)_eip : (uint32_t)_eip); \
457         } while (0)
458
459 /*
460  * Given the 'reg' portion of a ModRM byte, and a register block, return a
461  * pointer into the block that addresses the relevant register.
462  * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
463  */
464 static void *decode_register(u8 modrm_reg, unsigned long *regs,
465                              int highbyte_regs)
466 {
467         void *p;
468
469         p = &regs[modrm_reg];
470         if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
471                 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
472         return p;
473 }
474
475 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
476                            struct x86_emulate_ops *ops,
477                            void *ptr,
478                            u16 *size, unsigned long *address, int op_bytes)
479 {
480         int rc;
481
482         if (op_bytes == 2)
483                 op_bytes = 3;
484         *address = 0;
485         rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
486                            ctxt->vcpu);
487         if (rc)
488                 return rc;
489         rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
490                            ctxt->vcpu);
491         return rc;
492 }
493
494 int
495 x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
496 {
497         unsigned d;
498         u8 b, sib, twobyte = 0, rex_prefix = 0;
499         u8 modrm, modrm_mod = 0, modrm_reg = 0, modrm_rm = 0;
500         unsigned long *override_base = NULL;
501         unsigned int op_bytes, ad_bytes, lock_prefix = 0, rep_prefix = 0, i;
502         int rc = 0;
503         struct operand src, dst;
504         unsigned long cr2 = ctxt->cr2;
505         int mode = ctxt->mode;
506         unsigned long modrm_ea;
507         int use_modrm_ea, index_reg = 0, base_reg = 0, scale, rip_relative = 0;
508         int no_wb = 0;
509         u64 msr_data;
510
511         /* Shadow copy of register state. Committed on successful emulation. */
512         unsigned long _regs[NR_VCPU_REGS];
513         unsigned long _eip = ctxt->vcpu->rip, _eflags = ctxt->eflags;
514         unsigned long modrm_val = 0;
515
516         memcpy(_regs, ctxt->vcpu->regs, sizeof _regs);
517
518         switch (mode) {
519         case X86EMUL_MODE_REAL:
520         case X86EMUL_MODE_PROT16:
521                 op_bytes = ad_bytes = 2;
522                 break;
523         case X86EMUL_MODE_PROT32:
524                 op_bytes = ad_bytes = 4;
525                 break;
526 #ifdef CONFIG_X86_64
527         case X86EMUL_MODE_PROT64:
528                 op_bytes = 4;
529                 ad_bytes = 8;
530                 break;
531 #endif
532         default:
533                 return -1;
534         }
535
536         /* Legacy prefixes. */
537         for (i = 0; i < 8; i++) {
538                 switch (b = insn_fetch(u8, 1, _eip)) {
539                 case 0x66:      /* operand-size override */
540                         op_bytes ^= 6;  /* switch between 2/4 bytes */
541                         break;
542                 case 0x67:      /* address-size override */
543                         if (mode == X86EMUL_MODE_PROT64)
544                                 ad_bytes ^= 12; /* switch between 4/8 bytes */
545                         else
546                                 ad_bytes ^= 6;  /* switch between 2/4 bytes */
547                         break;
548                 case 0x2e:      /* CS override */
549                         override_base = &ctxt->cs_base;
550                         break;
551                 case 0x3e:      /* DS override */
552                         override_base = &ctxt->ds_base;
553                         break;
554                 case 0x26:      /* ES override */
555                         override_base = &ctxt->es_base;
556                         break;
557                 case 0x64:      /* FS override */
558                         override_base = &ctxt->fs_base;
559                         break;
560                 case 0x65:      /* GS override */
561                         override_base = &ctxt->gs_base;
562                         break;
563                 case 0x36:      /* SS override */
564                         override_base = &ctxt->ss_base;
565                         break;
566                 case 0xf0:      /* LOCK */
567                         lock_prefix = 1;
568                         break;
569                 case 0xf3:      /* REP/REPE/REPZ */
570                         rep_prefix = 1;
571                         break;
572                 case 0xf2:      /* REPNE/REPNZ */
573                         break;
574                 default:
575                         goto done_prefixes;
576                 }
577         }
578
579 done_prefixes:
580
581         /* REX prefix. */
582         if ((mode == X86EMUL_MODE_PROT64) && ((b & 0xf0) == 0x40)) {
583                 rex_prefix = b;
584                 if (b & 8)
585                         op_bytes = 8;   /* REX.W */
586                 modrm_reg = (b & 4) << 1;       /* REX.R */
587                 index_reg = (b & 2) << 2; /* REX.X */
588                 modrm_rm = base_reg = (b & 1) << 3; /* REG.B */
589                 b = insn_fetch(u8, 1, _eip);
590         }
591
592         /* Opcode byte(s). */
593         d = opcode_table[b];
594         if (d == 0) {
595                 /* Two-byte opcode? */
596                 if (b == 0x0f) {
597                         twobyte = 1;
598                         b = insn_fetch(u8, 1, _eip);
599                         d = twobyte_table[b];
600                 }
601
602                 /* Unrecognised? */
603                 if (d == 0)
604                         goto cannot_emulate;
605         }
606
607         /* ModRM and SIB bytes. */
608         if (d & ModRM) {
609                 modrm = insn_fetch(u8, 1, _eip);
610                 modrm_mod |= (modrm & 0xc0) >> 6;
611                 modrm_reg |= (modrm & 0x38) >> 3;
612                 modrm_rm |= (modrm & 0x07);
613                 modrm_ea = 0;
614                 use_modrm_ea = 1;
615
616                 if (modrm_mod == 3) {
617                         modrm_val = *(unsigned long *)
618                                 decode_register(modrm_rm, _regs, d & ByteOp);
619                         goto modrm_done;
620                 }
621
622                 if (ad_bytes == 2) {
623                         unsigned bx = _regs[VCPU_REGS_RBX];
624                         unsigned bp = _regs[VCPU_REGS_RBP];
625                         unsigned si = _regs[VCPU_REGS_RSI];
626                         unsigned di = _regs[VCPU_REGS_RDI];
627
628                         /* 16-bit ModR/M decode. */
629                         switch (modrm_mod) {
630                         case 0:
631                                 if (modrm_rm == 6)
632                                         modrm_ea += insn_fetch(u16, 2, _eip);
633                                 break;
634                         case 1:
635                                 modrm_ea += insn_fetch(s8, 1, _eip);
636                                 break;
637                         case 2:
638                                 modrm_ea += insn_fetch(u16, 2, _eip);
639                                 break;
640                         }
641                         switch (modrm_rm) {
642                         case 0:
643                                 modrm_ea += bx + si;
644                                 break;
645                         case 1:
646                                 modrm_ea += bx + di;
647                                 break;
648                         case 2:
649                                 modrm_ea += bp + si;
650                                 break;
651                         case 3:
652                                 modrm_ea += bp + di;
653                                 break;
654                         case 4:
655                                 modrm_ea += si;
656                                 break;
657                         case 5:
658                                 modrm_ea += di;
659                                 break;
660                         case 6:
661                                 if (modrm_mod != 0)
662                                         modrm_ea += bp;
663                                 break;
664                         case 7:
665                                 modrm_ea += bx;
666                                 break;
667                         }
668                         if (modrm_rm == 2 || modrm_rm == 3 ||
669                             (modrm_rm == 6 && modrm_mod != 0))
670                                 if (!override_base)
671                                         override_base = &ctxt->ss_base;
672                         modrm_ea = (u16)modrm_ea;
673                 } else {
674                         /* 32/64-bit ModR/M decode. */
675                         switch (modrm_rm) {
676                         case 4:
677                         case 12:
678                                 sib = insn_fetch(u8, 1, _eip);
679                                 index_reg |= (sib >> 3) & 7;
680                                 base_reg |= sib & 7;
681                                 scale = sib >> 6;
682
683                                 switch (base_reg) {
684                                 case 5:
685                                         if (modrm_mod != 0)
686                                                 modrm_ea += _regs[base_reg];
687                                         else
688                                                 modrm_ea += insn_fetch(s32, 4, _eip);
689                                         break;
690                                 default:
691                                         modrm_ea += _regs[base_reg];
692                                 }
693                                 switch (index_reg) {
694                                 case 4:
695                                         break;
696                                 default:
697                                         modrm_ea += _regs[index_reg] << scale;
698
699                                 }
700                                 break;
701                         case 5:
702                                 if (modrm_mod != 0)
703                                         modrm_ea += _regs[modrm_rm];
704                                 else if (mode == X86EMUL_MODE_PROT64)
705                                         rip_relative = 1;
706                                 break;
707                         default:
708                                 modrm_ea += _regs[modrm_rm];
709                                 break;
710                         }
711                         switch (modrm_mod) {
712                         case 0:
713                                 if (modrm_rm == 5)
714                                         modrm_ea += insn_fetch(s32, 4, _eip);
715                                 break;
716                         case 1:
717                                 modrm_ea += insn_fetch(s8, 1, _eip);
718                                 break;
719                         case 2:
720                                 modrm_ea += insn_fetch(s32, 4, _eip);
721                                 break;
722                         }
723                 }
724                 if (!override_base)
725                         override_base = &ctxt->ds_base;
726                 if (mode == X86EMUL_MODE_PROT64 &&
727                     override_base != &ctxt->fs_base &&
728                     override_base != &ctxt->gs_base)
729                         override_base = NULL;
730
731                 if (override_base)
732                         modrm_ea += *override_base;
733
734                 if (rip_relative) {
735                         modrm_ea += _eip;
736                         switch (d & SrcMask) {
737                         case SrcImmByte:
738                                 modrm_ea += 1;
739                                 break;
740                         case SrcImm:
741                                 if (d & ByteOp)
742                                         modrm_ea += 1;
743                                 else
744                                         if (op_bytes == 8)
745                                                 modrm_ea += 4;
746                                         else
747                                                 modrm_ea += op_bytes;
748                         }
749                 }
750                 if (ad_bytes != 8)
751                         modrm_ea = (u32)modrm_ea;
752                 cr2 = modrm_ea;
753         modrm_done:
754                 ;
755         }
756
757         /*
758          * Decode and fetch the source operand: register, memory
759          * or immediate.
760          */
761         switch (d & SrcMask) {
762         case SrcNone:
763                 break;
764         case SrcReg:
765                 src.type = OP_REG;
766                 if (d & ByteOp) {
767                         src.ptr = decode_register(modrm_reg, _regs,
768                                                   (rex_prefix == 0));
769                         src.val = src.orig_val = *(u8 *) src.ptr;
770                         src.bytes = 1;
771                 } else {
772                         src.ptr = decode_register(modrm_reg, _regs, 0);
773                         switch ((src.bytes = op_bytes)) {
774                         case 2:
775                                 src.val = src.orig_val = *(u16 *) src.ptr;
776                                 break;
777                         case 4:
778                                 src.val = src.orig_val = *(u32 *) src.ptr;
779                                 break;
780                         case 8:
781                                 src.val = src.orig_val = *(u64 *) src.ptr;
782                                 break;
783                         }
784                 }
785                 break;
786         case SrcMem16:
787                 src.bytes = 2;
788                 goto srcmem_common;
789         case SrcMem32:
790                 src.bytes = 4;
791                 goto srcmem_common;
792         case SrcMem:
793                 src.bytes = (d & ByteOp) ? 1 : op_bytes;
794               srcmem_common:
795                 src.type = OP_MEM;
796                 src.ptr = (unsigned long *)cr2;
797                 if ((rc = ops->read_emulated((unsigned long)src.ptr,
798                                              &src.val, src.bytes, ctxt->vcpu)) != 0)
799                         goto done;
800                 src.orig_val = src.val;
801                 break;
802         case SrcImm:
803                 src.type = OP_IMM;
804                 src.ptr = (unsigned long *)_eip;
805                 src.bytes = (d & ByteOp) ? 1 : op_bytes;
806                 if (src.bytes == 8)
807                         src.bytes = 4;
808                 /* NB. Immediates are sign-extended as necessary. */
809                 switch (src.bytes) {
810                 case 1:
811                         src.val = insn_fetch(s8, 1, _eip);
812                         break;
813                 case 2:
814                         src.val = insn_fetch(s16, 2, _eip);
815                         break;
816                 case 4:
817                         src.val = insn_fetch(s32, 4, _eip);
818                         break;
819                 }
820                 break;
821         case SrcImmByte:
822                 src.type = OP_IMM;
823                 src.ptr = (unsigned long *)_eip;
824                 src.bytes = 1;
825                 src.val = insn_fetch(s8, 1, _eip);
826                 break;
827         }
828
829         /* Decode and fetch the destination operand: register or memory. */
830         switch (d & DstMask) {
831         case ImplicitOps:
832                 /* Special instructions do their own operand decoding. */
833                 goto special_insn;
834         case DstReg:
835                 dst.type = OP_REG;
836                 if ((d & ByteOp)
837                     && !(twobyte && (b == 0xb6 || b == 0xb7))) {
838                         dst.ptr = decode_register(modrm_reg, _regs,
839                                                   (rex_prefix == 0));
840                         dst.val = *(u8 *) dst.ptr;
841                         dst.bytes = 1;
842                 } else {
843                         dst.ptr = decode_register(modrm_reg, _regs, 0);
844                         switch ((dst.bytes = op_bytes)) {
845                         case 2:
846                                 dst.val = *(u16 *)dst.ptr;
847                                 break;
848                         case 4:
849                                 dst.val = *(u32 *)dst.ptr;
850                                 break;
851                         case 8:
852                                 dst.val = *(u64 *)dst.ptr;
853                                 break;
854                         }
855                 }
856                 break;
857         case DstMem:
858                 dst.type = OP_MEM;
859                 dst.ptr = (unsigned long *)cr2;
860                 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
861                 if (d & BitOp) {
862                         unsigned long mask = ~(dst.bytes * 8 - 1);
863
864                         dst.ptr = (void *)dst.ptr + (src.val & mask) / 8;
865                 }
866                 if (!(d & Mov) && /* optimisation - avoid slow emulated read */
867                     ((rc = ops->read_emulated((unsigned long)dst.ptr,
868                                               &dst.val, dst.bytes, ctxt->vcpu)) != 0))
869                         goto done;
870                 break;
871         }
872         dst.orig_val = dst.val;
873
874         if (twobyte)
875                 goto twobyte_insn;
876
877         switch (b) {
878         case 0x00 ... 0x05:
879               add:              /* add */
880                 emulate_2op_SrcV("add", src, dst, _eflags);
881                 break;
882         case 0x08 ... 0x0d:
883               or:               /* or */
884                 emulate_2op_SrcV("or", src, dst, _eflags);
885                 break;
886         case 0x10 ... 0x15:
887               adc:              /* adc */
888                 emulate_2op_SrcV("adc", src, dst, _eflags);
889                 break;
890         case 0x18 ... 0x1d:
891               sbb:              /* sbb */
892                 emulate_2op_SrcV("sbb", src, dst, _eflags);
893                 break;
894         case 0x20 ... 0x23:
895               and:              /* and */
896                 emulate_2op_SrcV("and", src, dst, _eflags);
897                 break;
898         case 0x24:              /* and al imm8 */
899                 dst.type = OP_REG;
900                 dst.ptr = &_regs[VCPU_REGS_RAX];
901                 dst.val = *(u8 *)dst.ptr;
902                 dst.bytes = 1;
903                 dst.orig_val = dst.val;
904                 goto and;
905         case 0x25:              /* and ax imm16, or eax imm32 */
906                 dst.type = OP_REG;
907                 dst.bytes = op_bytes;
908                 dst.ptr = &_regs[VCPU_REGS_RAX];
909                 if (op_bytes == 2)
910                         dst.val = *(u16 *)dst.ptr;
911                 else
912                         dst.val = *(u32 *)dst.ptr;
913                 dst.orig_val = dst.val;
914                 goto and;
915         case 0x28 ... 0x2d:
916               sub:              /* sub */
917                 emulate_2op_SrcV("sub", src, dst, _eflags);
918                 break;
919         case 0x30 ... 0x35:
920               xor:              /* xor */
921                 emulate_2op_SrcV("xor", src, dst, _eflags);
922                 break;
923         case 0x38 ... 0x3d:
924               cmp:              /* cmp */
925                 emulate_2op_SrcV("cmp", src, dst, _eflags);
926                 break;
927         case 0x63:              /* movsxd */
928                 if (mode != X86EMUL_MODE_PROT64)
929                         goto cannot_emulate;
930                 dst.val = (s32) src.val;
931                 break;
932         case 0x80 ... 0x83:     /* Grp1 */
933                 switch (modrm_reg) {
934                 case 0:
935                         goto add;
936                 case 1:
937                         goto or;
938                 case 2:
939                         goto adc;
940                 case 3:
941                         goto sbb;
942                 case 4:
943                         goto and;
944                 case 5:
945                         goto sub;
946                 case 6:
947                         goto xor;
948                 case 7:
949                         goto cmp;
950                 }
951                 break;
952         case 0x84 ... 0x85:
953               test:             /* test */
954                 emulate_2op_SrcV("test", src, dst, _eflags);
955                 break;
956         case 0x86 ... 0x87:     /* xchg */
957                 /* Write back the register source. */
958                 switch (dst.bytes) {
959                 case 1:
960                         *(u8 *) src.ptr = (u8) dst.val;
961                         break;
962                 case 2:
963                         *(u16 *) src.ptr = (u16) dst.val;
964                         break;
965                 case 4:
966                         *src.ptr = (u32) dst.val;
967                         break;  /* 64b reg: zero-extend */
968                 case 8:
969                         *src.ptr = dst.val;
970                         break;
971                 }
972                 /*
973                  * Write back the memory destination with implicit LOCK
974                  * prefix.
975                  */
976                 dst.val = src.val;
977                 lock_prefix = 1;
978                 break;
979         case 0xa0 ... 0xa1:     /* mov */
980                 dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
981                 dst.val = src.val;
982                 _eip += ad_bytes;       /* skip src displacement */
983                 break;
984         case 0xa2 ... 0xa3:     /* mov */
985                 dst.val = (unsigned long)_regs[VCPU_REGS_RAX];
986                 _eip += ad_bytes;       /* skip dst displacement */
987                 break;
988         case 0x88 ... 0x8b:     /* mov */
989         case 0xc6 ... 0xc7:     /* mov (sole member of Grp11) */
990                 dst.val = src.val;
991                 break;
992         case 0x8f:              /* pop (sole member of Grp1a) */
993                 /* 64-bit mode: POP always pops a 64-bit operand. */
994                 if (mode == X86EMUL_MODE_PROT64)
995                         dst.bytes = 8;
996                 if ((rc = ops->read_std(register_address(ctxt->ss_base,
997                                                          _regs[VCPU_REGS_RSP]),
998                                         &dst.val, dst.bytes, ctxt->vcpu)) != 0)
999                         goto done;
1000                 register_address_increment(_regs[VCPU_REGS_RSP], dst.bytes);
1001                 break;
1002         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;
1029         case 0xd0 ... 0xd1:     /* Grp2 */
1030                 src.val = 1;
1031                 goto grp2;
1032         case 0xd2 ... 0xd3:     /* Grp2 */
1033                 src.val = _regs[VCPU_REGS_RCX];
1034                 goto grp2;
1035         case 0xe9: /* jmp rel */
1036         case 0xeb: /* jmp rel short */
1037                 JMP_REL(src.val);
1038                 no_wb = 1; /* Disable writeback. */
1039                 break;
1040         case 0xf6 ... 0xf7:     /* Grp3 */
1041                 switch (modrm_reg) {
1042                 case 0 ... 1:   /* test */
1043                         /*
1044                          * Special case in Grp3: test has an immediate
1045                          * source operand.
1046                          */
1047                         src.type = OP_IMM;
1048                         src.ptr = (unsigned long *)_eip;
1049                         src.bytes = (d & ByteOp) ? 1 : op_bytes;
1050                         if (src.bytes == 8)
1051                                 src.bytes = 4;
1052                         switch (src.bytes) {
1053                         case 1:
1054                                 src.val = insn_fetch(s8, 1, _eip);
1055                                 break;
1056                         case 2:
1057                                 src.val = insn_fetch(s16, 2, _eip);
1058                                 break;
1059                         case 4:
1060                                 src.val = insn_fetch(s32, 4, _eip);
1061                                 break;
1062                         }
1063                         goto test;
1064                 case 2: /* not */
1065                         dst.val = ~dst.val;
1066                         break;
1067                 case 3: /* neg */
1068                         emulate_1op("neg", dst, _eflags);
1069                         break;
1070                 default:
1071                         goto cannot_emulate;
1072                 }
1073                 break;
1074         case 0xfe ... 0xff:     /* Grp4/Grp5 */
1075                 switch (modrm_reg) {
1076                 case 0: /* inc */
1077                         emulate_1op("inc", dst, _eflags);
1078                         break;
1079                 case 1: /* dec */
1080                         emulate_1op("dec", dst, _eflags);
1081                         break;
1082                 case 6: /* push */
1083                         /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1084                         if (mode == X86EMUL_MODE_PROT64) {
1085                                 dst.bytes = 8;
1086                                 if ((rc = ops->read_std((unsigned long)dst.ptr,
1087                                                         &dst.val, 8,
1088                                                         ctxt->vcpu)) != 0)
1089                                         goto done;
1090                         }
1091                         register_address_increment(_regs[VCPU_REGS_RSP],
1092                                                    -dst.bytes);
1093                         if ((rc = ops->write_std(
1094                                      register_address(ctxt->ss_base,
1095                                                       _regs[VCPU_REGS_RSP]),
1096                                      &dst.val, dst.bytes, ctxt->vcpu)) != 0)
1097                                 goto done;
1098                         no_wb = 1;
1099                         break;
1100                 default:
1101                         goto cannot_emulate;
1102                 }
1103                 break;
1104         }
1105
1106 writeback:
1107         if (!no_wb) {
1108                 switch (dst.type) {
1109                 case OP_REG:
1110                         /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1111                         switch (dst.bytes) {
1112                         case 1:
1113                                 *(u8 *)dst.ptr = (u8)dst.val;
1114                                 break;
1115                         case 2:
1116                                 *(u16 *)dst.ptr = (u16)dst.val;
1117                                 break;
1118                         case 4:
1119                                 *dst.ptr = (u32)dst.val;
1120                                 break;  /* 64b: zero-ext */
1121                         case 8:
1122                                 *dst.ptr = dst.val;
1123                                 break;
1124                         }
1125                         break;
1126                 case OP_MEM:
1127                         if (lock_prefix)
1128                                 rc = ops->cmpxchg_emulated((unsigned long)dst.
1129                                                            ptr, &dst.orig_val,
1130                                                            &dst.val, dst.bytes,
1131                                                            ctxt->vcpu);
1132                         else
1133                                 rc = ops->write_emulated((unsigned long)dst.ptr,
1134                                                          &dst.val, dst.bytes,
1135                                                          ctxt->vcpu);
1136                         if (rc != 0)
1137                                 goto done;
1138                 default:
1139                         break;
1140                 }
1141         }
1142
1143         /* Commit shadow register state. */
1144         memcpy(ctxt->vcpu->regs, _regs, sizeof _regs);
1145         ctxt->eflags = _eflags;
1146         ctxt->vcpu->rip = _eip;
1147
1148 done:
1149         return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1150
1151 special_insn:
1152         if (twobyte)
1153                 goto twobyte_special_insn;
1154         switch(b) {
1155         case 0x50 ... 0x57:  /* push reg */
1156                 if (op_bytes == 2)
1157                         src.val = (u16) _regs[b & 0x7];
1158                 else
1159                         src.val = (u32) _regs[b & 0x7];
1160                 dst.type  = OP_MEM;
1161                 dst.bytes = op_bytes;
1162                 dst.val = src.val;
1163                 register_address_increment(_regs[VCPU_REGS_RSP], -op_bytes);
1164                 dst.ptr = (void *) register_address(
1165                         ctxt->ss_base, _regs[VCPU_REGS_RSP]);
1166                 break;
1167         case 0x6c:              /* insb */
1168         case 0x6d:              /* insw/insd */
1169                  if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1170                                 1,                                      /* in */
1171                                 (d & ByteOp) ? 1 : op_bytes,            /* size */
1172                                 rep_prefix ?
1173                                 address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
1174                                 (_eflags & EFLG_DF),                    /* down */
1175                                 register_address(ctxt->es_base,
1176                                                  _regs[VCPU_REGS_RDI]), /* address */
1177                                 rep_prefix,
1178                                 _regs[VCPU_REGS_RDX]                    /* port */
1179                                 ) == 0)
1180                         return -1;
1181                 return 0;
1182         case 0x6e:              /* outsb */
1183         case 0x6f:              /* outsw/outsd */
1184                 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1185                                 0,                                      /* in */
1186                                 (d & ByteOp) ? 1 : op_bytes,            /* size */
1187                                 rep_prefix ?
1188                                 address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
1189                                 (_eflags & EFLG_DF),                    /* down */
1190                                 register_address(override_base ?
1191                                                  *override_base : ctxt->ds_base,
1192                                                  _regs[VCPU_REGS_RSI]), /* address */
1193                                 rep_prefix,
1194                                 _regs[VCPU_REGS_RDX]                    /* port */
1195                                 ) == 0)
1196                         return -1;
1197                 return 0;
1198         }
1199         if (rep_prefix) {
1200                 if (_regs[VCPU_REGS_RCX] == 0) {
1201                         ctxt->vcpu->rip = _eip;
1202                         goto done;
1203                 }
1204                 _regs[VCPU_REGS_RCX]--;
1205                 _eip = ctxt->vcpu->rip;
1206         }
1207         switch (b) {
1208         case 0xa4 ... 0xa5:     /* movs */
1209                 dst.type = OP_MEM;
1210                 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1211                 dst.ptr = (unsigned long *)register_address(ctxt->es_base,
1212                                                         _regs[VCPU_REGS_RDI]);
1213                 if ((rc = ops->read_emulated(register_address(
1214                       override_base ? *override_base : ctxt->ds_base,
1215                       _regs[VCPU_REGS_RSI]), &dst.val, dst.bytes, ctxt->vcpu)) != 0)
1216                         goto done;
1217                 register_address_increment(_regs[VCPU_REGS_RSI],
1218                              (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1219                 register_address_increment(_regs[VCPU_REGS_RDI],
1220                              (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1221                 break;
1222         case 0xa6 ... 0xa7:     /* cmps */
1223                 DPRINTF("Urk! I don't handle CMPS.\n");
1224                 goto cannot_emulate;
1225         case 0xaa ... 0xab:     /* stos */
1226                 dst.type = OP_MEM;
1227                 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1228                 dst.ptr = (unsigned long *)cr2;
1229                 dst.val = _regs[VCPU_REGS_RAX];
1230                 register_address_increment(_regs[VCPU_REGS_RDI],
1231                              (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1232                 break;
1233         case 0xac ... 0xad:     /* lods */
1234                 dst.type = OP_REG;
1235                 dst.bytes = (d & ByteOp) ? 1 : op_bytes;
1236                 dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
1237                 if ((rc = ops->read_emulated(cr2, &dst.val, dst.bytes,
1238                                              ctxt->vcpu)) != 0)
1239                         goto done;
1240                 register_address_increment(_regs[VCPU_REGS_RSI],
1241                            (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
1242                 break;
1243         case 0xae ... 0xaf:     /* scas */
1244                 DPRINTF("Urk! I don't handle SCAS.\n");
1245                 goto cannot_emulate;
1246         case 0xf4:              /* hlt */
1247                 ctxt->vcpu->halt_request = 1;
1248                 goto done;
1249         case 0xc3: /* ret */
1250                 dst.ptr = &_eip;
1251                 goto pop_instruction;
1252         case 0x58 ... 0x5f: /* pop reg */
1253                 dst.ptr = (unsigned long *)&_regs[b & 0x7];
1254
1255 pop_instruction:
1256                 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1257                         _regs[VCPU_REGS_RSP]), dst.ptr, op_bytes, ctxt->vcpu))
1258                         != 0)
1259                         goto done;
1260
1261                 register_address_increment(_regs[VCPU_REGS_RSP], op_bytes);
1262                 no_wb = 1; /* Disable writeback. */
1263                 break;
1264         }
1265         goto writeback;
1266
1267 twobyte_insn:
1268         switch (b) {
1269         case 0x01: /* lgdt, lidt, lmsw */
1270                 /* Disable writeback. */
1271                 no_wb = 1;
1272                 switch (modrm_reg) {
1273                         u16 size;
1274                         unsigned long address;
1275
1276                 case 2: /* lgdt */
1277                         rc = read_descriptor(ctxt, ops, src.ptr,
1278                                              &size, &address, op_bytes);
1279                         if (rc)
1280                                 goto done;
1281                         realmode_lgdt(ctxt->vcpu, size, address);
1282                         break;
1283                 case 3: /* lidt */
1284                         rc = read_descriptor(ctxt, ops, src.ptr,
1285                                              &size, &address, op_bytes);
1286                         if (rc)
1287                                 goto done;
1288                         realmode_lidt(ctxt->vcpu, size, address);
1289                         break;
1290                 case 4: /* smsw */
1291                         if (modrm_mod != 3)
1292                                 goto cannot_emulate;
1293                         *(u16 *)&_regs[modrm_rm]
1294                                 = realmode_get_cr(ctxt->vcpu, 0);
1295                         break;
1296                 case 6: /* lmsw */
1297                         if (modrm_mod != 3)
1298                                 goto cannot_emulate;
1299                         realmode_lmsw(ctxt->vcpu, (u16)modrm_val, &_eflags);
1300                         break;
1301                 case 7: /* invlpg*/
1302                         emulate_invlpg(ctxt->vcpu, cr2);
1303                         break;
1304                 default:
1305                         goto cannot_emulate;
1306                 }
1307                 break;
1308         case 0x21: /* mov from dr to reg */
1309                 no_wb = 1;
1310                 if (modrm_mod != 3)
1311                         goto cannot_emulate;
1312                 rc = emulator_get_dr(ctxt, modrm_reg, &_regs[modrm_rm]);
1313                 break;
1314         case 0x23: /* mov from reg to dr */
1315                 no_wb = 1;
1316                 if (modrm_mod != 3)
1317                         goto cannot_emulate;
1318                 rc = emulator_set_dr(ctxt, modrm_reg, _regs[modrm_rm]);
1319                 break;
1320         case 0x40 ... 0x4f:     /* cmov */
1321                 dst.val = dst.orig_val = src.val;
1322                 no_wb = 1;
1323                 /*
1324                  * First, assume we're decoding an even cmov opcode
1325                  * (lsb == 0).
1326                  */
1327                 switch ((b & 15) >> 1) {
1328                 case 0: /* cmovo */
1329                         no_wb = (_eflags & EFLG_OF) ? 0 : 1;
1330                         break;
1331                 case 1: /* cmovb/cmovc/cmovnae */
1332                         no_wb = (_eflags & EFLG_CF) ? 0 : 1;
1333                         break;
1334                 case 2: /* cmovz/cmove */
1335                         no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
1336                         break;
1337                 case 3: /* cmovbe/cmovna */
1338                         no_wb = (_eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1;
1339                         break;
1340                 case 4: /* cmovs */
1341                         no_wb = (_eflags & EFLG_SF) ? 0 : 1;
1342                         break;
1343                 case 5: /* cmovp/cmovpe */
1344                         no_wb = (_eflags & EFLG_PF) ? 0 : 1;
1345                         break;
1346                 case 7: /* cmovle/cmovng */
1347                         no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
1348                         /* fall through */
1349                 case 6: /* cmovl/cmovnge */
1350                         no_wb &= (!(_eflags & EFLG_SF) !=
1351                               !(_eflags & EFLG_OF)) ? 0 : 1;
1352                         break;
1353                 }
1354                 /* Odd cmov opcodes (lsb == 1) have inverted sense. */
1355                 no_wb ^= b & 1;
1356                 break;
1357         case 0xb0 ... 0xb1:     /* cmpxchg */
1358                 /*
1359                  * Save real source value, then compare EAX against
1360                  * destination.
1361                  */
1362                 src.orig_val = src.val;
1363                 src.val = _regs[VCPU_REGS_RAX];
1364                 emulate_2op_SrcV("cmp", src, dst, _eflags);
1365                 if (_eflags & EFLG_ZF) {
1366                         /* Success: write back to memory. */
1367                         dst.val = src.orig_val;
1368                 } else {
1369                         /* Failure: write the value we saw to EAX. */
1370                         dst.type = OP_REG;
1371                         dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
1372                 }
1373                 break;
1374         case 0xa3:
1375               bt:               /* bt */
1376                 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1377                 emulate_2op_SrcV_nobyte("bt", src, dst, _eflags);
1378                 break;
1379         case 0xb3:
1380               btr:              /* btr */
1381                 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1382                 emulate_2op_SrcV_nobyte("btr", src, dst, _eflags);
1383                 break;
1384         case 0xab:
1385               bts:              /* bts */
1386                 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1387                 emulate_2op_SrcV_nobyte("bts", src, dst, _eflags);
1388                 break;
1389         case 0xb6 ... 0xb7:     /* movzx */
1390                 dst.bytes = op_bytes;
1391                 dst.val = (d & ByteOp) ? (u8) src.val : (u16) src.val;
1392                 break;
1393         case 0xbb:
1394               btc:              /* btc */
1395                 src.val &= (dst.bytes << 3) - 1; /* only subword offset */
1396                 emulate_2op_SrcV_nobyte("btc", src, dst, _eflags);
1397                 break;
1398         case 0xba:              /* Grp8 */
1399                 switch (modrm_reg & 3) {
1400                 case 0:
1401                         goto bt;
1402                 case 1:
1403                         goto bts;
1404                 case 2:
1405                         goto btr;
1406                 case 3:
1407                         goto btc;
1408                 }
1409                 break;
1410         case 0xbe ... 0xbf:     /* movsx */
1411                 dst.bytes = op_bytes;
1412                 dst.val = (d & ByteOp) ? (s8) src.val : (s16) src.val;
1413                 break;
1414         }
1415         goto writeback;
1416
1417 twobyte_special_insn:
1418         /* Disable writeback. */
1419         no_wb = 1;
1420         switch (b) {
1421         case 0x09:              /* wbinvd */
1422                 break;
1423         case 0x0d:              /* GrpP (prefetch) */
1424         case 0x18:              /* Grp16 (prefetch/nop) */
1425                 break;
1426         case 0x06:
1427                 emulate_clts(ctxt->vcpu);
1428                 break;
1429         case 0x20: /* mov cr, reg */
1430                 if (modrm_mod != 3)
1431                         goto cannot_emulate;
1432                 _regs[modrm_rm] = realmode_get_cr(ctxt->vcpu, modrm_reg);
1433                 break;
1434         case 0x22: /* mov reg, cr */
1435                 if (modrm_mod != 3)
1436                         goto cannot_emulate;
1437                 realmode_set_cr(ctxt->vcpu, modrm_reg, modrm_val, &_eflags);
1438                 break;
1439         case 0x30:
1440                 /* wrmsr */
1441                 msr_data = (u32)_regs[VCPU_REGS_RAX]
1442                         | ((u64)_regs[VCPU_REGS_RDX] << 32);
1443                 rc = kvm_set_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], msr_data);
1444                 if (rc) {
1445                         kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
1446                         _eip = ctxt->vcpu->rip;
1447                 }
1448                 rc = X86EMUL_CONTINUE;
1449                 break;
1450         case 0x32:
1451                 /* rdmsr */
1452                 rc = kvm_get_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], &msr_data);
1453                 if (rc) {
1454                         kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
1455                         _eip = ctxt->vcpu->rip;
1456                 } else {
1457                         _regs[VCPU_REGS_RAX] = (u32)msr_data;
1458                         _regs[VCPU_REGS_RDX] = msr_data >> 32;
1459                 }
1460                 rc = X86EMUL_CONTINUE;
1461                 break;
1462         case 0xc7:              /* Grp9 (cmpxchg8b) */
1463                 {
1464                         u64 old, new;
1465                         if ((rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu))
1466                                                                         != 0)
1467                                 goto done;
1468                         if (((u32) (old >> 0) != (u32) _regs[VCPU_REGS_RAX]) ||
1469                             ((u32) (old >> 32) != (u32) _regs[VCPU_REGS_RDX])) {
1470                                 _regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1471                                 _regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1472                                 _eflags &= ~EFLG_ZF;
1473                         } else {
1474                                 new = ((u64)_regs[VCPU_REGS_RCX] << 32)
1475                                         | (u32) _regs[VCPU_REGS_RBX];
1476                                 if ((rc = ops->cmpxchg_emulated(cr2, &old,
1477                                                           &new, 8, ctxt->vcpu)) != 0)
1478                                         goto done;
1479                                 _eflags |= EFLG_ZF;
1480                         }
1481                         break;
1482                 }
1483         }
1484         goto writeback;
1485
1486 cannot_emulate:
1487         DPRINTF("Cannot emulate %02x\n", b);
1488         return -1;
1489 }
1490
1491 #ifdef __XEN__
1492
1493 #include <asm/mm.h>
1494 #include <asm/uaccess.h>
1495
1496 int
1497 x86_emulate_read_std(unsigned long addr,
1498                      unsigned long *val,
1499                      unsigned int bytes, struct x86_emulate_ctxt *ctxt)
1500 {
1501         unsigned int rc;
1502
1503         *val = 0;
1504
1505         if ((rc = copy_from_user((void *)val, (void *)addr, bytes)) != 0) {
1506                 propagate_page_fault(addr + bytes - rc, 0);     /* read fault */
1507                 return X86EMUL_PROPAGATE_FAULT;
1508         }
1509
1510         return X86EMUL_CONTINUE;
1511 }
1512
1513 int
1514 x86_emulate_write_std(unsigned long addr,
1515                       unsigned long val,
1516                       unsigned int bytes, struct x86_emulate_ctxt *ctxt)
1517 {
1518         unsigned int rc;
1519
1520         if ((rc = copy_to_user((void *)addr, (void *)&val, bytes)) != 0) {
1521                 propagate_page_fault(addr + bytes - rc, PGERR_write_access);
1522                 return X86EMUL_PROPAGATE_FAULT;
1523         }
1524
1525         return X86EMUL_CONTINUE;
1526 }
1527
1528 #endif