KVM: x86 emulator: move 0F 01 sub-opcodes into their own functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kvm / emulate.c
blob8e1d0c8196bd7c030f15bd648a03b0ece433f9c5
1 /******************************************************************************
2 * emulate.c
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
12 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
23 #include <linux/kvm_host.h>
24 #include "kvm_cache_regs.h"
25 #include <linux/module.h>
26 #include <asm/kvm_emulate.h>
28 #include "x86.h"
29 #include "tss.h"
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
40 /* Operand sizes: 8-bit operands or specified/overridden size. */
41 #define ByteOp (1<<0) /* 8-bit operands. */
42 /* Destination operand type. */
43 #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44 #define DstReg (2<<1) /* Register operand. */
45 #define DstMem (3<<1) /* Memory operand. */
46 #define DstAcc (4<<1) /* Destination Accumulator */
47 #define DstDI (5<<1) /* Destination is in ES:(E)DI */
48 #define DstMem64 (6<<1) /* 64bit memory operand */
49 #define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
50 #define DstMask (7<<1)
51 /* Source operand type. */
52 #define SrcNone (0<<4) /* No source operand. */
53 #define SrcReg (1<<4) /* Register operand. */
54 #define SrcMem (2<<4) /* Memory operand. */
55 #define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56 #define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57 #define SrcImm (5<<4) /* Immediate operand. */
58 #define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
59 #define SrcOne (7<<4) /* Implied '1' */
60 #define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
61 #define SrcImmU (9<<4) /* Immediate operand, unsigned */
62 #define SrcSI (0xa<<4) /* Source is in the DS:RSI */
63 #define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64 #define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
65 #define SrcAcc (0xd<<4) /* Source Accumulator */
66 #define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
67 #define SrcMask (0xf<<4)
68 /* Generic ModRM decode. */
69 #define ModRM (1<<8)
70 /* Destination is only written; never read. */
71 #define Mov (1<<9)
72 #define BitOp (1<<10)
73 #define MemAbs (1<<11) /* Memory operand is absolute displacement */
74 #define String (1<<12) /* String instruction (rep capable) */
75 #define Stack (1<<13) /* Stack instruction (push/pop) */
76 #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77 #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
78 #define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */
79 #define Sse (1<<17) /* SSE Vector instruction */
80 #define RMExt (1<<18) /* Opcode extension in ModRM r/m if mod == 3 */
81 /* Misc flags */
82 #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
83 #define VendorSpecific (1<<22) /* Vendor specific instruction */
84 #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
85 #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
86 #define Undefined (1<<25) /* No Such Instruction */
87 #define Lock (1<<26) /* lock prefix is allowed for the instruction */
88 #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
89 #define No64 (1<<28)
90 /* Source 2 operand type */
91 #define Src2None (0<<29)
92 #define Src2CL (1<<29)
93 #define Src2ImmByte (2<<29)
94 #define Src2One (3<<29)
95 #define Src2Imm (4<<29)
96 #define Src2Mask (7<<29)
98 #define X2(x...) x, x
99 #define X3(x...) X2(x), x
100 #define X4(x...) X2(x), X2(x)
101 #define X5(x...) X4(x), x
102 #define X6(x...) X4(x), X2(x)
103 #define X7(x...) X4(x), X3(x)
104 #define X8(x...) X4(x), X4(x)
105 #define X16(x...) X8(x), X8(x)
107 struct opcode {
108 u32 flags;
109 u8 intercept;
110 union {
111 int (*execute)(struct x86_emulate_ctxt *ctxt);
112 struct opcode *group;
113 struct group_dual *gdual;
114 struct gprefix *gprefix;
115 } u;
116 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
119 struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
124 struct gprefix {
125 struct opcode pfx_no;
126 struct opcode pfx_66;
127 struct opcode pfx_f2;
128 struct opcode pfx_f3;
131 /* EFLAGS bit definitions. */
132 #define EFLG_ID (1<<21)
133 #define EFLG_VIP (1<<20)
134 #define EFLG_VIF (1<<19)
135 #define EFLG_AC (1<<18)
136 #define EFLG_VM (1<<17)
137 #define EFLG_RF (1<<16)
138 #define EFLG_IOPL (3<<12)
139 #define EFLG_NT (1<<14)
140 #define EFLG_OF (1<<11)
141 #define EFLG_DF (1<<10)
142 #define EFLG_IF (1<<9)
143 #define EFLG_TF (1<<8)
144 #define EFLG_SF (1<<7)
145 #define EFLG_ZF (1<<6)
146 #define EFLG_AF (1<<4)
147 #define EFLG_PF (1<<2)
148 #define EFLG_CF (1<<0)
150 #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
151 #define EFLG_RESERVED_ONE_MASK 2
154 * Instruction emulation:
155 * Most instructions are emulated directly via a fragment of inline assembly
156 * code. This allows us to save/restore EFLAGS and thus very easily pick up
157 * any modified flags.
160 #if defined(CONFIG_X86_64)
161 #define _LO32 "k" /* force 32-bit operand */
162 #define _STK "%%rsp" /* stack pointer */
163 #elif defined(__i386__)
164 #define _LO32 "" /* force 32-bit operand */
165 #define _STK "%%esp" /* stack pointer */
166 #endif
169 * These EFLAGS bits are restored from saved value during emulation, and
170 * any changes are written back to the saved value after emulation.
172 #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
174 /* Before executing instruction: restore necessary bits in EFLAGS. */
175 #define _PRE_EFLAGS(_sav, _msk, _tmp) \
176 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
177 "movl %"_sav",%"_LO32 _tmp"; " \
178 "push %"_tmp"; " \
179 "push %"_tmp"; " \
180 "movl %"_msk",%"_LO32 _tmp"; " \
181 "andl %"_LO32 _tmp",("_STK"); " \
182 "pushf; " \
183 "notl %"_LO32 _tmp"; " \
184 "andl %"_LO32 _tmp",("_STK"); " \
185 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
186 "pop %"_tmp"; " \
187 "orl %"_LO32 _tmp",("_STK"); " \
188 "popf; " \
189 "pop %"_sav"; "
191 /* After executing instruction: write-back necessary bits in EFLAGS. */
192 #define _POST_EFLAGS(_sav, _msk, _tmp) \
193 /* _sav |= EFLAGS & _msk; */ \
194 "pushf; " \
195 "pop %"_tmp"; " \
196 "andl %"_msk",%"_LO32 _tmp"; " \
197 "orl %"_LO32 _tmp",%"_sav"; "
199 #ifdef CONFIG_X86_64
200 #define ON64(x) x
201 #else
202 #define ON64(x)
203 #endif
205 #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
206 do { \
207 __asm__ __volatile__ ( \
208 _PRE_EFLAGS("0", "4", "2") \
209 _op _suffix " %"_x"3,%1; " \
210 _POST_EFLAGS("0", "4", "2") \
211 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
212 "=&r" (_tmp) \
213 : _y ((_src).val), "i" (EFLAGS_MASK)); \
214 } while (0)
217 /* Raw emulation: instruction has two explicit operands. */
218 #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
219 do { \
220 unsigned long _tmp; \
222 switch ((_dst).bytes) { \
223 case 2: \
224 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
225 break; \
226 case 4: \
227 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
228 break; \
229 case 8: \
230 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
231 break; \
233 } while (0)
235 #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
236 do { \
237 unsigned long _tmp; \
238 switch ((_dst).bytes) { \
239 case 1: \
240 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
241 break; \
242 default: \
243 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
244 _wx, _wy, _lx, _ly, _qx, _qy); \
245 break; \
247 } while (0)
249 /* Source operand is byte-sized and may be restricted to just %cl. */
250 #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
251 __emulate_2op(_op, _src, _dst, _eflags, \
252 "b", "c", "b", "c", "b", "c", "b", "c")
254 /* Source operand is byte, word, long or quad sized. */
255 #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
256 __emulate_2op(_op, _src, _dst, _eflags, \
257 "b", "q", "w", "r", _LO32, "r", "", "r")
259 /* Source operand is word, long or quad sized. */
260 #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
261 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
262 "w", "r", _LO32, "r", "", "r")
264 /* Instruction has three operands and one operand is stored in ECX register */
265 #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
266 do { \
267 unsigned long _tmp; \
268 _type _clv = (_cl).val; \
269 _type _srcv = (_src).val; \
270 _type _dstv = (_dst).val; \
272 __asm__ __volatile__ ( \
273 _PRE_EFLAGS("0", "5", "2") \
274 _op _suffix " %4,%1 \n" \
275 _POST_EFLAGS("0", "5", "2") \
276 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
277 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
278 ); \
280 (_cl).val = (unsigned long) _clv; \
281 (_src).val = (unsigned long) _srcv; \
282 (_dst).val = (unsigned long) _dstv; \
283 } while (0)
285 #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
286 do { \
287 switch ((_dst).bytes) { \
288 case 2: \
289 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
290 "w", unsigned short); \
291 break; \
292 case 4: \
293 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
294 "l", unsigned int); \
295 break; \
296 case 8: \
297 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
298 "q", unsigned long)); \
299 break; \
301 } while (0)
303 #define __emulate_1op(_op, _dst, _eflags, _suffix) \
304 do { \
305 unsigned long _tmp; \
307 __asm__ __volatile__ ( \
308 _PRE_EFLAGS("0", "3", "2") \
309 _op _suffix " %1; " \
310 _POST_EFLAGS("0", "3", "2") \
311 : "=m" (_eflags), "+m" ((_dst).val), \
312 "=&r" (_tmp) \
313 : "i" (EFLAGS_MASK)); \
314 } while (0)
316 /* Instruction has only one explicit operand (no source operand). */
317 #define emulate_1op(_op, _dst, _eflags) \
318 do { \
319 switch ((_dst).bytes) { \
320 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
321 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
322 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
323 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
325 } while (0)
327 #define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
328 do { \
329 unsigned long _tmp; \
331 __asm__ __volatile__ ( \
332 _PRE_EFLAGS("0", "4", "1") \
333 _op _suffix " %5; " \
334 _POST_EFLAGS("0", "4", "1") \
335 : "=m" (_eflags), "=&r" (_tmp), \
336 "+a" (_rax), "+d" (_rdx) \
337 : "i" (EFLAGS_MASK), "m" ((_src).val), \
338 "a" (_rax), "d" (_rdx)); \
339 } while (0)
341 #define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
342 do { \
343 unsigned long _tmp; \
345 __asm__ __volatile__ ( \
346 _PRE_EFLAGS("0", "5", "1") \
347 "1: \n\t" \
348 _op _suffix " %6; " \
349 "2: \n\t" \
350 _POST_EFLAGS("0", "5", "1") \
351 ".pushsection .fixup,\"ax\" \n\t" \
352 "3: movb $1, %4 \n\t" \
353 "jmp 2b \n\t" \
354 ".popsection \n\t" \
355 _ASM_EXTABLE(1b, 3b) \
356 : "=m" (_eflags), "=&r" (_tmp), \
357 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
358 : "i" (EFLAGS_MASK), "m" ((_src).val), \
359 "a" (_rax), "d" (_rdx)); \
360 } while (0)
362 /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
363 #define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
364 do { \
365 switch((_src).bytes) { \
366 case 1: \
367 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
368 _eflags, "b"); \
369 break; \
370 case 2: \
371 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
372 _eflags, "w"); \
373 break; \
374 case 4: \
375 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
376 _eflags, "l"); \
377 break; \
378 case 8: \
379 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
380 _eflags, "q")); \
381 break; \
383 } while (0)
385 #define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
386 do { \
387 switch((_src).bytes) { \
388 case 1: \
389 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
390 _eflags, "b", _ex); \
391 break; \
392 case 2: \
393 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
394 _eflags, "w", _ex); \
395 break; \
396 case 4: \
397 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
398 _eflags, "l", _ex); \
399 break; \
400 case 8: ON64( \
401 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
402 _eflags, "q", _ex)); \
403 break; \
405 } while (0)
407 /* Fetch next part of the instruction being emulated. */
408 #define insn_fetch(_type, _size, _eip) \
409 ({ unsigned long _x; \
410 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
411 if (rc != X86EMUL_CONTINUE) \
412 goto done; \
413 (_eip) += (_size); \
414 (_type)_x; \
417 #define insn_fetch_arr(_arr, _size, _eip) \
418 ({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
419 if (rc != X86EMUL_CONTINUE) \
420 goto done; \
421 (_eip) += (_size); \
424 static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
425 enum x86_intercept intercept,
426 enum x86_intercept_stage stage)
428 struct x86_instruction_info info = {
429 .intercept = intercept,
430 .rep_prefix = ctxt->decode.rep_prefix,
431 .modrm_mod = ctxt->decode.modrm_mod,
432 .modrm_reg = ctxt->decode.modrm_reg,
433 .modrm_rm = ctxt->decode.modrm_rm,
434 .src_val = ctxt->decode.src.val64,
435 .src_bytes = ctxt->decode.src.bytes,
436 .dst_bytes = ctxt->decode.dst.bytes,
437 .ad_bytes = ctxt->decode.ad_bytes,
438 .next_rip = ctxt->eip,
441 return ctxt->ops->intercept(ctxt, &info, stage);
444 static inline unsigned long ad_mask(struct decode_cache *c)
446 return (1UL << (c->ad_bytes << 3)) - 1;
449 /* Access/update address held in a register, based on addressing mode. */
450 static inline unsigned long
451 address_mask(struct decode_cache *c, unsigned long reg)
453 if (c->ad_bytes == sizeof(unsigned long))
454 return reg;
455 else
456 return reg & ad_mask(c);
459 static inline unsigned long
460 register_address(struct decode_cache *c, unsigned long reg)
462 return address_mask(c, reg);
465 static inline void
466 register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
468 if (c->ad_bytes == sizeof(unsigned long))
469 *reg += inc;
470 else
471 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
474 static inline void jmp_rel(struct decode_cache *c, int rel)
476 register_address_increment(c, &c->eip, rel);
479 static u32 desc_limit_scaled(struct desc_struct *desc)
481 u32 limit = get_desc_limit(desc);
483 return desc->g ? (limit << 12) | 0xfff : limit;
486 static void set_seg_override(struct decode_cache *c, int seg)
488 c->has_seg_override = true;
489 c->seg_override = seg;
492 static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
493 struct x86_emulate_ops *ops, int seg)
495 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
496 return 0;
498 return ops->get_cached_segment_base(ctxt, seg);
501 static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
502 struct x86_emulate_ops *ops,
503 struct decode_cache *c)
505 if (!c->has_seg_override)
506 return 0;
508 return c->seg_override;
511 static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
512 u32 error, bool valid)
514 ctxt->exception.vector = vec;
515 ctxt->exception.error_code = error;
516 ctxt->exception.error_code_valid = valid;
517 return X86EMUL_PROPAGATE_FAULT;
520 static int emulate_db(struct x86_emulate_ctxt *ctxt)
522 return emulate_exception(ctxt, DB_VECTOR, 0, false);
525 static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
527 return emulate_exception(ctxt, GP_VECTOR, err, true);
530 static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
532 return emulate_exception(ctxt, SS_VECTOR, err, true);
535 static int emulate_ud(struct x86_emulate_ctxt *ctxt)
537 return emulate_exception(ctxt, UD_VECTOR, 0, false);
540 static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
542 return emulate_exception(ctxt, TS_VECTOR, err, true);
545 static int emulate_de(struct x86_emulate_ctxt *ctxt)
547 return emulate_exception(ctxt, DE_VECTOR, 0, false);
550 static int emulate_nm(struct x86_emulate_ctxt *ctxt)
552 return emulate_exception(ctxt, NM_VECTOR, 0, false);
555 static int __linearize(struct x86_emulate_ctxt *ctxt,
556 struct segmented_address addr,
557 unsigned size, bool write, bool fetch,
558 ulong *linear)
560 struct decode_cache *c = &ctxt->decode;
561 struct desc_struct desc;
562 bool usable;
563 ulong la;
564 u32 lim;
565 unsigned cpl, rpl;
567 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
568 switch (ctxt->mode) {
569 case X86EMUL_MODE_REAL:
570 break;
571 case X86EMUL_MODE_PROT64:
572 if (((signed long)la << 16) >> 16 != la)
573 return emulate_gp(ctxt, 0);
574 break;
575 default:
576 usable = ctxt->ops->get_cached_descriptor(ctxt, &desc, NULL,
577 addr.seg);
578 if (!usable)
579 goto bad;
580 /* code segment or read-only data segment */
581 if (((desc.type & 8) || !(desc.type & 2)) && write)
582 goto bad;
583 /* unreadable code segment */
584 if (!fetch && (desc.type & 8) && !(desc.type & 2))
585 goto bad;
586 lim = desc_limit_scaled(&desc);
587 if ((desc.type & 8) || !(desc.type & 4)) {
588 /* expand-up segment */
589 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
590 goto bad;
591 } else {
592 /* exapand-down segment */
593 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
594 goto bad;
595 lim = desc.d ? 0xffffffff : 0xffff;
596 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
597 goto bad;
599 cpl = ctxt->ops->cpl(ctxt);
600 rpl = ctxt->ops->get_segment_selector(ctxt, addr.seg) & 3;
601 cpl = max(cpl, rpl);
602 if (!(desc.type & 8)) {
603 /* data segment */
604 if (cpl > desc.dpl)
605 goto bad;
606 } else if ((desc.type & 8) && !(desc.type & 4)) {
607 /* nonconforming code segment */
608 if (cpl != desc.dpl)
609 goto bad;
610 } else if ((desc.type & 8) && (desc.type & 4)) {
611 /* conforming code segment */
612 if (cpl < desc.dpl)
613 goto bad;
615 break;
617 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
618 la &= (u32)-1;
619 *linear = la;
620 return X86EMUL_CONTINUE;
621 bad:
622 if (addr.seg == VCPU_SREG_SS)
623 return emulate_ss(ctxt, addr.seg);
624 else
625 return emulate_gp(ctxt, addr.seg);
628 static int linearize(struct x86_emulate_ctxt *ctxt,
629 struct segmented_address addr,
630 unsigned size, bool write,
631 ulong *linear)
633 return __linearize(ctxt, addr, size, write, false, linear);
637 static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
638 struct segmented_address addr,
639 void *data,
640 unsigned size)
642 int rc;
643 ulong linear;
645 rc = linearize(ctxt, addr, size, false, &linear);
646 if (rc != X86EMUL_CONTINUE)
647 return rc;
648 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
651 static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
652 struct x86_emulate_ops *ops,
653 unsigned long eip, u8 *dest)
655 struct fetch_cache *fc = &ctxt->decode.fetch;
656 int rc;
657 int size, cur_size;
659 if (eip == fc->end) {
660 unsigned long linear;
661 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
662 cur_size = fc->end - fc->start;
663 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
664 rc = __linearize(ctxt, addr, size, false, true, &linear);
665 if (rc != X86EMUL_CONTINUE)
666 return rc;
667 rc = ops->fetch(ctxt, linear, fc->data + cur_size,
668 size, &ctxt->exception);
669 if (rc != X86EMUL_CONTINUE)
670 return rc;
671 fc->end += size;
673 *dest = fc->data[eip - fc->start];
674 return X86EMUL_CONTINUE;
677 static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
678 struct x86_emulate_ops *ops,
679 unsigned long eip, void *dest, unsigned size)
681 int rc;
683 /* x86 instructions are limited to 15 bytes. */
684 if (eip + size - ctxt->eip > 15)
685 return X86EMUL_UNHANDLEABLE;
686 while (size--) {
687 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
688 if (rc != X86EMUL_CONTINUE)
689 return rc;
691 return X86EMUL_CONTINUE;
695 * Given the 'reg' portion of a ModRM byte, and a register block, return a
696 * pointer into the block that addresses the relevant register.
697 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
699 static void *decode_register(u8 modrm_reg, unsigned long *regs,
700 int highbyte_regs)
702 void *p;
704 p = &regs[modrm_reg];
705 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
706 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
707 return p;
710 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
711 struct x86_emulate_ops *ops,
712 struct segmented_address addr,
713 u16 *size, unsigned long *address, int op_bytes)
715 int rc;
717 if (op_bytes == 2)
718 op_bytes = 3;
719 *address = 0;
720 rc = segmented_read_std(ctxt, addr, size, 2);
721 if (rc != X86EMUL_CONTINUE)
722 return rc;
723 addr.ea += 2;
724 rc = segmented_read_std(ctxt, addr, address, op_bytes);
725 return rc;
728 static int test_cc(unsigned int condition, unsigned int flags)
730 int rc = 0;
732 switch ((condition & 15) >> 1) {
733 case 0: /* o */
734 rc |= (flags & EFLG_OF);
735 break;
736 case 1: /* b/c/nae */
737 rc |= (flags & EFLG_CF);
738 break;
739 case 2: /* z/e */
740 rc |= (flags & EFLG_ZF);
741 break;
742 case 3: /* be/na */
743 rc |= (flags & (EFLG_CF|EFLG_ZF));
744 break;
745 case 4: /* s */
746 rc |= (flags & EFLG_SF);
747 break;
748 case 5: /* p/pe */
749 rc |= (flags & EFLG_PF);
750 break;
751 case 7: /* le/ng */
752 rc |= (flags & EFLG_ZF);
753 /* fall through */
754 case 6: /* l/nge */
755 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
756 break;
759 /* Odd condition identifiers (lsb == 1) have inverted sense. */
760 return (!!rc ^ (condition & 1));
763 static void fetch_register_operand(struct operand *op)
765 switch (op->bytes) {
766 case 1:
767 op->val = *(u8 *)op->addr.reg;
768 break;
769 case 2:
770 op->val = *(u16 *)op->addr.reg;
771 break;
772 case 4:
773 op->val = *(u32 *)op->addr.reg;
774 break;
775 case 8:
776 op->val = *(u64 *)op->addr.reg;
777 break;
781 static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
783 ctxt->ops->get_fpu(ctxt);
784 switch (reg) {
785 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
786 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
787 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
788 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
789 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
790 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
791 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
792 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
793 #ifdef CONFIG_X86_64
794 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
795 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
796 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
797 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
798 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
799 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
800 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
801 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
802 #endif
803 default: BUG();
805 ctxt->ops->put_fpu(ctxt);
808 static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
809 int reg)
811 ctxt->ops->get_fpu(ctxt);
812 switch (reg) {
813 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
814 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
815 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
816 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
817 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
818 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
819 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
820 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
821 #ifdef CONFIG_X86_64
822 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
823 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
824 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
825 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
826 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
827 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
828 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
829 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
830 #endif
831 default: BUG();
833 ctxt->ops->put_fpu(ctxt);
836 static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
837 struct operand *op,
838 struct decode_cache *c,
839 int inhibit_bytereg)
841 unsigned reg = c->modrm_reg;
842 int highbyte_regs = c->rex_prefix == 0;
844 if (!(c->d & ModRM))
845 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
847 if (c->d & Sse) {
848 op->type = OP_XMM;
849 op->bytes = 16;
850 op->addr.xmm = reg;
851 read_sse_reg(ctxt, &op->vec_val, reg);
852 return;
855 op->type = OP_REG;
856 if ((c->d & ByteOp) && !inhibit_bytereg) {
857 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
858 op->bytes = 1;
859 } else {
860 op->addr.reg = decode_register(reg, c->regs, 0);
861 op->bytes = c->op_bytes;
863 fetch_register_operand(op);
864 op->orig_val = op->val;
867 static int decode_modrm(struct x86_emulate_ctxt *ctxt,
868 struct x86_emulate_ops *ops,
869 struct operand *op)
871 struct decode_cache *c = &ctxt->decode;
872 u8 sib;
873 int index_reg = 0, base_reg = 0, scale;
874 int rc = X86EMUL_CONTINUE;
875 ulong modrm_ea = 0;
877 if (c->rex_prefix) {
878 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
879 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
880 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
883 c->modrm = insn_fetch(u8, 1, c->eip);
884 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
885 c->modrm_reg |= (c->modrm & 0x38) >> 3;
886 c->modrm_rm |= (c->modrm & 0x07);
887 c->modrm_seg = VCPU_SREG_DS;
889 if (c->modrm_mod == 3) {
890 op->type = OP_REG;
891 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
892 op->addr.reg = decode_register(c->modrm_rm,
893 c->regs, c->d & ByteOp);
894 if (c->d & Sse) {
895 op->type = OP_XMM;
896 op->bytes = 16;
897 op->addr.xmm = c->modrm_rm;
898 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
899 return rc;
901 fetch_register_operand(op);
902 return rc;
905 op->type = OP_MEM;
907 if (c->ad_bytes == 2) {
908 unsigned bx = c->regs[VCPU_REGS_RBX];
909 unsigned bp = c->regs[VCPU_REGS_RBP];
910 unsigned si = c->regs[VCPU_REGS_RSI];
911 unsigned di = c->regs[VCPU_REGS_RDI];
913 /* 16-bit ModR/M decode. */
914 switch (c->modrm_mod) {
915 case 0:
916 if (c->modrm_rm == 6)
917 modrm_ea += insn_fetch(u16, 2, c->eip);
918 break;
919 case 1:
920 modrm_ea += insn_fetch(s8, 1, c->eip);
921 break;
922 case 2:
923 modrm_ea += insn_fetch(u16, 2, c->eip);
924 break;
926 switch (c->modrm_rm) {
927 case 0:
928 modrm_ea += bx + si;
929 break;
930 case 1:
931 modrm_ea += bx + di;
932 break;
933 case 2:
934 modrm_ea += bp + si;
935 break;
936 case 3:
937 modrm_ea += bp + di;
938 break;
939 case 4:
940 modrm_ea += si;
941 break;
942 case 5:
943 modrm_ea += di;
944 break;
945 case 6:
946 if (c->modrm_mod != 0)
947 modrm_ea += bp;
948 break;
949 case 7:
950 modrm_ea += bx;
951 break;
953 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
954 (c->modrm_rm == 6 && c->modrm_mod != 0))
955 c->modrm_seg = VCPU_SREG_SS;
956 modrm_ea = (u16)modrm_ea;
957 } else {
958 /* 32/64-bit ModR/M decode. */
959 if ((c->modrm_rm & 7) == 4) {
960 sib = insn_fetch(u8, 1, c->eip);
961 index_reg |= (sib >> 3) & 7;
962 base_reg |= sib & 7;
963 scale = sib >> 6;
965 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
966 modrm_ea += insn_fetch(s32, 4, c->eip);
967 else
968 modrm_ea += c->regs[base_reg];
969 if (index_reg != 4)
970 modrm_ea += c->regs[index_reg] << scale;
971 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
972 if (ctxt->mode == X86EMUL_MODE_PROT64)
973 c->rip_relative = 1;
974 } else
975 modrm_ea += c->regs[c->modrm_rm];
976 switch (c->modrm_mod) {
977 case 0:
978 if (c->modrm_rm == 5)
979 modrm_ea += insn_fetch(s32, 4, c->eip);
980 break;
981 case 1:
982 modrm_ea += insn_fetch(s8, 1, c->eip);
983 break;
984 case 2:
985 modrm_ea += insn_fetch(s32, 4, c->eip);
986 break;
989 op->addr.mem.ea = modrm_ea;
990 done:
991 return rc;
994 static int decode_abs(struct x86_emulate_ctxt *ctxt,
995 struct x86_emulate_ops *ops,
996 struct operand *op)
998 struct decode_cache *c = &ctxt->decode;
999 int rc = X86EMUL_CONTINUE;
1001 op->type = OP_MEM;
1002 switch (c->ad_bytes) {
1003 case 2:
1004 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
1005 break;
1006 case 4:
1007 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
1008 break;
1009 case 8:
1010 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
1011 break;
1013 done:
1014 return rc;
1017 static void fetch_bit_operand(struct decode_cache *c)
1019 long sv = 0, mask;
1021 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
1022 mask = ~(c->dst.bytes * 8 - 1);
1024 if (c->src.bytes == 2)
1025 sv = (s16)c->src.val & (s16)mask;
1026 else if (c->src.bytes == 4)
1027 sv = (s32)c->src.val & (s32)mask;
1029 c->dst.addr.mem.ea += (sv >> 3);
1032 /* only subword offset */
1033 c->src.val &= (c->dst.bytes << 3) - 1;
1036 static int read_emulated(struct x86_emulate_ctxt *ctxt,
1037 struct x86_emulate_ops *ops,
1038 unsigned long addr, void *dest, unsigned size)
1040 int rc;
1041 struct read_cache *mc = &ctxt->decode.mem_read;
1043 while (size) {
1044 int n = min(size, 8u);
1045 size -= n;
1046 if (mc->pos < mc->end)
1047 goto read_cached;
1049 rc = ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1050 &ctxt->exception);
1051 if (rc != X86EMUL_CONTINUE)
1052 return rc;
1053 mc->end += n;
1055 read_cached:
1056 memcpy(dest, mc->data + mc->pos, n);
1057 mc->pos += n;
1058 dest += n;
1059 addr += n;
1061 return X86EMUL_CONTINUE;
1064 static int segmented_read(struct x86_emulate_ctxt *ctxt,
1065 struct segmented_address addr,
1066 void *data,
1067 unsigned size)
1069 int rc;
1070 ulong linear;
1072 rc = linearize(ctxt, addr, size, false, &linear);
1073 if (rc != X86EMUL_CONTINUE)
1074 return rc;
1075 return read_emulated(ctxt, ctxt->ops, linear, data, size);
1078 static int segmented_write(struct x86_emulate_ctxt *ctxt,
1079 struct segmented_address addr,
1080 const void *data,
1081 unsigned size)
1083 int rc;
1084 ulong linear;
1086 rc = linearize(ctxt, addr, size, true, &linear);
1087 if (rc != X86EMUL_CONTINUE)
1088 return rc;
1089 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1090 &ctxt->exception);
1093 static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1094 struct segmented_address addr,
1095 const void *orig_data, const void *data,
1096 unsigned size)
1098 int rc;
1099 ulong linear;
1101 rc = linearize(ctxt, addr, size, true, &linear);
1102 if (rc != X86EMUL_CONTINUE)
1103 return rc;
1104 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1105 size, &ctxt->exception);
1108 static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1109 struct x86_emulate_ops *ops,
1110 unsigned int size, unsigned short port,
1111 void *dest)
1113 struct read_cache *rc = &ctxt->decode.io_read;
1115 if (rc->pos == rc->end) { /* refill pio read ahead */
1116 struct decode_cache *c = &ctxt->decode;
1117 unsigned int in_page, n;
1118 unsigned int count = c->rep_prefix ?
1119 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1120 in_page = (ctxt->eflags & EFLG_DF) ?
1121 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1122 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1123 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1124 count);
1125 if (n == 0)
1126 n = 1;
1127 rc->pos = rc->end = 0;
1128 if (!ops->pio_in_emulated(ctxt, size, port, rc->data, n))
1129 return 0;
1130 rc->end = n * size;
1133 memcpy(dest, rc->data + rc->pos, size);
1134 rc->pos += size;
1135 return 1;
1138 static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1139 struct x86_emulate_ops *ops,
1140 u16 selector, struct desc_ptr *dt)
1142 if (selector & 1 << 2) {
1143 struct desc_struct desc;
1144 memset (dt, 0, sizeof *dt);
1145 if (!ops->get_cached_descriptor(ctxt, &desc, NULL,
1146 VCPU_SREG_LDTR))
1147 return;
1149 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1150 dt->address = get_desc_base(&desc);
1151 } else
1152 ops->get_gdt(ctxt, dt);
1155 /* allowed just for 8 bytes segments */
1156 static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1157 struct x86_emulate_ops *ops,
1158 u16 selector, struct desc_struct *desc)
1160 struct desc_ptr dt;
1161 u16 index = selector >> 3;
1162 int ret;
1163 ulong addr;
1165 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1167 if (dt.size < index * 8 + 7)
1168 return emulate_gp(ctxt, selector & 0xfffc);
1169 addr = dt.address + index * 8;
1170 ret = ops->read_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
1172 return ret;
1175 /* allowed just for 8 bytes segments */
1176 static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1177 struct x86_emulate_ops *ops,
1178 u16 selector, struct desc_struct *desc)
1180 struct desc_ptr dt;
1181 u16 index = selector >> 3;
1182 ulong addr;
1183 int ret;
1185 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1187 if (dt.size < index * 8 + 7)
1188 return emulate_gp(ctxt, selector & 0xfffc);
1190 addr = dt.address + index * 8;
1191 ret = ops->write_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
1193 return ret;
1196 /* Does not support long mode */
1197 static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1198 struct x86_emulate_ops *ops,
1199 u16 selector, int seg)
1201 struct desc_struct seg_desc;
1202 u8 dpl, rpl, cpl;
1203 unsigned err_vec = GP_VECTOR;
1204 u32 err_code = 0;
1205 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1206 int ret;
1208 memset(&seg_desc, 0, sizeof seg_desc);
1210 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1211 || ctxt->mode == X86EMUL_MODE_REAL) {
1212 /* set real mode segment descriptor */
1213 set_desc_base(&seg_desc, selector << 4);
1214 set_desc_limit(&seg_desc, 0xffff);
1215 seg_desc.type = 3;
1216 seg_desc.p = 1;
1217 seg_desc.s = 1;
1218 goto load;
1221 /* NULL selector is not valid for TR, CS and SS */
1222 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1223 && null_selector)
1224 goto exception;
1226 /* TR should be in GDT only */
1227 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1228 goto exception;
1230 if (null_selector) /* for NULL selector skip all following checks */
1231 goto load;
1233 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1234 if (ret != X86EMUL_CONTINUE)
1235 return ret;
1237 err_code = selector & 0xfffc;
1238 err_vec = GP_VECTOR;
1240 /* can't load system descriptor into segment selecor */
1241 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1242 goto exception;
1244 if (!seg_desc.p) {
1245 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1246 goto exception;
1249 rpl = selector & 3;
1250 dpl = seg_desc.dpl;
1251 cpl = ops->cpl(ctxt);
1253 switch (seg) {
1254 case VCPU_SREG_SS:
1256 * segment is not a writable data segment or segment
1257 * selector's RPL != CPL or segment selector's RPL != CPL
1259 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1260 goto exception;
1261 break;
1262 case VCPU_SREG_CS:
1263 if (!(seg_desc.type & 8))
1264 goto exception;
1266 if (seg_desc.type & 4) {
1267 /* conforming */
1268 if (dpl > cpl)
1269 goto exception;
1270 } else {
1271 /* nonconforming */
1272 if (rpl > cpl || dpl != cpl)
1273 goto exception;
1275 /* CS(RPL) <- CPL */
1276 selector = (selector & 0xfffc) | cpl;
1277 break;
1278 case VCPU_SREG_TR:
1279 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1280 goto exception;
1281 break;
1282 case VCPU_SREG_LDTR:
1283 if (seg_desc.s || seg_desc.type != 2)
1284 goto exception;
1285 break;
1286 default: /* DS, ES, FS, or GS */
1288 * segment is not a data or readable code segment or
1289 * ((segment is a data or nonconforming code segment)
1290 * and (both RPL and CPL > DPL))
1292 if ((seg_desc.type & 0xa) == 0x8 ||
1293 (((seg_desc.type & 0xc) != 0xc) &&
1294 (rpl > dpl && cpl > dpl)))
1295 goto exception;
1296 break;
1299 if (seg_desc.s) {
1300 /* mark segment as accessed */
1301 seg_desc.type |= 1;
1302 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1303 if (ret != X86EMUL_CONTINUE)
1304 return ret;
1306 load:
1307 ops->set_segment_selector(ctxt, selector, seg);
1308 ops->set_cached_descriptor(ctxt, &seg_desc, 0, seg);
1309 return X86EMUL_CONTINUE;
1310 exception:
1311 emulate_exception(ctxt, err_vec, err_code, true);
1312 return X86EMUL_PROPAGATE_FAULT;
1315 static void write_register_operand(struct operand *op)
1317 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1318 switch (op->bytes) {
1319 case 1:
1320 *(u8 *)op->addr.reg = (u8)op->val;
1321 break;
1322 case 2:
1323 *(u16 *)op->addr.reg = (u16)op->val;
1324 break;
1325 case 4:
1326 *op->addr.reg = (u32)op->val;
1327 break; /* 64b: zero-extend */
1328 case 8:
1329 *op->addr.reg = op->val;
1330 break;
1334 static inline int writeback(struct x86_emulate_ctxt *ctxt,
1335 struct x86_emulate_ops *ops)
1337 int rc;
1338 struct decode_cache *c = &ctxt->decode;
1340 switch (c->dst.type) {
1341 case OP_REG:
1342 write_register_operand(&c->dst);
1343 break;
1344 case OP_MEM:
1345 if (c->lock_prefix)
1346 rc = segmented_cmpxchg(ctxt,
1347 c->dst.addr.mem,
1348 &c->dst.orig_val,
1349 &c->dst.val,
1350 c->dst.bytes);
1351 else
1352 rc = segmented_write(ctxt,
1353 c->dst.addr.mem,
1354 &c->dst.val,
1355 c->dst.bytes);
1356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
1358 break;
1359 case OP_XMM:
1360 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1361 break;
1362 case OP_NONE:
1363 /* no writeback */
1364 break;
1365 default:
1366 break;
1368 return X86EMUL_CONTINUE;
1371 static int em_push(struct x86_emulate_ctxt *ctxt)
1373 struct decode_cache *c = &ctxt->decode;
1374 struct segmented_address addr;
1376 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
1377 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1378 addr.seg = VCPU_SREG_SS;
1380 /* Disable writeback. */
1381 c->dst.type = OP_NONE;
1382 return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
1385 static int emulate_pop(struct x86_emulate_ctxt *ctxt,
1386 struct x86_emulate_ops *ops,
1387 void *dest, int len)
1389 struct decode_cache *c = &ctxt->decode;
1390 int rc;
1391 struct segmented_address addr;
1393 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1394 addr.seg = VCPU_SREG_SS;
1395 rc = segmented_read(ctxt, addr, dest, len);
1396 if (rc != X86EMUL_CONTINUE)
1397 return rc;
1399 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
1400 return rc;
1403 static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1404 struct x86_emulate_ops *ops,
1405 void *dest, int len)
1407 int rc;
1408 unsigned long val, change_mask;
1409 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1410 int cpl = ops->cpl(ctxt);
1412 rc = emulate_pop(ctxt, ops, &val, len);
1413 if (rc != X86EMUL_CONTINUE)
1414 return rc;
1416 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1417 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1419 switch(ctxt->mode) {
1420 case X86EMUL_MODE_PROT64:
1421 case X86EMUL_MODE_PROT32:
1422 case X86EMUL_MODE_PROT16:
1423 if (cpl == 0)
1424 change_mask |= EFLG_IOPL;
1425 if (cpl <= iopl)
1426 change_mask |= EFLG_IF;
1427 break;
1428 case X86EMUL_MODE_VM86:
1429 if (iopl < 3)
1430 return emulate_gp(ctxt, 0);
1431 change_mask |= EFLG_IF;
1432 break;
1433 default: /* real mode */
1434 change_mask |= (EFLG_IOPL | EFLG_IF);
1435 break;
1438 *(unsigned long *)dest =
1439 (ctxt->eflags & ~change_mask) | (val & change_mask);
1441 return rc;
1444 static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1445 struct x86_emulate_ops *ops, int seg)
1447 struct decode_cache *c = &ctxt->decode;
1449 c->src.val = ops->get_segment_selector(ctxt, seg);
1451 return em_push(ctxt);
1454 static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1455 struct x86_emulate_ops *ops, int seg)
1457 struct decode_cache *c = &ctxt->decode;
1458 unsigned long selector;
1459 int rc;
1461 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1462 if (rc != X86EMUL_CONTINUE)
1463 return rc;
1465 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
1466 return rc;
1469 static int emulate_pusha(struct x86_emulate_ctxt *ctxt)
1471 struct decode_cache *c = &ctxt->decode;
1472 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1473 int rc = X86EMUL_CONTINUE;
1474 int reg = VCPU_REGS_RAX;
1476 while (reg <= VCPU_REGS_RDI) {
1477 (reg == VCPU_REGS_RSP) ?
1478 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1480 rc = em_push(ctxt);
1481 if (rc != X86EMUL_CONTINUE)
1482 return rc;
1484 ++reg;
1487 return rc;
1490 static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1491 struct x86_emulate_ops *ops)
1493 struct decode_cache *c = &ctxt->decode;
1494 int rc = X86EMUL_CONTINUE;
1495 int reg = VCPU_REGS_RDI;
1497 while (reg >= VCPU_REGS_RAX) {
1498 if (reg == VCPU_REGS_RSP) {
1499 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1500 c->op_bytes);
1501 --reg;
1504 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
1505 if (rc != X86EMUL_CONTINUE)
1506 break;
1507 --reg;
1509 return rc;
1512 int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1513 struct x86_emulate_ops *ops, int irq)
1515 struct decode_cache *c = &ctxt->decode;
1516 int rc;
1517 struct desc_ptr dt;
1518 gva_t cs_addr;
1519 gva_t eip_addr;
1520 u16 cs, eip;
1522 /* TODO: Add limit checks */
1523 c->src.val = ctxt->eflags;
1524 rc = em_push(ctxt);
1525 if (rc != X86EMUL_CONTINUE)
1526 return rc;
1528 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1530 c->src.val = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
1531 rc = em_push(ctxt);
1532 if (rc != X86EMUL_CONTINUE)
1533 return rc;
1535 c->src.val = c->eip;
1536 rc = em_push(ctxt);
1537 if (rc != X86EMUL_CONTINUE)
1538 return rc;
1540 ops->get_idt(ctxt, &dt);
1542 eip_addr = dt.address + (irq << 2);
1543 cs_addr = dt.address + (irq << 2) + 2;
1545 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
1546 if (rc != X86EMUL_CONTINUE)
1547 return rc;
1549 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
1550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
1553 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1554 if (rc != X86EMUL_CONTINUE)
1555 return rc;
1557 c->eip = eip;
1559 return rc;
1562 static int emulate_int(struct x86_emulate_ctxt *ctxt,
1563 struct x86_emulate_ops *ops, int irq)
1565 switch(ctxt->mode) {
1566 case X86EMUL_MODE_REAL:
1567 return emulate_int_real(ctxt, ops, irq);
1568 case X86EMUL_MODE_VM86:
1569 case X86EMUL_MODE_PROT16:
1570 case X86EMUL_MODE_PROT32:
1571 case X86EMUL_MODE_PROT64:
1572 default:
1573 /* Protected mode interrupts unimplemented yet */
1574 return X86EMUL_UNHANDLEABLE;
1578 static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1579 struct x86_emulate_ops *ops)
1581 struct decode_cache *c = &ctxt->decode;
1582 int rc = X86EMUL_CONTINUE;
1583 unsigned long temp_eip = 0;
1584 unsigned long temp_eflags = 0;
1585 unsigned long cs = 0;
1586 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1587 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1588 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1589 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1591 /* TODO: Add stack limit check */
1593 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1595 if (rc != X86EMUL_CONTINUE)
1596 return rc;
1598 if (temp_eip & ~0xffff)
1599 return emulate_gp(ctxt, 0);
1601 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1603 if (rc != X86EMUL_CONTINUE)
1604 return rc;
1606 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1608 if (rc != X86EMUL_CONTINUE)
1609 return rc;
1611 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1613 if (rc != X86EMUL_CONTINUE)
1614 return rc;
1616 c->eip = temp_eip;
1619 if (c->op_bytes == 4)
1620 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1621 else if (c->op_bytes == 2) {
1622 ctxt->eflags &= ~0xffff;
1623 ctxt->eflags |= temp_eflags;
1626 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1627 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1629 return rc;
1632 static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1633 struct x86_emulate_ops* ops)
1635 switch(ctxt->mode) {
1636 case X86EMUL_MODE_REAL:
1637 return emulate_iret_real(ctxt, ops);
1638 case X86EMUL_MODE_VM86:
1639 case X86EMUL_MODE_PROT16:
1640 case X86EMUL_MODE_PROT32:
1641 case X86EMUL_MODE_PROT64:
1642 default:
1643 /* iret from protected mode unimplemented yet */
1644 return X86EMUL_UNHANDLEABLE;
1648 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1649 struct x86_emulate_ops *ops)
1651 struct decode_cache *c = &ctxt->decode;
1653 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
1656 static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
1658 struct decode_cache *c = &ctxt->decode;
1659 switch (c->modrm_reg) {
1660 case 0: /* rol */
1661 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
1662 break;
1663 case 1: /* ror */
1664 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
1665 break;
1666 case 2: /* rcl */
1667 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
1668 break;
1669 case 3: /* rcr */
1670 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
1671 break;
1672 case 4: /* sal/shl */
1673 case 6: /* sal/shl */
1674 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
1675 break;
1676 case 5: /* shr */
1677 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
1678 break;
1679 case 7: /* sar */
1680 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
1681 break;
1685 static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
1686 struct x86_emulate_ops *ops)
1688 struct decode_cache *c = &ctxt->decode;
1689 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1690 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
1691 u8 de = 0;
1693 switch (c->modrm_reg) {
1694 case 0 ... 1: /* test */
1695 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
1696 break;
1697 case 2: /* not */
1698 c->dst.val = ~c->dst.val;
1699 break;
1700 case 3: /* neg */
1701 emulate_1op("neg", c->dst, ctxt->eflags);
1702 break;
1703 case 4: /* mul */
1704 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1705 break;
1706 case 5: /* imul */
1707 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1708 break;
1709 case 6: /* div */
1710 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1711 ctxt->eflags, de);
1712 break;
1713 case 7: /* idiv */
1714 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1715 ctxt->eflags, de);
1716 break;
1717 default:
1718 return X86EMUL_UNHANDLEABLE;
1720 if (de)
1721 return emulate_de(ctxt);
1722 return X86EMUL_CONTINUE;
1725 static int emulate_grp45(struct x86_emulate_ctxt *ctxt)
1727 struct decode_cache *c = &ctxt->decode;
1728 int rc = X86EMUL_CONTINUE;
1730 switch (c->modrm_reg) {
1731 case 0: /* inc */
1732 emulate_1op("inc", c->dst, ctxt->eflags);
1733 break;
1734 case 1: /* dec */
1735 emulate_1op("dec", c->dst, ctxt->eflags);
1736 break;
1737 case 2: /* call near abs */ {
1738 long int old_eip;
1739 old_eip = c->eip;
1740 c->eip = c->src.val;
1741 c->src.val = old_eip;
1742 rc = em_push(ctxt);
1743 break;
1745 case 4: /* jmp abs */
1746 c->eip = c->src.val;
1747 break;
1748 case 6: /* push */
1749 rc = em_push(ctxt);
1750 break;
1752 return rc;
1755 static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1756 struct x86_emulate_ops *ops)
1758 struct decode_cache *c = &ctxt->decode;
1759 u64 old = c->dst.orig_val64;
1761 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1762 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1763 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1764 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
1765 ctxt->eflags &= ~EFLG_ZF;
1766 } else {
1767 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1768 (u32) c->regs[VCPU_REGS_RBX];
1770 ctxt->eflags |= EFLG_ZF;
1772 return X86EMUL_CONTINUE;
1775 static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1776 struct x86_emulate_ops *ops)
1778 struct decode_cache *c = &ctxt->decode;
1779 int rc;
1780 unsigned long cs;
1782 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1783 if (rc != X86EMUL_CONTINUE)
1784 return rc;
1785 if (c->op_bytes == 4)
1786 c->eip = (u32)c->eip;
1787 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
1790 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1791 return rc;
1794 static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1795 struct x86_emulate_ops *ops, int seg)
1797 struct decode_cache *c = &ctxt->decode;
1798 unsigned short sel;
1799 int rc;
1801 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1803 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1804 if (rc != X86EMUL_CONTINUE)
1805 return rc;
1807 c->dst.val = c->src.val;
1808 return rc;
1811 static inline void
1812 setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1813 struct x86_emulate_ops *ops, struct desc_struct *cs,
1814 struct desc_struct *ss)
1816 memset(cs, 0, sizeof(struct desc_struct));
1817 ops->get_cached_descriptor(ctxt, cs, NULL, VCPU_SREG_CS);
1818 memset(ss, 0, sizeof(struct desc_struct));
1820 cs->l = 0; /* will be adjusted later */
1821 set_desc_base(cs, 0); /* flat segment */
1822 cs->g = 1; /* 4kb granularity */
1823 set_desc_limit(cs, 0xfffff); /* 4GB limit */
1824 cs->type = 0x0b; /* Read, Execute, Accessed */
1825 cs->s = 1;
1826 cs->dpl = 0; /* will be adjusted later */
1827 cs->p = 1;
1828 cs->d = 1;
1830 set_desc_base(ss, 0); /* flat segment */
1831 set_desc_limit(ss, 0xfffff); /* 4GB limit */
1832 ss->g = 1; /* 4kb granularity */
1833 ss->s = 1;
1834 ss->type = 0x03; /* Read/Write, Accessed */
1835 ss->d = 1; /* 32bit stack segment */
1836 ss->dpl = 0;
1837 ss->p = 1;
1840 static int
1841 emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1843 struct decode_cache *c = &ctxt->decode;
1844 struct desc_struct cs, ss;
1845 u64 msr_data;
1846 u16 cs_sel, ss_sel;
1847 u64 efer = 0;
1849 /* syscall is not available in real mode */
1850 if (ctxt->mode == X86EMUL_MODE_REAL ||
1851 ctxt->mode == X86EMUL_MODE_VM86)
1852 return emulate_ud(ctxt);
1854 ops->get_msr(ctxt, MSR_EFER, &efer);
1855 setup_syscalls_segments(ctxt, ops, &cs, &ss);
1857 ops->get_msr(ctxt, MSR_STAR, &msr_data);
1858 msr_data >>= 32;
1859 cs_sel = (u16)(msr_data & 0xfffc);
1860 ss_sel = (u16)(msr_data + 8);
1862 if (efer & EFER_LMA) {
1863 cs.d = 0;
1864 cs.l = 1;
1866 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1867 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1868 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1869 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
1871 c->regs[VCPU_REGS_RCX] = c->eip;
1872 if (efer & EFER_LMA) {
1873 #ifdef CONFIG_X86_64
1874 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1876 ops->get_msr(ctxt,
1877 ctxt->mode == X86EMUL_MODE_PROT64 ?
1878 MSR_LSTAR : MSR_CSTAR, &msr_data);
1879 c->eip = msr_data;
1881 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
1882 ctxt->eflags &= ~(msr_data | EFLG_RF);
1883 #endif
1884 } else {
1885 /* legacy mode */
1886 ops->get_msr(ctxt, MSR_STAR, &msr_data);
1887 c->eip = (u32)msr_data;
1889 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1892 return X86EMUL_CONTINUE;
1895 static int
1896 emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1898 struct decode_cache *c = &ctxt->decode;
1899 struct desc_struct cs, ss;
1900 u64 msr_data;
1901 u16 cs_sel, ss_sel;
1902 u64 efer = 0;
1904 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1905 /* inject #GP if in real mode */
1906 if (ctxt->mode == X86EMUL_MODE_REAL)
1907 return emulate_gp(ctxt, 0);
1909 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1910 * Therefore, we inject an #UD.
1912 if (ctxt->mode == X86EMUL_MODE_PROT64)
1913 return emulate_ud(ctxt);
1915 setup_syscalls_segments(ctxt, ops, &cs, &ss);
1917 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
1918 switch (ctxt->mode) {
1919 case X86EMUL_MODE_PROT32:
1920 if ((msr_data & 0xfffc) == 0x0)
1921 return emulate_gp(ctxt, 0);
1922 break;
1923 case X86EMUL_MODE_PROT64:
1924 if (msr_data == 0x0)
1925 return emulate_gp(ctxt, 0);
1926 break;
1929 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1930 cs_sel = (u16)msr_data;
1931 cs_sel &= ~SELECTOR_RPL_MASK;
1932 ss_sel = cs_sel + 8;
1933 ss_sel &= ~SELECTOR_RPL_MASK;
1934 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
1935 cs.d = 0;
1936 cs.l = 1;
1939 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1940 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1941 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1942 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
1944 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
1945 c->eip = msr_data;
1947 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
1948 c->regs[VCPU_REGS_RSP] = msr_data;
1950 return X86EMUL_CONTINUE;
1953 static int
1954 emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
1956 struct decode_cache *c = &ctxt->decode;
1957 struct desc_struct cs, ss;
1958 u64 msr_data;
1959 int usermode;
1960 u16 cs_sel, ss_sel;
1962 /* inject #GP if in real mode or Virtual 8086 mode */
1963 if (ctxt->mode == X86EMUL_MODE_REAL ||
1964 ctxt->mode == X86EMUL_MODE_VM86)
1965 return emulate_gp(ctxt, 0);
1967 setup_syscalls_segments(ctxt, ops, &cs, &ss);
1969 if ((c->rex_prefix & 0x8) != 0x0)
1970 usermode = X86EMUL_MODE_PROT64;
1971 else
1972 usermode = X86EMUL_MODE_PROT32;
1974 cs.dpl = 3;
1975 ss.dpl = 3;
1976 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
1977 switch (usermode) {
1978 case X86EMUL_MODE_PROT32:
1979 cs_sel = (u16)(msr_data + 16);
1980 if ((msr_data & 0xfffc) == 0x0)
1981 return emulate_gp(ctxt, 0);
1982 ss_sel = (u16)(msr_data + 24);
1983 break;
1984 case X86EMUL_MODE_PROT64:
1985 cs_sel = (u16)(msr_data + 32);
1986 if (msr_data == 0x0)
1987 return emulate_gp(ctxt, 0);
1988 ss_sel = cs_sel + 8;
1989 cs.d = 0;
1990 cs.l = 1;
1991 break;
1993 cs_sel |= SELECTOR_RPL_MASK;
1994 ss_sel |= SELECTOR_RPL_MASK;
1996 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1997 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1998 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1999 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
2001 c->eip = c->regs[VCPU_REGS_RDX];
2002 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
2004 return X86EMUL_CONTINUE;
2007 static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2008 struct x86_emulate_ops *ops)
2010 int iopl;
2011 if (ctxt->mode == X86EMUL_MODE_REAL)
2012 return false;
2013 if (ctxt->mode == X86EMUL_MODE_VM86)
2014 return true;
2015 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
2016 return ops->cpl(ctxt) > iopl;
2019 static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2020 struct x86_emulate_ops *ops,
2021 u16 port, u16 len)
2023 struct desc_struct tr_seg;
2024 u32 base3;
2025 int r;
2026 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
2027 unsigned mask = (1 << len) - 1;
2028 unsigned long base;
2030 ops->get_cached_descriptor(ctxt, &tr_seg, &base3, VCPU_SREG_TR);
2031 if (!tr_seg.p)
2032 return false;
2033 if (desc_limit_scaled(&tr_seg) < 103)
2034 return false;
2035 base = get_desc_base(&tr_seg);
2036 #ifdef CONFIG_X86_64
2037 base |= ((u64)base3) << 32;
2038 #endif
2039 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
2040 if (r != X86EMUL_CONTINUE)
2041 return false;
2042 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
2043 return false;
2044 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
2045 if (r != X86EMUL_CONTINUE)
2046 return false;
2047 if ((perm >> bit_idx) & mask)
2048 return false;
2049 return true;
2052 static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2053 struct x86_emulate_ops *ops,
2054 u16 port, u16 len)
2056 if (ctxt->perm_ok)
2057 return true;
2059 if (emulator_bad_iopl(ctxt, ops))
2060 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2061 return false;
2063 ctxt->perm_ok = true;
2065 return true;
2068 static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2069 struct x86_emulate_ops *ops,
2070 struct tss_segment_16 *tss)
2072 struct decode_cache *c = &ctxt->decode;
2074 tss->ip = c->eip;
2075 tss->flag = ctxt->eflags;
2076 tss->ax = c->regs[VCPU_REGS_RAX];
2077 tss->cx = c->regs[VCPU_REGS_RCX];
2078 tss->dx = c->regs[VCPU_REGS_RDX];
2079 tss->bx = c->regs[VCPU_REGS_RBX];
2080 tss->sp = c->regs[VCPU_REGS_RSP];
2081 tss->bp = c->regs[VCPU_REGS_RBP];
2082 tss->si = c->regs[VCPU_REGS_RSI];
2083 tss->di = c->regs[VCPU_REGS_RDI];
2085 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2086 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2087 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2088 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2089 tss->ldt = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
2092 static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2093 struct x86_emulate_ops *ops,
2094 struct tss_segment_16 *tss)
2096 struct decode_cache *c = &ctxt->decode;
2097 int ret;
2099 c->eip = tss->ip;
2100 ctxt->eflags = tss->flag | 2;
2101 c->regs[VCPU_REGS_RAX] = tss->ax;
2102 c->regs[VCPU_REGS_RCX] = tss->cx;
2103 c->regs[VCPU_REGS_RDX] = tss->dx;
2104 c->regs[VCPU_REGS_RBX] = tss->bx;
2105 c->regs[VCPU_REGS_RSP] = tss->sp;
2106 c->regs[VCPU_REGS_RBP] = tss->bp;
2107 c->regs[VCPU_REGS_RSI] = tss->si;
2108 c->regs[VCPU_REGS_RDI] = tss->di;
2111 * SDM says that segment selectors are loaded before segment
2112 * descriptors
2114 ops->set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2115 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2116 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2117 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2118 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2121 * Now load segment descriptors. If fault happenes at this stage
2122 * it is handled in a context of new task
2124 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2125 if (ret != X86EMUL_CONTINUE)
2126 return ret;
2127 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2128 if (ret != X86EMUL_CONTINUE)
2129 return ret;
2130 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2131 if (ret != X86EMUL_CONTINUE)
2132 return ret;
2133 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
2136 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2137 if (ret != X86EMUL_CONTINUE)
2138 return ret;
2140 return X86EMUL_CONTINUE;
2143 static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2144 struct x86_emulate_ops *ops,
2145 u16 tss_selector, u16 old_tss_sel,
2146 ulong old_tss_base, struct desc_struct *new_desc)
2148 struct tss_segment_16 tss_seg;
2149 int ret;
2150 u32 new_tss_base = get_desc_base(new_desc);
2152 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
2153 &ctxt->exception);
2154 if (ret != X86EMUL_CONTINUE)
2155 /* FIXME: need to provide precise fault address */
2156 return ret;
2158 save_state_to_tss16(ctxt, ops, &tss_seg);
2160 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
2161 &ctxt->exception);
2162 if (ret != X86EMUL_CONTINUE)
2163 /* FIXME: need to provide precise fault address */
2164 return ret;
2166 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
2167 &ctxt->exception);
2168 if (ret != X86EMUL_CONTINUE)
2169 /* FIXME: need to provide precise fault address */
2170 return ret;
2172 if (old_tss_sel != 0xffff) {
2173 tss_seg.prev_task_link = old_tss_sel;
2175 ret = ops->write_std(ctxt, new_tss_base,
2176 &tss_seg.prev_task_link,
2177 sizeof tss_seg.prev_task_link,
2178 &ctxt->exception);
2179 if (ret != X86EMUL_CONTINUE)
2180 /* FIXME: need to provide precise fault address */
2181 return ret;
2184 return load_state_from_tss16(ctxt, ops, &tss_seg);
2187 static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2188 struct x86_emulate_ops *ops,
2189 struct tss_segment_32 *tss)
2191 struct decode_cache *c = &ctxt->decode;
2193 tss->cr3 = ops->get_cr(ctxt, 3);
2194 tss->eip = c->eip;
2195 tss->eflags = ctxt->eflags;
2196 tss->eax = c->regs[VCPU_REGS_RAX];
2197 tss->ecx = c->regs[VCPU_REGS_RCX];
2198 tss->edx = c->regs[VCPU_REGS_RDX];
2199 tss->ebx = c->regs[VCPU_REGS_RBX];
2200 tss->esp = c->regs[VCPU_REGS_RSP];
2201 tss->ebp = c->regs[VCPU_REGS_RBP];
2202 tss->esi = c->regs[VCPU_REGS_RSI];
2203 tss->edi = c->regs[VCPU_REGS_RDI];
2205 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2206 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2207 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2208 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2209 tss->fs = ops->get_segment_selector(ctxt, VCPU_SREG_FS);
2210 tss->gs = ops->get_segment_selector(ctxt, VCPU_SREG_GS);
2211 tss->ldt_selector = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
2214 static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2215 struct x86_emulate_ops *ops,
2216 struct tss_segment_32 *tss)
2218 struct decode_cache *c = &ctxt->decode;
2219 int ret;
2221 if (ops->set_cr(ctxt, 3, tss->cr3))
2222 return emulate_gp(ctxt, 0);
2223 c->eip = tss->eip;
2224 ctxt->eflags = tss->eflags | 2;
2225 c->regs[VCPU_REGS_RAX] = tss->eax;
2226 c->regs[VCPU_REGS_RCX] = tss->ecx;
2227 c->regs[VCPU_REGS_RDX] = tss->edx;
2228 c->regs[VCPU_REGS_RBX] = tss->ebx;
2229 c->regs[VCPU_REGS_RSP] = tss->esp;
2230 c->regs[VCPU_REGS_RBP] = tss->ebp;
2231 c->regs[VCPU_REGS_RSI] = tss->esi;
2232 c->regs[VCPU_REGS_RDI] = tss->edi;
2235 * SDM says that segment selectors are loaded before segment
2236 * descriptors
2238 ops->set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2239 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2240 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2241 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2242 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2243 ops->set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2244 ops->set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
2247 * Now load segment descriptors. If fault happenes at this stage
2248 * it is handled in a context of new task
2250 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2251 if (ret != X86EMUL_CONTINUE)
2252 return ret;
2253 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2254 if (ret != X86EMUL_CONTINUE)
2255 return ret;
2256 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2257 if (ret != X86EMUL_CONTINUE)
2258 return ret;
2259 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2260 if (ret != X86EMUL_CONTINUE)
2261 return ret;
2262 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2263 if (ret != X86EMUL_CONTINUE)
2264 return ret;
2265 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2266 if (ret != X86EMUL_CONTINUE)
2267 return ret;
2268 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2269 if (ret != X86EMUL_CONTINUE)
2270 return ret;
2272 return X86EMUL_CONTINUE;
2275 static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2276 struct x86_emulate_ops *ops,
2277 u16 tss_selector, u16 old_tss_sel,
2278 ulong old_tss_base, struct desc_struct *new_desc)
2280 struct tss_segment_32 tss_seg;
2281 int ret;
2282 u32 new_tss_base = get_desc_base(new_desc);
2284 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
2285 &ctxt->exception);
2286 if (ret != X86EMUL_CONTINUE)
2287 /* FIXME: need to provide precise fault address */
2288 return ret;
2290 save_state_to_tss32(ctxt, ops, &tss_seg);
2292 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
2293 &ctxt->exception);
2294 if (ret != X86EMUL_CONTINUE)
2295 /* FIXME: need to provide precise fault address */
2296 return ret;
2298 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
2299 &ctxt->exception);
2300 if (ret != X86EMUL_CONTINUE)
2301 /* FIXME: need to provide precise fault address */
2302 return ret;
2304 if (old_tss_sel != 0xffff) {
2305 tss_seg.prev_task_link = old_tss_sel;
2307 ret = ops->write_std(ctxt, new_tss_base,
2308 &tss_seg.prev_task_link,
2309 sizeof tss_seg.prev_task_link,
2310 &ctxt->exception);
2311 if (ret != X86EMUL_CONTINUE)
2312 /* FIXME: need to provide precise fault address */
2313 return ret;
2316 return load_state_from_tss32(ctxt, ops, &tss_seg);
2319 static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
2320 struct x86_emulate_ops *ops,
2321 u16 tss_selector, int reason,
2322 bool has_error_code, u32 error_code)
2324 struct desc_struct curr_tss_desc, next_tss_desc;
2325 int ret;
2326 u16 old_tss_sel = ops->get_segment_selector(ctxt, VCPU_SREG_TR);
2327 ulong old_tss_base =
2328 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
2329 u32 desc_limit;
2331 /* FIXME: old_tss_base == ~0 ? */
2333 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2334 if (ret != X86EMUL_CONTINUE)
2335 return ret;
2336 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2337 if (ret != X86EMUL_CONTINUE)
2338 return ret;
2340 /* FIXME: check that next_tss_desc is tss */
2342 if (reason != TASK_SWITCH_IRET) {
2343 if ((tss_selector & 3) > next_tss_desc.dpl ||
2344 ops->cpl(ctxt) > next_tss_desc.dpl)
2345 return emulate_gp(ctxt, 0);
2348 desc_limit = desc_limit_scaled(&next_tss_desc);
2349 if (!next_tss_desc.p ||
2350 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2351 desc_limit < 0x2b)) {
2352 emulate_ts(ctxt, tss_selector & 0xfffc);
2353 return X86EMUL_PROPAGATE_FAULT;
2356 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2357 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2358 write_segment_descriptor(ctxt, ops, old_tss_sel,
2359 &curr_tss_desc);
2362 if (reason == TASK_SWITCH_IRET)
2363 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2365 /* set back link to prev task only if NT bit is set in eflags
2366 note that old_tss_sel is not used afetr this point */
2367 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2368 old_tss_sel = 0xffff;
2370 if (next_tss_desc.type & 8)
2371 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2372 old_tss_base, &next_tss_desc);
2373 else
2374 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2375 old_tss_base, &next_tss_desc);
2376 if (ret != X86EMUL_CONTINUE)
2377 return ret;
2379 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2380 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2382 if (reason != TASK_SWITCH_IRET) {
2383 next_tss_desc.type |= (1 << 1); /* set busy flag */
2384 write_segment_descriptor(ctxt, ops, tss_selector,
2385 &next_tss_desc);
2388 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
2389 ops->set_cached_descriptor(ctxt, &next_tss_desc, 0, VCPU_SREG_TR);
2390 ops->set_segment_selector(ctxt, tss_selector, VCPU_SREG_TR);
2392 if (has_error_code) {
2393 struct decode_cache *c = &ctxt->decode;
2395 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2396 c->lock_prefix = 0;
2397 c->src.val = (unsigned long) error_code;
2398 ret = em_push(ctxt);
2401 return ret;
2404 int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2405 u16 tss_selector, int reason,
2406 bool has_error_code, u32 error_code)
2408 struct x86_emulate_ops *ops = ctxt->ops;
2409 struct decode_cache *c = &ctxt->decode;
2410 int rc;
2412 c->eip = ctxt->eip;
2413 c->dst.type = OP_NONE;
2415 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2416 has_error_code, error_code);
2418 if (rc == X86EMUL_CONTINUE)
2419 ctxt->eip = c->eip;
2421 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
2424 static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
2425 int reg, struct operand *op)
2427 struct decode_cache *c = &ctxt->decode;
2428 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2430 register_address_increment(c, &c->regs[reg], df * op->bytes);
2431 op->addr.mem.ea = register_address(c, c->regs[reg]);
2432 op->addr.mem.seg = seg;
2435 static int em_das(struct x86_emulate_ctxt *ctxt)
2437 struct decode_cache *c = &ctxt->decode;
2438 u8 al, old_al;
2439 bool af, cf, old_cf;
2441 cf = ctxt->eflags & X86_EFLAGS_CF;
2442 al = c->dst.val;
2444 old_al = al;
2445 old_cf = cf;
2446 cf = false;
2447 af = ctxt->eflags & X86_EFLAGS_AF;
2448 if ((al & 0x0f) > 9 || af) {
2449 al -= 6;
2450 cf = old_cf | (al >= 250);
2451 af = true;
2452 } else {
2453 af = false;
2455 if (old_al > 0x99 || old_cf) {
2456 al -= 0x60;
2457 cf = true;
2460 c->dst.val = al;
2461 /* Set PF, ZF, SF */
2462 c->src.type = OP_IMM;
2463 c->src.val = 0;
2464 c->src.bytes = 1;
2465 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2466 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2467 if (cf)
2468 ctxt->eflags |= X86_EFLAGS_CF;
2469 if (af)
2470 ctxt->eflags |= X86_EFLAGS_AF;
2471 return X86EMUL_CONTINUE;
2474 static int em_call_far(struct x86_emulate_ctxt *ctxt)
2476 struct decode_cache *c = &ctxt->decode;
2477 u16 sel, old_cs;
2478 ulong old_eip;
2479 int rc;
2481 old_cs = ctxt->ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2482 old_eip = c->eip;
2484 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2485 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2486 return X86EMUL_CONTINUE;
2488 c->eip = 0;
2489 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2491 c->src.val = old_cs;
2492 rc = em_push(ctxt);
2493 if (rc != X86EMUL_CONTINUE)
2494 return rc;
2496 c->src.val = old_eip;
2497 return em_push(ctxt);
2500 static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2502 struct decode_cache *c = &ctxt->decode;
2503 int rc;
2505 c->dst.type = OP_REG;
2506 c->dst.addr.reg = &c->eip;
2507 c->dst.bytes = c->op_bytes;
2508 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2509 if (rc != X86EMUL_CONTINUE)
2510 return rc;
2511 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2512 return X86EMUL_CONTINUE;
2515 static int em_imul(struct x86_emulate_ctxt *ctxt)
2517 struct decode_cache *c = &ctxt->decode;
2519 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2520 return X86EMUL_CONTINUE;
2523 static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2525 struct decode_cache *c = &ctxt->decode;
2527 c->dst.val = c->src2.val;
2528 return em_imul(ctxt);
2531 static int em_cwd(struct x86_emulate_ctxt *ctxt)
2533 struct decode_cache *c = &ctxt->decode;
2535 c->dst.type = OP_REG;
2536 c->dst.bytes = c->src.bytes;
2537 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2538 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2540 return X86EMUL_CONTINUE;
2543 static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2545 struct decode_cache *c = &ctxt->decode;
2546 u64 tsc = 0;
2548 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
2549 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2550 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2551 return X86EMUL_CONTINUE;
2554 static int em_mov(struct x86_emulate_ctxt *ctxt)
2556 struct decode_cache *c = &ctxt->decode;
2557 c->dst.val = c->src.val;
2558 return X86EMUL_CONTINUE;
2561 static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2563 struct decode_cache *c = &ctxt->decode;
2564 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2565 return X86EMUL_CONTINUE;
2568 static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2570 struct decode_cache *c = &ctxt->decode;
2571 int rc;
2572 ulong linear;
2574 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
2575 if (rc == X86EMUL_CONTINUE)
2576 ctxt->ops->invlpg(ctxt, linear);
2577 /* Disable writeback. */
2578 c->dst.type = OP_NONE;
2579 return X86EMUL_CONTINUE;
2582 static int em_clts(struct x86_emulate_ctxt *ctxt)
2584 ulong cr0;
2586 cr0 = ctxt->ops->get_cr(ctxt, 0);
2587 cr0 &= ~X86_CR0_TS;
2588 ctxt->ops->set_cr(ctxt, 0, cr0);
2589 return X86EMUL_CONTINUE;
2592 static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2594 struct decode_cache *c = &ctxt->decode;
2595 int rc;
2597 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2598 return X86EMUL_UNHANDLEABLE;
2600 rc = ctxt->ops->fix_hypercall(ctxt);
2601 if (rc != X86EMUL_CONTINUE)
2602 return rc;
2604 /* Let the processor re-execute the fixed hypercall */
2605 c->eip = ctxt->eip;
2606 /* Disable writeback. */
2607 c->dst.type = OP_NONE;
2608 return X86EMUL_CONTINUE;
2611 static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2613 struct decode_cache *c = &ctxt->decode;
2614 struct desc_ptr desc_ptr;
2615 int rc;
2617 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2618 &desc_ptr.size, &desc_ptr.address,
2619 c->op_bytes);
2620 if (rc != X86EMUL_CONTINUE)
2621 return rc;
2622 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2623 /* Disable writeback. */
2624 c->dst.type = OP_NONE;
2625 return X86EMUL_CONTINUE;
2628 static int em_svm(struct x86_emulate_ctxt *ctxt)
2630 struct decode_cache *c = &ctxt->decode;
2631 int rc;
2633 switch (c->modrm_rm) {
2634 case 1:
2635 rc = ctxt->ops->fix_hypercall(ctxt);
2636 break;
2637 default:
2638 return X86EMUL_UNHANDLEABLE;
2640 /* Disable writeback. */
2641 c->dst.type = OP_NONE;
2642 return rc;
2645 static int em_lidt(struct x86_emulate_ctxt *ctxt)
2647 struct decode_cache *c = &ctxt->decode;
2648 struct desc_ptr desc_ptr;
2649 int rc;
2651 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2652 &desc_ptr.size,
2653 &desc_ptr.address,
2654 c->op_bytes);
2655 if (rc != X86EMUL_CONTINUE)
2656 return rc;
2657 ctxt->ops->set_idt(ctxt, &desc_ptr);
2658 /* Disable writeback. */
2659 c->dst.type = OP_NONE;
2660 return X86EMUL_CONTINUE;
2663 static int em_smsw(struct x86_emulate_ctxt *ctxt)
2665 struct decode_cache *c = &ctxt->decode;
2667 c->dst.bytes = 2;
2668 c->dst.val = ctxt->ops->get_cr(ctxt, 0);
2669 return X86EMUL_CONTINUE;
2672 static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2674 struct decode_cache *c = &ctxt->decode;
2675 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
2676 | (c->src.val & 0x0f));
2677 c->dst.type = OP_NONE;
2678 return X86EMUL_CONTINUE;
2681 static bool valid_cr(int nr)
2683 switch (nr) {
2684 case 0:
2685 case 2 ... 4:
2686 case 8:
2687 return true;
2688 default:
2689 return false;
2693 static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2695 struct decode_cache *c = &ctxt->decode;
2697 if (!valid_cr(c->modrm_reg))
2698 return emulate_ud(ctxt);
2700 return X86EMUL_CONTINUE;
2703 static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2705 struct decode_cache *c = &ctxt->decode;
2706 u64 new_val = c->src.val64;
2707 int cr = c->modrm_reg;
2708 u64 efer = 0;
2710 static u64 cr_reserved_bits[] = {
2711 0xffffffff00000000ULL,
2712 0, 0, 0, /* CR3 checked later */
2713 CR4_RESERVED_BITS,
2714 0, 0, 0,
2715 CR8_RESERVED_BITS,
2718 if (!valid_cr(cr))
2719 return emulate_ud(ctxt);
2721 if (new_val & cr_reserved_bits[cr])
2722 return emulate_gp(ctxt, 0);
2724 switch (cr) {
2725 case 0: {
2726 u64 cr4;
2727 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2728 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2729 return emulate_gp(ctxt, 0);
2731 cr4 = ctxt->ops->get_cr(ctxt, 4);
2732 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2734 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2735 !(cr4 & X86_CR4_PAE))
2736 return emulate_gp(ctxt, 0);
2738 break;
2740 case 3: {
2741 u64 rsvd = 0;
2743 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2744 if (efer & EFER_LMA)
2745 rsvd = CR3_L_MODE_RESERVED_BITS;
2746 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
2747 rsvd = CR3_PAE_RESERVED_BITS;
2748 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
2749 rsvd = CR3_NONPAE_RESERVED_BITS;
2751 if (new_val & rsvd)
2752 return emulate_gp(ctxt, 0);
2754 break;
2756 case 4: {
2757 u64 cr4;
2759 cr4 = ctxt->ops->get_cr(ctxt, 4);
2760 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2762 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2763 return emulate_gp(ctxt, 0);
2765 break;
2769 return X86EMUL_CONTINUE;
2772 static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2774 unsigned long dr7;
2776 ctxt->ops->get_dr(ctxt, 7, &dr7);
2778 /* Check if DR7.Global_Enable is set */
2779 return dr7 & (1 << 13);
2782 static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2784 struct decode_cache *c = &ctxt->decode;
2785 int dr = c->modrm_reg;
2786 u64 cr4;
2788 if (dr > 7)
2789 return emulate_ud(ctxt);
2791 cr4 = ctxt->ops->get_cr(ctxt, 4);
2792 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2793 return emulate_ud(ctxt);
2795 if (check_dr7_gd(ctxt))
2796 return emulate_db(ctxt);
2798 return X86EMUL_CONTINUE;
2801 static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2803 struct decode_cache *c = &ctxt->decode;
2804 u64 new_val = c->src.val64;
2805 int dr = c->modrm_reg;
2807 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2808 return emulate_gp(ctxt, 0);
2810 return check_dr_read(ctxt);
2813 static int check_svme(struct x86_emulate_ctxt *ctxt)
2815 u64 efer;
2817 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2819 if (!(efer & EFER_SVME))
2820 return emulate_ud(ctxt);
2822 return X86EMUL_CONTINUE;
2825 static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2827 u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
2829 /* Valid physical address? */
2830 if (rax & 0xffff000000000000ULL)
2831 return emulate_gp(ctxt, 0);
2833 return check_svme(ctxt);
2836 static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2838 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
2840 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
2841 return emulate_ud(ctxt);
2843 return X86EMUL_CONTINUE;
2846 static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2848 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
2849 u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
2851 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
2852 (rcx > 3))
2853 return emulate_gp(ctxt, 0);
2855 return X86EMUL_CONTINUE;
2858 static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2860 struct decode_cache *c = &ctxt->decode;
2862 c->dst.bytes = min(c->dst.bytes, 4u);
2863 if (!emulator_io_permited(ctxt, ctxt->ops, c->src.val, c->dst.bytes))
2864 return emulate_gp(ctxt, 0);
2866 return X86EMUL_CONTINUE;
2869 static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2871 struct decode_cache *c = &ctxt->decode;
2873 c->src.bytes = min(c->src.bytes, 4u);
2874 if (!emulator_io_permited(ctxt, ctxt->ops, c->dst.val, c->src.bytes))
2875 return emulate_gp(ctxt, 0);
2877 return X86EMUL_CONTINUE;
2880 #define D(_y) { .flags = (_y) }
2881 #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
2882 #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2883 .check_perm = (_p) }
2884 #define N D(0)
2885 #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
2886 #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2887 #define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2888 #define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2889 #define II(_f, _e, _i) \
2890 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
2891 #define IIP(_f, _e, _i, _p) \
2892 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2893 .check_perm = (_p) }
2894 #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
2896 #define D2bv(_f) D((_f) | ByteOp), D(_f)
2897 #define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
2898 #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2900 #define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2901 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2902 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2904 static struct opcode group7_rm1[] = {
2905 DI(SrcNone | ModRM | Priv, monitor),
2906 DI(SrcNone | ModRM | Priv, mwait),
2907 N, N, N, N, N, N,
2910 static struct opcode group7_rm3[] = {
2911 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
2912 DI(SrcNone | ModRM | Prot | VendorSpecific, vmmcall),
2913 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2914 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
2915 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
2916 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
2917 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
2918 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
2921 static struct opcode group7_rm7[] = {
2923 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
2924 N, N, N, N, N, N,
2926 static struct opcode group1[] = {
2927 X7(D(Lock)), N
2930 static struct opcode group1A[] = {
2931 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2934 static struct opcode group3[] = {
2935 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2936 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2937 X4(D(SrcMem | ModRM)),
2940 static struct opcode group4[] = {
2941 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2942 N, N, N, N, N, N,
2945 static struct opcode group5[] = {
2946 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2947 D(SrcMem | ModRM | Stack),
2948 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
2949 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2950 D(SrcMem | ModRM | Stack), N,
2953 static struct opcode group6[] = {
2954 DI(ModRM | Prot, sldt),
2955 DI(ModRM | Prot, str),
2956 DI(ModRM | Prot | Priv, lldt),
2957 DI(ModRM | Prot | Priv, ltr),
2958 N, N, N, N,
2961 static struct group_dual group7 = { {
2962 DI(ModRM | Mov | DstMem | Priv, sgdt),
2963 DI(ModRM | Mov | DstMem | Priv, sidt),
2964 DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
2965 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2966 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2967 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
2968 }, {
2969 D(SrcNone | ModRM | Priv | VendorSpecific), EXT(0, group7_rm1),
2970 N, EXT(0, group7_rm3),
2971 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2972 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7),
2973 } };
2975 static struct opcode group8[] = {
2976 N, N, N, N,
2977 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2978 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2981 static struct group_dual group9 = { {
2982 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2983 }, {
2984 N, N, N, N, N, N, N, N,
2985 } };
2987 static struct opcode group11[] = {
2988 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2991 static struct gprefix pfx_0f_6f_0f_7f = {
2992 N, N, N, I(Sse, em_movdqu),
2995 static struct opcode opcode_table[256] = {
2996 /* 0x00 - 0x07 */
2997 D6ALU(Lock),
2998 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2999 /* 0x08 - 0x0F */
3000 D6ALU(Lock),
3001 D(ImplicitOps | Stack | No64), N,
3002 /* 0x10 - 0x17 */
3003 D6ALU(Lock),
3004 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3005 /* 0x18 - 0x1F */
3006 D6ALU(Lock),
3007 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3008 /* 0x20 - 0x27 */
3009 D6ALU(Lock), N, N,
3010 /* 0x28 - 0x2F */
3011 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
3012 /* 0x30 - 0x37 */
3013 D6ALU(Lock), N, N,
3014 /* 0x38 - 0x3F */
3015 D6ALU(0), N, N,
3016 /* 0x40 - 0x4F */
3017 X16(D(DstReg)),
3018 /* 0x50 - 0x57 */
3019 X8(I(SrcReg | Stack, em_push)),
3020 /* 0x58 - 0x5F */
3021 X8(D(DstReg | Stack)),
3022 /* 0x60 - 0x67 */
3023 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3024 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3025 N, N, N, N,
3026 /* 0x68 - 0x6F */
3027 I(SrcImm | Mov | Stack, em_push),
3028 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
3029 I(SrcImmByte | Mov | Stack, em_push),
3030 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
3031 D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3032 D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
3033 /* 0x70 - 0x7F */
3034 X16(D(SrcImmByte)),
3035 /* 0x80 - 0x87 */
3036 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3037 G(DstMem | SrcImm | ModRM | Group, group1),
3038 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3039 G(DstMem | SrcImmByte | ModRM | Group, group1),
3040 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
3041 /* 0x88 - 0x8F */
3042 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3043 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
3044 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
3045 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
3046 /* 0x90 - 0x97 */
3047 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
3048 /* 0x98 - 0x9F */
3049 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
3050 I(SrcImmFAddr | No64, em_call_far), N,
3051 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
3052 /* 0xA0 - 0xA7 */
3053 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3054 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3055 I2bv(SrcSI | DstDI | Mov | String, em_mov),
3056 D2bv(SrcSI | DstDI | String),
3057 /* 0xA8 - 0xAF */
3058 D2bv(DstAcc | SrcImm),
3059 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3060 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
3061 D2bv(SrcAcc | DstDI | String),
3062 /* 0xB0 - 0xB7 */
3063 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
3064 /* 0xB8 - 0xBF */
3065 X8(I(DstReg | SrcImm | Mov, em_mov)),
3066 /* 0xC0 - 0xC7 */
3067 D2bv(DstMem | SrcImmByte | ModRM),
3068 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
3069 D(ImplicitOps | Stack),
3070 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
3071 G(ByteOp, group11), G(0, group11),
3072 /* 0xC8 - 0xCF */
3073 N, N, N, D(ImplicitOps | Stack),
3074 D(ImplicitOps), DI(SrcImmByte, intn),
3075 D(ImplicitOps | No64), DI(ImplicitOps, iret),
3076 /* 0xD0 - 0xD7 */
3077 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
3078 N, N, N, N,
3079 /* 0xD8 - 0xDF */
3080 N, N, N, N, N, N, N, N,
3081 /* 0xE0 - 0xE7 */
3082 X4(D(SrcImmByte)),
3083 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3084 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
3085 /* 0xE8 - 0xEF */
3086 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
3087 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
3088 D2bvIP(SrcNone | DstAcc, in, check_perm_in),
3089 D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
3090 /* 0xF0 - 0xF7 */
3091 N, DI(ImplicitOps, icebp), N, N,
3092 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3093 G(ByteOp, group3), G(0, group3),
3094 /* 0xF8 - 0xFF */
3095 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
3096 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3099 static struct opcode twobyte_table[256] = {
3100 /* 0x00 - 0x0F */
3101 G(0, group6), GD(0, &group7), N, N,
3102 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
3103 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
3104 N, D(ImplicitOps | ModRM), N, N,
3105 /* 0x10 - 0x1F */
3106 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3107 /* 0x20 - 0x2F */
3108 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
3109 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
3110 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
3111 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
3112 N, N, N, N,
3113 N, N, N, N, N, N, N, N,
3114 /* 0x30 - 0x3F */
3115 DI(ImplicitOps | Priv, wrmsr),
3116 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3117 DI(ImplicitOps | Priv, rdmsr),
3118 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
3119 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3120 N, N,
3121 N, N, N, N, N, N, N, N,
3122 /* 0x40 - 0x4F */
3123 X16(D(DstReg | SrcMem | ModRM | Mov)),
3124 /* 0x50 - 0x5F */
3125 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3126 /* 0x60 - 0x6F */
3127 N, N, N, N,
3128 N, N, N, N,
3129 N, N, N, N,
3130 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
3131 /* 0x70 - 0x7F */
3132 N, N, N, N,
3133 N, N, N, N,
3134 N, N, N, N,
3135 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
3136 /* 0x80 - 0x8F */
3137 X16(D(SrcImm)),
3138 /* 0x90 - 0x9F */
3139 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
3140 /* 0xA0 - 0xA7 */
3141 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
3142 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
3143 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3144 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3145 /* 0xA8 - 0xAF */
3146 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
3147 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3148 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3149 D(DstMem | SrcReg | Src2CL | ModRM),
3150 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
3151 /* 0xB0 - 0xB7 */
3152 D2bv(DstMem | SrcReg | ModRM | Lock),
3153 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3154 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3155 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
3156 /* 0xB8 - 0xBF */
3157 N, N,
3158 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3159 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3160 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
3161 /* 0xC0 - 0xCF */
3162 D2bv(DstMem | SrcReg | ModRM | Lock),
3163 N, D(DstMem | SrcReg | ModRM | Mov),
3164 N, N, N, GD(0, &group9),
3165 N, N, N, N, N, N, N, N,
3166 /* 0xD0 - 0xDF */
3167 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3168 /* 0xE0 - 0xEF */
3169 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3170 /* 0xF0 - 0xFF */
3171 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3174 #undef D
3175 #undef N
3176 #undef G
3177 #undef GD
3178 #undef I
3179 #undef GP
3180 #undef EXT
3182 #undef D2bv
3183 #undef D2bvIP
3184 #undef I2bv
3185 #undef D6ALU
3187 static unsigned imm_size(struct decode_cache *c)
3189 unsigned size;
3191 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3192 if (size == 8)
3193 size = 4;
3194 return size;
3197 static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3198 unsigned size, bool sign_extension)
3200 struct decode_cache *c = &ctxt->decode;
3201 struct x86_emulate_ops *ops = ctxt->ops;
3202 int rc = X86EMUL_CONTINUE;
3204 op->type = OP_IMM;
3205 op->bytes = size;
3206 op->addr.mem.ea = c->eip;
3207 /* NB. Immediates are sign-extended as necessary. */
3208 switch (op->bytes) {
3209 case 1:
3210 op->val = insn_fetch(s8, 1, c->eip);
3211 break;
3212 case 2:
3213 op->val = insn_fetch(s16, 2, c->eip);
3214 break;
3215 case 4:
3216 op->val = insn_fetch(s32, 4, c->eip);
3217 break;
3219 if (!sign_extension) {
3220 switch (op->bytes) {
3221 case 1:
3222 op->val &= 0xff;
3223 break;
3224 case 2:
3225 op->val &= 0xffff;
3226 break;
3227 case 4:
3228 op->val &= 0xffffffff;
3229 break;
3232 done:
3233 return rc;
3237 x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
3239 struct x86_emulate_ops *ops = ctxt->ops;
3240 struct decode_cache *c = &ctxt->decode;
3241 int rc = X86EMUL_CONTINUE;
3242 int mode = ctxt->mode;
3243 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
3244 bool op_prefix = false;
3245 struct opcode opcode, *g_mod012, *g_mod3;
3246 struct operand memop = { .type = OP_NONE };
3248 c->eip = ctxt->eip;
3249 c->fetch.start = c->eip;
3250 c->fetch.end = c->fetch.start + insn_len;
3251 if (insn_len > 0)
3252 memcpy(c->fetch.data, insn, insn_len);
3254 switch (mode) {
3255 case X86EMUL_MODE_REAL:
3256 case X86EMUL_MODE_VM86:
3257 case X86EMUL_MODE_PROT16:
3258 def_op_bytes = def_ad_bytes = 2;
3259 break;
3260 case X86EMUL_MODE_PROT32:
3261 def_op_bytes = def_ad_bytes = 4;
3262 break;
3263 #ifdef CONFIG_X86_64
3264 case X86EMUL_MODE_PROT64:
3265 def_op_bytes = 4;
3266 def_ad_bytes = 8;
3267 break;
3268 #endif
3269 default:
3270 return -1;
3273 c->op_bytes = def_op_bytes;
3274 c->ad_bytes = def_ad_bytes;
3276 /* Legacy prefixes. */
3277 for (;;) {
3278 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3279 case 0x66: /* operand-size override */
3280 op_prefix = true;
3281 /* switch between 2/4 bytes */
3282 c->op_bytes = def_op_bytes ^ 6;
3283 break;
3284 case 0x67: /* address-size override */
3285 if (mode == X86EMUL_MODE_PROT64)
3286 /* switch between 4/8 bytes */
3287 c->ad_bytes = def_ad_bytes ^ 12;
3288 else
3289 /* switch between 2/4 bytes */
3290 c->ad_bytes = def_ad_bytes ^ 6;
3291 break;
3292 case 0x26: /* ES override */
3293 case 0x2e: /* CS override */
3294 case 0x36: /* SS override */
3295 case 0x3e: /* DS override */
3296 set_seg_override(c, (c->b >> 3) & 3);
3297 break;
3298 case 0x64: /* FS override */
3299 case 0x65: /* GS override */
3300 set_seg_override(c, c->b & 7);
3301 break;
3302 case 0x40 ... 0x4f: /* REX */
3303 if (mode != X86EMUL_MODE_PROT64)
3304 goto done_prefixes;
3305 c->rex_prefix = c->b;
3306 continue;
3307 case 0xf0: /* LOCK */
3308 c->lock_prefix = 1;
3309 break;
3310 case 0xf2: /* REPNE/REPNZ */
3311 case 0xf3: /* REP/REPE/REPZ */
3312 c->rep_prefix = c->b;
3313 break;
3314 default:
3315 goto done_prefixes;
3318 /* Any legacy prefix after a REX prefix nullifies its effect. */
3320 c->rex_prefix = 0;
3323 done_prefixes:
3325 /* REX prefix. */
3326 if (c->rex_prefix & 8)
3327 c->op_bytes = 8; /* REX.W */
3329 /* Opcode byte(s). */
3330 opcode = opcode_table[c->b];
3331 /* Two-byte opcode? */
3332 if (c->b == 0x0f) {
3333 c->twobyte = 1;
3334 c->b = insn_fetch(u8, 1, c->eip);
3335 opcode = twobyte_table[c->b];
3337 c->d = opcode.flags;
3339 if (c->d & Group) {
3340 dual = c->d & GroupDual;
3341 c->modrm = insn_fetch(u8, 1, c->eip);
3342 --c->eip;
3344 if (c->d & GroupDual) {
3345 g_mod012 = opcode.u.gdual->mod012;
3346 g_mod3 = opcode.u.gdual->mod3;
3347 } else
3348 g_mod012 = g_mod3 = opcode.u.group;
3350 c->d &= ~(Group | GroupDual);
3352 goffset = (c->modrm >> 3) & 7;
3354 if ((c->modrm >> 6) == 3)
3355 opcode = g_mod3[goffset];
3356 else
3357 opcode = g_mod012[goffset];
3359 if (opcode.flags & RMExt) {
3360 goffset = c->modrm & 7;
3361 opcode = opcode.u.group[goffset];
3364 c->d |= opcode.flags;
3367 if (c->d & Prefix) {
3368 if (c->rep_prefix && op_prefix)
3369 return X86EMUL_UNHANDLEABLE;
3370 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3371 switch (simd_prefix) {
3372 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3373 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3374 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3375 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3377 c->d |= opcode.flags;
3380 c->execute = opcode.u.execute;
3381 c->check_perm = opcode.check_perm;
3382 c->intercept = opcode.intercept;
3384 /* Unrecognised? */
3385 if (c->d == 0 || (c->d & Undefined))
3386 return -1;
3388 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3389 return -1;
3391 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3392 c->op_bytes = 8;
3394 if (c->d & Op3264) {
3395 if (mode == X86EMUL_MODE_PROT64)
3396 c->op_bytes = 8;
3397 else
3398 c->op_bytes = 4;
3401 if (c->d & Sse)
3402 c->op_bytes = 16;
3404 /* ModRM and SIB bytes. */
3405 if (c->d & ModRM) {
3406 rc = decode_modrm(ctxt, ops, &memop);
3407 if (!c->has_seg_override)
3408 set_seg_override(c, c->modrm_seg);
3409 } else if (c->d & MemAbs)
3410 rc = decode_abs(ctxt, ops, &memop);
3411 if (rc != X86EMUL_CONTINUE)
3412 goto done;
3414 if (!c->has_seg_override)
3415 set_seg_override(c, VCPU_SREG_DS);
3417 memop.addr.mem.seg = seg_override(ctxt, ops, c);
3419 if (memop.type == OP_MEM && c->ad_bytes != 8)
3420 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
3422 if (memop.type == OP_MEM && c->rip_relative)
3423 memop.addr.mem.ea += c->eip;
3426 * Decode and fetch the source operand: register, memory
3427 * or immediate.
3429 switch (c->d & SrcMask) {
3430 case SrcNone:
3431 break;
3432 case SrcReg:
3433 decode_register_operand(ctxt, &c->src, c, 0);
3434 break;
3435 case SrcMem16:
3436 memop.bytes = 2;
3437 goto srcmem_common;
3438 case SrcMem32:
3439 memop.bytes = 4;
3440 goto srcmem_common;
3441 case SrcMem:
3442 memop.bytes = (c->d & ByteOp) ? 1 :
3443 c->op_bytes;
3444 srcmem_common:
3445 c->src = memop;
3446 break;
3447 case SrcImmU16:
3448 rc = decode_imm(ctxt, &c->src, 2, false);
3449 break;
3450 case SrcImm:
3451 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3452 break;
3453 case SrcImmU:
3454 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
3455 break;
3456 case SrcImmByte:
3457 rc = decode_imm(ctxt, &c->src, 1, true);
3458 break;
3459 case SrcImmUByte:
3460 rc = decode_imm(ctxt, &c->src, 1, false);
3461 break;
3462 case SrcAcc:
3463 c->src.type = OP_REG;
3464 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
3465 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
3466 fetch_register_operand(&c->src);
3467 break;
3468 case SrcOne:
3469 c->src.bytes = 1;
3470 c->src.val = 1;
3471 break;
3472 case SrcSI:
3473 c->src.type = OP_MEM;
3474 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
3475 c->src.addr.mem.ea =
3476 register_address(c, c->regs[VCPU_REGS_RSI]);
3477 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
3478 c->src.val = 0;
3479 break;
3480 case SrcImmFAddr:
3481 c->src.type = OP_IMM;
3482 c->src.addr.mem.ea = c->eip;
3483 c->src.bytes = c->op_bytes + 2;
3484 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3485 break;
3486 case SrcMemFAddr:
3487 memop.bytes = c->op_bytes + 2;
3488 goto srcmem_common;
3489 break;
3492 if (rc != X86EMUL_CONTINUE)
3493 goto done;
3496 * Decode and fetch the second source operand: register, memory
3497 * or immediate.
3499 switch (c->d & Src2Mask) {
3500 case Src2None:
3501 break;
3502 case Src2CL:
3503 c->src2.bytes = 1;
3504 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3505 break;
3506 case Src2ImmByte:
3507 rc = decode_imm(ctxt, &c->src2, 1, true);
3508 break;
3509 case Src2One:
3510 c->src2.bytes = 1;
3511 c->src2.val = 1;
3512 break;
3513 case Src2Imm:
3514 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3515 break;
3518 if (rc != X86EMUL_CONTINUE)
3519 goto done;
3521 /* Decode and fetch the destination operand: register or memory. */
3522 switch (c->d & DstMask) {
3523 case DstReg:
3524 decode_register_operand(ctxt, &c->dst, c,
3525 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3526 break;
3527 case DstImmUByte:
3528 c->dst.type = OP_IMM;
3529 c->dst.addr.mem.ea = c->eip;
3530 c->dst.bytes = 1;
3531 c->dst.val = insn_fetch(u8, 1, c->eip);
3532 break;
3533 case DstMem:
3534 case DstMem64:
3535 c->dst = memop;
3536 if ((c->d & DstMask) == DstMem64)
3537 c->dst.bytes = 8;
3538 else
3539 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
3540 if (c->d & BitOp)
3541 fetch_bit_operand(c);
3542 c->dst.orig_val = c->dst.val;
3543 break;
3544 case DstAcc:
3545 c->dst.type = OP_REG;
3546 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
3547 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
3548 fetch_register_operand(&c->dst);
3549 c->dst.orig_val = c->dst.val;
3550 break;
3551 case DstDI:
3552 c->dst.type = OP_MEM;
3553 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
3554 c->dst.addr.mem.ea =
3555 register_address(c, c->regs[VCPU_REGS_RDI]);
3556 c->dst.addr.mem.seg = VCPU_SREG_ES;
3557 c->dst.val = 0;
3558 break;
3559 case ImplicitOps:
3560 /* Special instructions do their own operand decoding. */
3561 default:
3562 c->dst.type = OP_NONE; /* Disable writeback. */
3563 return 0;
3566 done:
3567 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
3570 static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3572 struct decode_cache *c = &ctxt->decode;
3574 /* The second termination condition only applies for REPE
3575 * and REPNE. Test if the repeat string operation prefix is
3576 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3577 * corresponding termination condition according to:
3578 * - if REPE/REPZ and ZF = 0 then done
3579 * - if REPNE/REPNZ and ZF = 1 then done
3581 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3582 (c->b == 0xae) || (c->b == 0xaf))
3583 && (((c->rep_prefix == REPE_PREFIX) &&
3584 ((ctxt->eflags & EFLG_ZF) == 0))
3585 || ((c->rep_prefix == REPNE_PREFIX) &&
3586 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3587 return true;
3589 return false;
3593 x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
3595 struct x86_emulate_ops *ops = ctxt->ops;
3596 u64 msr_data;
3597 struct decode_cache *c = &ctxt->decode;
3598 int rc = X86EMUL_CONTINUE;
3599 int saved_dst_type = c->dst.type;
3600 int irq; /* Used for int 3, int, and into */
3602 ctxt->decode.mem_read.pos = 0;
3604 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
3605 rc = emulate_ud(ctxt);
3606 goto done;
3609 /* LOCK prefix is allowed only with some instructions */
3610 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
3611 rc = emulate_ud(ctxt);
3612 goto done;
3615 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
3616 rc = emulate_ud(ctxt);
3617 goto done;
3620 if ((c->d & Sse)
3621 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3622 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
3623 rc = emulate_ud(ctxt);
3624 goto done;
3627 if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
3628 rc = emulate_nm(ctxt);
3629 goto done;
3632 if (unlikely(ctxt->guest_mode) && c->intercept) {
3633 rc = emulator_check_intercept(ctxt, c->intercept,
3634 X86_ICPT_PRE_EXCEPT);
3635 if (rc != X86EMUL_CONTINUE)
3636 goto done;
3639 /* Privileged instruction can be executed only in CPL=0 */
3640 if ((c->d & Priv) && ops->cpl(ctxt)) {
3641 rc = emulate_gp(ctxt, 0);
3642 goto done;
3645 /* Instruction can only be executed in protected mode */
3646 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3647 rc = emulate_ud(ctxt);
3648 goto done;
3651 /* Do instruction specific permission checks */
3652 if (c->check_perm) {
3653 rc = c->check_perm(ctxt);
3654 if (rc != X86EMUL_CONTINUE)
3655 goto done;
3658 if (unlikely(ctxt->guest_mode) && c->intercept) {
3659 rc = emulator_check_intercept(ctxt, c->intercept,
3660 X86_ICPT_POST_EXCEPT);
3661 if (rc != X86EMUL_CONTINUE)
3662 goto done;
3665 if (c->rep_prefix && (c->d & String)) {
3666 /* All REP prefixes have the same first termination condition */
3667 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
3668 ctxt->eip = c->eip;
3669 goto done;
3673 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
3674 rc = segmented_read(ctxt, c->src.addr.mem,
3675 c->src.valptr, c->src.bytes);
3676 if (rc != X86EMUL_CONTINUE)
3677 goto done;
3678 c->src.orig_val64 = c->src.val64;
3681 if (c->src2.type == OP_MEM) {
3682 rc = segmented_read(ctxt, c->src2.addr.mem,
3683 &c->src2.val, c->src2.bytes);
3684 if (rc != X86EMUL_CONTINUE)
3685 goto done;
3688 if ((c->d & DstMask) == ImplicitOps)
3689 goto special_insn;
3692 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3693 /* optimisation - avoid slow emulated read if Mov */
3694 rc = segmented_read(ctxt, c->dst.addr.mem,
3695 &c->dst.val, c->dst.bytes);
3696 if (rc != X86EMUL_CONTINUE)
3697 goto done;
3699 c->dst.orig_val = c->dst.val;
3701 special_insn:
3703 if (unlikely(ctxt->guest_mode) && c->intercept) {
3704 rc = emulator_check_intercept(ctxt, c->intercept,
3705 X86_ICPT_POST_MEMACCESS);
3706 if (rc != X86EMUL_CONTINUE)
3707 goto done;
3710 if (c->execute) {
3711 rc = c->execute(ctxt);
3712 if (rc != X86EMUL_CONTINUE)
3713 goto done;
3714 goto writeback;
3717 if (c->twobyte)
3718 goto twobyte_insn;
3720 switch (c->b) {
3721 case 0x00 ... 0x05:
3722 add: /* add */
3723 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3724 break;
3725 case 0x06: /* push es */
3726 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
3727 break;
3728 case 0x07: /* pop es */
3729 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
3730 break;
3731 case 0x08 ... 0x0d:
3732 or: /* or */
3733 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
3734 break;
3735 case 0x0e: /* push cs */
3736 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
3737 break;
3738 case 0x10 ... 0x15:
3739 adc: /* adc */
3740 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
3741 break;
3742 case 0x16: /* push ss */
3743 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
3744 break;
3745 case 0x17: /* pop ss */
3746 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
3747 break;
3748 case 0x18 ... 0x1d:
3749 sbb: /* sbb */
3750 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
3751 break;
3752 case 0x1e: /* push ds */
3753 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
3754 break;
3755 case 0x1f: /* pop ds */
3756 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
3757 break;
3758 case 0x20 ... 0x25:
3759 and: /* and */
3760 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
3761 break;
3762 case 0x28 ... 0x2d:
3763 sub: /* sub */
3764 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
3765 break;
3766 case 0x30 ... 0x35:
3767 xor: /* xor */
3768 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
3769 break;
3770 case 0x38 ... 0x3d:
3771 cmp: /* cmp */
3772 c->dst.type = OP_NONE; /* Disable writeback. */
3773 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3774 break;
3775 case 0x40 ... 0x47: /* inc r16/r32 */
3776 emulate_1op("inc", c->dst, ctxt->eflags);
3777 break;
3778 case 0x48 ... 0x4f: /* dec r16/r32 */
3779 emulate_1op("dec", c->dst, ctxt->eflags);
3780 break;
3781 case 0x58 ... 0x5f: /* pop reg */
3782 pop_instruction:
3783 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
3784 break;
3785 case 0x60: /* pusha */
3786 rc = emulate_pusha(ctxt);
3787 break;
3788 case 0x61: /* popa */
3789 rc = emulate_popa(ctxt, ops);
3790 break;
3791 case 0x63: /* movsxd */
3792 if (ctxt->mode != X86EMUL_MODE_PROT64)
3793 goto cannot_emulate;
3794 c->dst.val = (s32) c->src.val;
3795 break;
3796 case 0x6c: /* insb */
3797 case 0x6d: /* insw/insd */
3798 c->src.val = c->regs[VCPU_REGS_RDX];
3799 goto do_io_in;
3800 case 0x6e: /* outsb */
3801 case 0x6f: /* outsw/outsd */
3802 c->dst.val = c->regs[VCPU_REGS_RDX];
3803 goto do_io_out;
3804 break;
3805 case 0x70 ... 0x7f: /* jcc (short) */
3806 if (test_cc(c->b, ctxt->eflags))
3807 jmp_rel(c, c->src.val);
3808 break;
3809 case 0x80 ... 0x83: /* Grp1 */
3810 switch (c->modrm_reg) {
3811 case 0:
3812 goto add;
3813 case 1:
3814 goto or;
3815 case 2:
3816 goto adc;
3817 case 3:
3818 goto sbb;
3819 case 4:
3820 goto and;
3821 case 5:
3822 goto sub;
3823 case 6:
3824 goto xor;
3825 case 7:
3826 goto cmp;
3828 break;
3829 case 0x84 ... 0x85:
3830 test:
3831 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
3832 break;
3833 case 0x86 ... 0x87: /* xchg */
3834 xchg:
3835 /* Write back the register source. */
3836 c->src.val = c->dst.val;
3837 write_register_operand(&c->src);
3839 * Write back the memory destination with implicit LOCK
3840 * prefix.
3842 c->dst.val = c->src.orig_val;
3843 c->lock_prefix = 1;
3844 break;
3845 case 0x8c: /* mov r/m, sreg */
3846 if (c->modrm_reg > VCPU_SREG_GS) {
3847 rc = emulate_ud(ctxt);
3848 goto done;
3850 c->dst.val = ops->get_segment_selector(ctxt, c->modrm_reg);
3851 break;
3852 case 0x8d: /* lea r16/r32, m */
3853 c->dst.val = c->src.addr.mem.ea;
3854 break;
3855 case 0x8e: { /* mov seg, r/m16 */
3856 uint16_t sel;
3858 sel = c->src.val;
3860 if (c->modrm_reg == VCPU_SREG_CS ||
3861 c->modrm_reg > VCPU_SREG_GS) {
3862 rc = emulate_ud(ctxt);
3863 goto done;
3866 if (c->modrm_reg == VCPU_SREG_SS)
3867 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3869 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
3871 c->dst.type = OP_NONE; /* Disable writeback. */
3872 break;
3874 case 0x8f: /* pop (sole member of Grp1a) */
3875 rc = emulate_grp1a(ctxt, ops);
3876 break;
3877 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3878 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
3879 break;
3880 goto xchg;
3881 case 0x98: /* cbw/cwde/cdqe */
3882 switch (c->op_bytes) {
3883 case 2: c->dst.val = (s8)c->dst.val; break;
3884 case 4: c->dst.val = (s16)c->dst.val; break;
3885 case 8: c->dst.val = (s32)c->dst.val; break;
3887 break;
3888 case 0x9c: /* pushf */
3889 c->src.val = (unsigned long) ctxt->eflags;
3890 rc = em_push(ctxt);
3891 break;
3892 case 0x9d: /* popf */
3893 c->dst.type = OP_REG;
3894 c->dst.addr.reg = &ctxt->eflags;
3895 c->dst.bytes = c->op_bytes;
3896 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3897 break;
3898 case 0xa6 ... 0xa7: /* cmps */
3899 goto cmp;
3900 case 0xa8 ... 0xa9: /* test ax, imm */
3901 goto test;
3902 case 0xae ... 0xaf: /* scas */
3903 goto cmp;
3904 case 0xc0 ... 0xc1:
3905 emulate_grp2(ctxt);
3906 break;
3907 case 0xc3: /* ret */
3908 c->dst.type = OP_REG;
3909 c->dst.addr.reg = &c->eip;
3910 c->dst.bytes = c->op_bytes;
3911 goto pop_instruction;
3912 case 0xc4: /* les */
3913 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
3914 break;
3915 case 0xc5: /* lds */
3916 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
3917 break;
3918 case 0xcb: /* ret far */
3919 rc = emulate_ret_far(ctxt, ops);
3920 break;
3921 case 0xcc: /* int3 */
3922 irq = 3;
3923 goto do_interrupt;
3924 case 0xcd: /* int n */
3925 irq = c->src.val;
3926 do_interrupt:
3927 rc = emulate_int(ctxt, ops, irq);
3928 break;
3929 case 0xce: /* into */
3930 if (ctxt->eflags & EFLG_OF) {
3931 irq = 4;
3932 goto do_interrupt;
3934 break;
3935 case 0xcf: /* iret */
3936 rc = emulate_iret(ctxt, ops);
3937 break;
3938 case 0xd0 ... 0xd1: /* Grp2 */
3939 emulate_grp2(ctxt);
3940 break;
3941 case 0xd2 ... 0xd3: /* Grp2 */
3942 c->src.val = c->regs[VCPU_REGS_RCX];
3943 emulate_grp2(ctxt);
3944 break;
3945 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3946 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3947 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3948 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3949 jmp_rel(c, c->src.val);
3950 break;
3951 case 0xe3: /* jcxz/jecxz/jrcxz */
3952 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3953 jmp_rel(c, c->src.val);
3954 break;
3955 case 0xe4: /* inb */
3956 case 0xe5: /* in */
3957 goto do_io_in;
3958 case 0xe6: /* outb */
3959 case 0xe7: /* out */
3960 goto do_io_out;
3961 case 0xe8: /* call (near) */ {
3962 long int rel = c->src.val;
3963 c->src.val = (unsigned long) c->eip;
3964 jmp_rel(c, rel);
3965 rc = em_push(ctxt);
3966 break;
3968 case 0xe9: /* jmp rel */
3969 goto jmp;
3970 case 0xea: { /* jmp far */
3971 unsigned short sel;
3972 jump_far:
3973 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3975 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
3976 goto done;
3978 c->eip = 0;
3979 memcpy(&c->eip, c->src.valptr, c->op_bytes);
3980 break;
3982 case 0xeb:
3983 jmp: /* jmp rel short */
3984 jmp_rel(c, c->src.val);
3985 c->dst.type = OP_NONE; /* Disable writeback. */
3986 break;
3987 case 0xec: /* in al,dx */
3988 case 0xed: /* in (e/r)ax,dx */
3989 c->src.val = c->regs[VCPU_REGS_RDX];
3990 do_io_in:
3991 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3992 &c->dst.val))
3993 goto done; /* IO is needed */
3994 break;
3995 case 0xee: /* out dx,al */
3996 case 0xef: /* out dx,(e/r)ax */
3997 c->dst.val = c->regs[VCPU_REGS_RDX];
3998 do_io_out:
3999 ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
4000 &c->src.val, 1);
4001 c->dst.type = OP_NONE; /* Disable writeback. */
4002 break;
4003 case 0xf4: /* hlt */
4004 ctxt->ops->halt(ctxt);
4005 break;
4006 case 0xf5: /* cmc */
4007 /* complement carry flag from eflags reg */
4008 ctxt->eflags ^= EFLG_CF;
4009 break;
4010 case 0xf6 ... 0xf7: /* Grp3 */
4011 rc = emulate_grp3(ctxt, ops);
4012 break;
4013 case 0xf8: /* clc */
4014 ctxt->eflags &= ~EFLG_CF;
4015 break;
4016 case 0xf9: /* stc */
4017 ctxt->eflags |= EFLG_CF;
4018 break;
4019 case 0xfa: /* cli */
4020 if (emulator_bad_iopl(ctxt, ops)) {
4021 rc = emulate_gp(ctxt, 0);
4022 goto done;
4023 } else
4024 ctxt->eflags &= ~X86_EFLAGS_IF;
4025 break;
4026 case 0xfb: /* sti */
4027 if (emulator_bad_iopl(ctxt, ops)) {
4028 rc = emulate_gp(ctxt, 0);
4029 goto done;
4030 } else {
4031 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
4032 ctxt->eflags |= X86_EFLAGS_IF;
4034 break;
4035 case 0xfc: /* cld */
4036 ctxt->eflags &= ~EFLG_DF;
4037 break;
4038 case 0xfd: /* std */
4039 ctxt->eflags |= EFLG_DF;
4040 break;
4041 case 0xfe: /* Grp4 */
4042 grp45:
4043 rc = emulate_grp45(ctxt);
4044 break;
4045 case 0xff: /* Grp5 */
4046 if (c->modrm_reg == 5)
4047 goto jump_far;
4048 goto grp45;
4049 default:
4050 goto cannot_emulate;
4053 if (rc != X86EMUL_CONTINUE)
4054 goto done;
4056 writeback:
4057 rc = writeback(ctxt, ops);
4058 if (rc != X86EMUL_CONTINUE)
4059 goto done;
4062 * restore dst type in case the decoding will be reused
4063 * (happens for string instruction )
4065 c->dst.type = saved_dst_type;
4067 if ((c->d & SrcMask) == SrcSI)
4068 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
4069 VCPU_REGS_RSI, &c->src);
4071 if ((c->d & DstMask) == DstDI)
4072 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
4073 &c->dst);
4075 if (c->rep_prefix && (c->d & String)) {
4076 struct read_cache *r = &ctxt->decode.io_read;
4077 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
4079 if (!string_insn_completed(ctxt)) {
4081 * Re-enter guest when pio read ahead buffer is empty
4082 * or, if it is not used, after each 1024 iteration.
4084 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
4085 (r->end == 0 || r->end != r->pos)) {
4087 * Reset read cache. Usually happens before
4088 * decode, but since instruction is restarted
4089 * we have to do it here.
4091 ctxt->decode.mem_read.end = 0;
4092 return EMULATION_RESTART;
4094 goto done; /* skip rip writeback */
4098 ctxt->eip = c->eip;
4100 done:
4101 if (rc == X86EMUL_PROPAGATE_FAULT)
4102 ctxt->have_exception = true;
4103 if (rc == X86EMUL_INTERCEPTED)
4104 return EMULATION_INTERCEPTED;
4106 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
4108 twobyte_insn:
4109 switch (c->b) {
4110 case 0x01: /* lgdt, lidt, lmsw */
4111 switch (c->modrm_reg) {
4112 case 0: /* vmcall */
4113 rc = em_vmcall(ctxt);
4114 break;
4115 case 2: /* lgdt */
4116 rc = em_lgdt(ctxt);
4117 break;
4118 case 3: /* lidt/vmmcall */
4119 if (c->modrm_mod == 3)
4120 return em_svm(ctxt);
4121 else
4122 return em_lidt(ctxt);
4123 break;
4124 case 4: /* smsw */
4125 rc = em_smsw(ctxt);
4126 break;
4127 case 6: /* lmsw */
4128 rc = em_lmsw(ctxt);
4129 break;
4130 case 5: /* not defined */
4131 rc = emulate_ud(ctxt);
4132 break;
4133 case 7: /* invlpg*/
4134 rc = em_invlpg(ctxt);
4135 break;
4136 default:
4137 goto cannot_emulate;
4139 break;
4140 case 0x05: /* syscall */
4141 rc = emulate_syscall(ctxt, ops);
4142 break;
4143 case 0x06:
4144 rc = em_clts(ctxt);
4145 break;
4146 case 0x09: /* wbinvd */
4147 (ctxt->ops->wbinvd)(ctxt);
4148 break;
4149 case 0x08: /* invd */
4150 case 0x0d: /* GrpP (prefetch) */
4151 case 0x18: /* Grp16 (prefetch/nop) */
4152 break;
4153 case 0x20: /* mov cr, reg */
4154 c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
4155 break;
4156 case 0x21: /* mov from dr to reg */
4157 ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
4158 break;
4159 case 0x22: /* mov reg, cr */
4160 if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
4161 emulate_gp(ctxt, 0);
4162 rc = X86EMUL_PROPAGATE_FAULT;
4163 goto done;
4165 c->dst.type = OP_NONE;
4166 break;
4167 case 0x23: /* mov from reg to dr */
4168 if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
4169 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
4170 ~0ULL : ~0U)) < 0) {
4171 /* #UD condition is already handled by the code above */
4172 emulate_gp(ctxt, 0);
4173 rc = X86EMUL_PROPAGATE_FAULT;
4174 goto done;
4177 c->dst.type = OP_NONE; /* no writeback */
4178 break;
4179 case 0x30:
4180 /* wrmsr */
4181 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4182 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
4183 if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
4184 emulate_gp(ctxt, 0);
4185 rc = X86EMUL_PROPAGATE_FAULT;
4186 goto done;
4188 rc = X86EMUL_CONTINUE;
4189 break;
4190 case 0x32:
4191 /* rdmsr */
4192 if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
4193 emulate_gp(ctxt, 0);
4194 rc = X86EMUL_PROPAGATE_FAULT;
4195 goto done;
4196 } else {
4197 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4198 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4200 rc = X86EMUL_CONTINUE;
4201 break;
4202 case 0x34: /* sysenter */
4203 rc = emulate_sysenter(ctxt, ops);
4204 break;
4205 case 0x35: /* sysexit */
4206 rc = emulate_sysexit(ctxt, ops);
4207 break;
4208 case 0x40 ... 0x4f: /* cmov */
4209 c->dst.val = c->dst.orig_val = c->src.val;
4210 if (!test_cc(c->b, ctxt->eflags))
4211 c->dst.type = OP_NONE; /* no writeback */
4212 break;
4213 case 0x80 ... 0x8f: /* jnz rel, etc*/
4214 if (test_cc(c->b, ctxt->eflags))
4215 jmp_rel(c, c->src.val);
4216 break;
4217 case 0x90 ... 0x9f: /* setcc r/m8 */
4218 c->dst.val = test_cc(c->b, ctxt->eflags);
4219 break;
4220 case 0xa0: /* push fs */
4221 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
4222 break;
4223 case 0xa1: /* pop fs */
4224 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
4225 break;
4226 case 0xa3:
4227 bt: /* bt */
4228 c->dst.type = OP_NONE;
4229 /* only subword offset */
4230 c->src.val &= (c->dst.bytes << 3) - 1;
4231 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
4232 break;
4233 case 0xa4: /* shld imm8, r, r/m */
4234 case 0xa5: /* shld cl, r, r/m */
4235 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4236 break;
4237 case 0xa8: /* push gs */
4238 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
4239 break;
4240 case 0xa9: /* pop gs */
4241 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
4242 break;
4243 case 0xab:
4244 bts: /* bts */
4245 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
4246 break;
4247 case 0xac: /* shrd imm8, r, r/m */
4248 case 0xad: /* shrd cl, r, r/m */
4249 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4250 break;
4251 case 0xae: /* clflush */
4252 break;
4253 case 0xb0 ... 0xb1: /* cmpxchg */
4255 * Save real source value, then compare EAX against
4256 * destination.
4258 c->src.orig_val = c->src.val;
4259 c->src.val = c->regs[VCPU_REGS_RAX];
4260 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4261 if (ctxt->eflags & EFLG_ZF) {
4262 /* Success: write back to memory. */
4263 c->dst.val = c->src.orig_val;
4264 } else {
4265 /* Failure: write the value we saw to EAX. */
4266 c->dst.type = OP_REG;
4267 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
4269 break;
4270 case 0xb2: /* lss */
4271 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
4272 break;
4273 case 0xb3:
4274 btr: /* btr */
4275 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
4276 break;
4277 case 0xb4: /* lfs */
4278 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
4279 break;
4280 case 0xb5: /* lgs */
4281 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
4282 break;
4283 case 0xb6 ... 0xb7: /* movzx */
4284 c->dst.bytes = c->op_bytes;
4285 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4286 : (u16) c->src.val;
4287 break;
4288 case 0xba: /* Grp8 */
4289 switch (c->modrm_reg & 3) {
4290 case 0:
4291 goto bt;
4292 case 1:
4293 goto bts;
4294 case 2:
4295 goto btr;
4296 case 3:
4297 goto btc;
4299 break;
4300 case 0xbb:
4301 btc: /* btc */
4302 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
4303 break;
4304 case 0xbc: { /* bsf */
4305 u8 zf;
4306 __asm__ ("bsf %2, %0; setz %1"
4307 : "=r"(c->dst.val), "=q"(zf)
4308 : "r"(c->src.val));
4309 ctxt->eflags &= ~X86_EFLAGS_ZF;
4310 if (zf) {
4311 ctxt->eflags |= X86_EFLAGS_ZF;
4312 c->dst.type = OP_NONE; /* Disable writeback. */
4314 break;
4316 case 0xbd: { /* bsr */
4317 u8 zf;
4318 __asm__ ("bsr %2, %0; setz %1"
4319 : "=r"(c->dst.val), "=q"(zf)
4320 : "r"(c->src.val));
4321 ctxt->eflags &= ~X86_EFLAGS_ZF;
4322 if (zf) {
4323 ctxt->eflags |= X86_EFLAGS_ZF;
4324 c->dst.type = OP_NONE; /* Disable writeback. */
4326 break;
4328 case 0xbe ... 0xbf: /* movsx */
4329 c->dst.bytes = c->op_bytes;
4330 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4331 (s16) c->src.val;
4332 break;
4333 case 0xc0 ... 0xc1: /* xadd */
4334 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4335 /* Write back the register source. */
4336 c->src.val = c->dst.orig_val;
4337 write_register_operand(&c->src);
4338 break;
4339 case 0xc3: /* movnti */
4340 c->dst.bytes = c->op_bytes;
4341 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4342 (u64) c->src.val;
4343 break;
4344 case 0xc7: /* Grp9 (cmpxchg8b) */
4345 rc = emulate_grp9(ctxt, ops);
4346 break;
4347 default:
4348 goto cannot_emulate;
4351 if (rc != X86EMUL_CONTINUE)
4352 goto done;
4354 goto writeback;
4356 cannot_emulate:
4357 return EMULATION_FAILED;