blob: 4390ec8c47a641490ba428705d43f23fb0c2a3b2 [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. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020050#define DstAcc (4<<1) /* Destination Accumulator */
51#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080052/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcNone (0<<4) /* No source operand. */
54#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
55#define SrcReg (1<<4) /* Register operand. */
56#define SrcMem (2<<4) /* Memory operand. */
57#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
58#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
59#define SrcImm (5<<4) /* Immediate operand. */
60#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
61#define SrcMask (7<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080062/* Generic ModRM decode. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020063#define ModRM (1<<7)
Avi Kivity6aa8b732006-12-10 02:21:36 -080064/* Destination is only written; never read. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020065#define Mov (1<<8)
66#define BitOp (1<<9)
67#define MemAbs (1<<10) /* Memory operand is absolute displacement */
68#define String (1<<12) /* String instruction (rep capable) */
69#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020070#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
71#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
72#define GroupMask 0xff /* Group number stored in bits 0:7 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
Avi Kivity43bb19c2008-01-18 12:46:50 +020074enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020075 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020076 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020077};
78
Avi Kivityc7e75a32007-10-28 16:34:25 +020079static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080080 /* 0x00 - 0x07 */
81 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
82 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
83 0, 0, 0, 0,
84 /* 0x08 - 0x0F */
85 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
86 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
87 0, 0, 0, 0,
88 /* 0x10 - 0x17 */
89 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
90 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
91 0, 0, 0, 0,
92 /* 0x18 - 0x1F */
93 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
94 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
95 0, 0, 0, 0,
96 /* 0x20 - 0x27 */
97 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
98 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Nitin A Kamble19eb9382007-08-17 15:17:41 +030099 SrcImmByte, SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 /* 0x28 - 0x2F */
101 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
103 0, 0, 0, 0,
104 /* 0x30 - 0x37 */
105 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
106 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
107 0, 0, 0, 0,
108 /* 0x38 - 0x3F */
109 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
111 0, 0, 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700112 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200113 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700114 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200115 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300116 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200117 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
118 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300119 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200120 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
121 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700122 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700124 0, 0, 0, 0,
125 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300126 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300127 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
128 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300129 /* 0x70 - 0x77 */
130 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
131 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
132 /* 0x78 - 0x7F */
133 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
134 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200136 Group | Group1_80, Group | Group1_81,
137 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
139 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
140 /* 0x88 - 0x8F */
141 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
142 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200143 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200144 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300145 /* 0x90 - 0x97 */
146 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
147 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200148 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200150 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
151 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200152 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
153 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200155 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
156 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
157 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300158 /* 0xB0 - 0xB7 */
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 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
163 /* 0xB8 - 0xBF */
164 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
165 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
166 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
167 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300169 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200170 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300171 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 /* 0xC8 - 0xCF */
173 0, 0, 0, 0, 0, 0, 0, 0,
174 /* 0xD0 - 0xD7 */
175 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
176 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
177 0, 0, 0, 0,
178 /* 0xD8 - 0xDF */
179 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300180 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300181 0, 0, 0, 0,
182 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
183 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300184 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200185 ImplicitOps | Stack, SrcImm | ImplicitOps,
186 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300187 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
188 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0xF0 - 0xF7 */
190 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200191 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700193 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300194 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195};
196
Avi Kivity038e51d2007-01-22 20:40:40 -0800197static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200199 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200200 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 /* 0x10 - 0x1F */
202 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
203 /* 0x20 - 0x2F */
204 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0,
206 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300207 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 /* 0x40 - 0x47 */
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 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
213 /* 0x48 - 0x4F */
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 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
218 /* 0x50 - 0x5F */
219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220 /* 0x60 - 0x6F */
221 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222 /* 0x70 - 0x7F */
223 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300225 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
226 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
227 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
228 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 /* 0x90 - 0x9F */
230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
231 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800232 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 /* 0xA8 - 0xAF */
Glauber Costa2a7c5b82008-07-10 17:08:15 -0300234 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 /* 0xB0 - 0xB7 */
236 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800237 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
239 DstReg | SrcMem16 | ModRM | Mov,
240 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800241 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
243 DstReg | SrcMem16 | ModRM | Mov,
244 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800245 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
246 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247 /* 0xD0 - 0xDF */
248 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 /* 0xE0 - 0xEF */
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0xF0 - 0xFF */
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
253};
254
Avi Kivitye09d0822008-01-18 12:38:59 +0200255static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200256 [Group1_80*8] =
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 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
261 [Group1_81*8] =
262 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
263 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
264 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
265 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
266 [Group1_82*8] =
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 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
271 [Group1_83*8] =
272 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
273 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
274 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
275 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200276 [Group1A*8] =
277 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200278 [Group3_Byte*8] =
279 ByteOp | SrcImm | DstMem | ModRM, 0,
280 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
281 0, 0, 0, 0,
282 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400283 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300284 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200285 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200286 [Group4*8] =
287 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
288 0, 0, 0, 0, 0, 0,
289 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300290 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
291 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300292 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200293 [Group7*8] =
294 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300295 SrcNone | ModRM | DstMem | Mov, 0,
296 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200297};
298
299static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200300 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300301 SrcNone | ModRM, 0, 0, 0,
302 SrcNone | ModRM | DstMem | Mov, 0,
303 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200304};
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306/* EFLAGS bit definitions. */
307#define EFLG_OF (1<<11)
308#define EFLG_DF (1<<10)
309#define EFLG_SF (1<<7)
310#define EFLG_ZF (1<<6)
311#define EFLG_AF (1<<4)
312#define EFLG_PF (1<<2)
313#define EFLG_CF (1<<0)
314
315/*
316 * Instruction emulation:
317 * Most instructions are emulated directly via a fragment of inline assembly
318 * code. This allows us to save/restore EFLAGS and thus very easily pick up
319 * any modified flags.
320 */
321
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800322#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323#define _LO32 "k" /* force 32-bit operand */
324#define _STK "%%rsp" /* stack pointer */
325#elif defined(__i386__)
326#define _LO32 "" /* force 32-bit operand */
327#define _STK "%%esp" /* stack pointer */
328#endif
329
330/*
331 * These EFLAGS bits are restored from saved value during emulation, and
332 * any changes are written back to the saved value after emulation.
333 */
334#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
335
336/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200337#define _PRE_EFLAGS(_sav, _msk, _tmp) \
338 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
339 "movl %"_sav",%"_LO32 _tmp"; " \
340 "push %"_tmp"; " \
341 "push %"_tmp"; " \
342 "movl %"_msk",%"_LO32 _tmp"; " \
343 "andl %"_LO32 _tmp",("_STK"); " \
344 "pushf; " \
345 "notl %"_LO32 _tmp"; " \
346 "andl %"_LO32 _tmp",("_STK"); " \
347 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
348 "pop %"_tmp"; " \
349 "orl %"_LO32 _tmp",("_STK"); " \
350 "popf; " \
351 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352
353/* After executing instruction: write-back necessary bits in EFLAGS. */
354#define _POST_EFLAGS(_sav, _msk, _tmp) \
355 /* _sav |= EFLAGS & _msk; */ \
356 "pushf; " \
357 "pop %"_tmp"; " \
358 "andl %"_msk",%"_LO32 _tmp"; " \
359 "orl %"_LO32 _tmp",%"_sav"; "
360
361/* Raw emulation: instruction has two explicit operands. */
362#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
363 do { \
364 unsigned long _tmp; \
365 \
366 switch ((_dst).bytes) { \
367 case 2: \
368 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400369 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400371 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372 : "=m" (_eflags), "=m" ((_dst).val), \
373 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400374 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 break; \
376 case 4: \
377 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400378 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400380 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 : "=m" (_eflags), "=m" ((_dst).val), \
382 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400383 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 break; \
385 case 8: \
386 __emulate_2op_8byte(_op, _src, _dst, \
387 _eflags, _qx, _qy); \
388 break; \
389 } \
390 } while (0)
391
392#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
393 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800394 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400395 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 case 1: \
397 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400398 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400400 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800402 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400403 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 break; \
405 default: \
406 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
407 _wx, _wy, _lx, _ly, _qx, _qy); \
408 break; \
409 } \
410 } while (0)
411
412/* Source operand is byte-sized and may be restricted to just %cl. */
413#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
414 __emulate_2op(_op, _src, _dst, _eflags, \
415 "b", "c", "b", "c", "b", "c", "b", "c")
416
417/* Source operand is byte, word, long or quad sized. */
418#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
419 __emulate_2op(_op, _src, _dst, _eflags, \
420 "b", "q", "w", "r", _LO32, "r", "", "r")
421
422/* Source operand is word, long or quad sized. */
423#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
424 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
425 "w", "r", _LO32, "r", "", "r")
426
427/* Instruction has only one explicit operand (no source operand). */
428#define emulate_1op(_op, _dst, _eflags) \
429 do { \
430 unsigned long _tmp; \
431 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400432 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 case 1: \
434 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400435 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400437 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 : "=m" (_eflags), "=m" ((_dst).val), \
439 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400440 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 break; \
442 case 2: \
443 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400444 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400446 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 : "=m" (_eflags), "=m" ((_dst).val), \
448 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400449 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 break; \
451 case 4: \
452 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400453 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400455 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 : "=m" (_eflags), "=m" ((_dst).val), \
457 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400458 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 break; \
460 case 8: \
461 __emulate_1op_8byte(_op, _dst, _eflags); \
462 break; \
463 } \
464 } while (0)
465
466/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800467#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
469 do { \
470 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400471 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400473 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400475 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 } while (0)
477
478#define __emulate_1op_8byte(_op, _dst, _eflags) \
479 do { \
480 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400481 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400483 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400485 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 } while (0)
487
488#elif defined(__i386__)
489#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
490#define __emulate_1op_8byte(_op, _dst, _eflags)
491#endif /* __i386__ */
492
493/* Fetch next part of the instruction being emulated. */
494#define insn_fetch(_type, _size, _eip) \
495({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200496 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400497 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 goto done; \
499 (_eip) += (_size); \
500 (_type)_x; \
501})
502
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800503static inline unsigned long ad_mask(struct decode_cache *c)
504{
505 return (1UL << (c->ad_bytes << 3)) - 1;
506}
507
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800509static inline unsigned long
510address_mask(struct decode_cache *c, unsigned long reg)
511{
512 if (c->ad_bytes == sizeof(unsigned long))
513 return reg;
514 else
515 return reg & ad_mask(c);
516}
517
518static inline unsigned long
519register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
520{
521 return base + address_mask(c, reg);
522}
523
Harvey Harrison7a9572752008-02-19 07:40:41 -0800524static inline void
525register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
526{
527 if (c->ad_bytes == sizeof(unsigned long))
528 *reg += inc;
529 else
530 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
531}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532
Harvey Harrison7a9572752008-02-19 07:40:41 -0800533static inline void jmp_rel(struct decode_cache *c, int rel)
534{
535 register_address_increment(c, &c->eip, rel);
536}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300537
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300538static void set_seg_override(struct decode_cache *c, int seg)
539{
540 c->has_seg_override = true;
541 c->seg_override = seg;
542}
543
544static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
545{
546 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
547 return 0;
548
549 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
550}
551
552static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
553 struct decode_cache *c)
554{
555 if (!c->has_seg_override)
556 return 0;
557
558 return seg_base(ctxt, c->seg_override);
559}
560
561static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
562{
563 return seg_base(ctxt, VCPU_SREG_ES);
564}
565
566static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
567{
568 return seg_base(ctxt, VCPU_SREG_SS);
569}
570
Avi Kivity62266862007-11-20 13:15:52 +0200571static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
572 struct x86_emulate_ops *ops,
573 unsigned long linear, u8 *dest)
574{
575 struct fetch_cache *fc = &ctxt->decode.fetch;
576 int rc;
577 int size;
578
579 if (linear < fc->start || linear >= fc->end) {
580 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
581 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
582 if (rc)
583 return rc;
584 fc->start = linear;
585 fc->end = linear + size;
586 }
587 *dest = fc->data[linear - fc->start];
588 return 0;
589}
590
591static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
592 struct x86_emulate_ops *ops,
593 unsigned long eip, void *dest, unsigned size)
594{
595 int rc = 0;
596
597 eip += ctxt->cs_base;
598 while (size--) {
599 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
600 if (rc)
601 return rc;
602 }
603 return 0;
604}
605
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000606/*
607 * Given the 'reg' portion of a ModRM byte, and a register block, return a
608 * pointer into the block that addresses the relevant register.
609 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
610 */
611static void *decode_register(u8 modrm_reg, unsigned long *regs,
612 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613{
614 void *p;
615
616 p = &regs[modrm_reg];
617 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
618 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
619 return p;
620}
621
622static int read_descriptor(struct x86_emulate_ctxt *ctxt,
623 struct x86_emulate_ops *ops,
624 void *ptr,
625 u16 *size, unsigned long *address, int op_bytes)
626{
627 int rc;
628
629 if (op_bytes == 2)
630 op_bytes = 3;
631 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300632 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
633 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 if (rc)
635 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300636 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
637 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 return rc;
639}
640
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300641static int test_cc(unsigned int condition, unsigned int flags)
642{
643 int rc = 0;
644
645 switch ((condition & 15) >> 1) {
646 case 0: /* o */
647 rc |= (flags & EFLG_OF);
648 break;
649 case 1: /* b/c/nae */
650 rc |= (flags & EFLG_CF);
651 break;
652 case 2: /* z/e */
653 rc |= (flags & EFLG_ZF);
654 break;
655 case 3: /* be/na */
656 rc |= (flags & (EFLG_CF|EFLG_ZF));
657 break;
658 case 4: /* s */
659 rc |= (flags & EFLG_SF);
660 break;
661 case 5: /* p/pe */
662 rc |= (flags & EFLG_PF);
663 break;
664 case 7: /* le/ng */
665 rc |= (flags & EFLG_ZF);
666 /* fall through */
667 case 6: /* l/nge */
668 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
669 break;
670 }
671
672 /* Odd condition identifiers (lsb == 1) have inverted sense. */
673 return (!!rc ^ (condition & 1));
674}
675
Avi Kivity3c118e22007-10-31 10:27:04 +0200676static void decode_register_operand(struct operand *op,
677 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200678 int inhibit_bytereg)
679{
Avi Kivity33615aa2007-10-31 11:15:56 +0200680 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200681 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200682
683 if (!(c->d & ModRM))
684 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200685 op->type = OP_REG;
686 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200687 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200688 op->val = *(u8 *)op->ptr;
689 op->bytes = 1;
690 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200691 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200692 op->bytes = c->op_bytes;
693 switch (op->bytes) {
694 case 2:
695 op->val = *(u16 *)op->ptr;
696 break;
697 case 4:
698 op->val = *(u32 *)op->ptr;
699 break;
700 case 8:
701 op->val = *(u64 *) op->ptr;
702 break;
703 }
704 }
705 op->orig_val = op->val;
706}
707
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200708static int decode_modrm(struct x86_emulate_ctxt *ctxt,
709 struct x86_emulate_ops *ops)
710{
711 struct decode_cache *c = &ctxt->decode;
712 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700713 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 int rc = 0;
715
716 if (c->rex_prefix) {
717 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
718 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
719 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
720 }
721
722 c->modrm = insn_fetch(u8, 1, c->eip);
723 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
724 c->modrm_reg |= (c->modrm & 0x38) >> 3;
725 c->modrm_rm |= (c->modrm & 0x07);
726 c->modrm_ea = 0;
727 c->use_modrm_ea = 1;
728
729 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300730 c->modrm_ptr = decode_register(c->modrm_rm,
731 c->regs, c->d & ByteOp);
732 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733 return rc;
734 }
735
736 if (c->ad_bytes == 2) {
737 unsigned bx = c->regs[VCPU_REGS_RBX];
738 unsigned bp = c->regs[VCPU_REGS_RBP];
739 unsigned si = c->regs[VCPU_REGS_RSI];
740 unsigned di = c->regs[VCPU_REGS_RDI];
741
742 /* 16-bit ModR/M decode. */
743 switch (c->modrm_mod) {
744 case 0:
745 if (c->modrm_rm == 6)
746 c->modrm_ea += insn_fetch(u16, 2, c->eip);
747 break;
748 case 1:
749 c->modrm_ea += insn_fetch(s8, 1, c->eip);
750 break;
751 case 2:
752 c->modrm_ea += insn_fetch(u16, 2, c->eip);
753 break;
754 }
755 switch (c->modrm_rm) {
756 case 0:
757 c->modrm_ea += bx + si;
758 break;
759 case 1:
760 c->modrm_ea += bx + di;
761 break;
762 case 2:
763 c->modrm_ea += bp + si;
764 break;
765 case 3:
766 c->modrm_ea += bp + di;
767 break;
768 case 4:
769 c->modrm_ea += si;
770 break;
771 case 5:
772 c->modrm_ea += di;
773 break;
774 case 6:
775 if (c->modrm_mod != 0)
776 c->modrm_ea += bp;
777 break;
778 case 7:
779 c->modrm_ea += bx;
780 break;
781 }
782 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
783 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300784 if (!c->has_seg_override)
785 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786 c->modrm_ea = (u16)c->modrm_ea;
787 } else {
788 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700789 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790 sib = insn_fetch(u8, 1, c->eip);
791 index_reg |= (sib >> 3) & 7;
792 base_reg |= sib & 7;
793 scale = sib >> 6;
794
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700795 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
796 c->modrm_ea += insn_fetch(s32, 4, c->eip);
797 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200798 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700799 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200800 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700801 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
802 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700803 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700804 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200805 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200806 switch (c->modrm_mod) {
807 case 0:
808 if (c->modrm_rm == 5)
809 c->modrm_ea += insn_fetch(s32, 4, c->eip);
810 break;
811 case 1:
812 c->modrm_ea += insn_fetch(s8, 1, c->eip);
813 break;
814 case 2:
815 c->modrm_ea += insn_fetch(s32, 4, c->eip);
816 break;
817 }
818 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200819done:
820 return rc;
821}
822
823static int decode_abs(struct x86_emulate_ctxt *ctxt,
824 struct x86_emulate_ops *ops)
825{
826 struct decode_cache *c = &ctxt->decode;
827 int rc = 0;
828
829 switch (c->ad_bytes) {
830 case 2:
831 c->modrm_ea = insn_fetch(u16, 2, c->eip);
832 break;
833 case 4:
834 c->modrm_ea = insn_fetch(u32, 4, c->eip);
835 break;
836 case 8:
837 c->modrm_ea = insn_fetch(u64, 8, c->eip);
838 break;
839 }
840done:
841 return rc;
842}
843
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200845x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200847 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200850 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851
852 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853
Laurent Viviere4e03de2007-09-18 11:52:50 +0200854 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300855 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300856 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800857 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858
859 switch (mode) {
860 case X86EMUL_MODE_REAL:
861 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200862 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 break;
864 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200865 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800867#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200869 def_op_bytes = 4;
870 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 break;
872#endif
873 default:
874 return -1;
875 }
876
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200877 c->op_bytes = def_op_bytes;
878 c->ad_bytes = def_ad_bytes;
879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200881 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200882 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200884 /* switch between 2/4 bytes */
885 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 break;
887 case 0x67: /* address-size override */
888 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200889 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200890 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200893 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300896 case 0x2e: /* CS override */
897 case 0x36: /* SS override */
898 case 0x3e: /* DS override */
899 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 break;
901 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300903 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200905 case 0x40 ... 0x4f: /* REX */
906 if (mode != X86EMUL_MODE_PROT64)
907 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200908 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200909 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200911 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200913 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100914 c->rep_prefix = REPNE_PREFIX;
915 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100917 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 default:
920 goto done_prefixes;
921 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200922
923 /* Any legacy prefix after a REX prefix nullifies its effect. */
924
Avi Kivity33615aa2007-10-31 11:15:56 +0200925 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 }
927
928done_prefixes:
929
930 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200931 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200932 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200933 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200936 c->d = opcode_table[c->b];
937 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200939 if (c->b == 0x0f) {
940 c->twobyte = 1;
941 c->b = insn_fetch(u8, 1, c->eip);
942 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200944 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Avi Kivitye09d0822008-01-18 12:38:59 +0200946 if (c->d & Group) {
947 group = c->d & GroupMask;
948 c->modrm = insn_fetch(u8, 1, c->eip);
949 --c->eip;
950
951 group = (group << 3) + ((c->modrm >> 3) & 7);
952 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
953 c->d = group2_table[group];
954 else
955 c->d = group_table[group];
956 }
957
958 /* Unrecognised? */
959 if (c->d == 0) {
960 DPRINTF("Cannot emulate %02x\n", c->b);
961 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 }
963
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200964 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
965 c->op_bytes = 8;
966
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200968 if (c->d & ModRM)
969 rc = decode_modrm(ctxt, ops);
970 else if (c->d & MemAbs)
971 rc = decode_abs(ctxt, ops);
972 if (rc)
973 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300975 if (!c->has_seg_override)
976 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200977
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300978 if (!(!c->twobyte && c->b == 0x8d))
979 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200980
981 if (c->ad_bytes != 8)
982 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 /*
984 * Decode and fetch the source operand: register, memory
985 * or immediate.
986 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200987 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 case SrcNone:
989 break;
990 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200991 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 break;
993 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200994 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 goto srcmem_common;
996 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200997 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 goto srcmem_common;
999 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001000 c->src.bytes = (c->d & ByteOp) ? 1 :
1001 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001002 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001003 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001004 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001005 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001006 /*
1007 * For instructions with a ModR/M byte, switch to register
1008 * access if Mod = 3.
1009 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 if ((c->d & ModRM) && c->modrm_mod == 3) {
1011 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001012 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001013 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001014 break;
1015 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 break;
1018 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001019 c->src.type = OP_IMM;
1020 c->src.ptr = (unsigned long *)c->eip;
1021 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1022 if (c->src.bytes == 8)
1023 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001025 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001027 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 break;
1029 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001030 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 break;
1032 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001033 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 break;
1035 }
1036 break;
1037 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001038 c->src.type = OP_IMM;
1039 c->src.ptr = (unsigned long *)c->eip;
1040 c->src.bytes = 1;
1041 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 break;
1043 }
1044
Avi Kivity038e51d2007-01-22 20:40:40 -08001045 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001047 case ImplicitOps:
1048 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001049 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001050 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001051 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001052 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001053 break;
1054 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001055 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001056 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001057 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001058 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001059 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001060 break;
1061 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001062 c->dst.type = OP_MEM;
1063 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001064 case DstAcc:
1065 c->dst.type = OP_REG;
1066 c->dst.bytes = c->op_bytes;
1067 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1068 switch (c->op_bytes) {
1069 case 1:
1070 c->dst.val = *(u8 *)c->dst.ptr;
1071 break;
1072 case 2:
1073 c->dst.val = *(u16 *)c->dst.ptr;
1074 break;
1075 case 4:
1076 c->dst.val = *(u32 *)c->dst.ptr;
1077 break;
1078 }
1079 c->dst.orig_val = c->dst.val;
1080 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001081 }
1082
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001083 if (c->rip_relative)
1084 c->modrm_ea += c->eip;
1085
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001086done:
1087 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1088}
1089
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001090static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1091{
1092 struct decode_cache *c = &ctxt->decode;
1093
1094 c->dst.type = OP_MEM;
1095 c->dst.bytes = c->op_bytes;
1096 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001097 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001098 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001099 c->regs[VCPU_REGS_RSP]);
1100}
1101
1102static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1103 struct x86_emulate_ops *ops)
1104{
1105 struct decode_cache *c = &ctxt->decode;
1106 int rc;
1107
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001108 rc = ops->read_std(register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109 c->regs[VCPU_REGS_RSP]),
1110 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1111 if (rc != 0)
1112 return rc;
1113
Harvey Harrison7a9572752008-02-19 07:40:41 -08001114 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115
1116 return 0;
1117}
1118
Laurent Vivier05f086f2007-09-24 11:10:55 +02001119static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001120{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001121 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001122 switch (c->modrm_reg) {
1123 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001124 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001125 break;
1126 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001127 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001128 break;
1129 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001130 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001131 break;
1132 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001133 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134 break;
1135 case 4: /* sal/shl */
1136 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001137 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001138 break;
1139 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001140 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001141 break;
1142 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001143 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001144 break;
1145 }
1146}
1147
1148static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001149 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150{
1151 struct decode_cache *c = &ctxt->decode;
1152 int rc = 0;
1153
1154 switch (c->modrm_reg) {
1155 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001156 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001157 break;
1158 case 2: /* not */
1159 c->dst.val = ~c->dst.val;
1160 break;
1161 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001162 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001163 break;
1164 default:
1165 DPRINTF("Cannot emulate %02x\n", c->b);
1166 rc = X86EMUL_UNHANDLEABLE;
1167 break;
1168 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 return rc;
1170}
1171
1172static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001173 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001174{
1175 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001176
1177 switch (c->modrm_reg) {
1178 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001179 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001180 break;
1181 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001182 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001183 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001184 case 2: /* call near abs */ {
1185 long int old_eip;
1186 old_eip = c->eip;
1187 c->eip = c->src.val;
1188 c->src.val = old_eip;
1189 emulate_push(ctxt);
1190 break;
1191 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001192 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001193 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194 break;
1195 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001196 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198 }
1199 return 0;
1200}
1201
1202static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1203 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001204 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001205{
1206 struct decode_cache *c = &ctxt->decode;
1207 u64 old, new;
1208 int rc;
1209
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001210 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001211 if (rc != 0)
1212 return rc;
1213
1214 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1215 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1216
1217 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1218 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001219 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001220
1221 } else {
1222 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1223 (u32) c->regs[VCPU_REGS_RBX];
1224
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001225 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001226 if (rc != 0)
1227 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001228 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001229 }
1230 return 0;
1231}
1232
1233static inline int writeback(struct x86_emulate_ctxt *ctxt,
1234 struct x86_emulate_ops *ops)
1235{
1236 int rc;
1237 struct decode_cache *c = &ctxt->decode;
1238
1239 switch (c->dst.type) {
1240 case OP_REG:
1241 /* The 4-byte case *is* correct:
1242 * in 64-bit mode we zero-extend.
1243 */
1244 switch (c->dst.bytes) {
1245 case 1:
1246 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1247 break;
1248 case 2:
1249 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1250 break;
1251 case 4:
1252 *c->dst.ptr = (u32)c->dst.val;
1253 break; /* 64b: zero-ext */
1254 case 8:
1255 *c->dst.ptr = c->dst.val;
1256 break;
1257 }
1258 break;
1259 case OP_MEM:
1260 if (c->lock_prefix)
1261 rc = ops->cmpxchg_emulated(
1262 (unsigned long)c->dst.ptr,
1263 &c->dst.orig_val,
1264 &c->dst.val,
1265 c->dst.bytes,
1266 ctxt->vcpu);
1267 else
1268 rc = ops->write_emulated(
1269 (unsigned long)c->dst.ptr,
1270 &c->dst.val,
1271 c->dst.bytes,
1272 ctxt->vcpu);
1273 if (rc != 0)
1274 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001275 break;
1276 case OP_NONE:
1277 /* no writeback */
1278 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001279 default:
1280 break;
1281 }
1282 return 0;
1283}
1284
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001285int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001286x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001287{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001288 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001289 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001290 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001291 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001292 unsigned int port;
1293 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001294 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001295
Laurent Vivier34273182007-09-18 11:27:37 +02001296 /* Shadow copy of register state. Committed on successful emulation.
1297 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1298 * modify them.
1299 */
1300
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001302 saved_eip = c->eip;
1303
Avi Kivityc7e75a32007-10-28 16:34:25 +02001304 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001305 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001306
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001307 if (c->rep_prefix && (c->d & String)) {
1308 /* All REP prefixes have the same first termination condition */
1309 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001310 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001311 goto done;
1312 }
1313 /* The second termination condition only applies for REPE
1314 * and REPNE. Test if the repeat string operation prefix is
1315 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1316 * corresponding termination condition according to:
1317 * - if REPE/REPZ and ZF = 0 then done
1318 * - if REPNE/REPNZ and ZF = 1 then done
1319 */
1320 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1321 (c->b == 0xae) || (c->b == 0xaf)) {
1322 if ((c->rep_prefix == REPE_PREFIX) &&
1323 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001324 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001325 goto done;
1326 }
1327 if ((c->rep_prefix == REPNE_PREFIX) &&
1328 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001329 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001330 goto done;
1331 }
1332 }
1333 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001334 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001335 }
1336
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001337 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001338 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001339 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001340 rc = ops->read_emulated((unsigned long)c->src.ptr,
1341 &c->src.val,
1342 c->src.bytes,
1343 ctxt->vcpu);
1344 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001345 goto done;
1346 c->src.orig_val = c->src.val;
1347 }
1348
1349 if ((c->d & DstMask) == ImplicitOps)
1350 goto special_insn;
1351
1352
1353 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001354 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001355 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1356 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001357 if (c->d & BitOp) {
1358 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001359
Laurent Viviere4e03de2007-09-18 11:52:50 +02001360 c->dst.ptr = (void *)c->dst.ptr +
1361 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001362 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001363 if (!(c->d & Mov) &&
1364 /* optimisation - avoid slow emulated read */
1365 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1366 &c->dst.val,
1367 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001368 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001369 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001370 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001371
Avi Kivity018a98d2007-11-27 19:30:56 +02001372special_insn:
1373
Laurent Viviere4e03de2007-09-18 11:52:50 +02001374 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 goto twobyte_insn;
1376
Laurent Viviere4e03de2007-09-18 11:52:50 +02001377 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 case 0x00 ... 0x05:
1379 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001380 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 break;
1382 case 0x08 ... 0x0d:
1383 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001384 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 break;
1386 case 0x10 ... 0x15:
1387 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 break;
1390 case 0x18 ... 0x1d:
1391 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001392 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001394 case 0x20 ... 0x23:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001396 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 break;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001398 case 0x24: /* and al imm8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001399 c->dst.type = OP_REG;
1400 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1401 c->dst.val = *(u8 *)c->dst.ptr;
1402 c->dst.bytes = 1;
1403 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001404 goto and;
1405 case 0x25: /* and ax imm16, or eax imm32 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001406 c->dst.type = OP_REG;
1407 c->dst.bytes = c->op_bytes;
1408 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1409 if (c->op_bytes == 2)
1410 c->dst.val = *(u16 *)c->dst.ptr;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001411 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001412 c->dst.val = *(u32 *)c->dst.ptr;
1413 c->dst.orig_val = c->dst.val;
Nitin A Kamble19eb9382007-08-17 15:17:41 +03001414 goto and;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 case 0x28 ... 0x2d:
1416 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001417 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 break;
1419 case 0x30 ... 0x35:
1420 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001421 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 break;
1423 case 0x38 ... 0x3d:
1424 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001425 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001427 case 0x40 ... 0x47: /* inc r16/r32 */
1428 emulate_1op("inc", c->dst, ctxt->eflags);
1429 break;
1430 case 0x48 ... 0x4f: /* dec r16/r32 */
1431 emulate_1op("dec", c->dst, ctxt->eflags);
1432 break;
1433 case 0x50 ... 0x57: /* push reg */
1434 c->dst.type = OP_MEM;
1435 c->dst.bytes = c->op_bytes;
1436 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001437 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001438 -c->op_bytes);
1439 c->dst.ptr = (void *) register_address(
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001440 c, ss_base(ctxt), c->regs[VCPU_REGS_RSP]);
Avi Kivity33615aa2007-10-31 11:15:56 +02001441 break;
1442 case 0x58 ... 0x5f: /* pop reg */
1443 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001444 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001445 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1446 c->op_bytes, ctxt->vcpu)) != 0)
1447 goto done;
1448
Harvey Harrison7a9572752008-02-19 07:40:41 -08001449 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001450 c->op_bytes);
1451 c->dst.type = OP_NONE; /* Disable writeback. */
1452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001454 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001456 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001458 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001459 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001460 emulate_push(ctxt);
1461 break;
1462 case 0x6c: /* insb */
1463 case 0x6d: /* insw/insd */
1464 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1465 1,
1466 (c->d & ByteOp) ? 1 : c->op_bytes,
1467 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001468 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001469 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001470 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001471 c->regs[VCPU_REGS_RDI]),
1472 c->rep_prefix,
1473 c->regs[VCPU_REGS_RDX]) == 0) {
1474 c->eip = saved_eip;
1475 return -1;
1476 }
1477 return 0;
1478 case 0x6e: /* outsb */
1479 case 0x6f: /* outsw/outsd */
1480 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1481 0,
1482 (c->d & ByteOp) ? 1 : c->op_bytes,
1483 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001484 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001485 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001486 register_address(c,
1487 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001488 c->regs[VCPU_REGS_RSI]),
1489 c->rep_prefix,
1490 c->regs[VCPU_REGS_RDX]) == 0) {
1491 c->eip = saved_eip;
1492 return -1;
1493 }
1494 return 0;
1495 case 0x70 ... 0x7f: /* jcc (short) */ {
1496 int rel = insn_fetch(s8, 1, c->eip);
1497
1498 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001499 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001500 break;
1501 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001503 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 case 0:
1505 goto add;
1506 case 1:
1507 goto or;
1508 case 2:
1509 goto adc;
1510 case 3:
1511 goto sbb;
1512 case 4:
1513 goto and;
1514 case 5:
1515 goto sub;
1516 case 6:
1517 goto xor;
1518 case 7:
1519 goto cmp;
1520 }
1521 break;
1522 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001523 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 break;
1525 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001526 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001528 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001530 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 break;
1532 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001533 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534 break;
1535 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001536 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 break; /* 64b reg: zero-extend */
1538 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001539 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 break;
1541 }
1542 /*
1543 * Write back the memory destination with implicit LOCK
1544 * prefix.
1545 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001546 c->dst.val = c->src.val;
1547 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001550 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001551 case 0x8c: { /* mov r/m, sreg */
1552 struct kvm_segment segreg;
1553
1554 if (c->modrm_reg <= 5)
1555 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1556 else {
1557 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1558 c->modrm);
1559 goto cannot_emulate;
1560 }
1561 c->dst.val = segreg.selector;
1562 break;
1563 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001564 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001565 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001566 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001567 case 0x8e: { /* mov seg, r/m16 */
1568 uint16_t sel;
1569 int type_bits;
1570 int err;
1571
1572 sel = c->src.val;
1573 if (c->modrm_reg <= 5) {
1574 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1575 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1576 type_bits, c->modrm_reg);
1577 } else {
1578 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1579 c->modrm);
1580 goto cannot_emulate;
1581 }
1582
1583 if (err < 0)
1584 goto cannot_emulate;
1585
1586 c->dst.type = OP_NONE; /* Disable writeback. */
1587 break;
1588 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001590 rc = emulate_grp1a(ctxt, ops);
1591 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001594 case 0x90: /* nop / xchg r8,rax */
1595 if (!(c->rex_prefix & 1)) { /* nop */
1596 c->dst.type = OP_NONE;
1597 break;
1598 }
1599 case 0x91 ... 0x97: /* xchg reg,rax */
1600 c->src.type = c->dst.type = OP_REG;
1601 c->src.bytes = c->dst.bytes = c->op_bytes;
1602 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1603 c->src.val = *(c->src.ptr);
1604 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001605 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001606 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001607 emulate_push(ctxt);
1608 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001609 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001610 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001611 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001612 case 0xa0 ... 0xa1: /* mov */
1613 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1614 c->dst.val = c->src.val;
1615 break;
1616 case 0xa2 ... 0xa3: /* mov */
1617 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1618 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001620 c->dst.type = OP_MEM;
1621 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001622 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001623 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001624 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001625 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001626 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001627 c->regs[VCPU_REGS_RSI]),
1628 &c->dst.val,
1629 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001631 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001632 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001633 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001634 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001635 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001636 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 break;
1638 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001639 c->src.type = OP_NONE; /* Disable writeback. */
1640 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001641 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001642 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001643 c->regs[VCPU_REGS_RSI]);
1644 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1645 &c->src.val,
1646 c->src.bytes,
1647 ctxt->vcpu)) != 0)
1648 goto done;
1649
1650 c->dst.type = OP_NONE; /* Disable writeback. */
1651 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001652 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001653 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001654 c->regs[VCPU_REGS_RDI]);
1655 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1656 &c->dst.val,
1657 c->dst.bytes,
1658 ctxt->vcpu)) != 0)
1659 goto done;
1660
1661 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1662
1663 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1664
Harvey Harrison7a9572752008-02-19 07:40:41 -08001665 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001666 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1667 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001668 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001669 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1670 : c->dst.bytes);
1671
1672 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001674 c->dst.type = OP_MEM;
1675 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001676 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001677 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001678 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001679 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001680 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001681 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 break;
1684 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 c->dst.type = OP_REG;
1686 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1687 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001688 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001689 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001690 c->regs[VCPU_REGS_RSI]),
1691 &c->dst.val,
1692 c->dst.bytes,
1693 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001695 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001696 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001697 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 break;
1699 case 0xae ... 0xaf: /* scas */
1700 DPRINTF("Urk! I don't handle SCAS.\n");
1701 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001702 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001703 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001704 case 0xc0 ... 0xc1:
1705 emulate_grp2(ctxt);
1706 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001707 case 0xc3: /* ret */
1708 c->dst.ptr = &c->eip;
1709 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001710 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1711 mov:
1712 c->dst.val = c->src.val;
1713 break;
1714 case 0xd0 ... 0xd1: /* Grp2 */
1715 c->src.val = 1;
1716 emulate_grp2(ctxt);
1717 break;
1718 case 0xd2 ... 0xd3: /* Grp2 */
1719 c->src.val = c->regs[VCPU_REGS_RCX];
1720 emulate_grp2(ctxt);
1721 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001722 case 0xe4: /* inb */
1723 case 0xe5: /* in */
1724 port = insn_fetch(u8, 1, c->eip);
1725 io_dir_in = 1;
1726 goto do_io;
1727 case 0xe6: /* outb */
1728 case 0xe7: /* out */
1729 port = insn_fetch(u8, 1, c->eip);
1730 io_dir_in = 0;
1731 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001732 case 0xe8: /* call (near) */ {
1733 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001734 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001735 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001736 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001737 break;
1738 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001739 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001740 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001741 default:
1742 DPRINTF("Call: Invalid op_bytes\n");
1743 goto cannot_emulate;
1744 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001745 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001746 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001747 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001748 emulate_push(ctxt);
1749 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001750 }
1751 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001752 goto jmp;
1753 case 0xea: /* jmp far */ {
1754 uint32_t eip;
1755 uint16_t sel;
1756
1757 switch (c->op_bytes) {
1758 case 2:
1759 eip = insn_fetch(u16, 2, c->eip);
1760 break;
1761 case 4:
1762 eip = insn_fetch(u32, 4, c->eip);
1763 break;
1764 default:
1765 DPRINTF("jmp far: Invalid op_bytes\n");
1766 goto cannot_emulate;
1767 }
1768 sel = insn_fetch(u16, 2, c->eip);
1769 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1770 DPRINTF("jmp far: Failed to load CS descriptor\n");
1771 goto cannot_emulate;
1772 }
1773
1774 c->eip = eip;
1775 break;
1776 }
1777 case 0xeb:
1778 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001779 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001780 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001781 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001782 case 0xec: /* in al,dx */
1783 case 0xed: /* in (e/r)ax,dx */
1784 port = c->regs[VCPU_REGS_RDX];
1785 io_dir_in = 1;
1786 goto do_io;
1787 case 0xee: /* out al,dx */
1788 case 0xef: /* out (e/r)ax,dx */
1789 port = c->regs[VCPU_REGS_RDX];
1790 io_dir_in = 0;
1791 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1792 (c->d & ByteOp) ? 1 : c->op_bytes,
1793 port) != 0) {
1794 c->eip = saved_eip;
1795 goto cannot_emulate;
1796 }
1797 return 0;
Avi Kivity111de5d2007-11-27 19:14:21 +02001798 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001799 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001800 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001801 case 0xf5: /* cmc */
1802 /* complement carry flag from eflags reg */
1803 ctxt->eflags ^= EFLG_CF;
1804 c->dst.type = OP_NONE; /* Disable writeback. */
1805 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001806 case 0xf6 ... 0xf7: /* Grp3 */
1807 rc = emulate_grp3(ctxt, ops);
1808 if (rc != 0)
1809 goto done;
1810 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001811 case 0xf8: /* clc */
1812 ctxt->eflags &= ~EFLG_CF;
1813 c->dst.type = OP_NONE; /* Disable writeback. */
1814 break;
1815 case 0xfa: /* cli */
1816 ctxt->eflags &= ~X86_EFLAGS_IF;
1817 c->dst.type = OP_NONE; /* Disable writeback. */
1818 break;
1819 case 0xfb: /* sti */
1820 ctxt->eflags |= X86_EFLAGS_IF;
1821 c->dst.type = OP_NONE; /* Disable writeback. */
1822 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001823 case 0xfc: /* cld */
1824 ctxt->eflags &= ~EFLG_DF;
1825 c->dst.type = OP_NONE; /* Disable writeback. */
1826 break;
1827 case 0xfd: /* std */
1828 ctxt->eflags |= EFLG_DF;
1829 c->dst.type = OP_NONE; /* Disable writeback. */
1830 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001831 case 0xfe ... 0xff: /* Grp4/Grp5 */
1832 rc = emulate_grp45(ctxt, ops);
1833 if (rc != 0)
1834 goto done;
1835 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001837
1838writeback:
1839 rc = writeback(ctxt, ops);
1840 if (rc != 0)
1841 goto done;
1842
1843 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001844 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001845 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001846
1847done:
1848 if (rc == X86EMUL_UNHANDLEABLE) {
1849 c->eip = saved_eip;
1850 return -1;
1851 }
1852 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853
1854twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001855 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001857 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 u16 size;
1859 unsigned long address;
1860
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001861 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001862 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001863 goto cannot_emulate;
1864
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001865 rc = kvm_fix_hypercall(ctxt->vcpu);
1866 if (rc)
1867 goto done;
1868
Avi Kivity33e38852008-05-21 15:34:25 +03001869 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001870 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001871 /* Disable writeback. */
1872 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001873 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001875 rc = read_descriptor(ctxt, ops, c->src.ptr,
1876 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 if (rc)
1878 goto done;
1879 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001880 /* Disable writeback. */
1881 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001883 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001884 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001885 rc = kvm_fix_hypercall(ctxt->vcpu);
1886 if (rc)
1887 goto done;
1888 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001889 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001890 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001891 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001892 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001893 if (rc)
1894 goto done;
1895 realmode_lidt(ctxt->vcpu, size, address);
1896 }
Avi Kivity16286d02008-04-14 14:40:50 +03001897 /* Disable writeback. */
1898 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 break;
1900 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001901 c->dst.bytes = 2;
1902 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 break;
1904 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001905 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1906 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001907 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 break;
1909 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001910 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001911 /* Disable writeback. */
1912 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 break;
1914 default:
1915 goto cannot_emulate;
1916 }
1917 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001918 case 0x06:
1919 emulate_clts(ctxt->vcpu);
1920 c->dst.type = OP_NONE;
1921 break;
1922 case 0x08: /* invd */
1923 case 0x09: /* wbinvd */
1924 case 0x0d: /* GrpP (prefetch) */
1925 case 0x18: /* Grp16 (prefetch/nop) */
1926 c->dst.type = OP_NONE;
1927 break;
1928 case 0x20: /* mov cr, reg */
1929 if (c->modrm_mod != 3)
1930 goto cannot_emulate;
1931 c->regs[c->modrm_rm] =
1932 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1933 c->dst.type = OP_NONE; /* no writeback */
1934 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001936 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001938 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001939 if (rc)
1940 goto cannot_emulate;
1941 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001943 case 0x22: /* mov reg, cr */
1944 if (c->modrm_mod != 3)
1945 goto cannot_emulate;
1946 realmode_set_cr(ctxt->vcpu,
1947 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1948 c->dst.type = OP_NONE;
1949 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001951 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001953 rc = emulator_set_dr(ctxt, c->modrm_reg,
1954 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001955 if (rc)
1956 goto cannot_emulate;
1957 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001959 case 0x30:
1960 /* wrmsr */
1961 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1962 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1963 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1964 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001965 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001966 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001967 }
1968 rc = X86EMUL_CONTINUE;
1969 c->dst.type = OP_NONE;
1970 break;
1971 case 0x32:
1972 /* rdmsr */
1973 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1974 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001975 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001976 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001977 } else {
1978 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1979 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1980 }
1981 rc = X86EMUL_CONTINUE;
1982 c->dst.type = OP_NONE;
1983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001985 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001986 if (!test_cc(c->b, ctxt->eflags))
1987 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001989 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1990 long int rel;
1991
1992 switch (c->op_bytes) {
1993 case 2:
1994 rel = insn_fetch(s16, 2, c->eip);
1995 break;
1996 case 4:
1997 rel = insn_fetch(s32, 4, c->eip);
1998 break;
1999 case 8:
2000 rel = insn_fetch(s64, 8, c->eip);
2001 break;
2002 default:
2003 DPRINTF("jnz: Invalid op_bytes\n");
2004 goto cannot_emulate;
2005 }
2006 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08002007 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02002008 c->dst.type = OP_NONE;
2009 break;
2010 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03002011 case 0xa3:
2012 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002013 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002014 /* only subword offset */
2015 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002016 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002017 break;
2018 case 0xab:
2019 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002020 /* only subword offset */
2021 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002022 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002023 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002024 case 0xae: /* clflush */
2025 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 case 0xb0 ... 0xb1: /* cmpxchg */
2027 /*
2028 * Save real source value, then compare EAX against
2029 * destination.
2030 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002031 c->src.orig_val = c->src.val;
2032 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002033 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2034 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002036 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 } else {
2038 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002039 c->dst.type = OP_REG;
2040 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 }
2042 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043 case 0xb3:
2044 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002045 /* only subword offset */
2046 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002047 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002050 c->dst.bytes = c->op_bytes;
2051 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2052 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002055 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 case 0:
2057 goto bt;
2058 case 1:
2059 goto bts;
2060 case 2:
2061 goto btr;
2062 case 3:
2063 goto btc;
2064 }
2065 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002066 case 0xbb:
2067 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002068 /* only subword offset */
2069 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002070 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002073 c->dst.bytes = c->op_bytes;
2074 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2075 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002077 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002078 c->dst.bytes = c->op_bytes;
2079 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2080 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002081 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002083 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002084 if (rc != 0)
2085 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002086 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002087 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 }
2089 goto writeback;
2090
2091cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002092 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002093 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 return -1;
2095}