1 /******************************************************************************
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
6 * Copyright (c) 2005 Keir Fraser
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
9 * privileged instructions:
11 * Copyright (C) 2006 Qumranet
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
25 #include <public/xen.h>
26 #define DPRINTF(_f, _a ...) printf(_f , ## _a)
30 #define DPRINTF(x...) do {} while (0)
32 #include "x86_emulate.h"
33 #include <linux/module.h>
36 * Opcode effective-address decode tables.
37 * Note that we only emulate instructions that have at least one memory
38 * operand (excluding implicit stack references). We assume that stack
39 * references and instruction fetches will never occur in special memory
40 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
44 /* Operand sizes: 8-bit operands or specified/overridden size. */
45 #define ByteOp (1<<0) /* 8-bit operands. */
46 /* Destination operand type. */
47 #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
48 #define DstReg (2<<1) /* Register operand. */
49 #define DstMem (3<<1) /* Memory operand. */
50 #define DstMask (3<<1)
51 /* Source operand type. */
52 #define SrcNone (0<<3) /* No source operand. */
53 #define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
54 #define SrcReg (1<<3) /* Register operand. */
55 #define SrcMem (2<<3) /* Memory operand. */
56 #define SrcMem16 (3<<3) /* Memory operand (16-bit). */
57 #define SrcMem32 (4<<3) /* Memory operand (32-bit). */
58 #define SrcImm (5<<3) /* Immediate operand. */
59 #define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
60 #define SrcMask (7<<3)
61 /* Generic ModRM decode. */
63 /* Destination is only written; never read. */
66 #define MemAbs (1<<9) /* Memory operand is absolute displacement */
67 #define String (1<<10) /* String instruction (rep capable) */
69 static u16 opcode_table[256] = {
71 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
72 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
75 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
76 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
79 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
80 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
83 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
84 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
87 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
88 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
89 SrcImmByte, SrcImm, 0, 0,
91 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
99 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
103 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
105 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
107 SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg, SrcReg,
109 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
111 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
114 0, 0, ImplicitOps|Mov, 0,
115 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
116 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
118 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
119 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
121 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
122 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
124 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
125 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
126 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
127 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
129 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
130 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
131 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
135 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
136 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
137 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
138 ByteOp | ImplicitOps | String, ImplicitOps | String,
140 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
141 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
142 ByteOp | ImplicitOps | String, ImplicitOps | String,
144 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
147 0, ImplicitOps, 0, 0,
148 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
150 0, 0, 0, 0, 0, 0, 0, 0,
152 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
153 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
156 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0,
160 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
163 ImplicitOps, ImplicitOps,
164 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
166 ImplicitOps, 0, ImplicitOps, ImplicitOps,
167 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
170 static u16 twobyte_table[256] = {
172 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
173 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
177 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
178 0, 0, 0, 0, 0, 0, 0, 0,
180 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
183 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
184 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
185 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
188 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
189 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
190 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
192 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
199 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
200 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
201 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
207 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
209 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
210 DstMem | SrcReg | ModRM | BitOp,
211 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
212 DstReg | SrcMem16 | ModRM | Mov,
214 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
215 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
216 DstReg | SrcMem16 | ModRM | Mov,
218 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
219 0, 0, 0, 0, 0, 0, 0, 0,
221 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
228 /* EFLAGS bit definitions. */
229 #define EFLG_OF (1<<11)
230 #define EFLG_DF (1<<10)
231 #define EFLG_SF (1<<7)
232 #define EFLG_ZF (1<<6)
233 #define EFLG_AF (1<<4)
234 #define EFLG_PF (1<<2)
235 #define EFLG_CF (1<<0)
238 * Instruction emulation:
239 * Most instructions are emulated directly via a fragment of inline assembly
240 * code. This allows us to save/restore EFLAGS and thus very easily pick up
241 * any modified flags.
244 #if defined(CONFIG_X86_64)
245 #define _LO32 "k" /* force 32-bit operand */
246 #define _STK "%%rsp" /* stack pointer */
247 #elif defined(__i386__)
248 #define _LO32 "" /* force 32-bit operand */
249 #define _STK "%%esp" /* stack pointer */
253 * These EFLAGS bits are restored from saved value during emulation, and
254 * any changes are written back to the saved value after emulation.
256 #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
258 /* Before executing instruction: restore necessary bits in EFLAGS. */
259 #define _PRE_EFLAGS(_sav, _msk, _tmp) \
260 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
261 "movl %"_sav",%"_LO32 _tmp"; " \
264 "movl %"_msk",%"_LO32 _tmp"; " \
265 "andl %"_LO32 _tmp",("_STK"); " \
267 "notl %"_LO32 _tmp"; " \
268 "andl %"_LO32 _tmp",("_STK"); " \
269 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
271 "orl %"_LO32 _tmp",("_STK"); " \
275 /* After executing instruction: write-back necessary bits in EFLAGS. */
276 #define _POST_EFLAGS(_sav, _msk, _tmp) \
277 /* _sav |= EFLAGS & _msk; */ \
280 "andl %"_msk",%"_LO32 _tmp"; " \
281 "orl %"_LO32 _tmp",%"_sav"; "
283 /* Raw emulation: instruction has two explicit operands. */
284 #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
286 unsigned long _tmp; \
288 switch ((_dst).bytes) { \
290 __asm__ __volatile__ ( \
291 _PRE_EFLAGS("0", "4", "2") \
292 _op"w %"_wx"3,%1; " \
293 _POST_EFLAGS("0", "4", "2") \
294 : "=m" (_eflags), "=m" ((_dst).val), \
296 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
299 __asm__ __volatile__ ( \
300 _PRE_EFLAGS("0", "4", "2") \
301 _op"l %"_lx"3,%1; " \
302 _POST_EFLAGS("0", "4", "2") \
303 : "=m" (_eflags), "=m" ((_dst).val), \
305 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
308 __emulate_2op_8byte(_op, _src, _dst, \
309 _eflags, _qx, _qy); \
314 #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
316 unsigned long _tmp; \
317 switch ((_dst).bytes) { \
319 __asm__ __volatile__ ( \
320 _PRE_EFLAGS("0", "4", "2") \
321 _op"b %"_bx"3,%1; " \
322 _POST_EFLAGS("0", "4", "2") \
323 : "=m" (_eflags), "=m" ((_dst).val), \
325 : _by ((_src).val), "i" (EFLAGS_MASK)); \
328 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
329 _wx, _wy, _lx, _ly, _qx, _qy); \
334 /* Source operand is byte-sized and may be restricted to just %cl. */
335 #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
336 __emulate_2op(_op, _src, _dst, _eflags, \
337 "b", "c", "b", "c", "b", "c", "b", "c")
339 /* Source operand is byte, word, long or quad sized. */
340 #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
341 __emulate_2op(_op, _src, _dst, _eflags, \
342 "b", "q", "w", "r", _LO32, "r", "", "r")
344 /* Source operand is word, long or quad sized. */
345 #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
346 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
347 "w", "r", _LO32, "r", "", "r")
349 /* Instruction has only one explicit operand (no source operand). */
350 #define emulate_1op(_op, _dst, _eflags) \
352 unsigned long _tmp; \
354 switch ((_dst).bytes) { \
356 __asm__ __volatile__ ( \
357 _PRE_EFLAGS("0", "3", "2") \
359 _POST_EFLAGS("0", "3", "2") \
360 : "=m" (_eflags), "=m" ((_dst).val), \
362 : "i" (EFLAGS_MASK)); \
365 __asm__ __volatile__ ( \
366 _PRE_EFLAGS("0", "3", "2") \
368 _POST_EFLAGS("0", "3", "2") \
369 : "=m" (_eflags), "=m" ((_dst).val), \
371 : "i" (EFLAGS_MASK)); \
374 __asm__ __volatile__ ( \
375 _PRE_EFLAGS("0", "3", "2") \
377 _POST_EFLAGS("0", "3", "2") \
378 : "=m" (_eflags), "=m" ((_dst).val), \
380 : "i" (EFLAGS_MASK)); \
383 __emulate_1op_8byte(_op, _dst, _eflags); \
388 /* Emulate an instruction with quadword operands (x86/64 only). */
389 #if defined(CONFIG_X86_64)
390 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "4", "2") \
394 _op"q %"_qx"3,%1; " \
395 _POST_EFLAGS("0", "4", "2") \
396 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
397 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
400 #define __emulate_1op_8byte(_op, _dst, _eflags) \
402 __asm__ __volatile__ ( \
403 _PRE_EFLAGS("0", "3", "2") \
405 _POST_EFLAGS("0", "3", "2") \
406 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
407 : "i" (EFLAGS_MASK)); \
410 #elif defined(__i386__)
411 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
412 #define __emulate_1op_8byte(_op, _dst, _eflags)
413 #endif /* __i386__ */
415 /* Fetch next part of the instruction being emulated. */
416 #define insn_fetch(_type, _size, _eip) \
417 ({ unsigned long _x; \
418 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
425 /* Access/update address held in a register, based on addressing mode. */
426 #define address_mask(reg) \
427 ((c->ad_bytes == sizeof(unsigned long)) ? \
428 (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1)))
429 #define register_address(base, reg) \
430 ((base) + address_mask(reg))
431 #define register_address_increment(reg, inc) \
433 /* signed type ensures sign extension to long */ \
435 if (c->ad_bytes == sizeof(unsigned long)) \
439 ~((1UL << (c->ad_bytes << 3)) - 1)) | \
441 ((1UL << (c->ad_bytes << 3)) - 1)); \
444 #define JMP_REL(rel) \
446 register_address_increment(c->eip, rel); \
449 static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
450 struct x86_emulate_ops *ops,
451 unsigned long linear, u8 *dest)
453 struct fetch_cache *fc = &ctxt->decode.fetch;
457 if (linear < fc->start || linear >= fc->end) {
458 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
459 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
463 fc->end = linear + size;
465 *dest = fc->data[linear - fc->start];
469 static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
470 struct x86_emulate_ops *ops,
471 unsigned long eip, void *dest, unsigned size)
475 eip += ctxt->cs_base;
477 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
485 * Given the 'reg' portion of a ModRM byte, and a register block, return a
486 * pointer into the block that addresses the relevant register.
487 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
489 static void *decode_register(u8 modrm_reg, unsigned long *regs,
494 p = ®s[modrm_reg];
495 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
496 p = (unsigned char *)®s[modrm_reg & 3] + 1;
500 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
501 struct x86_emulate_ops *ops,
503 u16 *size, unsigned long *address, int op_bytes)
510 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
514 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
519 static int test_cc(unsigned int condition, unsigned int flags)
523 switch ((condition & 15) >> 1) {
525 rc |= (flags & EFLG_OF);
527 case 1: /* b/c/nae */
528 rc |= (flags & EFLG_CF);
531 rc |= (flags & EFLG_ZF);
534 rc |= (flags & (EFLG_CF|EFLG_ZF));
537 rc |= (flags & EFLG_SF);
540 rc |= (flags & EFLG_PF);
543 rc |= (flags & EFLG_ZF);
546 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
550 /* Odd condition identifiers (lsb == 1) have inverted sense. */
551 return (!!rc ^ (condition & 1));
554 static void decode_register_operand(struct operand *op,
555 struct decode_cache *c,
558 unsigned reg = c->modrm_reg;
559 int highbyte_regs = c->rex_prefix == 0;
562 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
564 if ((c->d & ByteOp) && !inhibit_bytereg) {
565 op->ptr = decode_register(reg, c->regs, highbyte_regs);
566 op->val = *(u8 *)op->ptr;
569 op->ptr = decode_register(reg, c->regs, 0);
570 op->bytes = c->op_bytes;
573 op->val = *(u16 *)op->ptr;
576 op->val = *(u32 *)op->ptr;
579 op->val = *(u64 *) op->ptr;
583 op->orig_val = op->val;
586 static int decode_modrm(struct x86_emulate_ctxt *ctxt,
587 struct x86_emulate_ops *ops)
589 struct decode_cache *c = &ctxt->decode;
591 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
595 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
596 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
597 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
600 c->modrm = insn_fetch(u8, 1, c->eip);
601 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
602 c->modrm_reg |= (c->modrm & 0x38) >> 3;
603 c->modrm_rm |= (c->modrm & 0x07);
607 if (c->modrm_mod == 3) {
608 c->modrm_val = *(unsigned long *)
609 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
613 if (c->ad_bytes == 2) {
614 unsigned bx = c->regs[VCPU_REGS_RBX];
615 unsigned bp = c->regs[VCPU_REGS_RBP];
616 unsigned si = c->regs[VCPU_REGS_RSI];
617 unsigned di = c->regs[VCPU_REGS_RDI];
619 /* 16-bit ModR/M decode. */
620 switch (c->modrm_mod) {
622 if (c->modrm_rm == 6)
623 c->modrm_ea += insn_fetch(u16, 2, c->eip);
626 c->modrm_ea += insn_fetch(s8, 1, c->eip);
629 c->modrm_ea += insn_fetch(u16, 2, c->eip);
632 switch (c->modrm_rm) {
634 c->modrm_ea += bx + si;
637 c->modrm_ea += bx + di;
640 c->modrm_ea += bp + si;
643 c->modrm_ea += bp + di;
652 if (c->modrm_mod != 0)
659 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
660 (c->modrm_rm == 6 && c->modrm_mod != 0))
661 if (!c->override_base)
662 c->override_base = &ctxt->ss_base;
663 c->modrm_ea = (u16)c->modrm_ea;
665 /* 32/64-bit ModR/M decode. */
666 switch (c->modrm_rm) {
669 sib = insn_fetch(u8, 1, c->eip);
670 index_reg |= (sib >> 3) & 7;
676 if (c->modrm_mod != 0)
677 c->modrm_ea += c->regs[base_reg];
680 insn_fetch(s32, 4, c->eip);
683 c->modrm_ea += c->regs[base_reg];
689 c->modrm_ea += c->regs[index_reg] << scale;
693 if (c->modrm_mod != 0)
694 c->modrm_ea += c->regs[c->modrm_rm];
695 else if (ctxt->mode == X86EMUL_MODE_PROT64)
699 c->modrm_ea += c->regs[c->modrm_rm];
702 switch (c->modrm_mod) {
704 if (c->modrm_rm == 5)
705 c->modrm_ea += insn_fetch(s32, 4, c->eip);
708 c->modrm_ea += insn_fetch(s8, 1, c->eip);
711 c->modrm_ea += insn_fetch(s32, 4, c->eip);
716 c->modrm_ea += c->eip;
717 switch (c->d & SrcMask) {
725 if (c->op_bytes == 8)
728 c->modrm_ea += c->op_bytes;
735 static int decode_abs(struct x86_emulate_ctxt *ctxt,
736 struct x86_emulate_ops *ops)
738 struct decode_cache *c = &ctxt->decode;
741 switch (c->ad_bytes) {
743 c->modrm_ea = insn_fetch(u16, 2, c->eip);
746 c->modrm_ea = insn_fetch(u32, 4, c->eip);
749 c->modrm_ea = insn_fetch(u64, 8, c->eip);
757 x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
759 struct decode_cache *c = &ctxt->decode;
761 int mode = ctxt->mode;
762 int def_op_bytes, def_ad_bytes;
764 /* Shadow copy of register state. Committed on successful emulation. */
766 memset(c, 0, sizeof(struct decode_cache));
767 c->eip = ctxt->vcpu->rip;
768 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
771 case X86EMUL_MODE_REAL:
772 case X86EMUL_MODE_PROT16:
773 def_op_bytes = def_ad_bytes = 2;
775 case X86EMUL_MODE_PROT32:
776 def_op_bytes = def_ad_bytes = 4;
779 case X86EMUL_MODE_PROT64:
788 c->op_bytes = def_op_bytes;
789 c->ad_bytes = def_ad_bytes;
791 /* Legacy prefixes. */
793 switch (c->b = insn_fetch(u8, 1, c->eip)) {
794 case 0x66: /* operand-size override */
795 /* switch between 2/4 bytes */
796 c->op_bytes = def_op_bytes ^ 6;
798 case 0x67: /* address-size override */
799 if (mode == X86EMUL_MODE_PROT64)
800 /* switch between 4/8 bytes */
801 c->ad_bytes = def_ad_bytes ^ 12;
803 /* switch between 2/4 bytes */
804 c->ad_bytes = def_ad_bytes ^ 6;
806 case 0x2e: /* CS override */
807 c->override_base = &ctxt->cs_base;
809 case 0x3e: /* DS override */
810 c->override_base = &ctxt->ds_base;
812 case 0x26: /* ES override */
813 c->override_base = &ctxt->es_base;
815 case 0x64: /* FS override */
816 c->override_base = &ctxt->fs_base;
818 case 0x65: /* GS override */
819 c->override_base = &ctxt->gs_base;
821 case 0x36: /* SS override */
822 c->override_base = &ctxt->ss_base;
824 case 0x40 ... 0x4f: /* REX */
825 if (mode != X86EMUL_MODE_PROT64)
827 c->rex_prefix = c->b;
829 case 0xf0: /* LOCK */
832 case 0xf2: /* REPNE/REPNZ */
833 c->rep_prefix = REPNE_PREFIX;
835 case 0xf3: /* REP/REPE/REPZ */
836 c->rep_prefix = REPE_PREFIX;
842 /* Any legacy prefix after a REX prefix nullifies its effect. */
851 if (c->rex_prefix & 8)
852 c->op_bytes = 8; /* REX.W */
854 /* Opcode byte(s). */
855 c->d = opcode_table[c->b];
857 /* Two-byte opcode? */
860 c->b = insn_fetch(u8, 1, c->eip);
861 c->d = twobyte_table[c->b];
866 DPRINTF("Cannot emulate %02x\n", c->b);
871 /* ModRM and SIB bytes. */
873 rc = decode_modrm(ctxt, ops);
874 else if (c->d & MemAbs)
875 rc = decode_abs(ctxt, ops);
879 if (!c->override_base)
880 c->override_base = &ctxt->ds_base;
881 if (mode == X86EMUL_MODE_PROT64 &&
882 c->override_base != &ctxt->fs_base &&
883 c->override_base != &ctxt->gs_base)
884 c->override_base = NULL;
886 if (c->override_base)
887 c->modrm_ea += *c->override_base;
889 if (c->ad_bytes != 8)
890 c->modrm_ea = (u32)c->modrm_ea;
892 * Decode and fetch the source operand: register, memory
895 switch (c->d & SrcMask) {
899 decode_register_operand(&c->src, c, 0);
908 c->src.bytes = (c->d & ByteOp) ? 1 :
910 /* Don't fetch the address for invlpg: it could be unmapped. */
911 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
915 * For instructions with a ModR/M byte, switch to register
918 if ((c->d & ModRM) && c->modrm_mod == 3) {
919 c->src.type = OP_REG;
922 c->src.type = OP_MEM;
925 c->src.type = OP_IMM;
926 c->src.ptr = (unsigned long *)c->eip;
927 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
928 if (c->src.bytes == 8)
930 /* NB. Immediates are sign-extended as necessary. */
931 switch (c->src.bytes) {
933 c->src.val = insn_fetch(s8, 1, c->eip);
936 c->src.val = insn_fetch(s16, 2, c->eip);
939 c->src.val = insn_fetch(s32, 4, c->eip);
944 c->src.type = OP_IMM;
945 c->src.ptr = (unsigned long *)c->eip;
947 c->src.val = insn_fetch(s8, 1, c->eip);
951 /* Decode and fetch the destination operand: register or memory. */
952 switch (c->d & DstMask) {
954 /* Special instructions do their own operand decoding. */
957 decode_register_operand(&c->dst, c,
958 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
961 if ((c->d & ModRM) && c->modrm_mod == 3) {
962 c->dst.type = OP_REG;
965 c->dst.type = OP_MEM;
970 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
973 static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
975 struct decode_cache *c = &ctxt->decode;
977 c->dst.type = OP_MEM;
978 c->dst.bytes = c->op_bytes;
979 c->dst.val = c->src.val;
980 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
981 c->dst.ptr = (void *) register_address(ctxt->ss_base,
982 c->regs[VCPU_REGS_RSP]);
985 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
986 struct x86_emulate_ops *ops)
988 struct decode_cache *c = &ctxt->decode;
991 /* 64-bit mode: POP always pops a 64-bit operand. */
993 if (ctxt->mode == X86EMUL_MODE_PROT64)
996 rc = ops->read_std(register_address(ctxt->ss_base,
997 c->regs[VCPU_REGS_RSP]),
998 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1002 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
1007 static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
1009 struct decode_cache *c = &ctxt->decode;
1010 switch (c->modrm_reg) {
1012 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
1015 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
1018 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
1021 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
1023 case 4: /* sal/shl */
1024 case 6: /* sal/shl */
1025 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
1028 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
1031 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
1036 static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
1037 struct x86_emulate_ops *ops)
1039 struct decode_cache *c = &ctxt->decode;
1042 switch (c->modrm_reg) {
1043 case 0 ... 1: /* test */
1045 * Special case in Grp3: test has an immediate
1048 c->src.type = OP_IMM;
1049 c->src.ptr = (unsigned long *)c->eip;
1050 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1051 if (c->src.bytes == 8)
1053 switch (c->src.bytes) {
1055 c->src.val = insn_fetch(s8, 1, c->eip);
1058 c->src.val = insn_fetch(s16, 2, c->eip);
1061 c->src.val = insn_fetch(s32, 4, c->eip);
1064 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1067 c->dst.val = ~c->dst.val;
1070 emulate_1op("neg", c->dst, ctxt->eflags);
1073 DPRINTF("Cannot emulate %02x\n", c->b);
1074 rc = X86EMUL_UNHANDLEABLE;
1081 static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
1082 struct x86_emulate_ops *ops)
1084 struct decode_cache *c = &ctxt->decode;
1087 switch (c->modrm_reg) {
1089 emulate_1op("inc", c->dst, ctxt->eflags);
1092 emulate_1op("dec", c->dst, ctxt->eflags);
1094 case 4: /* jmp abs */
1096 c->eip = c->dst.val;
1098 DPRINTF("Cannot emulate %02x\n", c->b);
1099 return X86EMUL_UNHANDLEABLE;
1104 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1106 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1108 rc = ops->read_std((unsigned long)c->dst.ptr,
1109 &c->dst.val, 8, ctxt->vcpu);
1113 register_address_increment(c->regs[VCPU_REGS_RSP],
1115 rc = ops->write_emulated(register_address(ctxt->ss_base,
1116 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1117 c->dst.bytes, ctxt->vcpu);
1120 c->dst.type = OP_NONE;
1123 DPRINTF("Cannot emulate %02x\n", c->b);
1124 return X86EMUL_UNHANDLEABLE;
1129 static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1130 struct x86_emulate_ops *ops,
1131 unsigned long memop)
1133 struct decode_cache *c = &ctxt->decode;
1137 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
1141 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1142 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1144 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1145 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1146 ctxt->eflags &= ~EFLG_ZF;
1149 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1150 (u32) c->regs[VCPU_REGS_RBX];
1152 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
1155 ctxt->eflags |= EFLG_ZF;
1160 static inline int writeback(struct x86_emulate_ctxt *ctxt,
1161 struct x86_emulate_ops *ops)
1164 struct decode_cache *c = &ctxt->decode;
1166 switch (c->dst.type) {
1168 /* The 4-byte case *is* correct:
1169 * in 64-bit mode we zero-extend.
1171 switch (c->dst.bytes) {
1173 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1176 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1179 *c->dst.ptr = (u32)c->dst.val;
1180 break; /* 64b: zero-ext */
1182 *c->dst.ptr = c->dst.val;
1188 rc = ops->cmpxchg_emulated(
1189 (unsigned long)c->dst.ptr,
1195 rc = ops->write_emulated(
1196 (unsigned long)c->dst.ptr,
1213 x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1215 unsigned long memop = 0;
1217 unsigned long saved_eip = 0;
1218 struct decode_cache *c = &ctxt->decode;
1221 /* Shadow copy of register state. Committed on successful emulation.
1222 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1226 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1229 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
1230 memop = c->modrm_ea;
1232 if (c->rep_prefix && (c->d & String)) {
1233 /* All REP prefixes have the same first termination condition */
1234 if (c->regs[VCPU_REGS_RCX] == 0) {
1235 ctxt->vcpu->rip = c->eip;
1238 /* The second termination condition only applies for REPE
1239 * and REPNE. Test if the repeat string operation prefix is
1240 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1241 * corresponding termination condition according to:
1242 * - if REPE/REPZ and ZF = 0 then done
1243 * - if REPNE/REPNZ and ZF = 1 then done
1245 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1246 (c->b == 0xae) || (c->b == 0xaf)) {
1247 if ((c->rep_prefix == REPE_PREFIX) &&
1248 ((ctxt->eflags & EFLG_ZF) == 0)) {
1249 ctxt->vcpu->rip = c->eip;
1252 if ((c->rep_prefix == REPNE_PREFIX) &&
1253 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
1254 ctxt->vcpu->rip = c->eip;
1258 c->regs[VCPU_REGS_RCX]--;
1259 c->eip = ctxt->vcpu->rip;
1262 if (c->src.type == OP_MEM) {
1263 c->src.ptr = (unsigned long *)memop;
1265 rc = ops->read_emulated((unsigned long)c->src.ptr,
1271 c->src.orig_val = c->src.val;
1274 if ((c->d & DstMask) == ImplicitOps)
1278 if (c->dst.type == OP_MEM) {
1279 c->dst.ptr = (unsigned long *)memop;
1280 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1283 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1285 c->dst.ptr = (void *)c->dst.ptr +
1286 (c->src.val & mask) / 8;
1288 if (!(c->d & Mov) &&
1289 /* optimisation - avoid slow emulated read */
1290 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1292 c->dst.bytes, ctxt->vcpu)) != 0))
1295 c->dst.orig_val = c->dst.val;
1305 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
1309 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
1313 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
1317 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
1321 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
1323 case 0x24: /* and al imm8 */
1324 c->dst.type = OP_REG;
1325 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1326 c->dst.val = *(u8 *)c->dst.ptr;
1328 c->dst.orig_val = c->dst.val;
1330 case 0x25: /* and ax imm16, or eax imm32 */
1331 c->dst.type = OP_REG;
1332 c->dst.bytes = c->op_bytes;
1333 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1334 if (c->op_bytes == 2)
1335 c->dst.val = *(u16 *)c->dst.ptr;
1337 c->dst.val = *(u32 *)c->dst.ptr;
1338 c->dst.orig_val = c->dst.val;
1342 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
1346 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
1350 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1352 case 0x40 ... 0x47: /* inc r16/r32 */
1353 emulate_1op("inc", c->dst, ctxt->eflags);
1355 case 0x48 ... 0x4f: /* dec r16/r32 */
1356 emulate_1op("dec", c->dst, ctxt->eflags);
1358 case 0x50 ... 0x57: /* push reg */
1359 c->dst.type = OP_MEM;
1360 c->dst.bytes = c->op_bytes;
1361 c->dst.val = c->src.val;
1362 register_address_increment(c->regs[VCPU_REGS_RSP],
1364 c->dst.ptr = (void *) register_address(
1365 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1367 case 0x58 ... 0x5f: /* pop reg */
1369 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1370 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1371 c->op_bytes, ctxt->vcpu)) != 0)
1374 register_address_increment(c->regs[VCPU_REGS_RSP],
1376 c->dst.type = OP_NONE; /* Disable writeback. */
1378 case 0x63: /* movsxd */
1379 if (ctxt->mode != X86EMUL_MODE_PROT64)
1380 goto cannot_emulate;
1381 c->dst.val = (s32) c->src.val;
1383 case 0x6a: /* push imm8 */
1385 c->src.val = insn_fetch(s8, 1, c->eip);
1388 case 0x6c: /* insb */
1389 case 0x6d: /* insw/insd */
1390 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1392 (c->d & ByteOp) ? 1 : c->op_bytes,
1394 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1395 (ctxt->eflags & EFLG_DF),
1396 register_address(ctxt->es_base,
1397 c->regs[VCPU_REGS_RDI]),
1399 c->regs[VCPU_REGS_RDX]) == 0) {
1404 case 0x6e: /* outsb */
1405 case 0x6f: /* outsw/outsd */
1406 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1408 (c->d & ByteOp) ? 1 : c->op_bytes,
1410 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1411 (ctxt->eflags & EFLG_DF),
1412 register_address(c->override_base ?
1415 c->regs[VCPU_REGS_RSI]),
1417 c->regs[VCPU_REGS_RDX]) == 0) {
1422 case 0x70 ... 0x7f: /* jcc (short) */ {
1423 int rel = insn_fetch(s8, 1, c->eip);
1425 if (test_cc(c->b, ctxt->eflags))
1429 case 0x80 ... 0x83: /* Grp1 */
1430 switch (c->modrm_reg) {
1450 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1452 case 0x86 ... 0x87: /* xchg */
1453 /* Write back the register source. */
1454 switch (c->dst.bytes) {
1456 *(u8 *) c->src.ptr = (u8) c->dst.val;
1459 *(u16 *) c->src.ptr = (u16) c->dst.val;
1462 *c->src.ptr = (u32) c->dst.val;
1463 break; /* 64b reg: zero-extend */
1465 *c->src.ptr = c->dst.val;
1469 * Write back the memory destination with implicit LOCK
1472 c->dst.val = c->src.val;
1475 case 0x88 ... 0x8b: /* mov */
1477 case 0x8d: /* lea r16/r32, m */
1478 c->dst.val = c->modrm_val;
1480 case 0x8f: /* pop (sole member of Grp1a) */
1481 rc = emulate_grp1a(ctxt, ops);
1485 case 0x9c: /* pushf */
1486 c->src.val = (unsigned long) ctxt->eflags;
1489 case 0x9d: /* popf */
1490 c->dst.ptr = (unsigned long *) &ctxt->eflags;
1491 goto pop_instruction;
1492 case 0xa0 ... 0xa1: /* mov */
1493 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1494 c->dst.val = c->src.val;
1496 case 0xa2 ... 0xa3: /* mov */
1497 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1499 case 0xa4 ... 0xa5: /* movs */
1500 c->dst.type = OP_MEM;
1501 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1502 c->dst.ptr = (unsigned long *)register_address(
1504 c->regs[VCPU_REGS_RDI]);
1505 if ((rc = ops->read_emulated(register_address(
1506 c->override_base ? *c->override_base :
1508 c->regs[VCPU_REGS_RSI]),
1510 c->dst.bytes, ctxt->vcpu)) != 0)
1512 register_address_increment(c->regs[VCPU_REGS_RSI],
1513 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1515 register_address_increment(c->regs[VCPU_REGS_RDI],
1516 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1519 case 0xa6 ... 0xa7: /* cmps */
1520 c->src.type = OP_NONE; /* Disable writeback. */
1521 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1522 c->src.ptr = (unsigned long *)register_address(
1523 c->override_base ? *c->override_base :
1525 c->regs[VCPU_REGS_RSI]);
1526 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1532 c->dst.type = OP_NONE; /* Disable writeback. */
1533 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1534 c->dst.ptr = (unsigned long *)register_address(
1536 c->regs[VCPU_REGS_RDI]);
1537 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1543 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1545 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1547 register_address_increment(c->regs[VCPU_REGS_RSI],
1548 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1550 register_address_increment(c->regs[VCPU_REGS_RDI],
1551 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1555 case 0xaa ... 0xab: /* stos */
1556 c->dst.type = OP_MEM;
1557 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1558 c->dst.ptr = (unsigned long *)register_address(
1560 c->regs[VCPU_REGS_RDI]);
1561 c->dst.val = c->regs[VCPU_REGS_RAX];
1562 register_address_increment(c->regs[VCPU_REGS_RDI],
1563 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1566 case 0xac ... 0xad: /* lods */
1567 c->dst.type = OP_REG;
1568 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1569 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1570 if ((rc = ops->read_emulated(register_address(
1571 c->override_base ? *c->override_base :
1573 c->regs[VCPU_REGS_RSI]),
1578 register_address_increment(c->regs[VCPU_REGS_RSI],
1579 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1582 case 0xae ... 0xaf: /* scas */
1583 DPRINTF("Urk! I don't handle SCAS.\n");
1584 goto cannot_emulate;
1588 case 0xc3: /* ret */
1589 c->dst.ptr = &c->eip;
1590 goto pop_instruction;
1591 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1593 c->dst.val = c->src.val;
1595 case 0xd0 ... 0xd1: /* Grp2 */
1599 case 0xd2 ... 0xd3: /* Grp2 */
1600 c->src.val = c->regs[VCPU_REGS_RCX];
1603 case 0xe8: /* call (near) */ {
1605 switch (c->op_bytes) {
1607 rel = insn_fetch(s16, 2, c->eip);
1610 rel = insn_fetch(s32, 4, c->eip);
1613 DPRINTF("Call: Invalid op_bytes\n");
1614 goto cannot_emulate;
1616 c->src.val = (unsigned long) c->eip;
1618 c->op_bytes = c->ad_bytes;
1622 case 0xe9: /* jmp rel */
1623 case 0xeb: /* jmp rel short */
1624 JMP_REL(c->src.val);
1625 c->dst.type = OP_NONE; /* Disable writeback. */
1627 case 0xf4: /* hlt */
1628 ctxt->vcpu->halt_request = 1;
1630 case 0xf5: /* cmc */
1631 /* complement carry flag from eflags reg */
1632 ctxt->eflags ^= EFLG_CF;
1633 c->dst.type = OP_NONE; /* Disable writeback. */
1635 case 0xf6 ... 0xf7: /* Grp3 */
1636 rc = emulate_grp3(ctxt, ops);
1640 case 0xf8: /* clc */
1641 ctxt->eflags &= ~EFLG_CF;
1642 c->dst.type = OP_NONE; /* Disable writeback. */
1644 case 0xfa: /* cli */
1645 ctxt->eflags &= ~X86_EFLAGS_IF;
1646 c->dst.type = OP_NONE; /* Disable writeback. */
1648 case 0xfb: /* sti */
1649 ctxt->eflags |= X86_EFLAGS_IF;
1650 c->dst.type = OP_NONE; /* Disable writeback. */
1652 case 0xfe ... 0xff: /* Grp4/Grp5 */
1653 rc = emulate_grp45(ctxt, ops);
1660 rc = writeback(ctxt, ops);
1664 /* Commit shadow register state. */
1665 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
1666 ctxt->vcpu->rip = c->eip;
1669 if (rc == X86EMUL_UNHANDLEABLE) {
1677 case 0x01: /* lgdt, lidt, lmsw */
1678 switch (c->modrm_reg) {
1680 unsigned long address;
1682 case 0: /* vmcall */
1683 if (c->modrm_mod != 3 || c->modrm_rm != 1)
1684 goto cannot_emulate;
1686 rc = kvm_fix_hypercall(ctxt->vcpu);
1690 kvm_emulate_hypercall(ctxt->vcpu);
1693 rc = read_descriptor(ctxt, ops, c->src.ptr,
1694 &size, &address, c->op_bytes);
1697 realmode_lgdt(ctxt->vcpu, size, address);
1699 case 3: /* lidt/vmmcall */
1700 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
1701 rc = kvm_fix_hypercall(ctxt->vcpu);
1704 kvm_emulate_hypercall(ctxt->vcpu);
1706 rc = read_descriptor(ctxt, ops, c->src.ptr,
1711 realmode_lidt(ctxt->vcpu, size, address);
1715 if (c->modrm_mod != 3)
1716 goto cannot_emulate;
1717 *(u16 *)&c->regs[c->modrm_rm]
1718 = realmode_get_cr(ctxt->vcpu, 0);
1721 if (c->modrm_mod != 3)
1722 goto cannot_emulate;
1723 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1727 emulate_invlpg(ctxt->vcpu, memop);
1730 goto cannot_emulate;
1732 /* Disable writeback. */
1733 c->dst.type = OP_NONE;
1736 emulate_clts(ctxt->vcpu);
1737 c->dst.type = OP_NONE;
1739 case 0x08: /* invd */
1740 case 0x09: /* wbinvd */
1741 case 0x0d: /* GrpP (prefetch) */
1742 case 0x18: /* Grp16 (prefetch/nop) */
1743 c->dst.type = OP_NONE;
1745 case 0x20: /* mov cr, reg */
1746 if (c->modrm_mod != 3)
1747 goto cannot_emulate;
1748 c->regs[c->modrm_rm] =
1749 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1750 c->dst.type = OP_NONE; /* no writeback */
1752 case 0x21: /* mov from dr to reg */
1753 if (c->modrm_mod != 3)
1754 goto cannot_emulate;
1755 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
1757 goto cannot_emulate;
1758 c->dst.type = OP_NONE; /* no writeback */
1760 case 0x22: /* mov reg, cr */
1761 if (c->modrm_mod != 3)
1762 goto cannot_emulate;
1763 realmode_set_cr(ctxt->vcpu,
1764 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1765 c->dst.type = OP_NONE;
1767 case 0x23: /* mov from reg to dr */
1768 if (c->modrm_mod != 3)
1769 goto cannot_emulate;
1770 rc = emulator_set_dr(ctxt, c->modrm_reg,
1771 c->regs[c->modrm_rm]);
1773 goto cannot_emulate;
1774 c->dst.type = OP_NONE; /* no writeback */
1778 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1779 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1780 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1782 kvm_inject_gp(ctxt->vcpu, 0);
1783 c->eip = ctxt->vcpu->rip;
1785 rc = X86EMUL_CONTINUE;
1786 c->dst.type = OP_NONE;
1790 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1792 kvm_inject_gp(ctxt->vcpu, 0);
1793 c->eip = ctxt->vcpu->rip;
1795 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1796 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1798 rc = X86EMUL_CONTINUE;
1799 c->dst.type = OP_NONE;
1801 case 0x40 ... 0x4f: /* cmov */
1802 c->dst.val = c->dst.orig_val = c->src.val;
1803 if (!test_cc(c->b, ctxt->eflags))
1804 c->dst.type = OP_NONE; /* no writeback */
1806 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1809 switch (c->op_bytes) {
1811 rel = insn_fetch(s16, 2, c->eip);
1814 rel = insn_fetch(s32, 4, c->eip);
1817 rel = insn_fetch(s64, 8, c->eip);
1820 DPRINTF("jnz: Invalid op_bytes\n");
1821 goto cannot_emulate;
1823 if (test_cc(c->b, ctxt->eflags))
1825 c->dst.type = OP_NONE;
1830 c->dst.type = OP_NONE;
1831 /* only subword offset */
1832 c->src.val &= (c->dst.bytes << 3) - 1;
1833 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
1837 /* only subword offset */
1838 c->src.val &= (c->dst.bytes << 3) - 1;
1839 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
1841 case 0xb0 ... 0xb1: /* cmpxchg */
1843 * Save real source value, then compare EAX against
1846 c->src.orig_val = c->src.val;
1847 c->src.val = c->regs[VCPU_REGS_RAX];
1848 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1849 if (ctxt->eflags & EFLG_ZF) {
1850 /* Success: write back to memory. */
1851 c->dst.val = c->src.orig_val;
1853 /* Failure: write the value we saw to EAX. */
1854 c->dst.type = OP_REG;
1855 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1860 /* only subword offset */
1861 c->src.val &= (c->dst.bytes << 3) - 1;
1862 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
1864 case 0xb6 ... 0xb7: /* movzx */
1865 c->dst.bytes = c->op_bytes;
1866 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1869 case 0xba: /* Grp8 */
1870 switch (c->modrm_reg & 3) {
1883 /* only subword offset */
1884 c->src.val &= (c->dst.bytes << 3) - 1;
1885 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
1887 case 0xbe ... 0xbf: /* movsx */
1888 c->dst.bytes = c->op_bytes;
1889 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1892 case 0xc3: /* movnti */
1893 c->dst.bytes = c->op_bytes;
1894 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1897 case 0xc7: /* Grp9 (cmpxchg8b) */
1898 rc = emulate_grp9(ctxt, ops, memop);
1901 c->dst.type = OP_NONE;
1907 DPRINTF("Cannot emulate %02x\n", c->b);