]> err.no Git - linux-2.6/blob - arch/x86/kvm/x86_emulate.c
KVM: x86 emulator: emulate nop and xchg reg, acc (opcodes 0x90 - 0x97)
[linux-2.6] / arch / x86 / 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 <linux/kvm_host.h>
29 #define DPRINTF(x...) do {} while (0)
30 #endif
31 #include <linux/module.h>
32 #include <asm/kvm_x86_emulate.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 #define MemAbs      (1<<9)      /* Memory operand is absolute displacement */
66 #define String      (1<<10)     /* String instruction (rep capable) */
67 #define Stack       (1<<11)     /* Stack instruction (push/pop) */
68 #define Group       (1<<14)     /* Bits 3:5 of modrm byte extend opcode */
69 #define GroupDual   (1<<15)     /* Alternate decoding of mod == 3 */
70 #define GroupMask   0xff        /* Group number stored in bits 0:7 */
71
72 enum {
73         Group1_80, Group1_81, Group1_82, Group1_83,
74         Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
75 };
76
77 static u16 opcode_table[256] = {
78         /* 0x00 - 0x07 */
79         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
80         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
81         0, 0, 0, 0,
82         /* 0x08 - 0x0F */
83         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
84         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
85         0, 0, 0, 0,
86         /* 0x10 - 0x17 */
87         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
88         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
89         0, 0, 0, 0,
90         /* 0x18 - 0x1F */
91         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
93         0, 0, 0, 0,
94         /* 0x20 - 0x27 */
95         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
97         SrcImmByte, SrcImm, 0, 0,
98         /* 0x28 - 0x2F */
99         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
101         0, 0, 0, 0,
102         /* 0x30 - 0x37 */
103         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
104         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
105         0, 0, 0, 0,
106         /* 0x38 - 0x3F */
107         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
108         ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
109         0, 0, 0, 0,
110         /* 0x40 - 0x47 */
111         DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
112         /* 0x48 - 0x4F */
113         DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
114         /* 0x50 - 0x57 */
115         SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
116         SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
117         /* 0x58 - 0x5F */
118         DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
119         DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
120         /* 0x60 - 0x67 */
121         0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
122         0, 0, 0, 0,
123         /* 0x68 - 0x6F */
124         SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
125         SrcNone  | ByteOp  | ImplicitOps, SrcNone  | ImplicitOps, /* insb, insw/insd */
126         SrcNone  | ByteOp  | ImplicitOps, SrcNone  | ImplicitOps, /* outsb, outsw/outsd */
127         /* 0x70 - 0x77 */
128         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
129         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
130         /* 0x78 - 0x7F */
131         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
132         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
133         /* 0x80 - 0x87 */
134         Group | Group1_80, Group | Group1_81,
135         Group | Group1_82, Group | Group1_83,
136         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
137         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
138         /* 0x88 - 0x8F */
139         ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
140         ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
141         DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
142         DstReg | SrcMem | ModRM | Mov, Group | Group1A,
143         /* 0x90 - 0x97 */
144         DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
145         /* 0x98 - 0x9F */
146         0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
147         /* 0xA0 - 0xA7 */
148         ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
149         ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
150         ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
151         ByteOp | ImplicitOps | String, ImplicitOps | String,
152         /* 0xA8 - 0xAF */
153         0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
154         ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
155         ByteOp | ImplicitOps | String, ImplicitOps | String,
156         /* 0xB0 - 0xBF */
157         0, 0, 0, 0, 0, 0, 0, 0,
158         DstReg | SrcImm | Mov, 0, 0, 0, 0, 0, 0, 0,
159         /* 0xC0 - 0xC7 */
160         ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
161         0, ImplicitOps | Stack, 0, 0,
162         ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
163         /* 0xC8 - 0xCF */
164         0, 0, 0, 0, 0, 0, 0, 0,
165         /* 0xD0 - 0xD7 */
166         ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
167         ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
168         0, 0, 0, 0,
169         /* 0xD8 - 0xDF */
170         0, 0, 0, 0, 0, 0, 0, 0,
171         /* 0xE0 - 0xE7 */
172         0, 0, 0, 0, 0, 0, 0, 0,
173         /* 0xE8 - 0xEF */
174         ImplicitOps | Stack, SrcImm | ImplicitOps,
175         ImplicitOps, SrcImmByte | ImplicitOps,
176         0, 0, 0, 0,
177         /* 0xF0 - 0xF7 */
178         0, 0, 0, 0,
179         ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
180         /* 0xF8 - 0xFF */
181         ImplicitOps, 0, ImplicitOps, ImplicitOps,
182         0, 0, Group | Group4, Group | Group5,
183 };
184
185 static u16 twobyte_table[256] = {
186         /* 0x00 - 0x0F */
187         0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
188         ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
189         /* 0x10 - 0x1F */
190         0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
191         /* 0x20 - 0x2F */
192         ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
193         0, 0, 0, 0, 0, 0, 0, 0,
194         /* 0x30 - 0x3F */
195         ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196         /* 0x40 - 0x47 */
197         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
198         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
199         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
200         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
201         /* 0x48 - 0x4F */
202         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
203         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
204         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
205         DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
206         /* 0x50 - 0x5F */
207         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
208         /* 0x60 - 0x6F */
209         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210         /* 0x70 - 0x7F */
211         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212         /* 0x80 - 0x8F */
213         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
214         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
215         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
216         ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
217         /* 0x90 - 0x9F */
218         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219         /* 0xA0 - 0xA7 */
220         0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
221         /* 0xA8 - 0xAF */
222         0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
223         /* 0xB0 - 0xB7 */
224         ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
225             DstMem | SrcReg | ModRM | BitOp,
226         0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
227             DstReg | SrcMem16 | ModRM | Mov,
228         /* 0xB8 - 0xBF */
229         0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
230         0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
231             DstReg | SrcMem16 | ModRM | Mov,
232         /* 0xC0 - 0xCF */
233         0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
234         0, 0, 0, 0, 0, 0, 0, 0,
235         /* 0xD0 - 0xDF */
236         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237         /* 0xE0 - 0xEF */
238         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239         /* 0xF0 - 0xFF */
240         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
241 };
242
243 static u16 group_table[] = {
244         [Group1_80*8] =
245         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
246         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
247         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
248         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
249         [Group1_81*8] =
250         DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
251         DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
252         DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
253         DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
254         [Group1_82*8] =
255         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
256         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
257         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
258         ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
259         [Group1_83*8] =
260         DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
261         DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
262         DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
263         DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
264         [Group1A*8] =
265         DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
266         [Group3_Byte*8] =
267         ByteOp | SrcImm | DstMem | ModRM, 0,
268         ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
269         0, 0, 0, 0,
270         [Group3*8] =
271         DstMem | SrcImm | ModRM | SrcImm, 0,
272         DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
273         0, 0, 0, 0,
274         [Group4*8] =
275         ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
276         0, 0, 0, 0, 0, 0,
277         [Group5*8] =
278         DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, 0, 0,
279         SrcMem | ModRM, 0, SrcMem | ModRM | Stack, 0,
280         [Group7*8] =
281         0, 0, ModRM | SrcMem, ModRM | SrcMem,
282         SrcNone | ModRM | DstMem | Mov, 0,
283         SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
284 };
285
286 static u16 group2_table[] = {
287         [Group7*8] =
288         SrcNone | ModRM, 0, 0, 0,
289         SrcNone | ModRM | DstMem | Mov, 0,
290         SrcMem16 | ModRM | Mov, 0,
291 };
292
293 /* EFLAGS bit definitions. */
294 #define EFLG_OF (1<<11)
295 #define EFLG_DF (1<<10)
296 #define EFLG_SF (1<<7)
297 #define EFLG_ZF (1<<6)
298 #define EFLG_AF (1<<4)
299 #define EFLG_PF (1<<2)
300 #define EFLG_CF (1<<0)
301
302 /*
303  * Instruction emulation:
304  * Most instructions are emulated directly via a fragment of inline assembly
305  * code. This allows us to save/restore EFLAGS and thus very easily pick up
306  * any modified flags.
307  */
308
309 #if defined(CONFIG_X86_64)
310 #define _LO32 "k"               /* force 32-bit operand */
311 #define _STK  "%%rsp"           /* stack pointer */
312 #elif defined(__i386__)
313 #define _LO32 ""                /* force 32-bit operand */
314 #define _STK  "%%esp"           /* stack pointer */
315 #endif
316
317 /*
318  * These EFLAGS bits are restored from saved value during emulation, and
319  * any changes are written back to the saved value after emulation.
320  */
321 #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
322
323 /* Before executing instruction: restore necessary bits in EFLAGS. */
324 #define _PRE_EFLAGS(_sav, _msk, _tmp)                                   \
325         /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
326         "movl %"_sav",%"_LO32 _tmp"; "                                  \
327         "push %"_tmp"; "                                                \
328         "push %"_tmp"; "                                                \
329         "movl %"_msk",%"_LO32 _tmp"; "                                  \
330         "andl %"_LO32 _tmp",("_STK"); "                                 \
331         "pushf; "                                                       \
332         "notl %"_LO32 _tmp"; "                                          \
333         "andl %"_LO32 _tmp",("_STK"); "                                 \
334         "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); "   \
335         "pop  %"_tmp"; "                                                \
336         "orl  %"_LO32 _tmp",("_STK"); "                                 \
337         "popf; "                                                        \
338         "pop  %"_sav"; "
339
340 /* After executing instruction: write-back necessary bits in EFLAGS. */
341 #define _POST_EFLAGS(_sav, _msk, _tmp) \
342         /* _sav |= EFLAGS & _msk; */            \
343         "pushf; "                               \
344         "pop  %"_tmp"; "                        \
345         "andl %"_msk",%"_LO32 _tmp"; "          \
346         "orl  %"_LO32 _tmp",%"_sav"; "
347
348 /* Raw emulation: instruction has two explicit operands. */
349 #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
350         do {                                                                \
351                 unsigned long _tmp;                                         \
352                                                                             \
353                 switch ((_dst).bytes) {                                     \
354                 case 2:                                                     \
355                         __asm__ __volatile__ (                              \
356                                 _PRE_EFLAGS("0", "4", "2")                  \
357                                 _op"w %"_wx"3,%1; "                         \
358                                 _POST_EFLAGS("0", "4", "2")                 \
359                                 : "=m" (_eflags), "=m" ((_dst).val),        \
360                                   "=&r" (_tmp)                              \
361                                 : _wy ((_src).val), "i" (EFLAGS_MASK));     \
362                         break;                                              \
363                 case 4:                                                     \
364                         __asm__ __volatile__ (                              \
365                                 _PRE_EFLAGS("0", "4", "2")                  \
366                                 _op"l %"_lx"3,%1; "                         \
367                                 _POST_EFLAGS("0", "4", "2")                 \
368                                 : "=m" (_eflags), "=m" ((_dst).val),        \
369                                   "=&r" (_tmp)                              \
370                                 : _ly ((_src).val), "i" (EFLAGS_MASK));     \
371                         break;                                              \
372                 case 8:                                                     \
373                         __emulate_2op_8byte(_op, _src, _dst,                \
374                                             _eflags, _qx, _qy);             \
375                         break;                                              \
376                 }                                                           \
377         } while (0)
378
379 #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
380         do {                                                                 \
381                 unsigned long __tmp;                                         \
382                 switch ((_dst).bytes) {                                      \
383                 case 1:                                                      \
384                         __asm__ __volatile__ (                               \
385                                 _PRE_EFLAGS("0", "4", "2")                   \
386                                 _op"b %"_bx"3,%1; "                          \
387                                 _POST_EFLAGS("0", "4", "2")                  \
388                                 : "=m" (_eflags), "=m" ((_dst).val),         \
389                                   "=&r" (__tmp)                              \
390                                 : _by ((_src).val), "i" (EFLAGS_MASK));      \
391                         break;                                               \
392                 default:                                                     \
393                         __emulate_2op_nobyte(_op, _src, _dst, _eflags,       \
394                                              _wx, _wy, _lx, _ly, _qx, _qy);  \
395                         break;                                               \
396                 }                                                            \
397         } while (0)
398
399 /* Source operand is byte-sized and may be restricted to just %cl. */
400 #define emulate_2op_SrcB(_op, _src, _dst, _eflags)                      \
401         __emulate_2op(_op, _src, _dst, _eflags,                         \
402                       "b", "c", "b", "c", "b", "c", "b", "c")
403
404 /* Source operand is byte, word, long or quad sized. */
405 #define emulate_2op_SrcV(_op, _src, _dst, _eflags)                      \
406         __emulate_2op(_op, _src, _dst, _eflags,                         \
407                       "b", "q", "w", "r", _LO32, "r", "", "r")
408
409 /* Source operand is word, long or quad sized. */
410 #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags)               \
411         __emulate_2op_nobyte(_op, _src, _dst, _eflags,                  \
412                              "w", "r", _LO32, "r", "", "r")
413
414 /* Instruction has only one explicit operand (no source operand). */
415 #define emulate_1op(_op, _dst, _eflags)                                    \
416         do {                                                            \
417                 unsigned long _tmp;                                     \
418                                                                         \
419                 switch ((_dst).bytes) {                                 \
420                 case 1:                                                 \
421                         __asm__ __volatile__ (                          \
422                                 _PRE_EFLAGS("0", "3", "2")              \
423                                 _op"b %1; "                             \
424                                 _POST_EFLAGS("0", "3", "2")             \
425                                 : "=m" (_eflags), "=m" ((_dst).val),    \
426                                   "=&r" (_tmp)                          \
427                                 : "i" (EFLAGS_MASK));                   \
428                         break;                                          \
429                 case 2:                                                 \
430                         __asm__ __volatile__ (                          \
431                                 _PRE_EFLAGS("0", "3", "2")              \
432                                 _op"w %1; "                             \
433                                 _POST_EFLAGS("0", "3", "2")             \
434                                 : "=m" (_eflags), "=m" ((_dst).val),    \
435                                   "=&r" (_tmp)                          \
436                                 : "i" (EFLAGS_MASK));                   \
437                         break;                                          \
438                 case 4:                                                 \
439                         __asm__ __volatile__ (                          \
440                                 _PRE_EFLAGS("0", "3", "2")              \
441                                 _op"l %1; "                             \
442                                 _POST_EFLAGS("0", "3", "2")             \
443                                 : "=m" (_eflags), "=m" ((_dst).val),    \
444                                   "=&r" (_tmp)                          \
445                                 : "i" (EFLAGS_MASK));                   \
446                         break;                                          \
447                 case 8:                                                 \
448                         __emulate_1op_8byte(_op, _dst, _eflags);        \
449                         break;                                          \
450                 }                                                       \
451         } while (0)
452
453 /* Emulate an instruction with quadword operands (x86/64 only). */
454 #if defined(CONFIG_X86_64)
455 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)           \
456         do {                                                              \
457                 __asm__ __volatile__ (                                    \
458                         _PRE_EFLAGS("0", "4", "2")                        \
459                         _op"q %"_qx"3,%1; "                               \
460                         _POST_EFLAGS("0", "4", "2")                       \
461                         : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
462                         : _qy ((_src).val), "i" (EFLAGS_MASK));         \
463         } while (0)
464
465 #define __emulate_1op_8byte(_op, _dst, _eflags)                           \
466         do {                                                              \
467                 __asm__ __volatile__ (                                    \
468                         _PRE_EFLAGS("0", "3", "2")                        \
469                         _op"q %1; "                                       \
470                         _POST_EFLAGS("0", "3", "2")                       \
471                         : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
472                         : "i" (EFLAGS_MASK));                             \
473         } while (0)
474
475 #elif defined(__i386__)
476 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
477 #define __emulate_1op_8byte(_op, _dst, _eflags)
478 #endif                          /* __i386__ */
479
480 /* Fetch next part of the instruction being emulated. */
481 #define insn_fetch(_type, _size, _eip)                                  \
482 ({      unsigned long _x;                                               \
483         rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size));            \
484         if (rc != 0)                                                    \
485                 goto done;                                              \
486         (_eip) += (_size);                                              \
487         (_type)_x;                                                      \
488 })
489
490 static inline unsigned long ad_mask(struct decode_cache *c)
491 {
492         return (1UL << (c->ad_bytes << 3)) - 1;
493 }
494
495 /* Access/update address held in a register, based on addressing mode. */
496 static inline unsigned long
497 address_mask(struct decode_cache *c, unsigned long reg)
498 {
499         if (c->ad_bytes == sizeof(unsigned long))
500                 return reg;
501         else
502                 return reg & ad_mask(c);
503 }
504
505 static inline unsigned long
506 register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
507 {
508         return base + address_mask(c, reg);
509 }
510
511 static inline void
512 register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
513 {
514         if (c->ad_bytes == sizeof(unsigned long))
515                 *reg += inc;
516         else
517                 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
518 }
519
520 static inline void jmp_rel(struct decode_cache *c, int rel)
521 {
522         register_address_increment(c, &c->eip, rel);
523 }
524
525 static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
526                               struct x86_emulate_ops *ops,
527                               unsigned long linear, u8 *dest)
528 {
529         struct fetch_cache *fc = &ctxt->decode.fetch;
530         int rc;
531         int size;
532
533         if (linear < fc->start || linear >= fc->end) {
534                 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
535                 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
536                 if (rc)
537                         return rc;
538                 fc->start = linear;
539                 fc->end = linear + size;
540         }
541         *dest = fc->data[linear - fc->start];
542         return 0;
543 }
544
545 static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
546                          struct x86_emulate_ops *ops,
547                          unsigned long eip, void *dest, unsigned size)
548 {
549         int rc = 0;
550
551         eip += ctxt->cs_base;
552         while (size--) {
553                 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
554                 if (rc)
555                         return rc;
556         }
557         return 0;
558 }
559
560 /*
561  * Given the 'reg' portion of a ModRM byte, and a register block, return a
562  * pointer into the block that addresses the relevant register.
563  * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
564  */
565 static void *decode_register(u8 modrm_reg, unsigned long *regs,
566                              int highbyte_regs)
567 {
568         void *p;
569
570         p = &regs[modrm_reg];
571         if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
572                 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
573         return p;
574 }
575
576 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
577                            struct x86_emulate_ops *ops,
578                            void *ptr,
579                            u16 *size, unsigned long *address, int op_bytes)
580 {
581         int rc;
582
583         if (op_bytes == 2)
584                 op_bytes = 3;
585         *address = 0;
586         rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
587                            ctxt->vcpu);
588         if (rc)
589                 return rc;
590         rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
591                            ctxt->vcpu);
592         return rc;
593 }
594
595 static int test_cc(unsigned int condition, unsigned int flags)
596 {
597         int rc = 0;
598
599         switch ((condition & 15) >> 1) {
600         case 0: /* o */
601                 rc |= (flags & EFLG_OF);
602                 break;
603         case 1: /* b/c/nae */
604                 rc |= (flags & EFLG_CF);
605                 break;
606         case 2: /* z/e */
607                 rc |= (flags & EFLG_ZF);
608                 break;
609         case 3: /* be/na */
610                 rc |= (flags & (EFLG_CF|EFLG_ZF));
611                 break;
612         case 4: /* s */
613                 rc |= (flags & EFLG_SF);
614                 break;
615         case 5: /* p/pe */
616                 rc |= (flags & EFLG_PF);
617                 break;
618         case 7: /* le/ng */
619                 rc |= (flags & EFLG_ZF);
620                 /* fall through */
621         case 6: /* l/nge */
622                 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
623                 break;
624         }
625
626         /* Odd condition identifiers (lsb == 1) have inverted sense. */
627         return (!!rc ^ (condition & 1));
628 }
629
630 static void decode_register_operand(struct operand *op,
631                                     struct decode_cache *c,
632                                     int inhibit_bytereg)
633 {
634         unsigned reg = c->modrm_reg;
635         int highbyte_regs = c->rex_prefix == 0;
636
637         if (!(c->d & ModRM))
638                 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
639         op->type = OP_REG;
640         if ((c->d & ByteOp) && !inhibit_bytereg) {
641                 op->ptr = decode_register(reg, c->regs, highbyte_regs);
642                 op->val = *(u8 *)op->ptr;
643                 op->bytes = 1;
644         } else {
645                 op->ptr = decode_register(reg, c->regs, 0);
646                 op->bytes = c->op_bytes;
647                 switch (op->bytes) {
648                 case 2:
649                         op->val = *(u16 *)op->ptr;
650                         break;
651                 case 4:
652                         op->val = *(u32 *)op->ptr;
653                         break;
654                 case 8:
655                         op->val = *(u64 *) op->ptr;
656                         break;
657                 }
658         }
659         op->orig_val = op->val;
660 }
661
662 static int decode_modrm(struct x86_emulate_ctxt *ctxt,
663                         struct x86_emulate_ops *ops)
664 {
665         struct decode_cache *c = &ctxt->decode;
666         u8 sib;
667         int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
668         int rc = 0;
669
670         if (c->rex_prefix) {
671                 c->modrm_reg = (c->rex_prefix & 4) << 1;        /* REX.R */
672                 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
673                 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
674         }
675
676         c->modrm = insn_fetch(u8, 1, c->eip);
677         c->modrm_mod |= (c->modrm & 0xc0) >> 6;
678         c->modrm_reg |= (c->modrm & 0x38) >> 3;
679         c->modrm_rm |= (c->modrm & 0x07);
680         c->modrm_ea = 0;
681         c->use_modrm_ea = 1;
682
683         if (c->modrm_mod == 3) {
684                 c->modrm_ptr = decode_register(c->modrm_rm,
685                                                c->regs, c->d & ByteOp);
686                 c->modrm_val = *(unsigned long *)c->modrm_ptr;
687                 return rc;
688         }
689
690         if (c->ad_bytes == 2) {
691                 unsigned bx = c->regs[VCPU_REGS_RBX];
692                 unsigned bp = c->regs[VCPU_REGS_RBP];
693                 unsigned si = c->regs[VCPU_REGS_RSI];
694                 unsigned di = c->regs[VCPU_REGS_RDI];
695
696                 /* 16-bit ModR/M decode. */
697                 switch (c->modrm_mod) {
698                 case 0:
699                         if (c->modrm_rm == 6)
700                                 c->modrm_ea += insn_fetch(u16, 2, c->eip);
701                         break;
702                 case 1:
703                         c->modrm_ea += insn_fetch(s8, 1, c->eip);
704                         break;
705                 case 2:
706                         c->modrm_ea += insn_fetch(u16, 2, c->eip);
707                         break;
708                 }
709                 switch (c->modrm_rm) {
710                 case 0:
711                         c->modrm_ea += bx + si;
712                         break;
713                 case 1:
714                         c->modrm_ea += bx + di;
715                         break;
716                 case 2:
717                         c->modrm_ea += bp + si;
718                         break;
719                 case 3:
720                         c->modrm_ea += bp + di;
721                         break;
722                 case 4:
723                         c->modrm_ea += si;
724                         break;
725                 case 5:
726                         c->modrm_ea += di;
727                         break;
728                 case 6:
729                         if (c->modrm_mod != 0)
730                                 c->modrm_ea += bp;
731                         break;
732                 case 7:
733                         c->modrm_ea += bx;
734                         break;
735                 }
736                 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
737                     (c->modrm_rm == 6 && c->modrm_mod != 0))
738                         if (!c->override_base)
739                                 c->override_base = &ctxt->ss_base;
740                 c->modrm_ea = (u16)c->modrm_ea;
741         } else {
742                 /* 32/64-bit ModR/M decode. */
743                 switch (c->modrm_rm) {
744                 case 4:
745                 case 12:
746                         sib = insn_fetch(u8, 1, c->eip);
747                         index_reg |= (sib >> 3) & 7;
748                         base_reg |= sib & 7;
749                         scale = sib >> 6;
750
751                         switch (base_reg) {
752                         case 5:
753                                 if (c->modrm_mod != 0)
754                                         c->modrm_ea += c->regs[base_reg];
755                                 else
756                                         c->modrm_ea +=
757                                                 insn_fetch(s32, 4, c->eip);
758                                 break;
759                         default:
760                                 c->modrm_ea += c->regs[base_reg];
761                         }
762                         switch (index_reg) {
763                         case 4:
764                                 break;
765                         default:
766                                 c->modrm_ea += c->regs[index_reg] << scale;
767                         }
768                         break;
769                 case 5:
770                         if (c->modrm_mod != 0)
771                                 c->modrm_ea += c->regs[c->modrm_rm];
772                         else if (ctxt->mode == X86EMUL_MODE_PROT64)
773                                 rip_relative = 1;
774                         break;
775                 default:
776                         c->modrm_ea += c->regs[c->modrm_rm];
777                         break;
778                 }
779                 switch (c->modrm_mod) {
780                 case 0:
781                         if (c->modrm_rm == 5)
782                                 c->modrm_ea += insn_fetch(s32, 4, c->eip);
783                         break;
784                 case 1:
785                         c->modrm_ea += insn_fetch(s8, 1, c->eip);
786                         break;
787                 case 2:
788                         c->modrm_ea += insn_fetch(s32, 4, c->eip);
789                         break;
790                 }
791         }
792         if (rip_relative) {
793                 c->modrm_ea += c->eip;
794                 switch (c->d & SrcMask) {
795                 case SrcImmByte:
796                         c->modrm_ea += 1;
797                         break;
798                 case SrcImm:
799                         if (c->d & ByteOp)
800                                 c->modrm_ea += 1;
801                         else
802                                 if (c->op_bytes == 8)
803                                         c->modrm_ea += 4;
804                                 else
805                                         c->modrm_ea += c->op_bytes;
806                 }
807         }
808 done:
809         return rc;
810 }
811
812 static int decode_abs(struct x86_emulate_ctxt *ctxt,
813                       struct x86_emulate_ops *ops)
814 {
815         struct decode_cache *c = &ctxt->decode;
816         int rc = 0;
817
818         switch (c->ad_bytes) {
819         case 2:
820                 c->modrm_ea = insn_fetch(u16, 2, c->eip);
821                 break;
822         case 4:
823                 c->modrm_ea = insn_fetch(u32, 4, c->eip);
824                 break;
825         case 8:
826                 c->modrm_ea = insn_fetch(u64, 8, c->eip);
827                 break;
828         }
829 done:
830         return rc;
831 }
832
833 int
834 x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
835 {
836         struct decode_cache *c = &ctxt->decode;
837         int rc = 0;
838         int mode = ctxt->mode;
839         int def_op_bytes, def_ad_bytes, group;
840
841         /* Shadow copy of register state. Committed on successful emulation. */
842
843         memset(c, 0, sizeof(struct decode_cache));
844         c->eip = ctxt->vcpu->arch.rip;
845         memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
846
847         switch (mode) {
848         case X86EMUL_MODE_REAL:
849         case X86EMUL_MODE_PROT16:
850                 def_op_bytes = def_ad_bytes = 2;
851                 break;
852         case X86EMUL_MODE_PROT32:
853                 def_op_bytes = def_ad_bytes = 4;
854                 break;
855 #ifdef CONFIG_X86_64
856         case X86EMUL_MODE_PROT64:
857                 def_op_bytes = 4;
858                 def_ad_bytes = 8;
859                 break;
860 #endif
861         default:
862                 return -1;
863         }
864
865         c->op_bytes = def_op_bytes;
866         c->ad_bytes = def_ad_bytes;
867
868         /* Legacy prefixes. */
869         for (;;) {
870                 switch (c->b = insn_fetch(u8, 1, c->eip)) {
871                 case 0x66:      /* operand-size override */
872                         /* switch between 2/4 bytes */
873                         c->op_bytes = def_op_bytes ^ 6;
874                         break;
875                 case 0x67:      /* address-size override */
876                         if (mode == X86EMUL_MODE_PROT64)
877                                 /* switch between 4/8 bytes */
878                                 c->ad_bytes = def_ad_bytes ^ 12;
879                         else
880                                 /* switch between 2/4 bytes */
881                                 c->ad_bytes = def_ad_bytes ^ 6;
882                         break;
883                 case 0x2e:      /* CS override */
884                         c->override_base = &ctxt->cs_base;
885                         break;
886                 case 0x3e:      /* DS override */
887                         c->override_base = &ctxt->ds_base;
888                         break;
889                 case 0x26:      /* ES override */
890                         c->override_base = &ctxt->es_base;
891                         break;
892                 case 0x64:      /* FS override */
893                         c->override_base = &ctxt->fs_base;
894                         break;
895                 case 0x65:      /* GS override */
896                         c->override_base = &ctxt->gs_base;
897                         break;
898                 case 0x36:      /* SS override */
899                         c->override_base = &ctxt->ss_base;
900                         break;
901                 case 0x40 ... 0x4f: /* REX */
902                         if (mode != X86EMUL_MODE_PROT64)
903                                 goto done_prefixes;
904                         c->rex_prefix = c->b;
905                         continue;
906                 case 0xf0:      /* LOCK */
907                         c->lock_prefix = 1;
908                         break;
909                 case 0xf2:      /* REPNE/REPNZ */
910                         c->rep_prefix = REPNE_PREFIX;
911                         break;
912                 case 0xf3:      /* REP/REPE/REPZ */
913                         c->rep_prefix = REPE_PREFIX;
914                         break;
915                 default:
916                         goto done_prefixes;
917                 }
918
919                 /* Any legacy prefix after a REX prefix nullifies its effect. */
920
921                 c->rex_prefix = 0;
922         }
923
924 done_prefixes:
925
926         /* REX prefix. */
927         if (c->rex_prefix)
928                 if (c->rex_prefix & 8)
929                         c->op_bytes = 8;        /* REX.W */
930
931         /* Opcode byte(s). */
932         c->d = opcode_table[c->b];
933         if (c->d == 0) {
934                 /* Two-byte opcode? */
935                 if (c->b == 0x0f) {
936                         c->twobyte = 1;
937                         c->b = insn_fetch(u8, 1, c->eip);
938                         c->d = twobyte_table[c->b];
939                 }
940         }
941
942         if (c->d & Group) {
943                 group = c->d & GroupMask;
944                 c->modrm = insn_fetch(u8, 1, c->eip);
945                 --c->eip;
946
947                 group = (group << 3) + ((c->modrm >> 3) & 7);
948                 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
949                         c->d = group2_table[group];
950                 else
951                         c->d = group_table[group];
952         }
953
954         /* Unrecognised? */
955         if (c->d == 0) {
956                 DPRINTF("Cannot emulate %02x\n", c->b);
957                 return -1;
958         }
959
960         if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
961                 c->op_bytes = 8;
962
963         /* ModRM and SIB bytes. */
964         if (c->d & ModRM)
965                 rc = decode_modrm(ctxt, ops);
966         else if (c->d & MemAbs)
967                 rc = decode_abs(ctxt, ops);
968         if (rc)
969                 goto done;
970
971         if (!c->override_base)
972                 c->override_base = &ctxt->ds_base;
973         if (mode == X86EMUL_MODE_PROT64 &&
974             c->override_base != &ctxt->fs_base &&
975             c->override_base != &ctxt->gs_base)
976                 c->override_base = NULL;
977
978         if (c->override_base)
979                 c->modrm_ea += *c->override_base;
980
981         if (c->ad_bytes != 8)
982                 c->modrm_ea = (u32)c->modrm_ea;
983         /*
984          * Decode and fetch the source operand: register, memory
985          * or immediate.
986          */
987         switch (c->d & SrcMask) {
988         case SrcNone:
989                 break;
990         case SrcReg:
991                 decode_register_operand(&c->src, c, 0);
992                 break;
993         case SrcMem16:
994                 c->src.bytes = 2;
995                 goto srcmem_common;
996         case SrcMem32:
997                 c->src.bytes = 4;
998                 goto srcmem_common;
999         case SrcMem:
1000                 c->src.bytes = (c->d & ByteOp) ? 1 :
1001                                                            c->op_bytes;
1002                 /* Don't fetch the address for invlpg: it could be unmapped. */
1003                 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
1004                         break;
1005         srcmem_common:
1006                 /*
1007                  * For instructions with a ModR/M byte, switch to register
1008                  * access if Mod = 3.
1009                  */
1010                 if ((c->d & ModRM) && c->modrm_mod == 3) {
1011                         c->src.type = OP_REG;
1012                         c->src.val = c->modrm_val;
1013                         c->src.ptr = c->modrm_ptr;
1014                         break;
1015                 }
1016                 c->src.type = OP_MEM;
1017                 break;
1018         case SrcImm:
1019                 c->src.type = OP_IMM;
1020                 c->src.ptr = (unsigned long *)c->eip;
1021                 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1022                 if (c->src.bytes == 8)
1023                         c->src.bytes = 4;
1024                 /* NB. Immediates are sign-extended as necessary. */
1025                 switch (c->src.bytes) {
1026                 case 1:
1027                         c->src.val = insn_fetch(s8, 1, c->eip);
1028                         break;
1029                 case 2:
1030                         c->src.val = insn_fetch(s16, 2, c->eip);
1031                         break;
1032                 case 4:
1033                         c->src.val = insn_fetch(s32, 4, c->eip);
1034                         break;
1035                 }
1036                 break;
1037         case SrcImmByte:
1038                 c->src.type = OP_IMM;
1039                 c->src.ptr = (unsigned long *)c->eip;
1040                 c->src.bytes = 1;
1041                 c->src.val = insn_fetch(s8, 1, c->eip);
1042                 break;
1043         }
1044
1045         /* Decode and fetch the destination operand: register or memory. */
1046         switch (c->d & DstMask) {
1047         case ImplicitOps:
1048                 /* Special instructions do their own operand decoding. */
1049                 return 0;
1050         case DstReg:
1051                 decode_register_operand(&c->dst, c,
1052                          c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
1053                 break;
1054         case DstMem:
1055                 if ((c->d & ModRM) && c->modrm_mod == 3) {
1056                         c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1057                         c->dst.type = OP_REG;
1058                         c->dst.val = c->dst.orig_val = c->modrm_val;
1059                         c->dst.ptr = c->modrm_ptr;
1060                         break;
1061                 }
1062                 c->dst.type = OP_MEM;
1063                 break;
1064         }
1065
1066 done:
1067         return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1068 }
1069
1070 static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1071 {
1072         struct decode_cache *c = &ctxt->decode;
1073
1074         c->dst.type  = OP_MEM;
1075         c->dst.bytes = c->op_bytes;
1076         c->dst.val = c->src.val;
1077         register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
1078         c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
1079                                                c->regs[VCPU_REGS_RSP]);
1080 }
1081
1082 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1083                                 struct x86_emulate_ops *ops)
1084 {
1085         struct decode_cache *c = &ctxt->decode;
1086         int rc;
1087
1088         rc = ops->read_std(register_address(c, ctxt->ss_base,
1089                                             c->regs[VCPU_REGS_RSP]),
1090                            &c->dst.val, c->dst.bytes, ctxt->vcpu);
1091         if (rc != 0)
1092                 return rc;
1093
1094         register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
1095
1096         return 0;
1097 }
1098
1099 static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
1100 {
1101         struct decode_cache *c = &ctxt->decode;
1102         switch (c->modrm_reg) {
1103         case 0: /* rol */
1104                 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
1105                 break;
1106         case 1: /* ror */
1107                 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
1108                 break;
1109         case 2: /* rcl */
1110                 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
1111                 break;
1112         case 3: /* rcr */
1113                 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
1114                 break;
1115         case 4: /* sal/shl */
1116         case 6: /* sal/shl */
1117                 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
1118                 break;
1119         case 5: /* shr */
1120                 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
1121                 break;
1122         case 7: /* sar */
1123                 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
1124                 break;
1125         }
1126 }
1127
1128 static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
1129                                struct x86_emulate_ops *ops)
1130 {
1131         struct decode_cache *c = &ctxt->decode;
1132         int rc = 0;
1133
1134         switch (c->modrm_reg) {
1135         case 0 ... 1:   /* test */
1136                 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1137                 break;
1138         case 2: /* not */
1139                 c->dst.val = ~c->dst.val;
1140                 break;
1141         case 3: /* neg */
1142                 emulate_1op("neg", c->dst, ctxt->eflags);
1143                 break;
1144         default:
1145                 DPRINTF("Cannot emulate %02x\n", c->b);
1146                 rc = X86EMUL_UNHANDLEABLE;
1147                 break;
1148         }
1149         return rc;
1150 }
1151
1152 static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
1153                                struct x86_emulate_ops *ops)
1154 {
1155         struct decode_cache *c = &ctxt->decode;
1156
1157         switch (c->modrm_reg) {
1158         case 0: /* inc */
1159                 emulate_1op("inc", c->dst, ctxt->eflags);
1160                 break;
1161         case 1: /* dec */
1162                 emulate_1op("dec", c->dst, ctxt->eflags);
1163                 break;
1164         case 4: /* jmp abs */
1165                 c->eip = c->src.val;
1166                 break;
1167         case 6: /* push */
1168                 emulate_push(ctxt);
1169                 break;
1170         }
1171         return 0;
1172 }
1173
1174 static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1175                                struct x86_emulate_ops *ops,
1176                                unsigned long memop)
1177 {
1178         struct decode_cache *c = &ctxt->decode;
1179         u64 old, new;
1180         int rc;
1181
1182         rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
1183         if (rc != 0)
1184                 return rc;
1185
1186         if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1187             ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1188
1189                 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1190                 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1191                 ctxt->eflags &= ~EFLG_ZF;
1192
1193         } else {
1194                 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1195                        (u32) c->regs[VCPU_REGS_RBX];
1196
1197                 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
1198                 if (rc != 0)
1199                         return rc;
1200                 ctxt->eflags |= EFLG_ZF;
1201         }
1202         return 0;
1203 }
1204
1205 static inline int writeback(struct x86_emulate_ctxt *ctxt,
1206                             struct x86_emulate_ops *ops)
1207 {
1208         int rc;
1209         struct decode_cache *c = &ctxt->decode;
1210
1211         switch (c->dst.type) {
1212         case OP_REG:
1213                 /* The 4-byte case *is* correct:
1214                  * in 64-bit mode we zero-extend.
1215                  */
1216                 switch (c->dst.bytes) {
1217                 case 1:
1218                         *(u8 *)c->dst.ptr = (u8)c->dst.val;
1219                         break;
1220                 case 2:
1221                         *(u16 *)c->dst.ptr = (u16)c->dst.val;
1222                         break;
1223                 case 4:
1224                         *c->dst.ptr = (u32)c->dst.val;
1225                         break;  /* 64b: zero-ext */
1226                 case 8:
1227                         *c->dst.ptr = c->dst.val;
1228                         break;
1229                 }
1230                 break;
1231         case OP_MEM:
1232                 if (c->lock_prefix)
1233                         rc = ops->cmpxchg_emulated(
1234                                         (unsigned long)c->dst.ptr,
1235                                         &c->dst.orig_val,
1236                                         &c->dst.val,
1237                                         c->dst.bytes,
1238                                         ctxt->vcpu);
1239                 else
1240                         rc = ops->write_emulated(
1241                                         (unsigned long)c->dst.ptr,
1242                                         &c->dst.val,
1243                                         c->dst.bytes,
1244                                         ctxt->vcpu);
1245                 if (rc != 0)
1246                         return rc;
1247                 break;
1248         case OP_NONE:
1249                 /* no writeback */
1250                 break;
1251         default:
1252                 break;
1253         }
1254         return 0;
1255 }
1256
1257 int
1258 x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1259 {
1260         unsigned long memop = 0;
1261         u64 msr_data;
1262         unsigned long saved_eip = 0;
1263         struct decode_cache *c = &ctxt->decode;
1264         int rc = 0;
1265
1266         /* Shadow copy of register state. Committed on successful emulation.
1267          * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1268          * modify them.
1269          */
1270
1271         memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
1272         saved_eip = c->eip;
1273
1274         if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
1275                 memop = c->modrm_ea;
1276
1277         if (c->rep_prefix && (c->d & String)) {
1278                 /* All REP prefixes have the same first termination condition */
1279                 if (c->regs[VCPU_REGS_RCX] == 0) {
1280                         ctxt->vcpu->arch.rip = c->eip;
1281                         goto done;
1282                 }
1283                 /* The second termination condition only applies for REPE
1284                  * and REPNE. Test if the repeat string operation prefix is
1285                  * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1286                  * corresponding termination condition according to:
1287                  *      - if REPE/REPZ and ZF = 0 then done
1288                  *      - if REPNE/REPNZ and ZF = 1 then done
1289                  */
1290                 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1291                                 (c->b == 0xae) || (c->b == 0xaf)) {
1292                         if ((c->rep_prefix == REPE_PREFIX) &&
1293                                 ((ctxt->eflags & EFLG_ZF) == 0)) {
1294                                         ctxt->vcpu->arch.rip = c->eip;
1295                                         goto done;
1296                         }
1297                         if ((c->rep_prefix == REPNE_PREFIX) &&
1298                                 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
1299                                 ctxt->vcpu->arch.rip = c->eip;
1300                                 goto done;
1301                         }
1302                 }
1303                 c->regs[VCPU_REGS_RCX]--;
1304                 c->eip = ctxt->vcpu->arch.rip;
1305         }
1306
1307         if (c->src.type == OP_MEM) {
1308                 c->src.ptr = (unsigned long *)memop;
1309                 c->src.val = 0;
1310                 rc = ops->read_emulated((unsigned long)c->src.ptr,
1311                                         &c->src.val,
1312                                         c->src.bytes,
1313                                         ctxt->vcpu);
1314                 if (rc != 0)
1315                         goto done;
1316                 c->src.orig_val = c->src.val;
1317         }
1318
1319         if ((c->d & DstMask) == ImplicitOps)
1320                 goto special_insn;
1321
1322
1323         if (c->dst.type == OP_MEM) {
1324                 c->dst.ptr = (unsigned long *)memop;
1325                 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1326                 c->dst.val = 0;
1327                 if (c->d & BitOp) {
1328                         unsigned long mask = ~(c->dst.bytes * 8 - 1);
1329
1330                         c->dst.ptr = (void *)c->dst.ptr +
1331                                                    (c->src.val & mask) / 8;
1332                 }
1333                 if (!(c->d & Mov) &&
1334                                    /* optimisation - avoid slow emulated read */
1335                     ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1336                                            &c->dst.val,
1337                                           c->dst.bytes, ctxt->vcpu)) != 0))
1338                         goto done;
1339         }
1340         c->dst.orig_val = c->dst.val;
1341
1342 special_insn:
1343
1344         if (c->twobyte)
1345                 goto twobyte_insn;
1346
1347         switch (c->b) {
1348         case 0x00 ... 0x05:
1349               add:              /* add */
1350                 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
1351                 break;
1352         case 0x08 ... 0x0d:
1353               or:               /* or */
1354                 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
1355                 break;
1356         case 0x10 ... 0x15:
1357               adc:              /* adc */
1358                 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
1359                 break;
1360         case 0x18 ... 0x1d:
1361               sbb:              /* sbb */
1362                 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
1363                 break;
1364         case 0x20 ... 0x23:
1365               and:              /* and */
1366                 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
1367                 break;
1368         case 0x24:              /* and al imm8 */
1369                 c->dst.type = OP_REG;
1370                 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1371                 c->dst.val = *(u8 *)c->dst.ptr;
1372                 c->dst.bytes = 1;
1373                 c->dst.orig_val = c->dst.val;
1374                 goto and;
1375         case 0x25:              /* and ax imm16, or eax imm32 */
1376                 c->dst.type = OP_REG;
1377                 c->dst.bytes = c->op_bytes;
1378                 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1379                 if (c->op_bytes == 2)
1380                         c->dst.val = *(u16 *)c->dst.ptr;
1381                 else
1382                         c->dst.val = *(u32 *)c->dst.ptr;
1383                 c->dst.orig_val = c->dst.val;
1384                 goto and;
1385         case 0x28 ... 0x2d:
1386               sub:              /* sub */
1387                 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
1388                 break;
1389         case 0x30 ... 0x35:
1390               xor:              /* xor */
1391                 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
1392                 break;
1393         case 0x38 ... 0x3d:
1394               cmp:              /* cmp */
1395                 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1396                 break;
1397         case 0x40 ... 0x47: /* inc r16/r32 */
1398                 emulate_1op("inc", c->dst, ctxt->eflags);
1399                 break;
1400         case 0x48 ... 0x4f: /* dec r16/r32 */
1401                 emulate_1op("dec", c->dst, ctxt->eflags);
1402                 break;
1403         case 0x50 ... 0x57:  /* push reg */
1404                 c->dst.type  = OP_MEM;
1405                 c->dst.bytes = c->op_bytes;
1406                 c->dst.val = c->src.val;
1407                 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1408                                            -c->op_bytes);
1409                 c->dst.ptr = (void *) register_address(
1410                         c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1411                 break;
1412         case 0x58 ... 0x5f: /* pop reg */
1413         pop_instruction:
1414                 if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
1415                         c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1416                         c->op_bytes, ctxt->vcpu)) != 0)
1417                         goto done;
1418
1419                 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1420                                            c->op_bytes);
1421                 c->dst.type = OP_NONE;  /* Disable writeback. */
1422                 break;
1423         case 0x63:              /* movsxd */
1424                 if (ctxt->mode != X86EMUL_MODE_PROT64)
1425                         goto cannot_emulate;
1426                 c->dst.val = (s32) c->src.val;
1427                 break;
1428         case 0x68: /* push imm */
1429         case 0x6a: /* push imm8 */
1430                 emulate_push(ctxt);
1431                 break;
1432         case 0x6c:              /* insb */
1433         case 0x6d:              /* insw/insd */
1434                  if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1435                                 1,
1436                                 (c->d & ByteOp) ? 1 : c->op_bytes,
1437                                 c->rep_prefix ?
1438                                 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
1439                                 (ctxt->eflags & EFLG_DF),
1440                                 register_address(c, ctxt->es_base,
1441                                                  c->regs[VCPU_REGS_RDI]),
1442                                 c->rep_prefix,
1443                                 c->regs[VCPU_REGS_RDX]) == 0) {
1444                         c->eip = saved_eip;
1445                         return -1;
1446                 }
1447                 return 0;
1448         case 0x6e:              /* outsb */
1449         case 0x6f:              /* outsw/outsd */
1450                 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1451                                 0,
1452                                 (c->d & ByteOp) ? 1 : c->op_bytes,
1453                                 c->rep_prefix ?
1454                                 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
1455                                 (ctxt->eflags & EFLG_DF),
1456                                 register_address(c, c->override_base ?
1457                                                         *c->override_base :
1458                                                         ctxt->ds_base,
1459                                                  c->regs[VCPU_REGS_RSI]),
1460                                 c->rep_prefix,
1461                                 c->regs[VCPU_REGS_RDX]) == 0) {
1462                         c->eip = saved_eip;
1463                         return -1;
1464                 }
1465                 return 0;
1466         case 0x70 ... 0x7f: /* jcc (short) */ {
1467                 int rel = insn_fetch(s8, 1, c->eip);
1468
1469                 if (test_cc(c->b, ctxt->eflags))
1470                         jmp_rel(c, rel);
1471                 break;
1472         }
1473         case 0x80 ... 0x83:     /* Grp1 */
1474                 switch (c->modrm_reg) {
1475                 case 0:
1476                         goto add;
1477                 case 1:
1478                         goto or;
1479                 case 2:
1480                         goto adc;
1481                 case 3:
1482                         goto sbb;
1483                 case 4:
1484                         goto and;
1485                 case 5:
1486                         goto sub;
1487                 case 6:
1488                         goto xor;
1489                 case 7:
1490                         goto cmp;
1491                 }
1492                 break;
1493         case 0x84 ... 0x85:
1494                 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1495                 break;
1496         case 0x86 ... 0x87:     /* xchg */
1497         xchg:
1498                 /* Write back the register source. */
1499                 switch (c->dst.bytes) {
1500                 case 1:
1501                         *(u8 *) c->src.ptr = (u8) c->dst.val;
1502                         break;
1503                 case 2:
1504                         *(u16 *) c->src.ptr = (u16) c->dst.val;
1505                         break;
1506                 case 4:
1507                         *c->src.ptr = (u32) c->dst.val;
1508                         break;  /* 64b reg: zero-extend */
1509                 case 8:
1510                         *c->src.ptr = c->dst.val;
1511                         break;
1512                 }
1513                 /*
1514                  * Write back the memory destination with implicit LOCK
1515                  * prefix.
1516                  */
1517                 c->dst.val = c->src.val;
1518                 c->lock_prefix = 1;
1519                 break;
1520         case 0x88 ... 0x8b:     /* mov */
1521                 goto mov;
1522         case 0x8c: { /* mov r/m, sreg */
1523                 struct kvm_segment segreg;
1524
1525                 if (c->modrm_reg <= 5)
1526                         kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1527                 else {
1528                         printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1529                                c->modrm);
1530                         goto cannot_emulate;
1531                 }
1532                 c->dst.val = segreg.selector;
1533                 break;
1534         }
1535         case 0x8d: /* lea r16/r32, m */
1536                 c->dst.val = c->modrm_ea;
1537                 break;
1538         case 0x8e: { /* mov seg, r/m16 */
1539                 uint16_t sel;
1540                 int type_bits;
1541                 int err;
1542
1543                 sel = c->src.val;
1544                 if (c->modrm_reg <= 5) {
1545                         type_bits = (c->modrm_reg == 1) ? 9 : 1;
1546                         err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1547                                                           type_bits, c->modrm_reg);
1548                 } else {
1549                         printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1550                                         c->modrm);
1551                         goto cannot_emulate;
1552                 }
1553
1554                 if (err < 0)
1555                         goto cannot_emulate;
1556
1557                 c->dst.type = OP_NONE;  /* Disable writeback. */
1558                 break;
1559         }
1560         case 0x8f:              /* pop (sole member of Grp1a) */
1561                 rc = emulate_grp1a(ctxt, ops);
1562                 if (rc != 0)
1563                         goto done;
1564                 break;
1565         case 0x90: /* nop / xchg r8,rax */
1566                 if (!(c->rex_prefix & 1)) { /* nop */
1567                         c->dst.type = OP_NONE;
1568                         break;
1569                 }
1570         case 0x91 ... 0x97: /* xchg reg,rax */
1571                 c->src.type = c->dst.type = OP_REG;
1572                 c->src.bytes = c->dst.bytes = c->op_bytes;
1573                 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1574                 c->src.val = *(c->src.ptr);
1575                 goto xchg;
1576         case 0x9c: /* pushf */
1577                 c->src.val =  (unsigned long) ctxt->eflags;
1578                 emulate_push(ctxt);
1579                 break;
1580         case 0x9d: /* popf */
1581                 c->dst.ptr = (unsigned long *) &ctxt->eflags;
1582                 goto pop_instruction;
1583         case 0xa0 ... 0xa1:     /* mov */
1584                 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1585                 c->dst.val = c->src.val;
1586                 break;
1587         case 0xa2 ... 0xa3:     /* mov */
1588                 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1589                 break;
1590         case 0xa4 ... 0xa5:     /* movs */
1591                 c->dst.type = OP_MEM;
1592                 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1593                 c->dst.ptr = (unsigned long *)register_address(c,
1594                                                    ctxt->es_base,
1595                                                    c->regs[VCPU_REGS_RDI]);
1596                 if ((rc = ops->read_emulated(register_address(c,
1597                       c->override_base ? *c->override_base :
1598                                         ctxt->ds_base,
1599                                         c->regs[VCPU_REGS_RSI]),
1600                                         &c->dst.val,
1601                                         c->dst.bytes, ctxt->vcpu)) != 0)
1602                         goto done;
1603                 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
1604                                        (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1605                                                            : c->dst.bytes);
1606                 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
1607                                        (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1608                                                            : c->dst.bytes);
1609                 break;
1610         case 0xa6 ... 0xa7:     /* cmps */
1611                 c->src.type = OP_NONE; /* Disable writeback. */
1612                 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1613                 c->src.ptr = (unsigned long *)register_address(c,
1614                                 c->override_base ? *c->override_base :
1615                                                    ctxt->ds_base,
1616                                                    c->regs[VCPU_REGS_RSI]);
1617                 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1618                                                 &c->src.val,
1619                                                 c->src.bytes,
1620                                                 ctxt->vcpu)) != 0)
1621                         goto done;
1622
1623                 c->dst.type = OP_NONE; /* Disable writeback. */
1624                 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1625                 c->dst.ptr = (unsigned long *)register_address(c,
1626                                                    ctxt->es_base,
1627                                                    c->regs[VCPU_REGS_RDI]);
1628                 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1629                                                 &c->dst.val,
1630                                                 c->dst.bytes,
1631                                                 ctxt->vcpu)) != 0)
1632                         goto done;
1633
1634                 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1635
1636                 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1637
1638                 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
1639                                        (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1640                                                                   : c->src.bytes);
1641                 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
1642                                        (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1643                                                                   : c->dst.bytes);
1644
1645                 break;
1646         case 0xaa ... 0xab:     /* stos */
1647                 c->dst.type = OP_MEM;
1648                 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1649                 c->dst.ptr = (unsigned long *)register_address(c,
1650                                                    ctxt->es_base,
1651                                                    c->regs[VCPU_REGS_RDI]);
1652                 c->dst.val = c->regs[VCPU_REGS_RAX];
1653                 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
1654                                        (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1655                                                            : c->dst.bytes);
1656                 break;
1657         case 0xac ... 0xad:     /* lods */
1658                 c->dst.type = OP_REG;
1659                 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1660                 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1661                 if ((rc = ops->read_emulated(register_address(c,
1662                                 c->override_base ? *c->override_base :
1663                                                    ctxt->ds_base,
1664                                                  c->regs[VCPU_REGS_RSI]),
1665                                                  &c->dst.val,
1666                                                  c->dst.bytes,
1667                                                  ctxt->vcpu)) != 0)
1668                         goto done;
1669                 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
1670                                        (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1671                                                            : c->dst.bytes);
1672                 break;
1673         case 0xae ... 0xaf:     /* scas */
1674                 DPRINTF("Urk! I don't handle SCAS.\n");
1675                 goto cannot_emulate;
1676         case 0xb8: /* mov r, imm */
1677                 goto mov;
1678         case 0xc0 ... 0xc1:
1679                 emulate_grp2(ctxt);
1680                 break;
1681         case 0xc3: /* ret */
1682                 c->dst.ptr = &c->eip;
1683                 goto pop_instruction;
1684         case 0xc6 ... 0xc7:     /* mov (sole member of Grp11) */
1685         mov:
1686                 c->dst.val = c->src.val;
1687                 break;
1688         case 0xd0 ... 0xd1:     /* Grp2 */
1689                 c->src.val = 1;
1690                 emulate_grp2(ctxt);
1691                 break;
1692         case 0xd2 ... 0xd3:     /* Grp2 */
1693                 c->src.val = c->regs[VCPU_REGS_RCX];
1694                 emulate_grp2(ctxt);
1695                 break;
1696         case 0xe8: /* call (near) */ {
1697                 long int rel;
1698                 switch (c->op_bytes) {
1699                 case 2:
1700                         rel = insn_fetch(s16, 2, c->eip);
1701                         break;
1702                 case 4:
1703                         rel = insn_fetch(s32, 4, c->eip);
1704                         break;
1705                 default:
1706                         DPRINTF("Call: Invalid op_bytes\n");
1707                         goto cannot_emulate;
1708                 }
1709                 c->src.val = (unsigned long) c->eip;
1710                 jmp_rel(c, rel);
1711                 c->op_bytes = c->ad_bytes;
1712                 emulate_push(ctxt);
1713                 break;
1714         }
1715         case 0xe9: /* jmp rel */
1716                 goto jmp;
1717         case 0xea: /* jmp far */ {
1718                 uint32_t eip;
1719                 uint16_t sel;
1720
1721                 switch (c->op_bytes) {
1722                 case 2:
1723                         eip = insn_fetch(u16, 2, c->eip);
1724                         break;
1725                 case 4:
1726                         eip = insn_fetch(u32, 4, c->eip);
1727                         break;
1728                 default:
1729                         DPRINTF("jmp far: Invalid op_bytes\n");
1730                         goto cannot_emulate;
1731                 }
1732                 sel = insn_fetch(u16, 2, c->eip);
1733                 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1734                         DPRINTF("jmp far: Failed to load CS descriptor\n");
1735                         goto cannot_emulate;
1736                 }
1737
1738                 c->eip = eip;
1739                 break;
1740         }
1741         case 0xeb:
1742               jmp:              /* jmp rel short */
1743                 jmp_rel(c, c->src.val);
1744                 c->dst.type = OP_NONE; /* Disable writeback. */
1745                 break;
1746         case 0xf4:              /* hlt */
1747                 ctxt->vcpu->arch.halt_request = 1;
1748                 goto done;
1749         case 0xf5:      /* cmc */
1750                 /* complement carry flag from eflags reg */
1751                 ctxt->eflags ^= EFLG_CF;
1752                 c->dst.type = OP_NONE;  /* Disable writeback. */
1753                 break;
1754         case 0xf6 ... 0xf7:     /* Grp3 */
1755                 rc = emulate_grp3(ctxt, ops);
1756                 if (rc != 0)
1757                         goto done;
1758                 break;
1759         case 0xf8: /* clc */
1760                 ctxt->eflags &= ~EFLG_CF;
1761                 c->dst.type = OP_NONE;  /* Disable writeback. */
1762                 break;
1763         case 0xfa: /* cli */
1764                 ctxt->eflags &= ~X86_EFLAGS_IF;
1765                 c->dst.type = OP_NONE;  /* Disable writeback. */
1766                 break;
1767         case 0xfb: /* sti */
1768                 ctxt->eflags |= X86_EFLAGS_IF;
1769                 c->dst.type = OP_NONE;  /* Disable writeback. */
1770                 break;
1771         case 0xfe ... 0xff:     /* Grp4/Grp5 */
1772                 rc = emulate_grp45(ctxt, ops);
1773                 if (rc != 0)
1774                         goto done;
1775                 break;
1776         }
1777
1778 writeback:
1779         rc = writeback(ctxt, ops);
1780         if (rc != 0)
1781                 goto done;
1782
1783         /* Commit shadow register state. */
1784         memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
1785         ctxt->vcpu->arch.rip = c->eip;
1786
1787 done:
1788         if (rc == X86EMUL_UNHANDLEABLE) {
1789                 c->eip = saved_eip;
1790                 return -1;
1791         }
1792         return 0;
1793
1794 twobyte_insn:
1795         switch (c->b) {
1796         case 0x01: /* lgdt, lidt, lmsw */
1797                 switch (c->modrm_reg) {
1798                         u16 size;
1799                         unsigned long address;
1800
1801                 case 0: /* vmcall */
1802                         if (c->modrm_mod != 3 || c->modrm_rm != 1)
1803                                 goto cannot_emulate;
1804
1805                         rc = kvm_fix_hypercall(ctxt->vcpu);
1806                         if (rc)
1807                                 goto done;
1808
1809                         /* Let the processor re-execute the fixed hypercall */
1810                         c->eip = ctxt->vcpu->arch.rip;
1811                         /* Disable writeback. */
1812                         c->dst.type = OP_NONE;
1813                         break;
1814                 case 2: /* lgdt */
1815                         rc = read_descriptor(ctxt, ops, c->src.ptr,
1816                                              &size, &address, c->op_bytes);
1817                         if (rc)
1818                                 goto done;
1819                         realmode_lgdt(ctxt->vcpu, size, address);
1820                         /* Disable writeback. */
1821                         c->dst.type = OP_NONE;
1822                         break;
1823                 case 3: /* lidt/vmmcall */
1824                         if (c->modrm_mod == 3 && c->modrm_rm == 1) {
1825                                 rc = kvm_fix_hypercall(ctxt->vcpu);
1826                                 if (rc)
1827                                         goto done;
1828                                 kvm_emulate_hypercall(ctxt->vcpu);
1829                         } else {
1830                                 rc = read_descriptor(ctxt, ops, c->src.ptr,
1831                                                      &size, &address,
1832                                                      c->op_bytes);
1833                                 if (rc)
1834                                         goto done;
1835                                 realmode_lidt(ctxt->vcpu, size, address);
1836                         }
1837                         /* Disable writeback. */
1838                         c->dst.type = OP_NONE;
1839                         break;
1840                 case 4: /* smsw */
1841                         c->dst.bytes = 2;
1842                         c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
1843                         break;
1844                 case 6: /* lmsw */
1845                         realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1846                                       &ctxt->eflags);
1847                         c->dst.type = OP_NONE;
1848                         break;
1849                 case 7: /* invlpg*/
1850                         emulate_invlpg(ctxt->vcpu, memop);
1851                         /* Disable writeback. */
1852                         c->dst.type = OP_NONE;
1853                         break;
1854                 default:
1855                         goto cannot_emulate;
1856                 }
1857                 break;
1858         case 0x06:
1859                 emulate_clts(ctxt->vcpu);
1860                 c->dst.type = OP_NONE;
1861                 break;
1862         case 0x08:              /* invd */
1863         case 0x09:              /* wbinvd */
1864         case 0x0d:              /* GrpP (prefetch) */
1865         case 0x18:              /* Grp16 (prefetch/nop) */
1866                 c->dst.type = OP_NONE;
1867                 break;
1868         case 0x20: /* mov cr, reg */
1869                 if (c->modrm_mod != 3)
1870                         goto cannot_emulate;
1871                 c->regs[c->modrm_rm] =
1872                                 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1873                 c->dst.type = OP_NONE;  /* no writeback */
1874                 break;
1875         case 0x21: /* mov from dr to reg */
1876                 if (c->modrm_mod != 3)
1877                         goto cannot_emulate;
1878                 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
1879                 if (rc)
1880                         goto cannot_emulate;
1881                 c->dst.type = OP_NONE;  /* no writeback */
1882                 break;
1883         case 0x22: /* mov reg, cr */
1884                 if (c->modrm_mod != 3)
1885                         goto cannot_emulate;
1886                 realmode_set_cr(ctxt->vcpu,
1887                                 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1888                 c->dst.type = OP_NONE;
1889                 break;
1890         case 0x23: /* mov from reg to dr */
1891                 if (c->modrm_mod != 3)
1892                         goto cannot_emulate;
1893                 rc = emulator_set_dr(ctxt, c->modrm_reg,
1894                                      c->regs[c->modrm_rm]);
1895                 if (rc)
1896                         goto cannot_emulate;
1897                 c->dst.type = OP_NONE;  /* no writeback */
1898                 break;
1899         case 0x30:
1900                 /* wrmsr */
1901                 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1902                         | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1903                 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1904                 if (rc) {
1905                         kvm_inject_gp(ctxt->vcpu, 0);
1906                         c->eip = ctxt->vcpu->arch.rip;
1907                 }
1908                 rc = X86EMUL_CONTINUE;
1909                 c->dst.type = OP_NONE;
1910                 break;
1911         case 0x32:
1912                 /* rdmsr */
1913                 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1914                 if (rc) {
1915                         kvm_inject_gp(ctxt->vcpu, 0);
1916                         c->eip = ctxt->vcpu->arch.rip;
1917                 } else {
1918                         c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1919                         c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1920                 }
1921                 rc = X86EMUL_CONTINUE;
1922                 c->dst.type = OP_NONE;
1923                 break;
1924         case 0x40 ... 0x4f:     /* cmov */
1925                 c->dst.val = c->dst.orig_val = c->src.val;
1926                 if (!test_cc(c->b, ctxt->eflags))
1927                         c->dst.type = OP_NONE; /* no writeback */
1928                 break;
1929         case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1930                 long int rel;
1931
1932                 switch (c->op_bytes) {
1933                 case 2:
1934                         rel = insn_fetch(s16, 2, c->eip);
1935                         break;
1936                 case 4:
1937                         rel = insn_fetch(s32, 4, c->eip);
1938                         break;
1939                 case 8:
1940                         rel = insn_fetch(s64, 8, c->eip);
1941                         break;
1942                 default:
1943                         DPRINTF("jnz: Invalid op_bytes\n");
1944                         goto cannot_emulate;
1945                 }
1946                 if (test_cc(c->b, ctxt->eflags))
1947                         jmp_rel(c, rel);
1948                 c->dst.type = OP_NONE;
1949                 break;
1950         }
1951         case 0xa3:
1952               bt:               /* bt */
1953                 c->dst.type = OP_NONE;
1954                 /* only subword offset */
1955                 c->src.val &= (c->dst.bytes << 3) - 1;
1956                 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
1957                 break;
1958         case 0xab:
1959               bts:              /* bts */
1960                 /* only subword offset */
1961                 c->src.val &= (c->dst.bytes << 3) - 1;
1962                 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
1963                 break;
1964         case 0xb0 ... 0xb1:     /* cmpxchg */
1965                 /*
1966                  * Save real source value, then compare EAX against
1967                  * destination.
1968                  */
1969                 c->src.orig_val = c->src.val;
1970                 c->src.val = c->regs[VCPU_REGS_RAX];
1971                 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1972                 if (ctxt->eflags & EFLG_ZF) {
1973                         /* Success: write back to memory. */
1974                         c->dst.val = c->src.orig_val;
1975                 } else {
1976                         /* Failure: write the value we saw to EAX. */
1977                         c->dst.type = OP_REG;
1978                         c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1979                 }
1980                 break;
1981         case 0xb3:
1982               btr:              /* btr */
1983                 /* only subword offset */
1984                 c->src.val &= (c->dst.bytes << 3) - 1;
1985                 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
1986                 break;
1987         case 0xb6 ... 0xb7:     /* movzx */
1988                 c->dst.bytes = c->op_bytes;
1989                 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1990                                                        : (u16) c->src.val;
1991                 break;
1992         case 0xba:              /* Grp8 */
1993                 switch (c->modrm_reg & 3) {
1994                 case 0:
1995                         goto bt;
1996                 case 1:
1997                         goto bts;
1998                 case 2:
1999                         goto btr;
2000                 case 3:
2001                         goto btc;
2002                 }
2003                 break;
2004         case 0xbb:
2005               btc:              /* btc */
2006                 /* only subword offset */
2007                 c->src.val &= (c->dst.bytes << 3) - 1;
2008                 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
2009                 break;
2010         case 0xbe ... 0xbf:     /* movsx */
2011                 c->dst.bytes = c->op_bytes;
2012                 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2013                                                         (s16) c->src.val;
2014                 break;
2015         case 0xc3:              /* movnti */
2016                 c->dst.bytes = c->op_bytes;
2017                 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2018                                                         (u64) c->src.val;
2019                 break;
2020         case 0xc7:              /* Grp9 (cmpxchg8b) */
2021                 rc = emulate_grp9(ctxt, ops, memop);
2022                 if (rc != 0)
2023                         goto done;
2024                 c->dst.type = OP_NONE;
2025                 break;
2026         }
2027         goto writeback;
2028
2029 cannot_emulate:
2030         DPRINTF("Cannot emulate %02x\n", c->b);
2031         c->eip = saved_eip;
2032         return -1;
2033 }