blob: 8f60ace1387493f63e60d2d945820d2e18e62111 [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,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020083 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080084 /* 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,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +020099 DstAcc | SrcImmByte, DstAcc | 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,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200111 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
112 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700113 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200114 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700115 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200116 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300117 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200118 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
119 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300120 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200121 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
122 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700123 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700125 0, 0, 0, 0,
126 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300127 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300128 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
129 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300130 /* 0x70 - 0x77 */
131 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
132 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
133 /* 0x78 - 0x7F */
134 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
135 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200137 Group | Group1_80, Group | Group1_81,
138 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
140 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
141 /* 0x88 - 0x8F */
142 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
143 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200144 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200145 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300146 /* 0x90 - 0x97 */
147 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
148 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200149 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200151 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
152 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200153 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
154 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200156 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
157 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
158 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300159 /* 0xB0 - 0xB7 */
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 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
164 /* 0xB8 - 0xBF */
165 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
166 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
167 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
168 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300170 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200171 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300172 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0xC8 - 0xCF */
174 0, 0, 0, 0, 0, 0, 0, 0,
175 /* 0xD0 - 0xD7 */
176 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
177 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
178 0, 0, 0, 0,
179 /* 0xD8 - 0xDF */
180 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300181 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300182 0, 0, 0, 0,
183 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
184 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300185 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200186 ImplicitOps | Stack, SrcImm | ImplicitOps,
187 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300188 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
189 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190 /* 0xF0 - 0xF7 */
191 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200192 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700194 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300195 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196};
197
Avi Kivity038e51d2007-01-22 20:40:40 -0800198static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200200 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200201 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0x10 - 0x1F */
203 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
204 /* 0x20 - 0x2F */
205 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
206 0, 0, 0, 0, 0, 0, 0, 0,
207 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300208 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0x40 - 0x47 */
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 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
214 /* 0x48 - 0x4F */
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 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
219 /* 0x50 - 0x5F */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0x60 - 0x6F */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 /* 0x70 - 0x7F */
224 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300226 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
227 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
228 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
229 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 /* 0x90 - 0x9F */
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800233 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0xA8 - 0xAF */
Glauber Costa2a7c5b82008-07-10 17:08:15 -0300235 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 /* 0xB0 - 0xB7 */
237 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800238 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
240 DstReg | SrcMem16 | ModRM | Mov,
241 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800242 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
244 DstReg | SrcMem16 | ModRM | Mov,
245 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800246 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
247 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* 0xD0 - 0xDF */
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /* 0xE0 - 0xEF */
251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 /* 0xF0 - 0xFF */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
254};
255
Avi Kivitye09d0822008-01-18 12:38:59 +0200256static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200257 [Group1_80*8] =
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 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
262 [Group1_81*8] =
263 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
264 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
265 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
266 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
267 [Group1_82*8] =
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 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
272 [Group1_83*8] =
273 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
274 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
275 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
276 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200277 [Group1A*8] =
278 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200279 [Group3_Byte*8] =
280 ByteOp | SrcImm | DstMem | ModRM, 0,
281 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
282 0, 0, 0, 0,
283 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400284 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300285 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200286 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200287 [Group4*8] =
288 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
289 0, 0, 0, 0, 0, 0,
290 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300291 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
292 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300293 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200294 [Group7*8] =
295 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300296 SrcNone | ModRM | DstMem | Mov, 0,
297 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200298};
299
300static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200301 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300302 SrcNone | ModRM, 0, 0, 0,
303 SrcNone | ModRM | DstMem | Mov, 0,
304 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200305};
306
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307/* EFLAGS bit definitions. */
308#define EFLG_OF (1<<11)
309#define EFLG_DF (1<<10)
310#define EFLG_SF (1<<7)
311#define EFLG_ZF (1<<6)
312#define EFLG_AF (1<<4)
313#define EFLG_PF (1<<2)
314#define EFLG_CF (1<<0)
315
316/*
317 * Instruction emulation:
318 * Most instructions are emulated directly via a fragment of inline assembly
319 * code. This allows us to save/restore EFLAGS and thus very easily pick up
320 * any modified flags.
321 */
322
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800323#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324#define _LO32 "k" /* force 32-bit operand */
325#define _STK "%%rsp" /* stack pointer */
326#elif defined(__i386__)
327#define _LO32 "" /* force 32-bit operand */
328#define _STK "%%esp" /* stack pointer */
329#endif
330
331/*
332 * These EFLAGS bits are restored from saved value during emulation, and
333 * any changes are written back to the saved value after emulation.
334 */
335#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
336
337/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200338#define _PRE_EFLAGS(_sav, _msk, _tmp) \
339 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
340 "movl %"_sav",%"_LO32 _tmp"; " \
341 "push %"_tmp"; " \
342 "push %"_tmp"; " \
343 "movl %"_msk",%"_LO32 _tmp"; " \
344 "andl %"_LO32 _tmp",("_STK"); " \
345 "pushf; " \
346 "notl %"_LO32 _tmp"; " \
347 "andl %"_LO32 _tmp",("_STK"); " \
348 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
349 "pop %"_tmp"; " \
350 "orl %"_LO32 _tmp",("_STK"); " \
351 "popf; " \
352 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353
354/* After executing instruction: write-back necessary bits in EFLAGS. */
355#define _POST_EFLAGS(_sav, _msk, _tmp) \
356 /* _sav |= EFLAGS & _msk; */ \
357 "pushf; " \
358 "pop %"_tmp"; " \
359 "andl %"_msk",%"_LO32 _tmp"; " \
360 "orl %"_LO32 _tmp",%"_sav"; "
361
362/* Raw emulation: instruction has two explicit operands. */
363#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
364 do { \
365 unsigned long _tmp; \
366 \
367 switch ((_dst).bytes) { \
368 case 2: \
369 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400370 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400372 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 : "=m" (_eflags), "=m" ((_dst).val), \
374 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400375 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376 break; \
377 case 4: \
378 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400379 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 : "=m" (_eflags), "=m" ((_dst).val), \
383 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400384 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 break; \
386 case 8: \
387 __emulate_2op_8byte(_op, _src, _dst, \
388 _eflags, _qx, _qy); \
389 break; \
390 } \
391 } while (0)
392
393#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
394 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800395 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400396 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 case 1: \
398 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400399 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400401 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800403 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400404 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 break; \
406 default: \
407 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
408 _wx, _wy, _lx, _ly, _qx, _qy); \
409 break; \
410 } \
411 } while (0)
412
413/* Source operand is byte-sized and may be restricted to just %cl. */
414#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
415 __emulate_2op(_op, _src, _dst, _eflags, \
416 "b", "c", "b", "c", "b", "c", "b", "c")
417
418/* Source operand is byte, word, long or quad sized. */
419#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
420 __emulate_2op(_op, _src, _dst, _eflags, \
421 "b", "q", "w", "r", _LO32, "r", "", "r")
422
423/* Source operand is word, long or quad sized. */
424#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
425 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
426 "w", "r", _LO32, "r", "", "r")
427
428/* Instruction has only one explicit operand (no source operand). */
429#define emulate_1op(_op, _dst, _eflags) \
430 do { \
431 unsigned long _tmp; \
432 \
Mike Dayd77c26f2007-10-08 09:02:08 -0400433 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 case 1: \
435 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400436 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 _op"b %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400438 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 : "=m" (_eflags), "=m" ((_dst).val), \
440 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400441 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 break; \
443 case 2: \
444 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400445 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 _op"w %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400447 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 : "=m" (_eflags), "=m" ((_dst).val), \
449 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400450 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 break; \
452 case 4: \
453 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400454 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 _op"l %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400456 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 : "=m" (_eflags), "=m" ((_dst).val), \
458 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400459 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 break; \
461 case 8: \
462 __emulate_1op_8byte(_op, _dst, _eflags); \
463 break; \
464 } \
465 } while (0)
466
467/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800468#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
470 do { \
471 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400472 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400474 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400476 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 } while (0)
478
479#define __emulate_1op_8byte(_op, _dst, _eflags) \
480 do { \
481 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400482 _PRE_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 _op"q %1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400484 _POST_EFLAGS("0", "3", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400486 : "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 } while (0)
488
489#elif defined(__i386__)
490#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
491#define __emulate_1op_8byte(_op, _dst, _eflags)
492#endif /* __i386__ */
493
494/* Fetch next part of the instruction being emulated. */
495#define insn_fetch(_type, _size, _eip) \
496({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200497 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400498 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 goto done; \
500 (_eip) += (_size); \
501 (_type)_x; \
502})
503
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800504static inline unsigned long ad_mask(struct decode_cache *c)
505{
506 return (1UL << (c->ad_bytes << 3)) - 1;
507}
508
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800510static inline unsigned long
511address_mask(struct decode_cache *c, unsigned long reg)
512{
513 if (c->ad_bytes == sizeof(unsigned long))
514 return reg;
515 else
516 return reg & ad_mask(c);
517}
518
519static inline unsigned long
520register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
521{
522 return base + address_mask(c, reg);
523}
524
Harvey Harrison7a9572752008-02-19 07:40:41 -0800525static inline void
526register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
527{
528 if (c->ad_bytes == sizeof(unsigned long))
529 *reg += inc;
530 else
531 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
532}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533
Harvey Harrison7a9572752008-02-19 07:40:41 -0800534static inline void jmp_rel(struct decode_cache *c, int rel)
535{
536 register_address_increment(c, &c->eip, rel);
537}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300538
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300539static void set_seg_override(struct decode_cache *c, int seg)
540{
541 c->has_seg_override = true;
542 c->seg_override = seg;
543}
544
545static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
546{
547 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
548 return 0;
549
550 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
551}
552
553static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
554 struct decode_cache *c)
555{
556 if (!c->has_seg_override)
557 return 0;
558
559 return seg_base(ctxt, c->seg_override);
560}
561
562static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
563{
564 return seg_base(ctxt, VCPU_SREG_ES);
565}
566
567static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
568{
569 return seg_base(ctxt, VCPU_SREG_SS);
570}
571
Avi Kivity62266862007-11-20 13:15:52 +0200572static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
573 struct x86_emulate_ops *ops,
574 unsigned long linear, u8 *dest)
575{
576 struct fetch_cache *fc = &ctxt->decode.fetch;
577 int rc;
578 int size;
579
580 if (linear < fc->start || linear >= fc->end) {
581 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
582 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
583 if (rc)
584 return rc;
585 fc->start = linear;
586 fc->end = linear + size;
587 }
588 *dest = fc->data[linear - fc->start];
589 return 0;
590}
591
592static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
593 struct x86_emulate_ops *ops,
594 unsigned long eip, void *dest, unsigned size)
595{
596 int rc = 0;
597
598 eip += ctxt->cs_base;
599 while (size--) {
600 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
601 if (rc)
602 return rc;
603 }
604 return 0;
605}
606
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000607/*
608 * Given the 'reg' portion of a ModRM byte, and a register block, return a
609 * pointer into the block that addresses the relevant register.
610 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
611 */
612static void *decode_register(u8 modrm_reg, unsigned long *regs,
613 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614{
615 void *p;
616
617 p = &regs[modrm_reg];
618 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
619 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
620 return p;
621}
622
623static int read_descriptor(struct x86_emulate_ctxt *ctxt,
624 struct x86_emulate_ops *ops,
625 void *ptr,
626 u16 *size, unsigned long *address, int op_bytes)
627{
628 int rc;
629
630 if (op_bytes == 2)
631 op_bytes = 3;
632 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300633 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
634 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 if (rc)
636 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300637 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
638 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 return rc;
640}
641
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300642static int test_cc(unsigned int condition, unsigned int flags)
643{
644 int rc = 0;
645
646 switch ((condition & 15) >> 1) {
647 case 0: /* o */
648 rc |= (flags & EFLG_OF);
649 break;
650 case 1: /* b/c/nae */
651 rc |= (flags & EFLG_CF);
652 break;
653 case 2: /* z/e */
654 rc |= (flags & EFLG_ZF);
655 break;
656 case 3: /* be/na */
657 rc |= (flags & (EFLG_CF|EFLG_ZF));
658 break;
659 case 4: /* s */
660 rc |= (flags & EFLG_SF);
661 break;
662 case 5: /* p/pe */
663 rc |= (flags & EFLG_PF);
664 break;
665 case 7: /* le/ng */
666 rc |= (flags & EFLG_ZF);
667 /* fall through */
668 case 6: /* l/nge */
669 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
670 break;
671 }
672
673 /* Odd condition identifiers (lsb == 1) have inverted sense. */
674 return (!!rc ^ (condition & 1));
675}
676
Avi Kivity3c118e22007-10-31 10:27:04 +0200677static void decode_register_operand(struct operand *op,
678 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200679 int inhibit_bytereg)
680{
Avi Kivity33615aa2007-10-31 11:15:56 +0200681 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200682 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200683
684 if (!(c->d & ModRM))
685 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200686 op->type = OP_REG;
687 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200688 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200689 op->val = *(u8 *)op->ptr;
690 op->bytes = 1;
691 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200692 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200693 op->bytes = c->op_bytes;
694 switch (op->bytes) {
695 case 2:
696 op->val = *(u16 *)op->ptr;
697 break;
698 case 4:
699 op->val = *(u32 *)op->ptr;
700 break;
701 case 8:
702 op->val = *(u64 *) op->ptr;
703 break;
704 }
705 }
706 op->orig_val = op->val;
707}
708
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200709static int decode_modrm(struct x86_emulate_ctxt *ctxt,
710 struct x86_emulate_ops *ops)
711{
712 struct decode_cache *c = &ctxt->decode;
713 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700714 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200715 int rc = 0;
716
717 if (c->rex_prefix) {
718 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
719 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
720 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
721 }
722
723 c->modrm = insn_fetch(u8, 1, c->eip);
724 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
725 c->modrm_reg |= (c->modrm & 0x38) >> 3;
726 c->modrm_rm |= (c->modrm & 0x07);
727 c->modrm_ea = 0;
728 c->use_modrm_ea = 1;
729
730 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300731 c->modrm_ptr = decode_register(c->modrm_rm,
732 c->regs, c->d & ByteOp);
733 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200734 return rc;
735 }
736
737 if (c->ad_bytes == 2) {
738 unsigned bx = c->regs[VCPU_REGS_RBX];
739 unsigned bp = c->regs[VCPU_REGS_RBP];
740 unsigned si = c->regs[VCPU_REGS_RSI];
741 unsigned di = c->regs[VCPU_REGS_RDI];
742
743 /* 16-bit ModR/M decode. */
744 switch (c->modrm_mod) {
745 case 0:
746 if (c->modrm_rm == 6)
747 c->modrm_ea += insn_fetch(u16, 2, c->eip);
748 break;
749 case 1:
750 c->modrm_ea += insn_fetch(s8, 1, c->eip);
751 break;
752 case 2:
753 c->modrm_ea += insn_fetch(u16, 2, c->eip);
754 break;
755 }
756 switch (c->modrm_rm) {
757 case 0:
758 c->modrm_ea += bx + si;
759 break;
760 case 1:
761 c->modrm_ea += bx + di;
762 break;
763 case 2:
764 c->modrm_ea += bp + si;
765 break;
766 case 3:
767 c->modrm_ea += bp + di;
768 break;
769 case 4:
770 c->modrm_ea += si;
771 break;
772 case 5:
773 c->modrm_ea += di;
774 break;
775 case 6:
776 if (c->modrm_mod != 0)
777 c->modrm_ea += bp;
778 break;
779 case 7:
780 c->modrm_ea += bx;
781 break;
782 }
783 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
784 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300785 if (!c->has_seg_override)
786 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200787 c->modrm_ea = (u16)c->modrm_ea;
788 } else {
789 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700790 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791 sib = insn_fetch(u8, 1, c->eip);
792 index_reg |= (sib >> 3) & 7;
793 base_reg |= sib & 7;
794 scale = sib >> 6;
795
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700796 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
797 c->modrm_ea += insn_fetch(s32, 4, c->eip);
798 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200799 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700800 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200801 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700802 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
803 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700804 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700805 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200806 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200807 switch (c->modrm_mod) {
808 case 0:
809 if (c->modrm_rm == 5)
810 c->modrm_ea += insn_fetch(s32, 4, c->eip);
811 break;
812 case 1:
813 c->modrm_ea += insn_fetch(s8, 1, c->eip);
814 break;
815 case 2:
816 c->modrm_ea += insn_fetch(s32, 4, c->eip);
817 break;
818 }
819 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200820done:
821 return rc;
822}
823
824static int decode_abs(struct x86_emulate_ctxt *ctxt,
825 struct x86_emulate_ops *ops)
826{
827 struct decode_cache *c = &ctxt->decode;
828 int rc = 0;
829
830 switch (c->ad_bytes) {
831 case 2:
832 c->modrm_ea = insn_fetch(u16, 2, c->eip);
833 break;
834 case 4:
835 c->modrm_ea = insn_fetch(u32, 4, c->eip);
836 break;
837 case 8:
838 c->modrm_ea = insn_fetch(u64, 8, c->eip);
839 break;
840 }
841done:
842 return rc;
843}
844
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200846x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200848 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200851 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852
853 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854
Laurent Viviere4e03de2007-09-18 11:52:50 +0200855 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300856 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300857 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800858 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859
860 switch (mode) {
861 case X86EMUL_MODE_REAL:
862 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200863 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 break;
865 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200866 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800868#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200870 def_op_bytes = 4;
871 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 break;
873#endif
874 default:
875 return -1;
876 }
877
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200878 c->op_bytes = def_op_bytes;
879 c->ad_bytes = def_ad_bytes;
880
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200882 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200883 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200885 /* switch between 2/4 bytes */
886 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 break;
888 case 0x67: /* address-size override */
889 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200890 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200891 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200894 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300897 case 0x2e: /* CS override */
898 case 0x36: /* SS override */
899 case 0x3e: /* DS override */
900 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 break;
902 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300904 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200906 case 0x40 ... 0x4f: /* REX */
907 if (mode != X86EMUL_MODE_PROT64)
908 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200909 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200910 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200912 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200914 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100915 c->rep_prefix = REPNE_PREFIX;
916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100918 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 default:
921 goto done_prefixes;
922 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200923
924 /* Any legacy prefix after a REX prefix nullifies its effect. */
925
Avi Kivity33615aa2007-10-31 11:15:56 +0200926 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 }
928
929done_prefixes:
930
931 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200933 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200934 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935
936 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200937 c->d = opcode_table[c->b];
938 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200940 if (c->b == 0x0f) {
941 c->twobyte = 1;
942 c->b = insn_fetch(u8, 1, c->eip);
943 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200945 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Avi Kivitye09d0822008-01-18 12:38:59 +0200947 if (c->d & Group) {
948 group = c->d & GroupMask;
949 c->modrm = insn_fetch(u8, 1, c->eip);
950 --c->eip;
951
952 group = (group << 3) + ((c->modrm >> 3) & 7);
953 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
954 c->d = group2_table[group];
955 else
956 c->d = group_table[group];
957 }
958
959 /* Unrecognised? */
960 if (c->d == 0) {
961 DPRINTF("Cannot emulate %02x\n", c->b);
962 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 }
964
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200965 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
966 c->op_bytes = 8;
967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 if (c->d & ModRM)
970 rc = decode_modrm(ctxt, ops);
971 else if (c->d & MemAbs)
972 rc = decode_abs(ctxt, ops);
973 if (rc)
974 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300976 if (!c->has_seg_override)
977 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200978
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300979 if (!(!c->twobyte && c->b == 0x8d))
980 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200981
982 if (c->ad_bytes != 8)
983 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 /*
985 * Decode and fetch the source operand: register, memory
986 * or immediate.
987 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200988 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 case SrcNone:
990 break;
991 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200992 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 break;
994 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200995 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 goto srcmem_common;
997 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200998 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 goto srcmem_common;
1000 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001001 c->src.bytes = (c->d & ByteOp) ? 1 :
1002 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001003 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001004 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001005 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001006 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001007 /*
1008 * For instructions with a ModR/M byte, switch to register
1009 * access if Mod = 3.
1010 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 if ((c->d & ModRM) && c->modrm_mod == 3) {
1012 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001013 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001014 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001015 break;
1016 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001017 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 break;
1019 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001020 c->src.type = OP_IMM;
1021 c->src.ptr = (unsigned long *)c->eip;
1022 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1023 if (c->src.bytes == 8)
1024 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001026 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001028 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 break;
1030 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001034 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 break;
1036 }
1037 break;
1038 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001039 c->src.type = OP_IMM;
1040 c->src.ptr = (unsigned long *)c->eip;
1041 c->src.bytes = 1;
1042 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 break;
1044 }
1045
Avi Kivity038e51d2007-01-22 20:40:40 -08001046 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001047 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001048 case ImplicitOps:
1049 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001050 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001051 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001052 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001053 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001054 break;
1055 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001056 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001057 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001058 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001059 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001060 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001061 break;
1062 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001063 c->dst.type = OP_MEM;
1064 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001065 case DstAcc:
1066 c->dst.type = OP_REG;
1067 c->dst.bytes = c->op_bytes;
1068 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1069 switch (c->op_bytes) {
1070 case 1:
1071 c->dst.val = *(u8 *)c->dst.ptr;
1072 break;
1073 case 2:
1074 c->dst.val = *(u16 *)c->dst.ptr;
1075 break;
1076 case 4:
1077 c->dst.val = *(u32 *)c->dst.ptr;
1078 break;
1079 }
1080 c->dst.orig_val = c->dst.val;
1081 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001082 }
1083
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001084 if (c->rip_relative)
1085 c->modrm_ea += c->eip;
1086
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001087done:
1088 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1089}
1090
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001091static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1092{
1093 struct decode_cache *c = &ctxt->decode;
1094
1095 c->dst.type = OP_MEM;
1096 c->dst.bytes = c->op_bytes;
1097 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001098 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001099 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001100 c->regs[VCPU_REGS_RSP]);
1101}
1102
1103static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1104 struct x86_emulate_ops *ops)
1105{
1106 struct decode_cache *c = &ctxt->decode;
1107 int rc;
1108
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001109 rc = ops->read_std(register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001110 c->regs[VCPU_REGS_RSP]),
1111 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1112 if (rc != 0)
1113 return rc;
1114
Harvey Harrison7a9572752008-02-19 07:40:41 -08001115 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001116
1117 return 0;
1118}
1119
Laurent Vivier05f086f2007-09-24 11:10:55 +02001120static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001122 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001123 switch (c->modrm_reg) {
1124 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001125 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001126 break;
1127 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001128 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001129 break;
1130 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001131 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001132 break;
1133 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001134 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001135 break;
1136 case 4: /* sal/shl */
1137 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001138 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001139 break;
1140 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001141 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001142 break;
1143 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001144 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001145 break;
1146 }
1147}
1148
1149static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001150 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001151{
1152 struct decode_cache *c = &ctxt->decode;
1153 int rc = 0;
1154
1155 switch (c->modrm_reg) {
1156 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001157 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001158 break;
1159 case 2: /* not */
1160 c->dst.val = ~c->dst.val;
1161 break;
1162 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001163 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164 break;
1165 default:
1166 DPRINTF("Cannot emulate %02x\n", c->b);
1167 rc = X86EMUL_UNHANDLEABLE;
1168 break;
1169 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001170 return rc;
1171}
1172
1173static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001174 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175{
1176 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001177
1178 switch (c->modrm_reg) {
1179 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001180 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001181 break;
1182 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001183 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001185 case 2: /* call near abs */ {
1186 long int old_eip;
1187 old_eip = c->eip;
1188 c->eip = c->src.val;
1189 c->src.val = old_eip;
1190 emulate_push(ctxt);
1191 break;
1192 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001193 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001194 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001195 break;
1196 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001197 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001199 }
1200 return 0;
1201}
1202
1203static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1204 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001205 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001206{
1207 struct decode_cache *c = &ctxt->decode;
1208 u64 old, new;
1209 int rc;
1210
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001211 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001212 if (rc != 0)
1213 return rc;
1214
1215 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1216 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1217
1218 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1219 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001220 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001221
1222 } else {
1223 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1224 (u32) c->regs[VCPU_REGS_RBX];
1225
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001226 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001227 if (rc != 0)
1228 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001229 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001230 }
1231 return 0;
1232}
1233
1234static inline int writeback(struct x86_emulate_ctxt *ctxt,
1235 struct x86_emulate_ops *ops)
1236{
1237 int rc;
1238 struct decode_cache *c = &ctxt->decode;
1239
1240 switch (c->dst.type) {
1241 case OP_REG:
1242 /* The 4-byte case *is* correct:
1243 * in 64-bit mode we zero-extend.
1244 */
1245 switch (c->dst.bytes) {
1246 case 1:
1247 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1248 break;
1249 case 2:
1250 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1251 break;
1252 case 4:
1253 *c->dst.ptr = (u32)c->dst.val;
1254 break; /* 64b: zero-ext */
1255 case 8:
1256 *c->dst.ptr = c->dst.val;
1257 break;
1258 }
1259 break;
1260 case OP_MEM:
1261 if (c->lock_prefix)
1262 rc = ops->cmpxchg_emulated(
1263 (unsigned long)c->dst.ptr,
1264 &c->dst.orig_val,
1265 &c->dst.val,
1266 c->dst.bytes,
1267 ctxt->vcpu);
1268 else
1269 rc = ops->write_emulated(
1270 (unsigned long)c->dst.ptr,
1271 &c->dst.val,
1272 c->dst.bytes,
1273 ctxt->vcpu);
1274 if (rc != 0)
1275 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001276 break;
1277 case OP_NONE:
1278 /* no writeback */
1279 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001280 default:
1281 break;
1282 }
1283 return 0;
1284}
1285
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001286int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001287x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001288{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001289 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001290 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001291 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001292 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001293 unsigned int port;
1294 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001295 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001296
Laurent Vivier34273182007-09-18 11:27:37 +02001297 /* Shadow copy of register state. Committed on successful emulation.
1298 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1299 * modify them.
1300 */
1301
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001302 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001303 saved_eip = c->eip;
1304
Avi Kivityc7e75a32007-10-28 16:34:25 +02001305 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001306 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001307
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001308 if (c->rep_prefix && (c->d & String)) {
1309 /* All REP prefixes have the same first termination condition */
1310 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001311 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001312 goto done;
1313 }
1314 /* The second termination condition only applies for REPE
1315 * and REPNE. Test if the repeat string operation prefix is
1316 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1317 * corresponding termination condition according to:
1318 * - if REPE/REPZ and ZF = 0 then done
1319 * - if REPNE/REPNZ and ZF = 1 then done
1320 */
1321 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1322 (c->b == 0xae) || (c->b == 0xaf)) {
1323 if ((c->rep_prefix == REPE_PREFIX) &&
1324 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001325 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001326 goto done;
1327 }
1328 if ((c->rep_prefix == REPNE_PREFIX) &&
1329 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001330 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001331 goto done;
1332 }
1333 }
1334 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001335 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001336 }
1337
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001338 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001339 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001340 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001341 rc = ops->read_emulated((unsigned long)c->src.ptr,
1342 &c->src.val,
1343 c->src.bytes,
1344 ctxt->vcpu);
1345 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001346 goto done;
1347 c->src.orig_val = c->src.val;
1348 }
1349
1350 if ((c->d & DstMask) == ImplicitOps)
1351 goto special_insn;
1352
1353
1354 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001355 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001356 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1357 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001358 if (c->d & BitOp) {
1359 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001360
Laurent Viviere4e03de2007-09-18 11:52:50 +02001361 c->dst.ptr = (void *)c->dst.ptr +
1362 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001363 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001364 if (!(c->d & Mov) &&
1365 /* optimisation - avoid slow emulated read */
1366 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1367 &c->dst.val,
1368 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001369 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001370 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001371 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001372
Avi Kivity018a98d2007-11-27 19:30:56 +02001373special_insn:
1374
Laurent Viviere4e03de2007-09-18 11:52:50 +02001375 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 goto twobyte_insn;
1377
Laurent Viviere4e03de2007-09-18 11:52:50 +02001378 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 case 0x00 ... 0x05:
1380 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 break;
1383 case 0x08 ... 0x0d:
1384 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 break;
1387 case 0x10 ... 0x15:
1388 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 break;
1391 case 0x18 ... 0x1d:
1392 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001393 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001395 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 break;
1399 case 0x28 ... 0x2d:
1400 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001401 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 break;
1403 case 0x30 ... 0x35:
1404 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001405 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 break;
1407 case 0x38 ... 0x3d:
1408 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001409 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001411 case 0x40 ... 0x47: /* inc r16/r32 */
1412 emulate_1op("inc", c->dst, ctxt->eflags);
1413 break;
1414 case 0x48 ... 0x4f: /* dec r16/r32 */
1415 emulate_1op("dec", c->dst, ctxt->eflags);
1416 break;
1417 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001418 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001419 break;
1420 case 0x58 ... 0x5f: /* pop reg */
1421 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001422 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001423 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1424 c->op_bytes, ctxt->vcpu)) != 0)
1425 goto done;
1426
Harvey Harrison7a9572752008-02-19 07:40:41 -08001427 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001428 c->op_bytes);
1429 c->dst.type = OP_NONE; /* Disable writeback. */
1430 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001432 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001434 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001436 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001437 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001438 emulate_push(ctxt);
1439 break;
1440 case 0x6c: /* insb */
1441 case 0x6d: /* insw/insd */
1442 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1443 1,
1444 (c->d & ByteOp) ? 1 : c->op_bytes,
1445 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001446 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001447 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001448 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001449 c->regs[VCPU_REGS_RDI]),
1450 c->rep_prefix,
1451 c->regs[VCPU_REGS_RDX]) == 0) {
1452 c->eip = saved_eip;
1453 return -1;
1454 }
1455 return 0;
1456 case 0x6e: /* outsb */
1457 case 0x6f: /* outsw/outsd */
1458 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1459 0,
1460 (c->d & ByteOp) ? 1 : c->op_bytes,
1461 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001462 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001463 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001464 register_address(c,
1465 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001466 c->regs[VCPU_REGS_RSI]),
1467 c->rep_prefix,
1468 c->regs[VCPU_REGS_RDX]) == 0) {
1469 c->eip = saved_eip;
1470 return -1;
1471 }
1472 return 0;
1473 case 0x70 ... 0x7f: /* jcc (short) */ {
1474 int rel = insn_fetch(s8, 1, c->eip);
1475
1476 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001477 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001478 break;
1479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001481 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 case 0:
1483 goto add;
1484 case 1:
1485 goto or;
1486 case 2:
1487 goto adc;
1488 case 3:
1489 goto sbb;
1490 case 4:
1491 goto and;
1492 case 5:
1493 goto sub;
1494 case 6:
1495 goto xor;
1496 case 7:
1497 goto cmp;
1498 }
1499 break;
1500 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001501 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 break;
1503 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001504 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001508 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 break;
1510 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001511 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 break;
1513 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001514 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 break; /* 64b reg: zero-extend */
1516 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001517 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 break;
1519 }
1520 /*
1521 * Write back the memory destination with implicit LOCK
1522 * prefix.
1523 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001524 c->dst.val = c->src.val;
1525 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001528 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001529 case 0x8c: { /* mov r/m, sreg */
1530 struct kvm_segment segreg;
1531
1532 if (c->modrm_reg <= 5)
1533 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1534 else {
1535 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1536 c->modrm);
1537 goto cannot_emulate;
1538 }
1539 c->dst.val = segreg.selector;
1540 break;
1541 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001542 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001543 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001544 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001545 case 0x8e: { /* mov seg, r/m16 */
1546 uint16_t sel;
1547 int type_bits;
1548 int err;
1549
1550 sel = c->src.val;
1551 if (c->modrm_reg <= 5) {
1552 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1553 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1554 type_bits, c->modrm_reg);
1555 } else {
1556 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1557 c->modrm);
1558 goto cannot_emulate;
1559 }
1560
1561 if (err < 0)
1562 goto cannot_emulate;
1563
1564 c->dst.type = OP_NONE; /* Disable writeback. */
1565 break;
1566 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001568 rc = emulate_grp1a(ctxt, ops);
1569 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001572 case 0x90: /* nop / xchg r8,rax */
1573 if (!(c->rex_prefix & 1)) { /* nop */
1574 c->dst.type = OP_NONE;
1575 break;
1576 }
1577 case 0x91 ... 0x97: /* xchg reg,rax */
1578 c->src.type = c->dst.type = OP_REG;
1579 c->src.bytes = c->dst.bytes = c->op_bytes;
1580 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1581 c->src.val = *(c->src.ptr);
1582 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001583 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001584 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001585 emulate_push(ctxt);
1586 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001587 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001588 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001589 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001590 case 0xa0 ... 0xa1: /* mov */
1591 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1592 c->dst.val = c->src.val;
1593 break;
1594 case 0xa2 ... 0xa3: /* mov */
1595 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1596 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001598 c->dst.type = OP_MEM;
1599 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001600 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001601 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001602 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001603 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001604 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001605 c->regs[VCPU_REGS_RSI]),
1606 &c->dst.val,
1607 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001609 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001610 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001611 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001612 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001613 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001614 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 break;
1616 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001617 c->src.type = OP_NONE; /* Disable writeback. */
1618 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001619 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001620 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001621 c->regs[VCPU_REGS_RSI]);
1622 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1623 &c->src.val,
1624 c->src.bytes,
1625 ctxt->vcpu)) != 0)
1626 goto done;
1627
1628 c->dst.type = OP_NONE; /* Disable writeback. */
1629 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001630 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001631 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001632 c->regs[VCPU_REGS_RDI]);
1633 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1634 &c->dst.val,
1635 c->dst.bytes,
1636 ctxt->vcpu)) != 0)
1637 goto done;
1638
1639 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1640
1641 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1642
Harvey Harrison7a9572752008-02-19 07:40:41 -08001643 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001644 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1645 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001646 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001647 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1648 : c->dst.bytes);
1649
1650 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001652 c->dst.type = OP_MEM;
1653 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001654 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001655 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001656 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001657 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001658 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001659 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001660 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661 break;
1662 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001663 c->dst.type = OP_REG;
1664 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1665 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001666 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001667 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001668 c->regs[VCPU_REGS_RSI]),
1669 &c->dst.val,
1670 c->dst.bytes,
1671 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001673 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001674 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001675 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676 break;
1677 case 0xae ... 0xaf: /* scas */
1678 DPRINTF("Urk! I don't handle SCAS.\n");
1679 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001680 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001681 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001682 case 0xc0 ... 0xc1:
1683 emulate_grp2(ctxt);
1684 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001685 case 0xc3: /* ret */
1686 c->dst.ptr = &c->eip;
1687 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001688 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1689 mov:
1690 c->dst.val = c->src.val;
1691 break;
1692 case 0xd0 ... 0xd1: /* Grp2 */
1693 c->src.val = 1;
1694 emulate_grp2(ctxt);
1695 break;
1696 case 0xd2 ... 0xd3: /* Grp2 */
1697 c->src.val = c->regs[VCPU_REGS_RCX];
1698 emulate_grp2(ctxt);
1699 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001700 case 0xe4: /* inb */
1701 case 0xe5: /* in */
1702 port = insn_fetch(u8, 1, c->eip);
1703 io_dir_in = 1;
1704 goto do_io;
1705 case 0xe6: /* outb */
1706 case 0xe7: /* out */
1707 port = insn_fetch(u8, 1, c->eip);
1708 io_dir_in = 0;
1709 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001710 case 0xe8: /* call (near) */ {
1711 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001712 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001713 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001714 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001715 break;
1716 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001717 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001718 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001719 default:
1720 DPRINTF("Call: Invalid op_bytes\n");
1721 goto cannot_emulate;
1722 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001724 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001725 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726 emulate_push(ctxt);
1727 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001728 }
1729 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001730 goto jmp;
1731 case 0xea: /* jmp far */ {
1732 uint32_t eip;
1733 uint16_t sel;
1734
1735 switch (c->op_bytes) {
1736 case 2:
1737 eip = insn_fetch(u16, 2, c->eip);
1738 break;
1739 case 4:
1740 eip = insn_fetch(u32, 4, c->eip);
1741 break;
1742 default:
1743 DPRINTF("jmp far: Invalid op_bytes\n");
1744 goto cannot_emulate;
1745 }
1746 sel = insn_fetch(u16, 2, c->eip);
1747 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1748 DPRINTF("jmp far: Failed to load CS descriptor\n");
1749 goto cannot_emulate;
1750 }
1751
1752 c->eip = eip;
1753 break;
1754 }
1755 case 0xeb:
1756 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001757 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001758 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001759 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001760 case 0xec: /* in al,dx */
1761 case 0xed: /* in (e/r)ax,dx */
1762 port = c->regs[VCPU_REGS_RDX];
1763 io_dir_in = 1;
1764 goto do_io;
1765 case 0xee: /* out al,dx */
1766 case 0xef: /* out (e/r)ax,dx */
1767 port = c->regs[VCPU_REGS_RDX];
1768 io_dir_in = 0;
1769 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1770 (c->d & ByteOp) ? 1 : c->op_bytes,
1771 port) != 0) {
1772 c->eip = saved_eip;
1773 goto cannot_emulate;
1774 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001775 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001776 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001777 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001778 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001779 case 0xf5: /* cmc */
1780 /* complement carry flag from eflags reg */
1781 ctxt->eflags ^= EFLG_CF;
1782 c->dst.type = OP_NONE; /* Disable writeback. */
1783 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001784 case 0xf6 ... 0xf7: /* Grp3 */
1785 rc = emulate_grp3(ctxt, ops);
1786 if (rc != 0)
1787 goto done;
1788 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001789 case 0xf8: /* clc */
1790 ctxt->eflags &= ~EFLG_CF;
1791 c->dst.type = OP_NONE; /* Disable writeback. */
1792 break;
1793 case 0xfa: /* cli */
1794 ctxt->eflags &= ~X86_EFLAGS_IF;
1795 c->dst.type = OP_NONE; /* Disable writeback. */
1796 break;
1797 case 0xfb: /* sti */
1798 ctxt->eflags |= X86_EFLAGS_IF;
1799 c->dst.type = OP_NONE; /* Disable writeback. */
1800 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001801 case 0xfc: /* cld */
1802 ctxt->eflags &= ~EFLG_DF;
1803 c->dst.type = OP_NONE; /* Disable writeback. */
1804 break;
1805 case 0xfd: /* std */
1806 ctxt->eflags |= EFLG_DF;
1807 c->dst.type = OP_NONE; /* Disable writeback. */
1808 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001809 case 0xfe ... 0xff: /* Grp4/Grp5 */
1810 rc = emulate_grp45(ctxt, ops);
1811 if (rc != 0)
1812 goto done;
1813 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001815
1816writeback:
1817 rc = writeback(ctxt, ops);
1818 if (rc != 0)
1819 goto done;
1820
1821 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001822 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001823 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001824
1825done:
1826 if (rc == X86EMUL_UNHANDLEABLE) {
1827 c->eip = saved_eip;
1828 return -1;
1829 }
1830 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831
1832twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001833 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001835 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 u16 size;
1837 unsigned long address;
1838
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001839 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001840 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001841 goto cannot_emulate;
1842
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001843 rc = kvm_fix_hypercall(ctxt->vcpu);
1844 if (rc)
1845 goto done;
1846
Avi Kivity33e38852008-05-21 15:34:25 +03001847 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001848 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001849 /* Disable writeback. */
1850 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001851 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001853 rc = read_descriptor(ctxt, ops, c->src.ptr,
1854 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 if (rc)
1856 goto done;
1857 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001858 /* Disable writeback. */
1859 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001861 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001862 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001863 rc = kvm_fix_hypercall(ctxt->vcpu);
1864 if (rc)
1865 goto done;
1866 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001867 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001868 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001869 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001870 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001871 if (rc)
1872 goto done;
1873 realmode_lidt(ctxt->vcpu, size, address);
1874 }
Avi Kivity16286d02008-04-14 14:40:50 +03001875 /* Disable writeback. */
1876 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 break;
1878 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001879 c->dst.bytes = 2;
1880 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 break;
1882 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001883 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1884 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001885 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886 break;
1887 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001888 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001889 /* Disable writeback. */
1890 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 break;
1892 default:
1893 goto cannot_emulate;
1894 }
1895 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001896 case 0x06:
1897 emulate_clts(ctxt->vcpu);
1898 c->dst.type = OP_NONE;
1899 break;
1900 case 0x08: /* invd */
1901 case 0x09: /* wbinvd */
1902 case 0x0d: /* GrpP (prefetch) */
1903 case 0x18: /* Grp16 (prefetch/nop) */
1904 c->dst.type = OP_NONE;
1905 break;
1906 case 0x20: /* mov cr, reg */
1907 if (c->modrm_mod != 3)
1908 goto cannot_emulate;
1909 c->regs[c->modrm_rm] =
1910 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1911 c->dst.type = OP_NONE; /* no writeback */
1912 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001914 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001916 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001917 if (rc)
1918 goto cannot_emulate;
1919 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001921 case 0x22: /* mov reg, cr */
1922 if (c->modrm_mod != 3)
1923 goto cannot_emulate;
1924 realmode_set_cr(ctxt->vcpu,
1925 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1926 c->dst.type = OP_NONE;
1927 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001929 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001931 rc = emulator_set_dr(ctxt, c->modrm_reg,
1932 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001933 if (rc)
1934 goto cannot_emulate;
1935 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001937 case 0x30:
1938 /* wrmsr */
1939 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1940 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1941 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1942 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001943 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001944 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001945 }
1946 rc = X86EMUL_CONTINUE;
1947 c->dst.type = OP_NONE;
1948 break;
1949 case 0x32:
1950 /* rdmsr */
1951 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1952 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001953 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001954 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001955 } else {
1956 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1957 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1958 }
1959 rc = X86EMUL_CONTINUE;
1960 c->dst.type = OP_NONE;
1961 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001963 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001964 if (!test_cc(c->b, ctxt->eflags))
1965 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001967 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1968 long int rel;
1969
1970 switch (c->op_bytes) {
1971 case 2:
1972 rel = insn_fetch(s16, 2, c->eip);
1973 break;
1974 case 4:
1975 rel = insn_fetch(s32, 4, c->eip);
1976 break;
1977 case 8:
1978 rel = insn_fetch(s64, 8, c->eip);
1979 break;
1980 default:
1981 DPRINTF("jnz: Invalid op_bytes\n");
1982 goto cannot_emulate;
1983 }
1984 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001985 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001986 c->dst.type = OP_NONE;
1987 break;
1988 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001989 case 0xa3:
1990 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001991 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001992 /* only subword offset */
1993 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001994 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001995 break;
1996 case 0xab:
1997 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001998 /* only subword offset */
1999 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002000 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002001 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002002 case 0xae: /* clflush */
2003 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 case 0xb0 ... 0xb1: /* cmpxchg */
2005 /*
2006 * Save real source value, then compare EAX against
2007 * destination.
2008 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002009 c->src.orig_val = c->src.val;
2010 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002011 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2012 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002014 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 } else {
2016 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002017 c->dst.type = OP_REG;
2018 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 }
2020 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 case 0xb3:
2022 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002023 /* only subword offset */
2024 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002025 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002028 c->dst.bytes = c->op_bytes;
2029 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2030 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002033 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 case 0:
2035 goto bt;
2036 case 1:
2037 goto bts;
2038 case 2:
2039 goto btr;
2040 case 3:
2041 goto btc;
2042 }
2043 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002044 case 0xbb:
2045 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002046 /* only subword offset */
2047 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002048 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002049 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002051 c->dst.bytes = c->op_bytes;
2052 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2053 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002055 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002056 c->dst.bytes = c->op_bytes;
2057 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2058 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002059 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002061 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002062 if (rc != 0)
2063 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002064 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002065 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 }
2067 goto writeback;
2068
2069cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002070 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002071 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072 return -1;
2073}