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