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