blob: 3ac2f1485223b7bee95df08ec3de03f0e814ba22 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
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 Kivityedf88412007-12-16 11:02:48 +020033#include <asm/kvm_x86_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
35/*
36 * Opcode effective-address decode tables.
37 * Note that we only emulate instructions that have at least one memory
38 * operand (excluding implicit stack references). We assume that stack
39 * references and instruction fetches will never occur in special memory
40 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
41 * not be handled.
42 */
43
44/* Operand sizes: 8-bit operands or specified/overridden size. */
45#define ByteOp (1<<0) /* 8-bit operands. */
46/* Destination operand type. */
47#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
48#define DstReg (2<<1) /* Register operand. */
49#define DstMem (3<<1) /* Memory operand. */
50#define DstMask (3<<1)
51/* Source operand type. */
52#define SrcNone (0<<3) /* No source operand. */
53#define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
54#define SrcReg (1<<3) /* Register operand. */
55#define SrcMem (2<<3) /* Memory operand. */
56#define SrcMem16 (3<<3) /* Memory operand (16-bit). */
57#define SrcMem32 (4<<3) /* Memory operand (32-bit). */
58#define SrcImm (5<<3) /* Immediate operand. */
59#define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
60#define SrcMask (7<<3)
61/* Generic ModRM decode. */
62#define ModRM (1<<6)
63/* Destination is only written; never read. */
64#define Mov (1<<7)
Avi Kivity038e51d2007-01-22 20:40:40 -080065#define BitOp (1<<8)
Avi Kivityc7e75a32007-10-28 16:34:25 +020066#define MemAbs (1<<9) /* Memory operand is absolute displacement */
Avi Kivityb9fa9d62007-11-27 19:05:37 +020067#define String (1<<10) /* String instruction (rep capable) */
Avi Kivity6e3d5df2007-12-06 18:14:14 +020068#define Stack (1<<11) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020069#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
70#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
71#define GroupMask 0xff /* Group number stored in bits 0:7 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Avi Kivity43bb19c2008-01-18 12:46:50 +020073enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020074 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020075 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020076};
77
Avi Kivityc7e75a32007-10-28 16:34:25 +020078static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080079 /* 0x00 - 0x07 */
80 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
81 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
82 0, 0, 0, 0,
83 /* 0x08 - 0x0F */
84 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
85 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
86 0, 0, 0, 0,
87 /* 0x10 - 0x17 */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
90 0, 0, 0, 0,
91 /* 0x18 - 0x1F */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x20 - 0x27 */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030098 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 /* 0x28 - 0x2F */
100 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
101 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
102 0, 0, 0, 0,
103 /* 0x30 - 0x37 */
104 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
105 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
106 0, 0, 0, 0,
107 /* 0x38 - 0x3F */
108 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
109 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
110 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700111 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200112 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700113 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200114 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300115 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200116 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
117 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300118 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200119 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
120 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700121 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700123 0, 0, 0, 0,
124 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300125 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300126 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
127 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300128 /* 0x70 - 0x77 */
129 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
130 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
131 /* 0x78 - 0x7F */
132 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
133 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200135 Group | Group1_80, Group | Group1_81,
136 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
138 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
139 /* 0x88 - 0x8F */
140 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
141 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200142 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200143 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300144 /* 0x90 - 0x97 */
145 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
146 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200147 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200149 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
150 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200151 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
152 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200154 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
155 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
156 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300157 /* 0xB0 - 0xB7 */
158 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
159 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
160 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
161 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
162 /* 0xB8 - 0xBF */
163 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
164 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
165 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
166 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300168 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200169 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300170 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 /* 0xC8 - 0xCF */
172 0, 0, 0, 0, 0, 0, 0, 0,
173 /* 0xD0 - 0xD7 */
174 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
175 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
176 0, 0, 0, 0,
177 /* 0xD8 - 0xDF */
178 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300179 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300180 0, 0, 0, 0,
181 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
182 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300183 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200184 ImplicitOps | Stack, SrcImm | ImplicitOps,
185 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300186 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
187 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188 /* 0xF0 - 0xF7 */
189 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200190 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700192 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300193 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194};
195
Avi Kivity038e51d2007-01-22 20:40:40 -0800196static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200198 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200199 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0x10 - 0x1F */
201 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
202 /* 0x20 - 0x2F */
203 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0,
205 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300206 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 /* 0x40 - 0x47 */
208 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
209 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
210 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
211 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
212 /* 0x48 - 0x4F */
213 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
214 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
215 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
216 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
217 /* 0x50 - 0x5F */
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0x60 - 0x6F */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0x70 - 0x7F */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300224 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
225 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
226 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
227 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x90 - 0x9F */
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
230 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800231 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 /* 0xA8 - 0xAF */
Glauber Costa2a7c5b82008-07-10 17:08:15 -0300233 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0xB0 - 0xB7 */
235 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800236 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
238 DstReg | SrcMem16 | ModRM | Mov,
239 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800240 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
242 DstReg | SrcMem16 | ModRM | Mov,
243 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800244 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
245 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 /* 0xD0 - 0xDF */
247 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248 /* 0xE0 - 0xEF */
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /* 0xF0 - 0xFF */
251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
252};
253
Avi Kivitye09d0822008-01-18 12:38:59 +0200254static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200255 [Group1_80*8] =
256 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
257 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
258 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
259 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
260 [Group1_81*8] =
261 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
262 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
263 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
264 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
265 [Group1_82*8] =
266 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
267 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
268 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
269 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
270 [Group1_83*8] =
271 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
272 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
273 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
274 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200275 [Group1A*8] =
276 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200277 [Group3_Byte*8] =
278 ByteOp | SrcImm | DstMem | ModRM, 0,
279 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
280 0, 0, 0, 0,
281 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400282 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300283 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200284 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200285 [Group4*8] =
286 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
287 0, 0, 0, 0, 0, 0,
288 [Group5*8] =
289 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, 0, 0,
290 SrcMem | ModRM, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200291 [Group7*8] =
292 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300293 SrcNone | ModRM | DstMem | Mov, 0,
294 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200295};
296
297static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200298 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300299 SrcNone | ModRM, 0, 0, 0,
300 SrcNone | ModRM | DstMem | Mov, 0,
301 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200302};
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304/* EFLAGS bit definitions. */
305#define EFLG_OF (1<<11)
306#define EFLG_DF (1<<10)
307#define EFLG_SF (1<<7)
308#define EFLG_ZF (1<<6)
309#define EFLG_AF (1<<4)
310#define EFLG_PF (1<<2)
311#define EFLG_CF (1<<0)
312
313/*
314 * Instruction emulation:
315 * Most instructions are emulated directly via a fragment of inline assembly
316 * code. This allows us to save/restore EFLAGS and thus very easily pick up
317 * any modified flags.
318 */
319
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800320#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321#define _LO32 "k" /* force 32-bit operand */
322#define _STK "%%rsp" /* stack pointer */
323#elif defined(__i386__)
324#define _LO32 "" /* force 32-bit operand */
325#define _STK "%%esp" /* stack pointer */
326#endif
327
328/*
329 * These EFLAGS bits are restored from saved value during emulation, and
330 * any changes are written back to the saved value after emulation.
331 */
332#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
333
334/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200335#define _PRE_EFLAGS(_sav, _msk, _tmp) \
336 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
337 "movl %"_sav",%"_LO32 _tmp"; " \
338 "push %"_tmp"; " \
339 "push %"_tmp"; " \
340 "movl %"_msk",%"_LO32 _tmp"; " \
341 "andl %"_LO32 _tmp",("_STK"); " \
342 "pushf; " \
343 "notl %"_LO32 _tmp"; " \
344 "andl %"_LO32 _tmp",("_STK"); " \
345 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
346 "pop %"_tmp"; " \
347 "orl %"_LO32 _tmp",("_STK"); " \
348 "popf; " \
349 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350
351/* After executing instruction: write-back necessary bits in EFLAGS. */
352#define _POST_EFLAGS(_sav, _msk, _tmp) \
353 /* _sav |= EFLAGS & _msk; */ \
354 "pushf; " \
355 "pop %"_tmp"; " \
356 "andl %"_msk",%"_LO32 _tmp"; " \
357 "orl %"_LO32 _tmp",%"_sav"; "
358
359/* Raw emulation: instruction has two explicit operands. */
360#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
361 do { \
362 unsigned long _tmp; \
363 \
364 switch ((_dst).bytes) { \
365 case 2: \
366 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400367 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400369 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 : "=m" (_eflags), "=m" ((_dst).val), \
371 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400372 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 break; \
374 case 4: \
375 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400376 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400378 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 : "=m" (_eflags), "=m" ((_dst).val), \
380 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 break; \
383 case 8: \
384 __emulate_2op_8byte(_op, _src, _dst, \
385 _eflags, _qx, _qy); \
386 break; \
387 } \
388 } while (0)
389
390#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
391 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800392 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400393 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394 case 1: \
395 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400396 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400398 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800400 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400401 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 break; \
403 default: \
404 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
405 _wx, _wy, _lx, _ly, _qx, _qy); \
406 break; \
407 } \
408 } while (0)
409
410/* Source operand is byte-sized and may be restricted to just %cl. */
411#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
412 __emulate_2op(_op, _src, _dst, _eflags, \
413 "b", "c", "b", "c", "b", "c", "b", "c")
414
415/* Source operand is byte, word, long or quad sized. */
416#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
417 __emulate_2op(_op, _src, _dst, _eflags, \
418 "b", "q", "w", "r", _LO32, "r", "", "r")
419
420/* Source operand is word, long or quad sized. */
421#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
422 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
423 "w", "r", _LO32, "r", "", "r")
424
425/* Instruction has only one explicit operand (no source operand). */
426#define emulate_1op(_op, _dst, _eflags) \
427 do { \
428 unsigned long _tmp; \
429 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400430 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 case 1: \
432 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400433 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400435 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 : "=m" (_eflags), "=m" ((_dst).val), \
437 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400438 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 break; \
440 case 2: \
441 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400444 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 : "=m" (_eflags), "=m" ((_dst).val), \
446 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400447 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 break; \
449 case 4: \
450 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400451 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400453 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 : "=m" (_eflags), "=m" ((_dst).val), \
455 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400456 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 break; \
458 case 8: \
459 __emulate_1op_8byte(_op, _dst, _eflags); \
460 break; \
461 } \
462 } while (0)
463
464/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800465#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
467 do { \
468 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400469 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400471 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400473 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 } while (0)
475
476#define __emulate_1op_8byte(_op, _dst, _eflags) \
477 do { \
478 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400479 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400481 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400483 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 } while (0)
485
486#elif defined(__i386__)
487#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
488#define __emulate_1op_8byte(_op, _dst, _eflags)
489#endif /* __i386__ */
490
491/* Fetch next part of the instruction being emulated. */
492#define insn_fetch(_type, _size, _eip) \
493({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200494 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400495 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 goto done; \
497 (_eip) += (_size); \
498 (_type)_x; \
499})
500
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800501static inline unsigned long ad_mask(struct decode_cache *c)
502{
503 return (1UL << (c->ad_bytes << 3)) - 1;
504}
505
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800507static inline unsigned long
508address_mask(struct decode_cache *c, unsigned long reg)
509{
510 if (c->ad_bytes == sizeof(unsigned long))
511 return reg;
512 else
513 return reg & ad_mask(c);
514}
515
516static inline unsigned long
517register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
518{
519 return base + address_mask(c, reg);
520}
521
Harvey Harrison7a9572752008-02-19 07:40:41 -0800522static inline void
523register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
524{
525 if (c->ad_bytes == sizeof(unsigned long))
526 *reg += inc;
527 else
528 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
529}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
Harvey Harrison7a9572752008-02-19 07:40:41 -0800531static inline void jmp_rel(struct decode_cache *c, int rel)
532{
533 register_address_increment(c, &c->eip, rel);
534}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300535
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300536static void set_seg_override(struct decode_cache *c, int seg)
537{
538 c->has_seg_override = true;
539 c->seg_override = seg;
540}
541
542static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
543{
544 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
545 return 0;
546
547 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
548}
549
550static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
551 struct decode_cache *c)
552{
553 if (!c->has_seg_override)
554 return 0;
555
556 return seg_base(ctxt, c->seg_override);
557}
558
559static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
560{
561 return seg_base(ctxt, VCPU_SREG_ES);
562}
563
564static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
565{
566 return seg_base(ctxt, VCPU_SREG_SS);
567}
568
Avi Kivity62266862007-11-20 13:15:52 +0200569static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
570 struct x86_emulate_ops *ops,
571 unsigned long linear, u8 *dest)
572{
573 struct fetch_cache *fc = &ctxt->decode.fetch;
574 int rc;
575 int size;
576
577 if (linear < fc->start || linear >= fc->end) {
578 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
579 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
580 if (rc)
581 return rc;
582 fc->start = linear;
583 fc->end = linear + size;
584 }
585 *dest = fc->data[linear - fc->start];
586 return 0;
587}
588
589static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
590 struct x86_emulate_ops *ops,
591 unsigned long eip, void *dest, unsigned size)
592{
593 int rc = 0;
594
595 eip += ctxt->cs_base;
596 while (size--) {
597 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
598 if (rc)
599 return rc;
600 }
601 return 0;
602}
603
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000604/*
605 * Given the 'reg' portion of a ModRM byte, and a register block, return a
606 * pointer into the block that addresses the relevant register.
607 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
608 */
609static void *decode_register(u8 modrm_reg, unsigned long *regs,
610 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611{
612 void *p;
613
614 p = &regs[modrm_reg];
615 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
616 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
617 return p;
618}
619
620static int read_descriptor(struct x86_emulate_ctxt *ctxt,
621 struct x86_emulate_ops *ops,
622 void *ptr,
623 u16 *size, unsigned long *address, int op_bytes)
624{
625 int rc;
626
627 if (op_bytes == 2)
628 op_bytes = 3;
629 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300630 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
631 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 if (rc)
633 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300634 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
635 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 return rc;
637}
638
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300639static int test_cc(unsigned int condition, unsigned int flags)
640{
641 int rc = 0;
642
643 switch ((condition & 15) >> 1) {
644 case 0: /* o */
645 rc |= (flags & EFLG_OF);
646 break;
647 case 1: /* b/c/nae */
648 rc |= (flags & EFLG_CF);
649 break;
650 case 2: /* z/e */
651 rc |= (flags & EFLG_ZF);
652 break;
653 case 3: /* be/na */
654 rc |= (flags & (EFLG_CF|EFLG_ZF));
655 break;
656 case 4: /* s */
657 rc |= (flags & EFLG_SF);
658 break;
659 case 5: /* p/pe */
660 rc |= (flags & EFLG_PF);
661 break;
662 case 7: /* le/ng */
663 rc |= (flags & EFLG_ZF);
664 /* fall through */
665 case 6: /* l/nge */
666 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
667 break;
668 }
669
670 /* Odd condition identifiers (lsb == 1) have inverted sense. */
671 return (!!rc ^ (condition & 1));
672}
673
Avi Kivity3c118e22007-10-31 10:27:04 +0200674static void decode_register_operand(struct operand *op,
675 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200676 int inhibit_bytereg)
677{
Avi Kivity33615aa2007-10-31 11:15:56 +0200678 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200679 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200680
681 if (!(c->d & ModRM))
682 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200683 op->type = OP_REG;
684 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200685 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200686 op->val = *(u8 *)op->ptr;
687 op->bytes = 1;
688 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200689 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200690 op->bytes = c->op_bytes;
691 switch (op->bytes) {
692 case 2:
693 op->val = *(u16 *)op->ptr;
694 break;
695 case 4:
696 op->val = *(u32 *)op->ptr;
697 break;
698 case 8:
699 op->val = *(u64 *) op->ptr;
700 break;
701 }
702 }
703 op->orig_val = op->val;
704}
705
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200706static int decode_modrm(struct x86_emulate_ctxt *ctxt,
707 struct x86_emulate_ops *ops)
708{
709 struct decode_cache *c = &ctxt->decode;
710 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700711 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200712 int rc = 0;
713
714 if (c->rex_prefix) {
715 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
716 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
717 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
718 }
719
720 c->modrm = insn_fetch(u8, 1, c->eip);
721 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
722 c->modrm_reg |= (c->modrm & 0x38) >> 3;
723 c->modrm_rm |= (c->modrm & 0x07);
724 c->modrm_ea = 0;
725 c->use_modrm_ea = 1;
726
727 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300728 c->modrm_ptr = decode_register(c->modrm_rm,
729 c->regs, c->d & ByteOp);
730 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200731 return rc;
732 }
733
734 if (c->ad_bytes == 2) {
735 unsigned bx = c->regs[VCPU_REGS_RBX];
736 unsigned bp = c->regs[VCPU_REGS_RBP];
737 unsigned si = c->regs[VCPU_REGS_RSI];
738 unsigned di = c->regs[VCPU_REGS_RDI];
739
740 /* 16-bit ModR/M decode. */
741 switch (c->modrm_mod) {
742 case 0:
743 if (c->modrm_rm == 6)
744 c->modrm_ea += insn_fetch(u16, 2, c->eip);
745 break;
746 case 1:
747 c->modrm_ea += insn_fetch(s8, 1, c->eip);
748 break;
749 case 2:
750 c->modrm_ea += insn_fetch(u16, 2, c->eip);
751 break;
752 }
753 switch (c->modrm_rm) {
754 case 0:
755 c->modrm_ea += bx + si;
756 break;
757 case 1:
758 c->modrm_ea += bx + di;
759 break;
760 case 2:
761 c->modrm_ea += bp + si;
762 break;
763 case 3:
764 c->modrm_ea += bp + di;
765 break;
766 case 4:
767 c->modrm_ea += si;
768 break;
769 case 5:
770 c->modrm_ea += di;
771 break;
772 case 6:
773 if (c->modrm_mod != 0)
774 c->modrm_ea += bp;
775 break;
776 case 7:
777 c->modrm_ea += bx;
778 break;
779 }
780 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
781 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300782 if (!c->has_seg_override)
783 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784 c->modrm_ea = (u16)c->modrm_ea;
785 } else {
786 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700787 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200788 sib = insn_fetch(u8, 1, c->eip);
789 index_reg |= (sib >> 3) & 7;
790 base_reg |= sib & 7;
791 scale = sib >> 6;
792
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700793 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
794 c->modrm_ea += insn_fetch(s32, 4, c->eip);
795 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200796 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700797 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200798 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700799 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
800 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700801 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700802 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200803 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804 switch (c->modrm_mod) {
805 case 0:
806 if (c->modrm_rm == 5)
807 c->modrm_ea += insn_fetch(s32, 4, c->eip);
808 break;
809 case 1:
810 c->modrm_ea += insn_fetch(s8, 1, c->eip);
811 break;
812 case 2:
813 c->modrm_ea += insn_fetch(s32, 4, c->eip);
814 break;
815 }
816 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817done:
818 return rc;
819}
820
821static int decode_abs(struct x86_emulate_ctxt *ctxt,
822 struct x86_emulate_ops *ops)
823{
824 struct decode_cache *c = &ctxt->decode;
825 int rc = 0;
826
827 switch (c->ad_bytes) {
828 case 2:
829 c->modrm_ea = insn_fetch(u16, 2, c->eip);
830 break;
831 case 4:
832 c->modrm_ea = insn_fetch(u32, 4, c->eip);
833 break;
834 case 8:
835 c->modrm_ea = insn_fetch(u64, 8, c->eip);
836 break;
837 }
838done:
839 return rc;
840}
841
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200843x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200845 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200848 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849
850 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851
Laurent Viviere4e03de2007-09-18 11:52:50 +0200852 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300853 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300854 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800855 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856
857 switch (mode) {
858 case X86EMUL_MODE_REAL:
859 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200860 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 break;
862 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200863 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800865#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200867 def_op_bytes = 4;
868 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 break;
870#endif
871 default:
872 return -1;
873 }
874
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200875 c->op_bytes = def_op_bytes;
876 c->ad_bytes = def_ad_bytes;
877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200879 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200880 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200882 /* switch between 2/4 bytes */
883 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 break;
885 case 0x67: /* address-size override */
886 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200887 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200888 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200890 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200891 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300894 case 0x2e: /* CS override */
895 case 0x36: /* SS override */
896 case 0x3e: /* DS override */
897 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 break;
899 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300901 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200903 case 0x40 ... 0x4f: /* REX */
904 if (mode != X86EMUL_MODE_PROT64)
905 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200906 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200907 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200909 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200911 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100912 c->rep_prefix = REPNE_PREFIX;
913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100915 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 default:
918 goto done_prefixes;
919 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200920
921 /* Any legacy prefix after a REX prefix nullifies its effect. */
922
Avi Kivity33615aa2007-10-31 11:15:56 +0200923 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 }
925
926done_prefixes:
927
928 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200930 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200931 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
933 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200934 c->d = opcode_table[c->b];
935 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200937 if (c->b == 0x0f) {
938 c->twobyte = 1;
939 c->b = insn_fetch(u8, 1, c->eip);
940 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200942 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
Avi Kivitye09d0822008-01-18 12:38:59 +0200944 if (c->d & Group) {
945 group = c->d & GroupMask;
946 c->modrm = insn_fetch(u8, 1, c->eip);
947 --c->eip;
948
949 group = (group << 3) + ((c->modrm >> 3) & 7);
950 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
951 c->d = group2_table[group];
952 else
953 c->d = group_table[group];
954 }
955
956 /* Unrecognised? */
957 if (c->d == 0) {
958 DPRINTF("Cannot emulate %02x\n", c->b);
959 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 }
961
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200962 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
963 c->op_bytes = 8;
964
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200966 if (c->d & ModRM)
967 rc = decode_modrm(ctxt, ops);
968 else if (c->d & MemAbs)
969 rc = decode_abs(ctxt, ops);
970 if (rc)
971 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300973 if (!c->has_seg_override)
974 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200975
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300976 if (!(!c->twobyte && c->b == 0x8d))
977 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200978
979 if (c->ad_bytes != 8)
980 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 /*
982 * Decode and fetch the source operand: register, memory
983 * or immediate.
984 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200985 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 case SrcNone:
987 break;
988 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200989 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 break;
991 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200992 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 goto srcmem_common;
994 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200995 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 goto srcmem_common;
997 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200998 c->src.bytes = (c->d & ByteOp) ? 1 :
999 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001000 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001001 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001002 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001003 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001004 /*
1005 * For instructions with a ModR/M byte, switch to register
1006 * access if Mod = 3.
1007 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 if ((c->d & ModRM) && c->modrm_mod == 3) {
1009 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001010 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001011 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001012 break;
1013 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 break;
1016 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001017 c->src.type = OP_IMM;
1018 c->src.ptr = (unsigned long *)c->eip;
1019 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1020 if (c->src.bytes == 8)
1021 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001023 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001025 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 break;
1027 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001028 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 break;
1030 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 }
1034 break;
1035 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001036 c->src.type = OP_IMM;
1037 c->src.ptr = (unsigned long *)c->eip;
1038 c->src.bytes = 1;
1039 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 break;
1041 }
1042
Avi Kivity038e51d2007-01-22 20:40:40 -08001043 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001044 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001045 case ImplicitOps:
1046 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001047 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001048 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001049 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001050 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001051 break;
1052 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001053 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001054 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001055 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001056 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001057 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001058 break;
1059 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001060 c->dst.type = OP_MEM;
1061 break;
1062 }
1063
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001064 if (c->rip_relative)
1065 c->modrm_ea += c->eip;
1066
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001067done:
1068 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1069}
1070
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001071static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1072{
1073 struct decode_cache *c = &ctxt->decode;
1074
1075 c->dst.type = OP_MEM;
1076 c->dst.bytes = c->op_bytes;
1077 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001078 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001079 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001080 c->regs[VCPU_REGS_RSP]);
1081}
1082
1083static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1084 struct x86_emulate_ops *ops)
1085{
1086 struct decode_cache *c = &ctxt->decode;
1087 int rc;
1088
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001089 rc = ops->read_std(register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001090 c->regs[VCPU_REGS_RSP]),
1091 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1092 if (rc != 0)
1093 return rc;
1094
Harvey Harrison7a9572752008-02-19 07:40:41 -08001095 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096
1097 return 0;
1098}
1099
Laurent Vivier05f086f2007-09-24 11:10:55 +02001100static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001101{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001102 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001103 switch (c->modrm_reg) {
1104 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001105 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001106 break;
1107 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001108 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109 break;
1110 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001111 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001112 break;
1113 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001114 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115 break;
1116 case 4: /* sal/shl */
1117 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001118 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001119 break;
1120 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001121 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001122 break;
1123 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001124 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001125 break;
1126 }
1127}
1128
1129static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001130 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001131{
1132 struct decode_cache *c = &ctxt->decode;
1133 int rc = 0;
1134
1135 switch (c->modrm_reg) {
1136 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001137 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001138 break;
1139 case 2: /* not */
1140 c->dst.val = ~c->dst.val;
1141 break;
1142 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001143 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001144 break;
1145 default:
1146 DPRINTF("Cannot emulate %02x\n", c->b);
1147 rc = X86EMUL_UNHANDLEABLE;
1148 break;
1149 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150 return rc;
1151}
1152
1153static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001154 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155{
1156 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001157
1158 switch (c->modrm_reg) {
1159 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001160 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001161 break;
1162 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001163 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164 break;
1165 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001166 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001167 break;
1168 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001169 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001170 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001171 }
1172 return 0;
1173}
1174
1175static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1176 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001177 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178{
1179 struct decode_cache *c = &ctxt->decode;
1180 u64 old, new;
1181 int rc;
1182
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001183 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184 if (rc != 0)
1185 return rc;
1186
1187 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1188 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1189
1190 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1191 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001192 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001193
1194 } else {
1195 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1196 (u32) c->regs[VCPU_REGS_RBX];
1197
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001198 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001199 if (rc != 0)
1200 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001201 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001202 }
1203 return 0;
1204}
1205
1206static inline int writeback(struct x86_emulate_ctxt *ctxt,
1207 struct x86_emulate_ops *ops)
1208{
1209 int rc;
1210 struct decode_cache *c = &ctxt->decode;
1211
1212 switch (c->dst.type) {
1213 case OP_REG:
1214 /* The 4-byte case *is* correct:
1215 * in 64-bit mode we zero-extend.
1216 */
1217 switch (c->dst.bytes) {
1218 case 1:
1219 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1220 break;
1221 case 2:
1222 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1223 break;
1224 case 4:
1225 *c->dst.ptr = (u32)c->dst.val;
1226 break; /* 64b: zero-ext */
1227 case 8:
1228 *c->dst.ptr = c->dst.val;
1229 break;
1230 }
1231 break;
1232 case OP_MEM:
1233 if (c->lock_prefix)
1234 rc = ops->cmpxchg_emulated(
1235 (unsigned long)c->dst.ptr,
1236 &c->dst.orig_val,
1237 &c->dst.val,
1238 c->dst.bytes,
1239 ctxt->vcpu);
1240 else
1241 rc = ops->write_emulated(
1242 (unsigned long)c->dst.ptr,
1243 &c->dst.val,
1244 c->dst.bytes,
1245 ctxt->vcpu);
1246 if (rc != 0)
1247 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001248 break;
1249 case OP_NONE:
1250 /* no writeback */
1251 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001252 default:
1253 break;
1254 }
1255 return 0;
1256}
1257
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001258int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001259x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001260{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001261 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001262 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001263 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001264 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001265 unsigned int port;
1266 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001267 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001268
Laurent Vivier34273182007-09-18 11:27:37 +02001269 /* Shadow copy of register state. Committed on successful emulation.
1270 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1271 * modify them.
1272 */
1273
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001274 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001275 saved_eip = c->eip;
1276
Avi Kivityc7e75a32007-10-28 16:34:25 +02001277 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001278 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001279
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001280 if (c->rep_prefix && (c->d & String)) {
1281 /* All REP prefixes have the same first termination condition */
1282 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001283 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001284 goto done;
1285 }
1286 /* The second termination condition only applies for REPE
1287 * and REPNE. Test if the repeat string operation prefix is
1288 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1289 * corresponding termination condition according to:
1290 * - if REPE/REPZ and ZF = 0 then done
1291 * - if REPNE/REPNZ and ZF = 1 then done
1292 */
1293 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1294 (c->b == 0xae) || (c->b == 0xaf)) {
1295 if ((c->rep_prefix == REPE_PREFIX) &&
1296 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001297 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001298 goto done;
1299 }
1300 if ((c->rep_prefix == REPNE_PREFIX) &&
1301 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001302 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001303 goto done;
1304 }
1305 }
1306 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001307 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001308 }
1309
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001310 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001311 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001312 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001313 rc = ops->read_emulated((unsigned long)c->src.ptr,
1314 &c->src.val,
1315 c->src.bytes,
1316 ctxt->vcpu);
1317 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001318 goto done;
1319 c->src.orig_val = c->src.val;
1320 }
1321
1322 if ((c->d & DstMask) == ImplicitOps)
1323 goto special_insn;
1324
1325
1326 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001327 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001328 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1329 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001330 if (c->d & BitOp) {
1331 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001332
Laurent Viviere4e03de2007-09-18 11:52:50 +02001333 c->dst.ptr = (void *)c->dst.ptr +
1334 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001335 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001336 if (!(c->d & Mov) &&
1337 /* optimisation - avoid slow emulated read */
1338 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1339 &c->dst.val,
1340 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001341 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001342 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001343 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001344
Avi Kivity018a98d2007-11-27 19:30:56 +02001345special_insn:
1346
Laurent Viviere4e03de2007-09-18 11:52:50 +02001347 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 goto twobyte_insn;
1349
Laurent Viviere4e03de2007-09-18 11:52:50 +02001350 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 case 0x00 ... 0x05:
1352 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001353 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 break;
1355 case 0x08 ... 0x0d:
1356 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001357 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 break;
1359 case 0x10 ... 0x15:
1360 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001361 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 break;
1363 case 0x18 ... 0x1d:
1364 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001365 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001367 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001369 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001371 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001372 c->dst.type = OP_REG;
1373 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1374 c->dst.val = *(u8 *)c->dst.ptr;
1375 c->dst.bytes = 1;
1376 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001377 goto and;
1378 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001379 c->dst.type = OP_REG;
1380 c->dst.bytes = c->op_bytes;
1381 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1382 if (c->op_bytes == 2)
1383 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001384 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001385 c->dst.val = *(u32 *)c->dst.ptr;
1386 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001387 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 case 0x28 ... 0x2d:
1389 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001390 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 break;
1392 case 0x30 ... 0x35:
1393 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001394 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 break;
1396 case 0x38 ... 0x3d:
1397 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001398 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001400 case 0x40 ... 0x47: /* inc r16/r32 */
1401 emulate_1op("inc", c->dst, ctxt->eflags);
1402 break;
1403 case 0x48 ... 0x4f: /* dec r16/r32 */
1404 emulate_1op("dec", c->dst, ctxt->eflags);
1405 break;
1406 case 0x50 ... 0x57: /* push reg */
1407 c->dst.type = OP_MEM;
1408 c->dst.bytes = c->op_bytes;
1409 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001410 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001411 -c->op_bytes);
1412 c->dst.ptr = (void *) register_address(
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001413 c, ss_base(ctxt), c->regs[VCPU_REGS_RSP]);
Avi Kivity33615aa2007-10-31 11:15:56 +02001414 break;
1415 case 0x58 ... 0x5f: /* pop reg */
1416 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001417 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001418 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1419 c->op_bytes, ctxt->vcpu)) != 0)
1420 goto done;
1421
Harvey Harrison7a9572752008-02-19 07:40:41 -08001422 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001423 c->op_bytes);
1424 c->dst.type = OP_NONE; /* Disable writeback. */
1425 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001427 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001429 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001431 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001432 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001433 emulate_push(ctxt);
1434 break;
1435 case 0x6c: /* insb */
1436 case 0x6d: /* insw/insd */
1437 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1438 1,
1439 (c->d & ByteOp) ? 1 : c->op_bytes,
1440 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001441 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001442 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001443 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001444 c->regs[VCPU_REGS_RDI]),
1445 c->rep_prefix,
1446 c->regs[VCPU_REGS_RDX]) == 0) {
1447 c->eip = saved_eip;
1448 return -1;
1449 }
1450 return 0;
1451 case 0x6e: /* outsb */
1452 case 0x6f: /* outsw/outsd */
1453 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1454 0,
1455 (c->d & ByteOp) ? 1 : c->op_bytes,
1456 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001457 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001458 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001459 register_address(c,
1460 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001461 c->regs[VCPU_REGS_RSI]),
1462 c->rep_prefix,
1463 c->regs[VCPU_REGS_RDX]) == 0) {
1464 c->eip = saved_eip;
1465 return -1;
1466 }
1467 return 0;
1468 case 0x70 ... 0x7f: /* jcc (short) */ {
1469 int rel = insn_fetch(s8, 1, c->eip);
1470
1471 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001472 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001473 break;
1474 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001476 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 case 0:
1478 goto add;
1479 case 1:
1480 goto or;
1481 case 2:
1482 goto adc;
1483 case 3:
1484 goto sbb;
1485 case 4:
1486 goto and;
1487 case 5:
1488 goto sub;
1489 case 6:
1490 goto xor;
1491 case 7:
1492 goto cmp;
1493 }
1494 break;
1495 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001496 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 break;
1498 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001499 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001501 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001503 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 break;
1505 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 break;
1508 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001509 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 break; /* 64b reg: zero-extend */
1511 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001512 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 break;
1514 }
1515 /*
1516 * Write back the memory destination with implicit LOCK
1517 * prefix.
1518 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001519 c->dst.val = c->src.val;
1520 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001523 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001524 case 0x8c: { /* mov r/m, sreg */
1525 struct kvm_segment segreg;
1526
1527 if (c->modrm_reg <= 5)
1528 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1529 else {
1530 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1531 c->modrm);
1532 goto cannot_emulate;
1533 }
1534 c->dst.val = segreg.selector;
1535 break;
1536 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001537 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001538 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001539 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001540 case 0x8e: { /* mov seg, r/m16 */
1541 uint16_t sel;
1542 int type_bits;
1543 int err;
1544
1545 sel = c->src.val;
1546 if (c->modrm_reg <= 5) {
1547 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1548 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1549 type_bits, c->modrm_reg);
1550 } else {
1551 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1552 c->modrm);
1553 goto cannot_emulate;
1554 }
1555
1556 if (err < 0)
1557 goto cannot_emulate;
1558
1559 c->dst.type = OP_NONE; /* Disable writeback. */
1560 break;
1561 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001563 rc = emulate_grp1a(ctxt, ops);
1564 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001567 case 0x90: /* nop / xchg r8,rax */
1568 if (!(c->rex_prefix & 1)) { /* nop */
1569 c->dst.type = OP_NONE;
1570 break;
1571 }
1572 case 0x91 ... 0x97: /* xchg reg,rax */
1573 c->src.type = c->dst.type = OP_REG;
1574 c->src.bytes = c->dst.bytes = c->op_bytes;
1575 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1576 c->src.val = *(c->src.ptr);
1577 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001578 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001579 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001580 emulate_push(ctxt);
1581 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001582 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001583 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001584 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001585 case 0xa0 ... 0xa1: /* mov */
1586 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1587 c->dst.val = c->src.val;
1588 break;
1589 case 0xa2 ... 0xa3: /* mov */
1590 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1591 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001593 c->dst.type = OP_MEM;
1594 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001595 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001596 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001597 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001598 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001599 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001600 c->regs[VCPU_REGS_RSI]),
1601 &c->dst.val,
1602 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001604 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001605 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001606 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001607 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001608 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 break;
1611 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001612 c->src.type = OP_NONE; /* Disable writeback. */
1613 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001614 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001615 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001616 c->regs[VCPU_REGS_RSI]);
1617 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1618 &c->src.val,
1619 c->src.bytes,
1620 ctxt->vcpu)) != 0)
1621 goto done;
1622
1623 c->dst.type = OP_NONE; /* Disable writeback. */
1624 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001625 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001626 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001627 c->regs[VCPU_REGS_RDI]);
1628 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1629 &c->dst.val,
1630 c->dst.bytes,
1631 ctxt->vcpu)) != 0)
1632 goto done;
1633
1634 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1635
1636 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1637
Harvey Harrison7a9572752008-02-19 07:40:41 -08001638 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001639 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1640 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001641 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001642 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1643 : c->dst.bytes);
1644
1645 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001647 c->dst.type = OP_MEM;
1648 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001649 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001650 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001651 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001652 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001653 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001654 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001655 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 break;
1657 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001658 c->dst.type = OP_REG;
1659 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1660 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001661 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001662 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001663 c->regs[VCPU_REGS_RSI]),
1664 &c->dst.val,
1665 c->dst.bytes,
1666 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001668 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001669 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001670 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 break;
1672 case 0xae ... 0xaf: /* scas */
1673 DPRINTF("Urk! I don't handle SCAS.\n");
1674 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001675 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001676 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001677 case 0xc0 ... 0xc1:
1678 emulate_grp2(ctxt);
1679 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001680 case 0xc3: /* ret */
1681 c->dst.ptr = &c->eip;
1682 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001683 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1684 mov:
1685 c->dst.val = c->src.val;
1686 break;
1687 case 0xd0 ... 0xd1: /* Grp2 */
1688 c->src.val = 1;
1689 emulate_grp2(ctxt);
1690 break;
1691 case 0xd2 ... 0xd3: /* Grp2 */
1692 c->src.val = c->regs[VCPU_REGS_RCX];
1693 emulate_grp2(ctxt);
1694 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001695 case 0xe4: /* inb */
1696 case 0xe5: /* in */
1697 port = insn_fetch(u8, 1, c->eip);
1698 io_dir_in = 1;
1699 goto do_io;
1700 case 0xe6: /* outb */
1701 case 0xe7: /* out */
1702 port = insn_fetch(u8, 1, c->eip);
1703 io_dir_in = 0;
1704 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001705 case 0xe8: /* call (near) */ {
1706 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001707 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001708 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001709 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001710 break;
1711 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001712 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001713 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001714 default:
1715 DPRINTF("Call: Invalid op_bytes\n");
1716 goto cannot_emulate;
1717 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001718 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001719 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001720 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721 emulate_push(ctxt);
1722 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001723 }
1724 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001725 goto jmp;
1726 case 0xea: /* jmp far */ {
1727 uint32_t eip;
1728 uint16_t sel;
1729
1730 switch (c->op_bytes) {
1731 case 2:
1732 eip = insn_fetch(u16, 2, c->eip);
1733 break;
1734 case 4:
1735 eip = insn_fetch(u32, 4, c->eip);
1736 break;
1737 default:
1738 DPRINTF("jmp far: Invalid op_bytes\n");
1739 goto cannot_emulate;
1740 }
1741 sel = insn_fetch(u16, 2, c->eip);
1742 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1743 DPRINTF("jmp far: Failed to load CS descriptor\n");
1744 goto cannot_emulate;
1745 }
1746
1747 c->eip = eip;
1748 break;
1749 }
1750 case 0xeb:
1751 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001752 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001753 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001754 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001755 case 0xec: /* in al,dx */
1756 case 0xed: /* in (e/r)ax,dx */
1757 port = c->regs[VCPU_REGS_RDX];
1758 io_dir_in = 1;
1759 goto do_io;
1760 case 0xee: /* out al,dx */
1761 case 0xef: /* out (e/r)ax,dx */
1762 port = c->regs[VCPU_REGS_RDX];
1763 io_dir_in = 0;
1764 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1765 (c->d & ByteOp) ? 1 : c->op_bytes,
1766 port) != 0) {
1767 c->eip = saved_eip;
1768 goto cannot_emulate;
1769 }
1770 return 0;
Avi Kivity111de5d2007-11-27 19:14:21 +02001771 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001772 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001773 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001774 case 0xf5: /* cmc */
1775 /* complement carry flag from eflags reg */
1776 ctxt->eflags ^= EFLG_CF;
1777 c->dst.type = OP_NONE; /* Disable writeback. */
1778 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001779 case 0xf6 ... 0xf7: /* Grp3 */
1780 rc = emulate_grp3(ctxt, ops);
1781 if (rc != 0)
1782 goto done;
1783 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001784 case 0xf8: /* clc */
1785 ctxt->eflags &= ~EFLG_CF;
1786 c->dst.type = OP_NONE; /* Disable writeback. */
1787 break;
1788 case 0xfa: /* cli */
1789 ctxt->eflags &= ~X86_EFLAGS_IF;
1790 c->dst.type = OP_NONE; /* Disable writeback. */
1791 break;
1792 case 0xfb: /* sti */
1793 ctxt->eflags |= X86_EFLAGS_IF;
1794 c->dst.type = OP_NONE; /* Disable writeback. */
1795 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001796 case 0xfc: /* cld */
1797 ctxt->eflags &= ~EFLG_DF;
1798 c->dst.type = OP_NONE; /* Disable writeback. */
1799 break;
1800 case 0xfd: /* std */
1801 ctxt->eflags |= EFLG_DF;
1802 c->dst.type = OP_NONE; /* Disable writeback. */
1803 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001804 case 0xfe ... 0xff: /* Grp4/Grp5 */
1805 rc = emulate_grp45(ctxt, ops);
1806 if (rc != 0)
1807 goto done;
1808 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001810
1811writeback:
1812 rc = writeback(ctxt, ops);
1813 if (rc != 0)
1814 goto done;
1815
1816 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001817 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001818 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001819
1820done:
1821 if (rc == X86EMUL_UNHANDLEABLE) {
1822 c->eip = saved_eip;
1823 return -1;
1824 }
1825 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826
1827twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001828 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001830 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 u16 size;
1832 unsigned long address;
1833
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001834 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001835 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001836 goto cannot_emulate;
1837
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001838 rc = kvm_fix_hypercall(ctxt->vcpu);
1839 if (rc)
1840 goto done;
1841
Avi Kivity33e38852008-05-21 15:34:25 +03001842 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001843 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001844 /* Disable writeback. */
1845 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001846 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001848 rc = read_descriptor(ctxt, ops, c->src.ptr,
1849 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 if (rc)
1851 goto done;
1852 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001853 /* Disable writeback. */
1854 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001856 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001857 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001858 rc = kvm_fix_hypercall(ctxt->vcpu);
1859 if (rc)
1860 goto done;
1861 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001862 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001863 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001864 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001865 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001866 if (rc)
1867 goto done;
1868 realmode_lidt(ctxt->vcpu, size, address);
1869 }
Avi Kivity16286d02008-04-14 14:40:50 +03001870 /* Disable writeback. */
1871 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 break;
1873 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001874 c->dst.bytes = 2;
1875 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 break;
1877 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001878 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1879 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001880 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 break;
1882 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001883 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001884 /* Disable writeback. */
1885 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886 break;
1887 default:
1888 goto cannot_emulate;
1889 }
1890 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001891 case 0x06:
1892 emulate_clts(ctxt->vcpu);
1893 c->dst.type = OP_NONE;
1894 break;
1895 case 0x08: /* invd */
1896 case 0x09: /* wbinvd */
1897 case 0x0d: /* GrpP (prefetch) */
1898 case 0x18: /* Grp16 (prefetch/nop) */
1899 c->dst.type = OP_NONE;
1900 break;
1901 case 0x20: /* mov cr, reg */
1902 if (c->modrm_mod != 3)
1903 goto cannot_emulate;
1904 c->regs[c->modrm_rm] =
1905 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1906 c->dst.type = OP_NONE; /* no writeback */
1907 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001909 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001911 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001912 if (rc)
1913 goto cannot_emulate;
1914 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001916 case 0x22: /* mov reg, cr */
1917 if (c->modrm_mod != 3)
1918 goto cannot_emulate;
1919 realmode_set_cr(ctxt->vcpu,
1920 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1921 c->dst.type = OP_NONE;
1922 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001924 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 rc = emulator_set_dr(ctxt, c->modrm_reg,
1927 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001928 if (rc)
1929 goto cannot_emulate;
1930 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001932 case 0x30:
1933 /* wrmsr */
1934 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1935 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1936 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1937 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001938 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001939 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001940 }
1941 rc = X86EMUL_CONTINUE;
1942 c->dst.type = OP_NONE;
1943 break;
1944 case 0x32:
1945 /* rdmsr */
1946 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1947 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001948 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001949 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001950 } else {
1951 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1952 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1953 }
1954 rc = X86EMUL_CONTINUE;
1955 c->dst.type = OP_NONE;
1956 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001958 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001959 if (!test_cc(c->b, ctxt->eflags))
1960 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001962 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1963 long int rel;
1964
1965 switch (c->op_bytes) {
1966 case 2:
1967 rel = insn_fetch(s16, 2, c->eip);
1968 break;
1969 case 4:
1970 rel = insn_fetch(s32, 4, c->eip);
1971 break;
1972 case 8:
1973 rel = insn_fetch(s64, 8, c->eip);
1974 break;
1975 default:
1976 DPRINTF("jnz: Invalid op_bytes\n");
1977 goto cannot_emulate;
1978 }
1979 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001980 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001981 c->dst.type = OP_NONE;
1982 break;
1983 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001984 case 0xa3:
1985 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001986 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001987 /* only subword offset */
1988 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001989 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001990 break;
1991 case 0xab:
1992 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001993 /* only subword offset */
1994 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001995 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001996 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03001997 case 0xae: /* clflush */
1998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999 case 0xb0 ... 0xb1: /* cmpxchg */
2000 /*
2001 * Save real source value, then compare EAX against
2002 * destination.
2003 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002004 c->src.orig_val = c->src.val;
2005 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002006 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2007 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002009 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 } else {
2011 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002012 c->dst.type = OP_REG;
2013 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 }
2015 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 case 0xb3:
2017 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002018 /* only subword offset */
2019 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002020 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002023 c->dst.bytes = c->op_bytes;
2024 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2025 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002028 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029 case 0:
2030 goto bt;
2031 case 1:
2032 goto bts;
2033 case 2:
2034 goto btr;
2035 case 3:
2036 goto btc;
2037 }
2038 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002039 case 0xbb:
2040 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002041 /* only subword offset */
2042 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002043 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002044 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002046 c->dst.bytes = c->op_bytes;
2047 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2048 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002050 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002051 c->dst.bytes = c->op_bytes;
2052 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2053 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002054 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002056 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002057 if (rc != 0)
2058 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002059 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 }
2062 goto writeback;
2063
2064cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002065 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002066 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 return -1;
2068}