blob: 7e8faea4651e1a89dc2755f044681b11f94a78e2 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Andre Przywarae99f0502009-06-17 15:50:33 +020035#include "mmu.h" /* for is_long_mode() */
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
79#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010080/* Source 2 operand type */
81#define Src2None (0<<29)
82#define Src2CL (1<<29)
83#define Src2ImmByte (2<<29)
84#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030085#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010086#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087
Avi Kivity43bb19c2008-01-18 12:46:50 +020088enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020089 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020090 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020091};
92
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010093static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080094 /* 0x00 - 0x07 */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +030097 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +030098 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 /* 0x08 - 0x0F */
100 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
101 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200102 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
103 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104 /* 0x10 - 0x17 */
105 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
106 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300107 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300108 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109 /* 0x18 - 0x1F */
110 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
111 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300112 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300113 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 /* 0x20 - 0x27 */
115 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
116 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200117 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 /* 0x28 - 0x2F */
119 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
121 0, 0, 0, 0,
122 /* 0x30 - 0x37 */
123 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
124 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
125 0, 0, 0, 0,
126 /* 0x38 - 0x3F */
127 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
128 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200129 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
130 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700131 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200132 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700133 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200134 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300135 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200136 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
137 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300138 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200139 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
140 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700141 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200142 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
143 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700144 0, 0, 0, 0,
145 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300146 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300147 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
148 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300149 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300150 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
151 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300152 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300153 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
154 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200156 Group | Group1_80, Group | Group1_81,
157 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
159 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
160 /* 0x88 - 0x8F */
161 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
162 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200163 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200164 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300165 /* 0x90 - 0x97 */
166 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
167 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300168 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300169 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200171 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
172 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200173 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
174 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200176 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
177 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
178 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300179 /* 0xB0 - 0xB7 */
180 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
181 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
182 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
183 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
184 /* 0xB8 - 0xBF */
185 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
186 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
187 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
188 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300190 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200191 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300192 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300194 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300195 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 /* 0xD0 - 0xD7 */
197 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
198 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
199 0, 0, 0, 0,
200 /* 0xD8 - 0xDF */
201 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300202 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300203 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300204 ByteOp | SrcImmUByte, SrcImmUByte,
205 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300206 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300207 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300208 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300209 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
210 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211 /* 0xF0 - 0xF7 */
212 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200213 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700215 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300216 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217};
218
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100219static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 /* 0x00 - 0x0F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200221 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200222 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 /* 0x10 - 0x1F */
224 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
225 /* 0x20 - 0x2F */
226 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
227 0, 0, 0, 0, 0, 0, 0, 0,
228 /* 0x30 - 0x3F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200229 ImplicitOps, 0, ImplicitOps, 0,
230 ImplicitOps, ImplicitOps, 0, 0,
231 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0x40 - 0x47 */
233 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
234 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
235 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
236 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
237 /* 0x48 - 0x4F */
238 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
239 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
240 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
241 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
242 /* 0x50 - 0x5F */
243 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244 /* 0x60 - 0x6F */
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246 /* 0x70 - 0x7F */
247 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300249 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
250 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 /* 0x90 - 0x9F */
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
253 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300254 ImplicitOps | Stack, ImplicitOps | Stack,
255 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100256 DstMem | SrcReg | Src2ImmByte | ModRM,
257 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300259 ImplicitOps | Stack, ImplicitOps | Stack,
260 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100261 DstMem | SrcReg | Src2ImmByte | ModRM,
262 DstMem | SrcReg | Src2CL | ModRM,
263 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264 /* 0xB0 - 0xB7 */
265 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800266 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
268 DstReg | SrcMem16 | ModRM | Mov,
269 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800270 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
272 DstReg | SrcMem16 | ModRM | Mov,
273 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800274 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
275 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 /* 0xD0 - 0xDF */
277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278 /* 0xE0 - 0xEF */
279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280 /* 0xF0 - 0xFF */
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
282};
283
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100284static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200285 [Group1_80*8] =
286 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
287 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
288 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
289 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
290 [Group1_81*8] =
291 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
292 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
293 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
294 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
295 [Group1_82*8] =
296 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
297 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
298 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
299 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
300 [Group1_83*8] =
301 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
302 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
303 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
304 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200305 [Group1A*8] =
306 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200307 [Group3_Byte*8] =
308 ByteOp | SrcImm | DstMem | ModRM, 0,
309 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
310 0, 0, 0, 0,
311 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400312 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300313 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200314 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200315 [Group4*8] =
316 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
317 0, 0, 0, 0, 0, 0,
318 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300319 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
320 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300321 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200322 [Group7*8] =
323 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300324 SrcNone | ModRM | DstMem | Mov, 0,
325 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200326};
327
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100328static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200329 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000330 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300331 SrcNone | ModRM | DstMem | Mov, 0,
332 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200333};
334
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200336#define EFLG_VM (1<<17)
337#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338#define EFLG_OF (1<<11)
339#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200340#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341#define EFLG_SF (1<<7)
342#define EFLG_ZF (1<<6)
343#define EFLG_AF (1<<4)
344#define EFLG_PF (1<<2)
345#define EFLG_CF (1<<0)
346
347/*
348 * Instruction emulation:
349 * Most instructions are emulated directly via a fragment of inline assembly
350 * code. This allows us to save/restore EFLAGS and thus very easily pick up
351 * any modified flags.
352 */
353
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800354#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355#define _LO32 "k" /* force 32-bit operand */
356#define _STK "%%rsp" /* stack pointer */
357#elif defined(__i386__)
358#define _LO32 "" /* force 32-bit operand */
359#define _STK "%%esp" /* stack pointer */
360#endif
361
362/*
363 * These EFLAGS bits are restored from saved value during emulation, and
364 * any changes are written back to the saved value after emulation.
365 */
366#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
367
368/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200369#define _PRE_EFLAGS(_sav, _msk, _tmp) \
370 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
371 "movl %"_sav",%"_LO32 _tmp"; " \
372 "push %"_tmp"; " \
373 "push %"_tmp"; " \
374 "movl %"_msk",%"_LO32 _tmp"; " \
375 "andl %"_LO32 _tmp",("_STK"); " \
376 "pushf; " \
377 "notl %"_LO32 _tmp"; " \
378 "andl %"_LO32 _tmp",("_STK"); " \
379 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
380 "pop %"_tmp"; " \
381 "orl %"_LO32 _tmp",("_STK"); " \
382 "popf; " \
383 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384
385/* After executing instruction: write-back necessary bits in EFLAGS. */
386#define _POST_EFLAGS(_sav, _msk, _tmp) \
387 /* _sav |= EFLAGS & _msk; */ \
388 "pushf; " \
389 "pop %"_tmp"; " \
390 "andl %"_msk",%"_LO32 _tmp"; " \
391 "orl %"_LO32 _tmp",%"_sav"; "
392
Avi Kivitydda96d82008-11-26 15:14:10 +0200393#ifdef CONFIG_X86_64
394#define ON64(x) x
395#else
396#define ON64(x)
397#endif
398
Avi Kivity6b7ad612008-11-26 15:30:45 +0200399#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
400 do { \
401 __asm__ __volatile__ ( \
402 _PRE_EFLAGS("0", "4", "2") \
403 _op _suffix " %"_x"3,%1; " \
404 _POST_EFLAGS("0", "4", "2") \
405 : "=m" (_eflags), "=m" ((_dst).val), \
406 "=&r" (_tmp) \
407 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200408 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200409
410
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411/* Raw emulation: instruction has two explicit operands. */
412#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200413 do { \
414 unsigned long _tmp; \
415 \
416 switch ((_dst).bytes) { \
417 case 2: \
418 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
419 break; \
420 case 4: \
421 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
422 break; \
423 case 8: \
424 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
425 break; \
426 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 } while (0)
428
429#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
430 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200431 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400432 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200434 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 break; \
436 default: \
437 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
438 _wx, _wy, _lx, _ly, _qx, _qy); \
439 break; \
440 } \
441 } while (0)
442
443/* Source operand is byte-sized and may be restricted to just %cl. */
444#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
445 __emulate_2op(_op, _src, _dst, _eflags, \
446 "b", "c", "b", "c", "b", "c", "b", "c")
447
448/* Source operand is byte, word, long or quad sized. */
449#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
450 __emulate_2op(_op, _src, _dst, _eflags, \
451 "b", "q", "w", "r", _LO32, "r", "", "r")
452
453/* Source operand is word, long or quad sized. */
454#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
455 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
456 "w", "r", _LO32, "r", "", "r")
457
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100458/* Instruction has three operands and one operand is stored in ECX register */
459#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
460 do { \
461 unsigned long _tmp; \
462 _type _clv = (_cl).val; \
463 _type _srcv = (_src).val; \
464 _type _dstv = (_dst).val; \
465 \
466 __asm__ __volatile__ ( \
467 _PRE_EFLAGS("0", "5", "2") \
468 _op _suffix " %4,%1 \n" \
469 _POST_EFLAGS("0", "5", "2") \
470 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
471 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
472 ); \
473 \
474 (_cl).val = (unsigned long) _clv; \
475 (_src).val = (unsigned long) _srcv; \
476 (_dst).val = (unsigned long) _dstv; \
477 } while (0)
478
479#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
480 do { \
481 switch ((_dst).bytes) { \
482 case 2: \
483 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
484 "w", unsigned short); \
485 break; \
486 case 4: \
487 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
488 "l", unsigned int); \
489 break; \
490 case 8: \
491 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
492 "q", unsigned long)); \
493 break; \
494 } \
495 } while (0)
496
Avi Kivitydda96d82008-11-26 15:14:10 +0200497#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 do { \
499 unsigned long _tmp; \
500 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200501 __asm__ __volatile__ ( \
502 _PRE_EFLAGS("0", "3", "2") \
503 _op _suffix " %1; " \
504 _POST_EFLAGS("0", "3", "2") \
505 : "=m" (_eflags), "+m" ((_dst).val), \
506 "=&r" (_tmp) \
507 : "i" (EFLAGS_MASK)); \
508 } while (0)
509
510/* Instruction has only one explicit operand (no source operand). */
511#define emulate_1op(_op, _dst, _eflags) \
512 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400513 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200514 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
515 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
516 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
517 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 } \
519 } while (0)
520
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521/* Fetch next part of the instruction being emulated. */
522#define insn_fetch(_type, _size, _eip) \
523({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200524 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400525 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 goto done; \
527 (_eip) += (_size); \
528 (_type)_x; \
529})
530
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800531static inline unsigned long ad_mask(struct decode_cache *c)
532{
533 return (1UL << (c->ad_bytes << 3)) - 1;
534}
535
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800537static inline unsigned long
538address_mask(struct decode_cache *c, unsigned long reg)
539{
540 if (c->ad_bytes == sizeof(unsigned long))
541 return reg;
542 else
543 return reg & ad_mask(c);
544}
545
546static inline unsigned long
547register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
548{
549 return base + address_mask(c, reg);
550}
551
Harvey Harrison7a9572752008-02-19 07:40:41 -0800552static inline void
553register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
554{
555 if (c->ad_bytes == sizeof(unsigned long))
556 *reg += inc;
557 else
558 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
559}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560
Harvey Harrison7a9572752008-02-19 07:40:41 -0800561static inline void jmp_rel(struct decode_cache *c, int rel)
562{
563 register_address_increment(c, &c->eip, rel);
564}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300565
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300566static void set_seg_override(struct decode_cache *c, int seg)
567{
568 c->has_seg_override = true;
569 c->seg_override = seg;
570}
571
572static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
573{
574 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
575 return 0;
576
577 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
578}
579
580static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
581 struct decode_cache *c)
582{
583 if (!c->has_seg_override)
584 return 0;
585
586 return seg_base(ctxt, c->seg_override);
587}
588
589static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
590{
591 return seg_base(ctxt, VCPU_SREG_ES);
592}
593
594static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
595{
596 return seg_base(ctxt, VCPU_SREG_SS);
597}
598
Avi Kivity62266862007-11-20 13:15:52 +0200599static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
600 struct x86_emulate_ops *ops,
601 unsigned long linear, u8 *dest)
602{
603 struct fetch_cache *fc = &ctxt->decode.fetch;
604 int rc;
605 int size;
606
607 if (linear < fc->start || linear >= fc->end) {
608 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
609 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
610 if (rc)
611 return rc;
612 fc->start = linear;
613 fc->end = linear + size;
614 }
615 *dest = fc->data[linear - fc->start];
616 return 0;
617}
618
619static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
620 struct x86_emulate_ops *ops,
621 unsigned long eip, void *dest, unsigned size)
622{
623 int rc = 0;
624
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200625 /* x86 instructions are limited to 15 bytes. */
626 if (eip + size - ctxt->decode.eip_orig > 15)
627 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200628 eip += ctxt->cs_base;
629 while (size--) {
630 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
631 if (rc)
632 return rc;
633 }
634 return 0;
635}
636
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000637/*
638 * Given the 'reg' portion of a ModRM byte, and a register block, return a
639 * pointer into the block that addresses the relevant register.
640 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
641 */
642static void *decode_register(u8 modrm_reg, unsigned long *regs,
643 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644{
645 void *p;
646
647 p = &regs[modrm_reg];
648 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
649 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
650 return p;
651}
652
653static int read_descriptor(struct x86_emulate_ctxt *ctxt,
654 struct x86_emulate_ops *ops,
655 void *ptr,
656 u16 *size, unsigned long *address, int op_bytes)
657{
658 int rc;
659
660 if (op_bytes == 2)
661 op_bytes = 3;
662 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300663 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
664 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 if (rc)
666 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300667 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
668 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 return rc;
670}
671
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300672static int test_cc(unsigned int condition, unsigned int flags)
673{
674 int rc = 0;
675
676 switch ((condition & 15) >> 1) {
677 case 0: /* o */
678 rc |= (flags & EFLG_OF);
679 break;
680 case 1: /* b/c/nae */
681 rc |= (flags & EFLG_CF);
682 break;
683 case 2: /* z/e */
684 rc |= (flags & EFLG_ZF);
685 break;
686 case 3: /* be/na */
687 rc |= (flags & (EFLG_CF|EFLG_ZF));
688 break;
689 case 4: /* s */
690 rc |= (flags & EFLG_SF);
691 break;
692 case 5: /* p/pe */
693 rc |= (flags & EFLG_PF);
694 break;
695 case 7: /* le/ng */
696 rc |= (flags & EFLG_ZF);
697 /* fall through */
698 case 6: /* l/nge */
699 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
700 break;
701 }
702
703 /* Odd condition identifiers (lsb == 1) have inverted sense. */
704 return (!!rc ^ (condition & 1));
705}
706
Avi Kivity3c118e22007-10-31 10:27:04 +0200707static void decode_register_operand(struct operand *op,
708 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200709 int inhibit_bytereg)
710{
Avi Kivity33615aa2007-10-31 11:15:56 +0200711 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200712 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200713
714 if (!(c->d & ModRM))
715 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200716 op->type = OP_REG;
717 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200718 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200719 op->val = *(u8 *)op->ptr;
720 op->bytes = 1;
721 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200722 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200723 op->bytes = c->op_bytes;
724 switch (op->bytes) {
725 case 2:
726 op->val = *(u16 *)op->ptr;
727 break;
728 case 4:
729 op->val = *(u32 *)op->ptr;
730 break;
731 case 8:
732 op->val = *(u64 *) op->ptr;
733 break;
734 }
735 }
736 op->orig_val = op->val;
737}
738
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200739static int decode_modrm(struct x86_emulate_ctxt *ctxt,
740 struct x86_emulate_ops *ops)
741{
742 struct decode_cache *c = &ctxt->decode;
743 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700744 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200745 int rc = 0;
746
747 if (c->rex_prefix) {
748 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
749 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
750 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
751 }
752
753 c->modrm = insn_fetch(u8, 1, c->eip);
754 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
755 c->modrm_reg |= (c->modrm & 0x38) >> 3;
756 c->modrm_rm |= (c->modrm & 0x07);
757 c->modrm_ea = 0;
758 c->use_modrm_ea = 1;
759
760 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300761 c->modrm_ptr = decode_register(c->modrm_rm,
762 c->regs, c->d & ByteOp);
763 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200764 return rc;
765 }
766
767 if (c->ad_bytes == 2) {
768 unsigned bx = c->regs[VCPU_REGS_RBX];
769 unsigned bp = c->regs[VCPU_REGS_RBP];
770 unsigned si = c->regs[VCPU_REGS_RSI];
771 unsigned di = c->regs[VCPU_REGS_RDI];
772
773 /* 16-bit ModR/M decode. */
774 switch (c->modrm_mod) {
775 case 0:
776 if (c->modrm_rm == 6)
777 c->modrm_ea += insn_fetch(u16, 2, c->eip);
778 break;
779 case 1:
780 c->modrm_ea += insn_fetch(s8, 1, c->eip);
781 break;
782 case 2:
783 c->modrm_ea += insn_fetch(u16, 2, c->eip);
784 break;
785 }
786 switch (c->modrm_rm) {
787 case 0:
788 c->modrm_ea += bx + si;
789 break;
790 case 1:
791 c->modrm_ea += bx + di;
792 break;
793 case 2:
794 c->modrm_ea += bp + si;
795 break;
796 case 3:
797 c->modrm_ea += bp + di;
798 break;
799 case 4:
800 c->modrm_ea += si;
801 break;
802 case 5:
803 c->modrm_ea += di;
804 break;
805 case 6:
806 if (c->modrm_mod != 0)
807 c->modrm_ea += bp;
808 break;
809 case 7:
810 c->modrm_ea += bx;
811 break;
812 }
813 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
814 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300815 if (!c->has_seg_override)
816 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817 c->modrm_ea = (u16)c->modrm_ea;
818 } else {
819 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700820 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200821 sib = insn_fetch(u8, 1, c->eip);
822 index_reg |= (sib >> 3) & 7;
823 base_reg |= sib & 7;
824 scale = sib >> 6;
825
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700826 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
827 c->modrm_ea += insn_fetch(s32, 4, c->eip);
828 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200829 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700830 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200831 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700832 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
833 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700834 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700835 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200836 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200837 switch (c->modrm_mod) {
838 case 0:
839 if (c->modrm_rm == 5)
840 c->modrm_ea += insn_fetch(s32, 4, c->eip);
841 break;
842 case 1:
843 c->modrm_ea += insn_fetch(s8, 1, c->eip);
844 break;
845 case 2:
846 c->modrm_ea += insn_fetch(s32, 4, c->eip);
847 break;
848 }
849 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200850done:
851 return rc;
852}
853
854static int decode_abs(struct x86_emulate_ctxt *ctxt,
855 struct x86_emulate_ops *ops)
856{
857 struct decode_cache *c = &ctxt->decode;
858 int rc = 0;
859
860 switch (c->ad_bytes) {
861 case 2:
862 c->modrm_ea = insn_fetch(u16, 2, c->eip);
863 break;
864 case 4:
865 c->modrm_ea = insn_fetch(u32, 4, c->eip);
866 break;
867 case 8:
868 c->modrm_ea = insn_fetch(u64, 8, c->eip);
869 break;
870 }
871done:
872 return rc;
873}
874
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200876x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200878 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200881 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882
883 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
Laurent Viviere4e03de2007-09-18 11:52:50 +0200885 memset(c, 0, sizeof(struct decode_cache));
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200886 c->eip = c->eip_orig = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300887 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800888 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889
890 switch (mode) {
891 case X86EMUL_MODE_REAL:
892 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200893 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 break;
895 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200896 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800898#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200900 def_op_bytes = 4;
901 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 break;
903#endif
904 default:
905 return -1;
906 }
907
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200908 c->op_bytes = def_op_bytes;
909 c->ad_bytes = def_ad_bytes;
910
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200912 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200913 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200915 /* switch between 2/4 bytes */
916 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 break;
918 case 0x67: /* address-size override */
919 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200920 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200921 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200923 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200924 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300927 case 0x2e: /* CS override */
928 case 0x36: /* SS override */
929 case 0x3e: /* DS override */
930 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 break;
932 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300934 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200936 case 0x40 ... 0x4f: /* REX */
937 if (mode != X86EMUL_MODE_PROT64)
938 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200939 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200940 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200942 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200944 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100945 c->rep_prefix = REPNE_PREFIX;
946 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100948 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 default:
951 goto done_prefixes;
952 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200953
954 /* Any legacy prefix after a REX prefix nullifies its effect. */
955
Avi Kivity33615aa2007-10-31 11:15:56 +0200956 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 }
958
959done_prefixes:
960
961 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200963 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200964 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
966 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200967 c->d = opcode_table[c->b];
968 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200970 if (c->b == 0x0f) {
971 c->twobyte = 1;
972 c->b = insn_fetch(u8, 1, c->eip);
973 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200975 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300977 if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
978 kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");;
979 return -1;
980 }
981
Avi Kivitye09d0822008-01-18 12:38:59 +0200982 if (c->d & Group) {
983 group = c->d & GroupMask;
984 c->modrm = insn_fetch(u8, 1, c->eip);
985 --c->eip;
986
987 group = (group << 3) + ((c->modrm >> 3) & 7);
988 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
989 c->d = group2_table[group];
990 else
991 c->d = group_table[group];
992 }
993
994 /* Unrecognised? */
995 if (c->d == 0) {
996 DPRINTF("Cannot emulate %02x\n", c->b);
997 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 }
999
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001000 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1001 c->op_bytes = 8;
1002
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 if (c->d & ModRM)
1005 rc = decode_modrm(ctxt, ops);
1006 else if (c->d & MemAbs)
1007 rc = decode_abs(ctxt, ops);
1008 if (rc)
1009 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001011 if (!c->has_seg_override)
1012 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001013
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001014 if (!(!c->twobyte && c->b == 0x8d))
1015 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001016
1017 if (c->ad_bytes != 8)
1018 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 /*
1020 * Decode and fetch the source operand: register, memory
1021 * or immediate.
1022 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001023 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 case SrcNone:
1025 break;
1026 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001027 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 break;
1029 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001030 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 goto srcmem_common;
1032 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001033 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 goto srcmem_common;
1035 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001036 c->src.bytes = (c->d & ByteOp) ? 1 :
1037 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001038 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001039 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001040 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001041 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001042 /*
1043 * For instructions with a ModR/M byte, switch to register
1044 * access if Mod = 3.
1045 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 if ((c->d & ModRM) && c->modrm_mod == 3) {
1047 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001048 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001049 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001050 break;
1051 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001052 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 break;
1054 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001055 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001056 c->src.type = OP_IMM;
1057 c->src.ptr = (unsigned long *)c->eip;
1058 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1059 if (c->src.bytes == 8)
1060 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001062 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001064 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 break;
1066 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001067 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 break;
1069 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001070 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 break;
1072 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001073 if ((c->d & SrcMask) == SrcImmU) {
1074 switch (c->src.bytes) {
1075 case 1:
1076 c->src.val &= 0xff;
1077 break;
1078 case 2:
1079 c->src.val &= 0xffff;
1080 break;
1081 case 4:
1082 c->src.val &= 0xffffffff;
1083 break;
1084 }
1085 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 break;
1087 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001088 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001089 c->src.type = OP_IMM;
1090 c->src.ptr = (unsigned long *)c->eip;
1091 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001092 if ((c->d & SrcMask) == SrcImmByte)
1093 c->src.val = insn_fetch(s8, 1, c->eip);
1094 else
1095 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001097 case SrcOne:
1098 c->src.bytes = 1;
1099 c->src.val = 1;
1100 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 }
1102
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001103 /*
1104 * Decode and fetch the second source operand: register, memory
1105 * or immediate.
1106 */
1107 switch (c->d & Src2Mask) {
1108 case Src2None:
1109 break;
1110 case Src2CL:
1111 c->src2.bytes = 1;
1112 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1113 break;
1114 case Src2ImmByte:
1115 c->src2.type = OP_IMM;
1116 c->src2.ptr = (unsigned long *)c->eip;
1117 c->src2.bytes = 1;
1118 c->src2.val = insn_fetch(u8, 1, c->eip);
1119 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001120 case Src2Imm16:
1121 c->src2.type = OP_IMM;
1122 c->src2.ptr = (unsigned long *)c->eip;
1123 c->src2.bytes = 2;
1124 c->src2.val = insn_fetch(u16, 2, c->eip);
1125 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001126 case Src2One:
1127 c->src2.bytes = 1;
1128 c->src2.val = 1;
1129 break;
1130 }
1131
Avi Kivity038e51d2007-01-22 20:40:40 -08001132 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001133 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001134 case ImplicitOps:
1135 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001136 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001137 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001138 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001139 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001140 break;
1141 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001142 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001143 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001144 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001145 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001146 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001147 break;
1148 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001149 c->dst.type = OP_MEM;
1150 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001151 case DstAcc:
1152 c->dst.type = OP_REG;
1153 c->dst.bytes = c->op_bytes;
1154 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1155 switch (c->op_bytes) {
1156 case 1:
1157 c->dst.val = *(u8 *)c->dst.ptr;
1158 break;
1159 case 2:
1160 c->dst.val = *(u16 *)c->dst.ptr;
1161 break;
1162 case 4:
1163 c->dst.val = *(u32 *)c->dst.ptr;
1164 break;
1165 }
1166 c->dst.orig_val = c->dst.val;
1167 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001168 }
1169
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001170 if (c->rip_relative)
1171 c->modrm_ea += c->eip;
1172
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001173done:
1174 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1175}
1176
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001177static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1178{
1179 struct decode_cache *c = &ctxt->decode;
1180
1181 c->dst.type = OP_MEM;
1182 c->dst.bytes = c->op_bytes;
1183 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001184 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001185 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001186 c->regs[VCPU_REGS_RSP]);
1187}
1188
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001189static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001190 struct x86_emulate_ops *ops,
1191 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001192{
1193 struct decode_cache *c = &ctxt->decode;
1194 int rc;
1195
Avi Kivity781d0ed2008-11-27 18:00:28 +02001196 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1197 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001198 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001199 if (rc != 0)
1200 return rc;
1201
Avi Kivity350f69d2009-01-05 11:12:40 +02001202 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001203 return rc;
1204}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001205
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001206static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1207{
1208 struct decode_cache *c = &ctxt->decode;
1209 struct kvm_segment segment;
1210
1211 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1212
1213 c->src.val = segment.selector;
1214 emulate_push(ctxt);
1215}
1216
1217static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1218 struct x86_emulate_ops *ops, int seg)
1219{
1220 struct decode_cache *c = &ctxt->decode;
1221 unsigned long selector;
1222 int rc;
1223
1224 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1225 if (rc != 0)
1226 return rc;
1227
1228 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg);
1229 return rc;
1230}
1231
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001232static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1233{
1234 struct decode_cache *c = &ctxt->decode;
1235 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1236 int reg = VCPU_REGS_RAX;
1237
1238 while (reg <= VCPU_REGS_RDI) {
1239 (reg == VCPU_REGS_RSP) ?
1240 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1241
1242 emulate_push(ctxt);
1243 ++reg;
1244 }
1245}
1246
1247static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1248 struct x86_emulate_ops *ops)
1249{
1250 struct decode_cache *c = &ctxt->decode;
1251 int rc = 0;
1252 int reg = VCPU_REGS_RDI;
1253
1254 while (reg >= VCPU_REGS_RAX) {
1255 if (reg == VCPU_REGS_RSP) {
1256 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1257 c->op_bytes);
1258 --reg;
1259 }
1260
1261 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
1262 if (rc != 0)
1263 break;
1264 --reg;
1265 }
1266 return rc;
1267}
1268
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001269static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1270 struct x86_emulate_ops *ops)
1271{
1272 struct decode_cache *c = &ctxt->decode;
1273 int rc;
1274
Avi Kivity350f69d2009-01-05 11:12:40 +02001275 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001276 if (rc != 0)
1277 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001278 return 0;
1279}
1280
Laurent Vivier05f086f2007-09-24 11:10:55 +02001281static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001282{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001283 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001284 switch (c->modrm_reg) {
1285 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001286 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001287 break;
1288 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001289 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001290 break;
1291 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001292 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293 break;
1294 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001295 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001296 break;
1297 case 4: /* sal/shl */
1298 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001299 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300 break;
1301 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001302 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001303 break;
1304 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001305 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001306 break;
1307 }
1308}
1309
1310static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001311 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001312{
1313 struct decode_cache *c = &ctxt->decode;
1314 int rc = 0;
1315
1316 switch (c->modrm_reg) {
1317 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001318 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001319 break;
1320 case 2: /* not */
1321 c->dst.val = ~c->dst.val;
1322 break;
1323 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001324 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001325 break;
1326 default:
1327 DPRINTF("Cannot emulate %02x\n", c->b);
1328 rc = X86EMUL_UNHANDLEABLE;
1329 break;
1330 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001331 return rc;
1332}
1333
1334static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001335 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001336{
1337 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001338
1339 switch (c->modrm_reg) {
1340 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001341 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001342 break;
1343 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001344 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001345 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001346 case 2: /* call near abs */ {
1347 long int old_eip;
1348 old_eip = c->eip;
1349 c->eip = c->src.val;
1350 c->src.val = old_eip;
1351 emulate_push(ctxt);
1352 break;
1353 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001354 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001355 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001356 break;
1357 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001358 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001359 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001360 }
1361 return 0;
1362}
1363
1364static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1365 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001366 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001367{
1368 struct decode_cache *c = &ctxt->decode;
1369 u64 old, new;
1370 int rc;
1371
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001372 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373 if (rc != 0)
1374 return rc;
1375
1376 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1377 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1378
1379 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1380 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382
1383 } else {
1384 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1385 (u32) c->regs[VCPU_REGS_RBX];
1386
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001387 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001388 if (rc != 0)
1389 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001390 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001391 }
1392 return 0;
1393}
1394
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001395static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1396 struct x86_emulate_ops *ops)
1397{
1398 struct decode_cache *c = &ctxt->decode;
1399 int rc;
1400 unsigned long cs;
1401
1402 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1403 if (rc)
1404 return rc;
1405 if (c->op_bytes == 4)
1406 c->eip = (u32)c->eip;
1407 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1408 if (rc)
1409 return rc;
1410 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1411 return rc;
1412}
1413
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001414static inline int writeback(struct x86_emulate_ctxt *ctxt,
1415 struct x86_emulate_ops *ops)
1416{
1417 int rc;
1418 struct decode_cache *c = &ctxt->decode;
1419
1420 switch (c->dst.type) {
1421 case OP_REG:
1422 /* The 4-byte case *is* correct:
1423 * in 64-bit mode we zero-extend.
1424 */
1425 switch (c->dst.bytes) {
1426 case 1:
1427 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1428 break;
1429 case 2:
1430 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1431 break;
1432 case 4:
1433 *c->dst.ptr = (u32)c->dst.val;
1434 break; /* 64b: zero-ext */
1435 case 8:
1436 *c->dst.ptr = c->dst.val;
1437 break;
1438 }
1439 break;
1440 case OP_MEM:
1441 if (c->lock_prefix)
1442 rc = ops->cmpxchg_emulated(
1443 (unsigned long)c->dst.ptr,
1444 &c->dst.orig_val,
1445 &c->dst.val,
1446 c->dst.bytes,
1447 ctxt->vcpu);
1448 else
1449 rc = ops->write_emulated(
1450 (unsigned long)c->dst.ptr,
1451 &c->dst.val,
1452 c->dst.bytes,
1453 ctxt->vcpu);
1454 if (rc != 0)
1455 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001456 break;
1457 case OP_NONE:
1458 /* no writeback */
1459 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001460 default:
1461 break;
1462 }
1463 return 0;
1464}
1465
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301466static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001467{
1468 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1469 /*
1470 * an sti; sti; sequence only disable interrupts for the first
1471 * instruction. So, if the last instruction, be it emulated or
1472 * not, left the system with the INT_STI flag enabled, it
1473 * means that the last instruction is an sti. We should not
1474 * leave the flag on in this case. The same goes for mov ss
1475 */
1476 if (!(int_shadow & mask))
1477 ctxt->interruptibility = mask;
1478}
1479
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001480static inline void
1481setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1482 struct kvm_segment *cs, struct kvm_segment *ss)
1483{
1484 memset(cs, 0, sizeof(struct kvm_segment));
1485 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1486 memset(ss, 0, sizeof(struct kvm_segment));
1487
1488 cs->l = 0; /* will be adjusted later */
1489 cs->base = 0; /* flat segment */
1490 cs->g = 1; /* 4kb granularity */
1491 cs->limit = 0xffffffff; /* 4GB limit */
1492 cs->type = 0x0b; /* Read, Execute, Accessed */
1493 cs->s = 1;
1494 cs->dpl = 0; /* will be adjusted later */
1495 cs->present = 1;
1496 cs->db = 1;
1497
1498 ss->unusable = 0;
1499 ss->base = 0; /* flat segment */
1500 ss->limit = 0xffffffff; /* 4GB limit */
1501 ss->g = 1; /* 4kb granularity */
1502 ss->s = 1;
1503 ss->type = 0x03; /* Read/Write, Accessed */
1504 ss->db = 1; /* 32bit stack segment */
1505 ss->dpl = 0;
1506 ss->present = 1;
1507}
1508
1509static int
1510emulate_syscall(struct x86_emulate_ctxt *ctxt)
1511{
1512 struct decode_cache *c = &ctxt->decode;
1513 struct kvm_segment cs, ss;
1514 u64 msr_data;
1515
1516 /* syscall is not available in real mode */
1517 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
1518 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE))
1519 return -1;
1520
1521 setup_syscalls_segments(ctxt, &cs, &ss);
1522
1523 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1524 msr_data >>= 32;
1525 cs.selector = (u16)(msr_data & 0xfffc);
1526 ss.selector = (u16)(msr_data + 8);
1527
1528 if (is_long_mode(ctxt->vcpu)) {
1529 cs.db = 0;
1530 cs.l = 1;
1531 }
1532 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1533 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1534
1535 c->regs[VCPU_REGS_RCX] = c->eip;
1536 if (is_long_mode(ctxt->vcpu)) {
1537#ifdef CONFIG_X86_64
1538 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1539
1540 kvm_x86_ops->get_msr(ctxt->vcpu,
1541 ctxt->mode == X86EMUL_MODE_PROT64 ?
1542 MSR_LSTAR : MSR_CSTAR, &msr_data);
1543 c->eip = msr_data;
1544
1545 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1546 ctxt->eflags &= ~(msr_data | EFLG_RF);
1547#endif
1548 } else {
1549 /* legacy mode */
1550 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1551 c->eip = (u32)msr_data;
1552
1553 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1554 }
1555
1556 return 0;
1557}
1558
Andre Przywara8c604352009-06-18 12:56:01 +02001559static int
1560emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1561{
1562 struct decode_cache *c = &ctxt->decode;
1563 struct kvm_segment cs, ss;
1564 u64 msr_data;
1565
1566 /* inject #UD if LOCK prefix is used */
1567 if (c->lock_prefix)
1568 return -1;
1569
1570 /* inject #GP if in real mode or paging is disabled */
1571 if (ctxt->mode == X86EMUL_MODE_REAL ||
1572 !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1573 kvm_inject_gp(ctxt->vcpu, 0);
1574 return -1;
1575 }
1576
1577 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1578 * Therefore, we inject an #UD.
1579 */
1580 if (ctxt->mode == X86EMUL_MODE_PROT64)
1581 return -1;
1582
1583 setup_syscalls_segments(ctxt, &cs, &ss);
1584
1585 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1586 switch (ctxt->mode) {
1587 case X86EMUL_MODE_PROT32:
1588 if ((msr_data & 0xfffc) == 0x0) {
1589 kvm_inject_gp(ctxt->vcpu, 0);
1590 return -1;
1591 }
1592 break;
1593 case X86EMUL_MODE_PROT64:
1594 if (msr_data == 0x0) {
1595 kvm_inject_gp(ctxt->vcpu, 0);
1596 return -1;
1597 }
1598 break;
1599 }
1600
1601 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1602 cs.selector = (u16)msr_data;
1603 cs.selector &= ~SELECTOR_RPL_MASK;
1604 ss.selector = cs.selector + 8;
1605 ss.selector &= ~SELECTOR_RPL_MASK;
1606 if (ctxt->mode == X86EMUL_MODE_PROT64
1607 || is_long_mode(ctxt->vcpu)) {
1608 cs.db = 0;
1609 cs.l = 1;
1610 }
1611
1612 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1613 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1614
1615 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1616 c->eip = msr_data;
1617
1618 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1619 c->regs[VCPU_REGS_RSP] = msr_data;
1620
1621 return 0;
1622}
1623
Andre Przywara4668f052009-06-18 12:56:02 +02001624static int
1625emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1626{
1627 struct decode_cache *c = &ctxt->decode;
1628 struct kvm_segment cs, ss;
1629 u64 msr_data;
1630 int usermode;
1631
1632 /* inject #UD if LOCK prefix is used */
1633 if (c->lock_prefix)
1634 return -1;
1635
1636 /* inject #GP if in real mode or paging is disabled */
1637 if (ctxt->mode == X86EMUL_MODE_REAL
1638 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1639 kvm_inject_gp(ctxt->vcpu, 0);
1640 return -1;
1641 }
1642
1643 /* sysexit must be called from CPL 0 */
1644 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1645 kvm_inject_gp(ctxt->vcpu, 0);
1646 return -1;
1647 }
1648
1649 setup_syscalls_segments(ctxt, &cs, &ss);
1650
1651 if ((c->rex_prefix & 0x8) != 0x0)
1652 usermode = X86EMUL_MODE_PROT64;
1653 else
1654 usermode = X86EMUL_MODE_PROT32;
1655
1656 cs.dpl = 3;
1657 ss.dpl = 3;
1658 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1659 switch (usermode) {
1660 case X86EMUL_MODE_PROT32:
1661 cs.selector = (u16)(msr_data + 16);
1662 if ((msr_data & 0xfffc) == 0x0) {
1663 kvm_inject_gp(ctxt->vcpu, 0);
1664 return -1;
1665 }
1666 ss.selector = (u16)(msr_data + 24);
1667 break;
1668 case X86EMUL_MODE_PROT64:
1669 cs.selector = (u16)(msr_data + 32);
1670 if (msr_data == 0x0) {
1671 kvm_inject_gp(ctxt->vcpu, 0);
1672 return -1;
1673 }
1674 ss.selector = cs.selector + 8;
1675 cs.db = 0;
1676 cs.l = 1;
1677 break;
1678 }
1679 cs.selector |= SELECTOR_RPL_MASK;
1680 ss.selector |= SELECTOR_RPL_MASK;
1681
1682 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1683 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1684
1685 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1686 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1687
1688 return 0;
1689}
1690
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001691int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001692x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001693{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001694 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001695 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001696 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001697 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001698 unsigned int port;
1699 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001700 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001701
Glauber Costa310b5d32009-05-12 16:21:06 -04001702 ctxt->interruptibility = 0;
1703
Laurent Vivier34273182007-09-18 11:27:37 +02001704 /* Shadow copy of register state. Committed on successful emulation.
1705 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1706 * modify them.
1707 */
1708
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001709 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001710 saved_eip = c->eip;
1711
Avi Kivityc7e75a32007-10-28 16:34:25 +02001712 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001713 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001714
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001715 if (c->rep_prefix && (c->d & String)) {
1716 /* All REP prefixes have the same first termination condition */
1717 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001718 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001719 goto done;
1720 }
1721 /* The second termination condition only applies for REPE
1722 * and REPNE. Test if the repeat string operation prefix is
1723 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1724 * corresponding termination condition according to:
1725 * - if REPE/REPZ and ZF = 0 then done
1726 * - if REPNE/REPNZ and ZF = 1 then done
1727 */
1728 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1729 (c->b == 0xae) || (c->b == 0xaf)) {
1730 if ((c->rep_prefix == REPE_PREFIX) &&
1731 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001732 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001733 goto done;
1734 }
1735 if ((c->rep_prefix == REPNE_PREFIX) &&
1736 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001737 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001738 goto done;
1739 }
1740 }
1741 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001742 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001743 }
1744
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001745 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001746 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001747 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001748 rc = ops->read_emulated((unsigned long)c->src.ptr,
1749 &c->src.val,
1750 c->src.bytes,
1751 ctxt->vcpu);
1752 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001753 goto done;
1754 c->src.orig_val = c->src.val;
1755 }
1756
1757 if ((c->d & DstMask) == ImplicitOps)
1758 goto special_insn;
1759
1760
1761 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001762 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001763 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1764 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001765 if (c->d & BitOp) {
1766 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001767
Laurent Viviere4e03de2007-09-18 11:52:50 +02001768 c->dst.ptr = (void *)c->dst.ptr +
1769 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001770 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001771 if (!(c->d & Mov) &&
1772 /* optimisation - avoid slow emulated read */
1773 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1774 &c->dst.val,
1775 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001776 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001777 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001778 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001779
Avi Kivity018a98d2007-11-27 19:30:56 +02001780special_insn:
1781
Laurent Viviere4e03de2007-09-18 11:52:50 +02001782 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 goto twobyte_insn;
1784
Laurent Viviere4e03de2007-09-18 11:52:50 +02001785 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 case 0x00 ... 0x05:
1787 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001788 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001790 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001791 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1792 break;
1793 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001794 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1795 if (rc != 0)
1796 goto done;
1797 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 case 0x08 ... 0x0d:
1799 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001800 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001802 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001803 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1804 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 case 0x10 ... 0x15:
1806 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001807 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001809 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001810 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1811 break;
1812 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001813 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1814 if (rc != 0)
1815 goto done;
1816 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817 case 0x18 ... 0x1d:
1818 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001819 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001822 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1823 break;
1824 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001825 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1826 if (rc != 0)
1827 goto done;
1828 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001829 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001831 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 break;
1833 case 0x28 ... 0x2d:
1834 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001835 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 break;
1837 case 0x30 ... 0x35:
1838 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001839 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 break;
1841 case 0x38 ... 0x3d:
1842 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001843 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001845 case 0x40 ... 0x47: /* inc r16/r32 */
1846 emulate_1op("inc", c->dst, ctxt->eflags);
1847 break;
1848 case 0x48 ... 0x4f: /* dec r16/r32 */
1849 emulate_1op("dec", c->dst, ctxt->eflags);
1850 break;
1851 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001852 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001853 break;
1854 case 0x58 ... 0x5f: /* pop reg */
1855 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001856 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001857 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001858 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001859 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001860 case 0x60: /* pusha */
1861 emulate_pusha(ctxt);
1862 break;
1863 case 0x61: /* popa */
1864 rc = emulate_popa(ctxt, ops);
1865 if (rc != 0)
1866 goto done;
1867 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001869 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001871 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001873 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001874 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001875 emulate_push(ctxt);
1876 break;
1877 case 0x6c: /* insb */
1878 case 0x6d: /* insw/insd */
Avi Kivity851ba692009-08-24 11:10:17 +03001879 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001880 1,
1881 (c->d & ByteOp) ? 1 : c->op_bytes,
1882 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001883 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001884 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001885 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001886 c->regs[VCPU_REGS_RDI]),
1887 c->rep_prefix,
1888 c->regs[VCPU_REGS_RDX]) == 0) {
1889 c->eip = saved_eip;
1890 return -1;
1891 }
1892 return 0;
1893 case 0x6e: /* outsb */
1894 case 0x6f: /* outsw/outsd */
Avi Kivity851ba692009-08-24 11:10:17 +03001895 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001896 0,
1897 (c->d & ByteOp) ? 1 : c->op_bytes,
1898 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001899 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001900 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001901 register_address(c,
1902 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001903 c->regs[VCPU_REGS_RSI]),
1904 c->rep_prefix,
1905 c->regs[VCPU_REGS_RDX]) == 0) {
1906 c->eip = saved_eip;
1907 return -1;
1908 }
1909 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001910 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001911 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001912 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001915 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 case 0:
1917 goto add;
1918 case 1:
1919 goto or;
1920 case 2:
1921 goto adc;
1922 case 3:
1923 goto sbb;
1924 case 4:
1925 goto and;
1926 case 5:
1927 goto sub;
1928 case 6:
1929 goto xor;
1930 case 7:
1931 goto cmp;
1932 }
1933 break;
1934 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001935 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 break;
1937 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001938 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001940 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001942 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 break;
1944 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001945 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 break;
1947 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001948 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 break; /* 64b reg: zero-extend */
1950 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001951 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 break;
1953 }
1954 /*
1955 * Write back the memory destination with implicit LOCK
1956 * prefix.
1957 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001958 c->dst.val = c->src.val;
1959 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001962 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001963 case 0x8c: { /* mov r/m, sreg */
1964 struct kvm_segment segreg;
1965
1966 if (c->modrm_reg <= 5)
1967 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1968 else {
1969 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1970 c->modrm);
1971 goto cannot_emulate;
1972 }
1973 c->dst.val = segreg.selector;
1974 break;
1975 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001976 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001977 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001978 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001979 case 0x8e: { /* mov seg, r/m16 */
1980 uint16_t sel;
1981 int type_bits;
1982 int err;
1983
1984 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001985 if (c->modrm_reg == VCPU_SREG_SS)
1986 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1987
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001988 if (c->modrm_reg <= 5) {
1989 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1990 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1991 type_bits, c->modrm_reg);
1992 } else {
1993 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1994 c->modrm);
1995 goto cannot_emulate;
1996 }
1997
1998 if (err < 0)
1999 goto cannot_emulate;
2000
2001 c->dst.type = OP_NONE; /* Disable writeback. */
2002 break;
2003 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002005 rc = emulate_grp1a(ctxt, ops);
2006 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002009 case 0x90: /* nop / xchg r8,rax */
2010 if (!(c->rex_prefix & 1)) { /* nop */
2011 c->dst.type = OP_NONE;
2012 break;
2013 }
2014 case 0x91 ... 0x97: /* xchg reg,rax */
2015 c->src.type = c->dst.type = OP_REG;
2016 c->src.bytes = c->dst.bytes = c->op_bytes;
2017 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2018 c->src.val = *(c->src.ptr);
2019 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002020 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002021 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022 emulate_push(ctxt);
2023 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002024 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002025 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002026 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002027 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002028 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002029 case 0xa0 ... 0xa1: /* mov */
2030 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2031 c->dst.val = c->src.val;
2032 break;
2033 case 0xa2 ... 0xa3: /* mov */
2034 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2035 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002037 c->dst.type = OP_MEM;
2038 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002039 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002040 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002041 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08002042 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002043 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002044 c->regs[VCPU_REGS_RSI]),
2045 &c->dst.val,
2046 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002048 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002049 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002050 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002051 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002052 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002053 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 break;
2055 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002056 c->src.type = OP_NONE; /* Disable writeback. */
2057 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002058 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002059 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002060 c->regs[VCPU_REGS_RSI]);
2061 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
2062 &c->src.val,
2063 c->src.bytes,
2064 ctxt->vcpu)) != 0)
2065 goto done;
2066
2067 c->dst.type = OP_NONE; /* Disable writeback. */
2068 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002069 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002070 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002071 c->regs[VCPU_REGS_RDI]);
2072 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
2073 &c->dst.val,
2074 c->dst.bytes,
2075 ctxt->vcpu)) != 0)
2076 goto done;
2077
2078 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2079
2080 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2081
Harvey Harrison7a9572752008-02-19 07:40:41 -08002082 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002083 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2084 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002085 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002086 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2087 : c->dst.bytes);
2088
2089 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002091 c->dst.type = OP_MEM;
2092 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002093 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002094 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002095 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002096 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002097 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002098 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002099 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 break;
2101 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002102 c->dst.type = OP_REG;
2103 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2104 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08002105 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002106 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002107 c->regs[VCPU_REGS_RSI]),
2108 &c->dst.val,
2109 c->dst.bytes,
2110 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002112 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002113 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002114 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 break;
2116 case 0xae ... 0xaf: /* scas */
2117 DPRINTF("Urk! I don't handle SCAS.\n");
2118 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002119 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002120 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002121 case 0xc0 ... 0xc1:
2122 emulate_grp2(ctxt);
2123 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002124 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002125 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002126 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002127 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002128 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002129 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2130 mov:
2131 c->dst.val = c->src.val;
2132 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002133 case 0xcb: /* ret far */
2134 rc = emulate_ret_far(ctxt, ops);
2135 if (rc)
2136 goto done;
2137 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002138 case 0xd0 ... 0xd1: /* Grp2 */
2139 c->src.val = 1;
2140 emulate_grp2(ctxt);
2141 break;
2142 case 0xd2 ... 0xd3: /* Grp2 */
2143 c->src.val = c->regs[VCPU_REGS_RCX];
2144 emulate_grp2(ctxt);
2145 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002146 case 0xe4: /* inb */
2147 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002148 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002149 io_dir_in = 1;
2150 goto do_io;
2151 case 0xe6: /* outb */
2152 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002153 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002154 io_dir_in = 0;
2155 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002156 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002157 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002158 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002159 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002160 emulate_push(ctxt);
2161 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002162 }
2163 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002164 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002165 case 0xea: /* jmp far */
2166 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2167 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002168 DPRINTF("jmp far: Failed to load CS descriptor\n");
2169 goto cannot_emulate;
2170 }
2171
Gleb Natapov782b8772009-04-12 13:36:25 +03002172 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002173 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002174 case 0xeb:
2175 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002176 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002177 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002178 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002179 case 0xec: /* in al,dx */
2180 case 0xed: /* in (e/r)ax,dx */
2181 port = c->regs[VCPU_REGS_RDX];
2182 io_dir_in = 1;
2183 goto do_io;
2184 case 0xee: /* out al,dx */
2185 case 0xef: /* out (e/r)ax,dx */
2186 port = c->regs[VCPU_REGS_RDX];
2187 io_dir_in = 0;
Avi Kivity851ba692009-08-24 11:10:17 +03002188 do_io: if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002189 (c->d & ByteOp) ? 1 : c->op_bytes,
2190 port) != 0) {
2191 c->eip = saved_eip;
2192 goto cannot_emulate;
2193 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002194 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002195 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002196 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002197 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002198 case 0xf5: /* cmc */
2199 /* complement carry flag from eflags reg */
2200 ctxt->eflags ^= EFLG_CF;
2201 c->dst.type = OP_NONE; /* Disable writeback. */
2202 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002203 case 0xf6 ... 0xf7: /* Grp3 */
2204 rc = emulate_grp3(ctxt, ops);
2205 if (rc != 0)
2206 goto done;
2207 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002208 case 0xf8: /* clc */
2209 ctxt->eflags &= ~EFLG_CF;
2210 c->dst.type = OP_NONE; /* Disable writeback. */
2211 break;
2212 case 0xfa: /* cli */
2213 ctxt->eflags &= ~X86_EFLAGS_IF;
2214 c->dst.type = OP_NONE; /* Disable writeback. */
2215 break;
2216 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04002217 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02002218 ctxt->eflags |= X86_EFLAGS_IF;
2219 c->dst.type = OP_NONE; /* Disable writeback. */
2220 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002221 case 0xfc: /* cld */
2222 ctxt->eflags &= ~EFLG_DF;
2223 c->dst.type = OP_NONE; /* Disable writeback. */
2224 break;
2225 case 0xfd: /* std */
2226 ctxt->eflags |= EFLG_DF;
2227 c->dst.type = OP_NONE; /* Disable writeback. */
2228 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002229 case 0xfe ... 0xff: /* Grp4/Grp5 */
2230 rc = emulate_grp45(ctxt, ops);
2231 if (rc != 0)
2232 goto done;
2233 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002235
2236writeback:
2237 rc = writeback(ctxt, ops);
2238 if (rc != 0)
2239 goto done;
2240
2241 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002242 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002243 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002244
2245done:
2246 if (rc == X86EMUL_UNHANDLEABLE) {
2247 c->eip = saved_eip;
2248 return -1;
2249 }
2250 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251
2252twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002253 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002255 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 u16 size;
2257 unsigned long address;
2258
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002259 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002260 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002261 goto cannot_emulate;
2262
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002263 rc = kvm_fix_hypercall(ctxt->vcpu);
2264 if (rc)
2265 goto done;
2266
Avi Kivity33e38852008-05-21 15:34:25 +03002267 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002268 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002269 /* Disable writeback. */
2270 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002271 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002273 rc = read_descriptor(ctxt, ops, c->src.ptr,
2274 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 if (rc)
2276 goto done;
2277 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002278 /* Disable writeback. */
2279 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002281 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002282 if (c->modrm_mod == 3) {
2283 switch (c->modrm_rm) {
2284 case 1:
2285 rc = kvm_fix_hypercall(ctxt->vcpu);
2286 if (rc)
2287 goto done;
2288 break;
2289 default:
2290 goto cannot_emulate;
2291 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002292 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002293 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002294 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002295 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002296 if (rc)
2297 goto done;
2298 realmode_lidt(ctxt->vcpu, size, address);
2299 }
Avi Kivity16286d02008-04-14 14:40:50 +03002300 /* Disable writeback. */
2301 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 break;
2303 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002304 c->dst.bytes = 2;
2305 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 break;
2307 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002308 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2309 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002310 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 break;
2312 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002313 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002314 /* Disable writeback. */
2315 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 break;
2317 default:
2318 goto cannot_emulate;
2319 }
2320 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002321 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002322 if (emulate_syscall(ctxt) == -1)
2323 goto cannot_emulate;
2324 else
2325 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002326 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002327 case 0x06:
2328 emulate_clts(ctxt->vcpu);
2329 c->dst.type = OP_NONE;
2330 break;
2331 case 0x08: /* invd */
2332 case 0x09: /* wbinvd */
2333 case 0x0d: /* GrpP (prefetch) */
2334 case 0x18: /* Grp16 (prefetch/nop) */
2335 c->dst.type = OP_NONE;
2336 break;
2337 case 0x20: /* mov cr, reg */
2338 if (c->modrm_mod != 3)
2339 goto cannot_emulate;
2340 c->regs[c->modrm_rm] =
2341 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2342 c->dst.type = OP_NONE; /* no writeback */
2343 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002345 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002347 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002348 if (rc)
2349 goto cannot_emulate;
2350 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002352 case 0x22: /* mov reg, cr */
2353 if (c->modrm_mod != 3)
2354 goto cannot_emulate;
2355 realmode_set_cr(ctxt->vcpu,
2356 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2357 c->dst.type = OP_NONE;
2358 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002360 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002362 rc = emulator_set_dr(ctxt, c->modrm_reg,
2363 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002364 if (rc)
2365 goto cannot_emulate;
2366 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002368 case 0x30:
2369 /* wrmsr */
2370 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2371 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2372 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2373 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002374 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002375 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002376 }
2377 rc = X86EMUL_CONTINUE;
2378 c->dst.type = OP_NONE;
2379 break;
2380 case 0x32:
2381 /* rdmsr */
2382 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2383 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002384 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002385 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002386 } else {
2387 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2388 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2389 }
2390 rc = X86EMUL_CONTINUE;
2391 c->dst.type = OP_NONE;
2392 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002393 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002394 if (emulate_sysenter(ctxt) == -1)
2395 goto cannot_emulate;
2396 else
2397 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002398 break;
2399 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002400 if (emulate_sysexit(ctxt) == -1)
2401 goto cannot_emulate;
2402 else
2403 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002404 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002406 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002407 if (!test_cc(c->b, ctxt->eflags))
2408 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002410 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002411 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002412 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002413 c->dst.type = OP_NONE;
2414 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002415 case 0xa0: /* push fs */
2416 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2417 break;
2418 case 0xa1: /* pop fs */
2419 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2420 if (rc != 0)
2421 goto done;
2422 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002423 case 0xa3:
2424 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002425 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002426 /* only subword offset */
2427 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002428 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002429 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002430 case 0xa4: /* shld imm8, r, r/m */
2431 case 0xa5: /* shld cl, r, r/m */
2432 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2433 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002434 case 0xa8: /* push gs */
2435 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2436 break;
2437 case 0xa9: /* pop gs */
2438 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2439 if (rc != 0)
2440 goto done;
2441 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002442 case 0xab:
2443 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002444 /* only subword offset */
2445 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002446 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002447 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002448 case 0xac: /* shrd imm8, r, r/m */
2449 case 0xad: /* shrd cl, r, r/m */
2450 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2451 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002452 case 0xae: /* clflush */
2453 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 case 0xb0 ... 0xb1: /* cmpxchg */
2455 /*
2456 * Save real source value, then compare EAX against
2457 * destination.
2458 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002459 c->src.orig_val = c->src.val;
2460 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002461 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2462 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002464 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 } else {
2466 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002467 c->dst.type = OP_REG;
2468 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 }
2470 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 case 0xb3:
2472 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002473 /* only subword offset */
2474 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002475 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002477 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002478 c->dst.bytes = c->op_bytes;
2479 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2480 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002483 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 case 0:
2485 goto bt;
2486 case 1:
2487 goto bts;
2488 case 2:
2489 goto btr;
2490 case 3:
2491 goto btc;
2492 }
2493 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002494 case 0xbb:
2495 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002496 /* only subword offset */
2497 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002498 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002499 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002501 c->dst.bytes = c->op_bytes;
2502 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2503 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002505 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002506 c->dst.bytes = c->op_bytes;
2507 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2508 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002509 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002511 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002512 if (rc != 0)
2513 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002514 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002515 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 }
2517 goto writeback;
2518
2519cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002520 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002521 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 return -1;
2523}