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 )
29 #define DPRINTF(x...) do {} while (0)
31 #include "x86_emulate.h"
32 #include <linux/module.h>
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
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. */
62 /* Destination is only written; never read. */
66 static u8 opcode_table[256] = {
68 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
69 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
72 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
73 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
76 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
77 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
86 SrcImmByte, SrcImm, 0, 0,
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
103 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
105 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
106 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
108 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
111 0, 0, ImplicitOps|Mov, 0,
112 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
113 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
115 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
116 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
118 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
119 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
121 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
122 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
123 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
124 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
126 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
127 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
128 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov,
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0,
132 ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
133 ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
134 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
135 ByteOp | ImplicitOps, ImplicitOps,
137 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
138 ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
139 ByteOp | ImplicitOps, ImplicitOps,
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
144 0, ImplicitOps, 0, 0,
145 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
147 0, 0, 0, 0, 0, 0, 0, 0,
149 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
150 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
153 0, 0, 0, 0, 0, 0, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0,
157 ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
161 ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
164 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
167 static u16 twobyte_table[256] = {
169 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
170 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
174 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0,
177 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
180 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
181 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
182 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
184 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
185 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
186 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
187 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
196 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
197 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
198 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
204 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
206 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
207 DstMem | SrcReg | ModRM | BitOp,
208 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
209 DstReg | SrcMem16 | ModRM | Mov,
211 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
212 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
213 DstReg | SrcMem16 | ModRM | Mov,
215 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
216 0, 0, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
225 /* EFLAGS bit definitions. */
226 #define EFLG_OF (1<<11)
227 #define EFLG_DF (1<<10)
228 #define EFLG_SF (1<<7)
229 #define EFLG_ZF (1<<6)
230 #define EFLG_AF (1<<4)
231 #define EFLG_PF (1<<2)
232 #define EFLG_CF (1<<0)
235 * Instruction emulation:
236 * Most instructions are emulated directly via a fragment of inline assembly
237 * code. This allows us to save/restore EFLAGS and thus very easily pick up
238 * any modified flags.
241 #if defined(CONFIG_X86_64)
242 #define _LO32 "k" /* force 32-bit operand */
243 #define _STK "%%rsp" /* stack pointer */
244 #elif defined(__i386__)
245 #define _LO32 "" /* force 32-bit operand */
246 #define _STK "%%esp" /* stack pointer */
250 * These EFLAGS bits are restored from saved value during emulation, and
251 * any changes are written back to the saved value after emulation.
253 #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
255 /* Before executing instruction: restore necessary bits in EFLAGS. */
256 #define _PRE_EFLAGS(_sav, _msk, _tmp) \
257 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
259 "movl %"_msk",%"_LO32 _tmp"; " \
260 "andl %"_LO32 _tmp",("_STK"); " \
262 "notl %"_LO32 _tmp"; " \
263 "andl %"_LO32 _tmp",("_STK"); " \
265 "orl %"_LO32 _tmp",("_STK"); " \
267 /* _sav &= ~msk; */ \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "notl %"_LO32 _tmp"; " \
270 "andl %"_LO32 _tmp",%"_sav"; "
272 /* After executing instruction: write-back necessary bits in EFLAGS. */
273 #define _POST_EFLAGS(_sav, _msk, _tmp) \
274 /* _sav |= EFLAGS & _msk; */ \
277 "andl %"_msk",%"_LO32 _tmp"; " \
278 "orl %"_LO32 _tmp",%"_sav"; "
280 /* Raw emulation: instruction has two explicit operands. */
281 #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
283 unsigned long _tmp; \
285 switch ((_dst).bytes) { \
287 __asm__ __volatile__ ( \
288 _PRE_EFLAGS("0","4","2") \
289 _op"w %"_wx"3,%1; " \
290 _POST_EFLAGS("0","4","2") \
291 : "=m" (_eflags), "=m" ((_dst).val), \
293 : _wy ((_src).val), "i" (EFLAGS_MASK) ); \
296 __asm__ __volatile__ ( \
297 _PRE_EFLAGS("0","4","2") \
298 _op"l %"_lx"3,%1; " \
299 _POST_EFLAGS("0","4","2") \
300 : "=m" (_eflags), "=m" ((_dst).val), \
302 : _ly ((_src).val), "i" (EFLAGS_MASK) ); \
305 __emulate_2op_8byte(_op, _src, _dst, \
306 _eflags, _qx, _qy); \
311 #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
313 unsigned long _tmp; \
314 switch ( (_dst).bytes ) \
317 __asm__ __volatile__ ( \
318 _PRE_EFLAGS("0","4","2") \
319 _op"b %"_bx"3,%1; " \
320 _POST_EFLAGS("0","4","2") \
321 : "=m" (_eflags), "=m" ((_dst).val), \
323 : _by ((_src).val), "i" (EFLAGS_MASK) ); \
326 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
327 _wx, _wy, _lx, _ly, _qx, _qy); \
332 /* Source operand is byte-sized and may be restricted to just %cl. */
333 #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
334 __emulate_2op(_op, _src, _dst, _eflags, \
335 "b", "c", "b", "c", "b", "c", "b", "c")
337 /* Source operand is byte, word, long or quad sized. */
338 #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
339 __emulate_2op(_op, _src, _dst, _eflags, \
340 "b", "q", "w", "r", _LO32, "r", "", "r")
342 /* Source operand is word, long or quad sized. */
343 #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
344 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
345 "w", "r", _LO32, "r", "", "r")
347 /* Instruction has only one explicit operand (no source operand). */
348 #define emulate_1op(_op, _dst, _eflags) \
350 unsigned long _tmp; \
352 switch ( (_dst).bytes ) \
355 __asm__ __volatile__ ( \
356 _PRE_EFLAGS("0","3","2") \
358 _POST_EFLAGS("0","3","2") \
359 : "=m" (_eflags), "=m" ((_dst).val), \
361 : "i" (EFLAGS_MASK) ); \
364 __asm__ __volatile__ ( \
365 _PRE_EFLAGS("0","3","2") \
367 _POST_EFLAGS("0","3","2") \
368 : "=m" (_eflags), "=m" ((_dst).val), \
370 : "i" (EFLAGS_MASK) ); \
373 __asm__ __volatile__ ( \
374 _PRE_EFLAGS("0","3","2") \
376 _POST_EFLAGS("0","3","2") \
377 : "=m" (_eflags), "=m" ((_dst).val), \
379 : "i" (EFLAGS_MASK) ); \
382 __emulate_1op_8byte(_op, _dst, _eflags); \
387 /* Emulate an instruction with quadword operands (x86/64 only). */
388 #if defined(CONFIG_X86_64)
389 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
391 __asm__ __volatile__ ( \
392 _PRE_EFLAGS("0","4","2") \
393 _op"q %"_qx"3,%1; " \
394 _POST_EFLAGS("0","4","2") \
395 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
396 : _qy ((_src).val), "i" (EFLAGS_MASK) ); \
399 #define __emulate_1op_8byte(_op, _dst, _eflags) \
401 __asm__ __volatile__ ( \
402 _PRE_EFLAGS("0","3","2") \
404 _POST_EFLAGS("0","3","2") \
405 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
406 : "i" (EFLAGS_MASK) ); \
409 #elif defined(__i386__)
410 #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
411 #define __emulate_1op_8byte(_op, _dst, _eflags)
412 #endif /* __i386__ */
414 /* Fetch next part of the instruction being emulated. */
415 #define insn_fetch(_type, _size, _eip) \
416 ({ unsigned long _x; \
417 rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
418 (_size), ctxt->vcpu); \
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); \
450 * Given the 'reg' portion of a ModRM byte, and a register block, return a
451 * pointer into the block that addresses the relevant register.
452 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
454 static void *decode_register(u8 modrm_reg, unsigned long *regs,
459 p = ®s[modrm_reg];
460 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
461 p = (unsigned char *)®s[modrm_reg & 3] + 1;
465 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
466 struct x86_emulate_ops *ops,
468 u16 *size, unsigned long *address, int op_bytes)
475 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
479 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
484 static int test_cc(unsigned int condition, unsigned int flags)
488 switch ((condition & 15) >> 1) {
490 rc |= (flags & EFLG_OF);
492 case 1: /* b/c/nae */
493 rc |= (flags & EFLG_CF);
496 rc |= (flags & EFLG_ZF);
499 rc |= (flags & (EFLG_CF|EFLG_ZF));
502 rc |= (flags & EFLG_SF);
505 rc |= (flags & EFLG_PF);
508 rc |= (flags & EFLG_ZF);
511 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
515 /* Odd condition identifiers (lsb == 1) have inverted sense. */
516 return (!!rc ^ (condition & 1));
520 x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
522 struct decode_cache *c = &ctxt->decode;
523 u8 sib, rex_prefix = 0;
526 int mode = ctxt->mode;
527 int index_reg = 0, base_reg = 0, scale, rip_relative = 0;
529 /* Shadow copy of register state. Committed on successful emulation. */
531 memset(c, 0, sizeof(struct decode_cache));
532 c->eip = ctxt->vcpu->rip;
533 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
536 case X86EMUL_MODE_REAL:
537 case X86EMUL_MODE_PROT16:
538 c->op_bytes = c->ad_bytes = 2;
540 case X86EMUL_MODE_PROT32:
541 c->op_bytes = c->ad_bytes = 4;
544 case X86EMUL_MODE_PROT64:
553 /* Legacy prefixes. */
554 for (i = 0; i < 8; i++) {
555 switch (c->b = insn_fetch(u8, 1, c->eip)) {
556 case 0x66: /* operand-size override */
557 c->op_bytes ^= 6; /* switch between 2/4 bytes */
559 case 0x67: /* address-size override */
560 if (mode == X86EMUL_MODE_PROT64)
561 /* switch between 4/8 bytes */
564 /* switch between 2/4 bytes */
567 case 0x2e: /* CS override */
568 c->override_base = &ctxt->cs_base;
570 case 0x3e: /* DS override */
571 c->override_base = &ctxt->ds_base;
573 case 0x26: /* ES override */
574 c->override_base = &ctxt->es_base;
576 case 0x64: /* FS override */
577 c->override_base = &ctxt->fs_base;
579 case 0x65: /* GS override */
580 c->override_base = &ctxt->gs_base;
582 case 0x36: /* SS override */
583 c->override_base = &ctxt->ss_base;
585 case 0xf0: /* LOCK */
588 case 0xf2: /* REPNE/REPNZ */
589 case 0xf3: /* REP/REPE/REPZ */
600 if ((mode == X86EMUL_MODE_PROT64) && ((c->b & 0xf0) == 0x40)) {
603 c->op_bytes = 8; /* REX.W */
604 c->modrm_reg = (c->b & 4) << 1; /* REX.R */
605 index_reg = (c->b & 2) << 2; /* REX.X */
606 c->modrm_rm = base_reg = (c->b & 1) << 3; /* REG.B */
607 c->b = insn_fetch(u8, 1, c->eip);
610 /* Opcode byte(s). */
611 c->d = opcode_table[c->b];
613 /* Two-byte opcode? */
616 c->b = insn_fetch(u8, 1, c->eip);
617 c->d = twobyte_table[c->b];
622 DPRINTF("Cannot emulate %02x\n", c->b);
627 /* ModRM and SIB bytes. */
629 c->modrm = insn_fetch(u8, 1, c->eip);
630 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
631 c->modrm_reg |= (c->modrm & 0x38) >> 3;
632 c->modrm_rm |= (c->modrm & 0x07);
636 if (c->modrm_mod == 3) {
637 c->modrm_val = *(unsigned long *)
638 decode_register(c->modrm_rm, c->regs, c->d & ByteOp);
642 if (c->ad_bytes == 2) {
643 unsigned bx = c->regs[VCPU_REGS_RBX];
644 unsigned bp = c->regs[VCPU_REGS_RBP];
645 unsigned si = c->regs[VCPU_REGS_RSI];
646 unsigned di = c->regs[VCPU_REGS_RDI];
648 /* 16-bit ModR/M decode. */
649 switch (c->modrm_mod) {
651 if (c->modrm_rm == 6)
653 insn_fetch(u16, 2, c->eip);
656 c->modrm_ea += insn_fetch(s8, 1, c->eip);
659 c->modrm_ea += insn_fetch(u16, 2, c->eip);
662 switch (c->modrm_rm) {
664 c->modrm_ea += bx + si;
667 c->modrm_ea += bx + di;
670 c->modrm_ea += bp + si;
673 c->modrm_ea += bp + di;
682 if (c->modrm_mod != 0)
689 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
690 (c->modrm_rm == 6 && c->modrm_mod != 0))
691 if (!c->override_base)
692 c->override_base = &ctxt->ss_base;
693 c->modrm_ea = (u16)c->modrm_ea;
695 /* 32/64-bit ModR/M decode. */
696 switch (c->modrm_rm) {
699 sib = insn_fetch(u8, 1, c->eip);
700 index_reg |= (sib >> 3) & 7;
706 if (c->modrm_mod != 0)
711 insn_fetch(s32, 4, c->eip);
714 c->modrm_ea += c->regs[base_reg];
721 c->regs[index_reg] << scale;
726 if (c->modrm_mod != 0)
727 c->modrm_ea += c->regs[c->modrm_rm];
728 else if (mode == X86EMUL_MODE_PROT64)
732 c->modrm_ea += c->regs[c->modrm_rm];
735 switch (c->modrm_mod) {
737 if (c->modrm_rm == 5)
739 insn_fetch(s32, 4, c->eip);
742 c->modrm_ea += insn_fetch(s8, 1, c->eip);
745 c->modrm_ea += insn_fetch(s32, 4, c->eip);
749 if (!c->override_base)
750 c->override_base = &ctxt->ds_base;
751 if (mode == X86EMUL_MODE_PROT64 &&
752 c->override_base != &ctxt->fs_base &&
753 c->override_base != &ctxt->gs_base)
754 c->override_base = NULL;
756 if (c->override_base)
757 c->modrm_ea += *c->override_base;
760 c->modrm_ea += c->eip;
761 switch (c->d & SrcMask) {
769 if (c->op_bytes == 8)
772 c->modrm_ea += c->op_bytes;
775 if (c->ad_bytes != 8)
776 c->modrm_ea = (u32)c->modrm_ea;
782 * Decode and fetch the source operand: register, memory
785 switch (c->d & SrcMask) {
789 c->src.type = OP_REG;
792 decode_register(c->modrm_reg, c->regs,
794 c->src.val = c->src.orig_val = *(u8 *)c->src.ptr;
798 decode_register(c->modrm_reg, c->regs, 0);
799 switch ((c->src.bytes = c->op_bytes)) {
801 c->src.val = c->src.orig_val =
805 c->src.val = c->src.orig_val =
809 c->src.val = c->src.orig_val =
822 c->src.bytes = (c->d & ByteOp) ? 1 :
824 /* Don't fetch the address for invlpg: it could be unmapped. */
825 if (c->twobyte && c->b == 0x01
826 && c->modrm_reg == 7)
830 * For instructions with a ModR/M byte, switch to register
833 if ((c->d & ModRM) && c->modrm_mod == 3) {
834 c->src.type = OP_REG;
837 c->src.type = OP_MEM;
840 c->src.type = OP_IMM;
841 c->src.ptr = (unsigned long *)c->eip;
842 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
843 if (c->src.bytes == 8)
845 /* NB. Immediates are sign-extended as necessary. */
846 switch (c->src.bytes) {
848 c->src.val = insn_fetch(s8, 1, c->eip);
851 c->src.val = insn_fetch(s16, 2, c->eip);
854 c->src.val = insn_fetch(s32, 4, c->eip);
859 c->src.type = OP_IMM;
860 c->src.ptr = (unsigned long *)c->eip;
862 c->src.val = insn_fetch(s8, 1, c->eip);
866 /* Decode and fetch the destination operand: register or memory. */
867 switch (c->d & DstMask) {
869 /* Special instructions do their own operand decoding. */
872 c->dst.type = OP_REG;
875 (c->b == 0xb6 || c->b == 0xb7))) {
877 decode_register(c->modrm_reg, c->regs,
879 c->dst.val = *(u8 *) c->dst.ptr;
883 decode_register(c->modrm_reg, c->regs, 0);
884 switch ((c->dst.bytes = c->op_bytes)) {
886 c->dst.val = *(u16 *)c->dst.ptr;
889 c->dst.val = *(u32 *)c->dst.ptr;
892 c->dst.val = *(u64 *)c->dst.ptr;
898 if ((c->d & ModRM) && c->modrm_mod == 3) {
899 c->dst.type = OP_REG;
902 c->dst.type = OP_MEM;
907 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
910 static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
912 struct decode_cache *c = &ctxt->decode;
914 c->dst.type = OP_MEM;
915 c->dst.bytes = c->op_bytes;
916 c->dst.val = c->src.val;
917 register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes);
918 c->dst.ptr = (void *) register_address(ctxt->ss_base,
919 c->regs[VCPU_REGS_RSP]);
922 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
923 struct x86_emulate_ops *ops)
925 struct decode_cache *c = &ctxt->decode;
928 /* 64-bit mode: POP always pops a 64-bit operand. */
930 if (ctxt->mode == X86EMUL_MODE_PROT64)
933 rc = ops->read_std(register_address(ctxt->ss_base,
934 c->regs[VCPU_REGS_RSP]),
935 &c->dst.val, c->dst.bytes, ctxt->vcpu);
939 register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes);
944 static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
946 struct decode_cache *c = &ctxt->decode;
947 switch (c->modrm_reg) {
949 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
952 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
955 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
958 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
960 case 4: /* sal/shl */
961 case 6: /* sal/shl */
962 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
965 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
968 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
973 static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
974 struct x86_emulate_ops *ops)
976 struct decode_cache *c = &ctxt->decode;
979 switch (c->modrm_reg) {
980 case 0 ... 1: /* test */
982 * Special case in Grp3: test has an immediate
985 c->src.type = OP_IMM;
986 c->src.ptr = (unsigned long *)c->eip;
987 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
988 if (c->src.bytes == 8)
990 switch (c->src.bytes) {
992 c->src.val = insn_fetch(s8, 1, c->eip);
995 c->src.val = insn_fetch(s16, 2, c->eip);
998 c->src.val = insn_fetch(s32, 4, c->eip);
1001 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1004 c->dst.val = ~c->dst.val;
1007 emulate_1op("neg", c->dst, ctxt->eflags);
1010 DPRINTF("Cannot emulate %02x\n", c->b);
1011 rc = X86EMUL_UNHANDLEABLE;
1018 static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
1019 struct x86_emulate_ops *ops)
1021 struct decode_cache *c = &ctxt->decode;
1024 switch (c->modrm_reg) {
1026 emulate_1op("inc", c->dst, ctxt->eflags);
1029 emulate_1op("dec", c->dst, ctxt->eflags);
1031 case 4: /* jmp abs */
1033 c->eip = c->dst.val;
1035 DPRINTF("Cannot emulate %02x\n", c->b);
1036 return X86EMUL_UNHANDLEABLE;
1041 /* 64-bit mode: PUSH always pushes a 64-bit operand. */
1043 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1045 rc = ops->read_std((unsigned long)c->dst.ptr,
1046 &c->dst.val, 8, ctxt->vcpu);
1050 register_address_increment(c->regs[VCPU_REGS_RSP],
1052 rc = ops->write_emulated(register_address(ctxt->ss_base,
1053 c->regs[VCPU_REGS_RSP]), &c->dst.val,
1054 c->dst.bytes, ctxt->vcpu);
1057 c->dst.type = OP_NONE;
1060 DPRINTF("Cannot emulate %02x\n", c->b);
1061 return X86EMUL_UNHANDLEABLE;
1066 static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1067 struct x86_emulate_ops *ops,
1070 struct decode_cache *c = &ctxt->decode;
1074 rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu);
1078 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1079 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1081 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1082 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1083 ctxt->eflags &= ~EFLG_ZF;
1086 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1087 (u32) c->regs[VCPU_REGS_RBX];
1089 rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu);
1092 ctxt->eflags |= EFLG_ZF;
1097 static inline int writeback(struct x86_emulate_ctxt *ctxt,
1098 struct x86_emulate_ops *ops)
1101 struct decode_cache *c = &ctxt->decode;
1103 switch (c->dst.type) {
1105 /* The 4-byte case *is* correct:
1106 * in 64-bit mode we zero-extend.
1108 switch (c->dst.bytes) {
1110 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1113 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1116 *c->dst.ptr = (u32)c->dst.val;
1117 break; /* 64b: zero-ext */
1119 *c->dst.ptr = c->dst.val;
1125 rc = ops->cmpxchg_emulated(
1126 (unsigned long)c->dst.ptr,
1132 rc = ops->write_emulated(
1133 (unsigned long)c->dst.ptr,
1150 x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1152 unsigned long cr2 = ctxt->cr2;
1154 unsigned long saved_eip = 0;
1155 struct decode_cache *c = &ctxt->decode;
1158 /* Shadow copy of register state. Committed on successful emulation.
1159 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1163 memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs);
1166 if ((c->d & ModRM) && (c->modrm_mod != 3))
1169 if (c->src.type == OP_MEM) {
1170 c->src.ptr = (unsigned long *)cr2;
1172 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1177 c->src.orig_val = c->src.val;
1180 if ((c->d & DstMask) == ImplicitOps)
1184 if (c->dst.type == OP_MEM) {
1185 c->dst.ptr = (unsigned long *)cr2;
1186 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1189 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1191 c->dst.ptr = (void *)c->dst.ptr +
1192 (c->src.val & mask) / 8;
1194 if (!(c->d & Mov) &&
1195 /* optimisation - avoid slow emulated read */
1196 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1198 c->dst.bytes, ctxt->vcpu)) != 0))
1201 c->dst.orig_val = c->dst.val;
1209 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
1213 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
1217 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
1221 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
1225 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
1227 case 0x24: /* and al imm8 */
1228 c->dst.type = OP_REG;
1229 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1230 c->dst.val = *(u8 *)c->dst.ptr;
1232 c->dst.orig_val = c->dst.val;
1234 case 0x25: /* and ax imm16, or eax imm32 */
1235 c->dst.type = OP_REG;
1236 c->dst.bytes = c->op_bytes;
1237 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1238 if (c->op_bytes == 2)
1239 c->dst.val = *(u16 *)c->dst.ptr;
1241 c->dst.val = *(u32 *)c->dst.ptr;
1242 c->dst.orig_val = c->dst.val;
1246 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
1250 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
1254 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1256 case 0x63: /* movsxd */
1257 if (ctxt->mode != X86EMUL_MODE_PROT64)
1258 goto cannot_emulate;
1259 c->dst.val = (s32) c->src.val;
1261 case 0x80 ... 0x83: /* Grp1 */
1262 switch (c->modrm_reg) {
1282 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1284 case 0x86 ... 0x87: /* xchg */
1285 /* Write back the register source. */
1286 switch (c->dst.bytes) {
1288 *(u8 *) c->src.ptr = (u8) c->dst.val;
1291 *(u16 *) c->src.ptr = (u16) c->dst.val;
1294 *c->src.ptr = (u32) c->dst.val;
1295 break; /* 64b reg: zero-extend */
1297 *c->src.ptr = c->dst.val;
1301 * Write back the memory destination with implicit LOCK
1304 c->dst.val = c->src.val;
1307 case 0x88 ... 0x8b: /* mov */
1309 case 0x8d: /* lea r16/r32, m */
1310 c->dst.val = c->modrm_val;
1312 case 0x8f: /* pop (sole member of Grp1a) */
1313 rc = emulate_grp1a(ctxt, ops);
1317 case 0xa0 ... 0xa1: /* mov */
1318 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1319 c->dst.val = c->src.val;
1320 /* skip src displacement */
1321 c->eip += c->ad_bytes;
1323 case 0xa2 ... 0xa3: /* mov */
1324 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1325 /* skip c->dst displacement */
1326 c->eip += c->ad_bytes;
1331 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1333 c->dst.val = c->src.val;
1335 case 0xd0 ... 0xd1: /* Grp2 */
1339 case 0xd2 ... 0xd3: /* Grp2 */
1340 c->src.val = c->regs[VCPU_REGS_RCX];
1343 case 0xf6 ... 0xf7: /* Grp3 */
1344 rc = emulate_grp3(ctxt, ops);
1348 case 0xfe ... 0xff: /* Grp4/Grp5 */
1349 rc = emulate_grp45(ctxt, ops);
1356 rc = writeback(ctxt, ops);
1360 /* Commit shadow register state. */
1361 memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs);
1362 ctxt->vcpu->rip = c->eip;
1365 if (rc == X86EMUL_UNHANDLEABLE) {
1373 goto twobyte_special_insn;
1375 case 0x50 ... 0x57: /* push reg */
1376 if (c->op_bytes == 2)
1377 c->src.val = (u16) c->regs[c->b & 0x7];
1379 c->src.val = (u32) c->regs[c->b & 0x7];
1380 c->dst.type = OP_MEM;
1381 c->dst.bytes = c->op_bytes;
1382 c->dst.val = c->src.val;
1383 register_address_increment(c->regs[VCPU_REGS_RSP],
1385 c->dst.ptr = (void *) register_address(
1386 ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
1388 case 0x58 ... 0x5f: /* pop reg */
1389 c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7];
1391 if ((rc = ops->read_std(register_address(ctxt->ss_base,
1392 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1393 c->op_bytes, ctxt->vcpu)) != 0)
1396 register_address_increment(c->regs[VCPU_REGS_RSP],
1398 c->dst.type = OP_NONE; /* Disable writeback. */
1400 case 0x6a: /* push imm8 */
1402 c->src.val = insn_fetch(s8, 1, c->eip);
1405 case 0x6c: /* insb */
1406 case 0x6d: /* insw/insd */
1407 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1409 (c->d & ByteOp) ? 1 : c->op_bytes,
1411 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1412 (ctxt->eflags & EFLG_DF),
1413 register_address(ctxt->es_base,
1414 c->regs[VCPU_REGS_RDI]),
1416 c->regs[VCPU_REGS_RDX]) == 0) {
1421 case 0x6e: /* outsb */
1422 case 0x6f: /* outsw/outsd */
1423 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1425 (c->d & ByteOp) ? 1 : c->op_bytes,
1427 address_mask(c->regs[VCPU_REGS_RCX]) : 1,
1428 (ctxt->eflags & EFLG_DF),
1429 register_address(c->override_base ?
1432 c->regs[VCPU_REGS_RSI]),
1434 c->regs[VCPU_REGS_RDX]) == 0) {
1439 case 0x70 ... 0x7f: /* jcc (short) */ {
1440 int rel = insn_fetch(s8, 1, c->eip);
1442 if (test_cc(c->b, ctxt->eflags))
1446 case 0x9c: /* pushf */
1447 c->src.val = (unsigned long) ctxt->eflags;
1450 case 0x9d: /* popf */
1451 c->dst.ptr = (unsigned long *) &ctxt->eflags;
1452 goto pop_instruction;
1453 case 0xc3: /* ret */
1454 c->dst.ptr = &c->eip;
1455 goto pop_instruction;
1456 case 0xf4: /* hlt */
1457 ctxt->vcpu->halt_request = 1;
1460 if (c->rep_prefix) {
1461 if (c->regs[VCPU_REGS_RCX] == 0) {
1462 ctxt->vcpu->rip = c->eip;
1465 c->regs[VCPU_REGS_RCX]--;
1466 c->eip = ctxt->vcpu->rip;
1469 case 0xa4 ... 0xa5: /* movs */
1470 c->dst.type = OP_MEM;
1471 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1472 c->dst.ptr = (unsigned long *)register_address(
1474 c->regs[VCPU_REGS_RDI]);
1475 if ((rc = ops->read_emulated(register_address(
1476 c->override_base ? *c->override_base :
1478 c->regs[VCPU_REGS_RSI]),
1480 c->dst.bytes, ctxt->vcpu)) != 0)
1482 register_address_increment(c->regs[VCPU_REGS_RSI],
1483 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1485 register_address_increment(c->regs[VCPU_REGS_RDI],
1486 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1489 case 0xa6 ... 0xa7: /* cmps */
1490 DPRINTF("Urk! I don't handle CMPS.\n");
1491 goto cannot_emulate;
1492 case 0xaa ... 0xab: /* stos */
1493 c->dst.type = OP_MEM;
1494 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1495 c->dst.ptr = (unsigned long *)cr2;
1496 c->dst.val = c->regs[VCPU_REGS_RAX];
1497 register_address_increment(c->regs[VCPU_REGS_RDI],
1498 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1501 case 0xac ... 0xad: /* lods */
1502 c->dst.type = OP_REG;
1503 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1504 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1505 if ((rc = ops->read_emulated(cr2, &c->dst.val,
1509 register_address_increment(c->regs[VCPU_REGS_RSI],
1510 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1513 case 0xae ... 0xaf: /* scas */
1514 DPRINTF("Urk! I don't handle SCAS.\n");
1515 goto cannot_emulate;
1516 case 0xe8: /* call (near) */ {
1518 switch (c->op_bytes) {
1520 rel = insn_fetch(s16, 2, c->eip);
1523 rel = insn_fetch(s32, 4, c->eip);
1526 rel = insn_fetch(s64, 8, c->eip);
1529 DPRINTF("Call: Invalid op_bytes\n");
1530 goto cannot_emulate;
1532 c->src.val = (unsigned long) c->eip;
1534 c->op_bytes = c->ad_bytes;
1538 case 0xe9: /* jmp rel */
1539 case 0xeb: /* jmp rel short */
1540 JMP_REL(c->src.val);
1541 c->dst.type = OP_NONE; /* Disable writeback. */
1550 case 0x01: /* lgdt, lidt, lmsw */
1551 switch (c->modrm_reg) {
1553 unsigned long address;
1555 case 0: /* vmcall */
1556 if (c->modrm_mod != 3 || c->modrm_rm != 1)
1557 goto cannot_emulate;
1559 rc = kvm_fix_hypercall(ctxt->vcpu);
1563 kvm_emulate_hypercall(ctxt->vcpu);
1566 rc = read_descriptor(ctxt, ops, c->src.ptr,
1567 &size, &address, c->op_bytes);
1570 realmode_lgdt(ctxt->vcpu, size, address);
1572 case 3: /* lidt/vmmcall */
1573 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
1574 rc = kvm_fix_hypercall(ctxt->vcpu);
1577 kvm_emulate_hypercall(ctxt->vcpu);
1579 rc = read_descriptor(ctxt, ops, c->src.ptr,
1584 realmode_lidt(ctxt->vcpu, size, address);
1588 if (c->modrm_mod != 3)
1589 goto cannot_emulate;
1590 *(u16 *)&c->regs[c->modrm_rm]
1591 = realmode_get_cr(ctxt->vcpu, 0);
1594 if (c->modrm_mod != 3)
1595 goto cannot_emulate;
1596 realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val,
1600 emulate_invlpg(ctxt->vcpu, cr2);
1603 goto cannot_emulate;
1605 /* Disable writeback. */
1606 c->dst.type = OP_NONE;
1608 case 0x21: /* mov from dr to reg */
1609 if (c->modrm_mod != 3)
1610 goto cannot_emulate;
1611 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
1613 goto cannot_emulate;
1614 c->dst.type = OP_NONE; /* no writeback */
1616 case 0x23: /* mov from reg to dr */
1617 if (c->modrm_mod != 3)
1618 goto cannot_emulate;
1619 rc = emulator_set_dr(ctxt, c->modrm_reg,
1620 c->regs[c->modrm_rm]);
1622 goto cannot_emulate;
1623 c->dst.type = OP_NONE; /* no writeback */
1625 case 0x40 ... 0x4f: /* cmov */
1626 c->dst.val = c->dst.orig_val = c->src.val;
1627 if (!test_cc(c->b, ctxt->eflags))
1628 c->dst.type = OP_NONE; /* no writeback */
1632 /* only subword offset */
1633 c->src.val &= (c->dst.bytes << 3) - 1;
1634 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
1638 /* only subword offset */
1639 c->src.val &= (c->dst.bytes << 3) - 1;
1640 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
1642 case 0xb0 ... 0xb1: /* cmpxchg */
1644 * Save real source value, then compare EAX against
1647 c->src.orig_val = c->src.val;
1648 c->src.val = c->regs[VCPU_REGS_RAX];
1649 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1650 if (ctxt->eflags & EFLG_ZF) {
1651 /* Success: write back to memory. */
1652 c->dst.val = c->src.orig_val;
1654 /* Failure: write the value we saw to EAX. */
1655 c->dst.type = OP_REG;
1656 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1661 /* only subword offset */
1662 c->src.val &= (c->dst.bytes << 3) - 1;
1663 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
1665 case 0xb6 ... 0xb7: /* movzx */
1666 c->dst.bytes = c->op_bytes;
1667 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1670 case 0xba: /* Grp8 */
1671 switch (c->modrm_reg & 3) {
1684 /* only subword offset */
1685 c->src.val &= (c->dst.bytes << 3) - 1;
1686 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
1688 case 0xbe ... 0xbf: /* movsx */
1689 c->dst.bytes = c->op_bytes;
1690 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
1693 case 0xc3: /* movnti */
1694 c->dst.bytes = c->op_bytes;
1695 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
1701 twobyte_special_insn:
1704 emulate_clts(ctxt->vcpu);
1706 case 0x08: /* invd */
1708 case 0x09: /* wbinvd */
1710 case 0x0d: /* GrpP (prefetch) */
1711 case 0x18: /* Grp16 (prefetch/nop) */
1713 case 0x20: /* mov cr, reg */
1714 if (c->modrm_mod != 3)
1715 goto cannot_emulate;
1716 c->regs[c->modrm_rm] =
1717 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1719 case 0x22: /* mov reg, cr */
1720 if (c->modrm_mod != 3)
1721 goto cannot_emulate;
1722 realmode_set_cr(ctxt->vcpu,
1723 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1727 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1728 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1729 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1731 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
1732 c->eip = ctxt->vcpu->rip;
1734 rc = X86EMUL_CONTINUE;
1738 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1740 kvm_x86_ops->inject_gp(ctxt->vcpu, 0);
1741 c->eip = ctxt->vcpu->rip;
1743 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1744 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1746 rc = X86EMUL_CONTINUE;
1748 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1751 switch (c->op_bytes) {
1753 rel = insn_fetch(s16, 2, c->eip);
1756 rel = insn_fetch(s32, 4, c->eip);
1759 rel = insn_fetch(s64, 8, c->eip);
1762 DPRINTF("jnz: Invalid op_bytes\n");
1763 goto cannot_emulate;
1765 if (test_cc(c->b, ctxt->eflags))
1769 case 0xc7: /* Grp9 (cmpxchg8b) */
1770 rc = emulate_grp9(ctxt, ops, cr2);
1775 /* Disable writeback. */
1776 c->dst.type = OP_NONE;
1780 DPRINTF("Cannot emulate %02x\n", c->b);