blob: 5f87d3e59195bcb5652586c26e601b232552038e [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
Avi Kivitydda96d82008-11-26 15:14:10 +0200362#ifdef CONFIG_X86_64
363#define ON64(x) x
364#else
365#define ON64(x)
366#endif
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368/* Raw emulation: instruction has two explicit operands. */
369#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
370 do { \
371 unsigned long _tmp; \
372 \
373 switch ((_dst).bytes) { \
374 case 2: \
375 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400376 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 _op"w %"_wx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400378 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 : "=m" (_eflags), "=m" ((_dst).val), \
380 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400381 : _wy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 break; \
383 case 4: \
384 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400385 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386 _op"l %"_lx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400387 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 : "=m" (_eflags), "=m" ((_dst).val), \
389 "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400390 : _ly ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 break; \
392 case 8: \
393 __emulate_2op_8byte(_op, _src, _dst, \
394 _eflags, _qx, _qy); \
395 break; \
396 } \
397 } while (0)
398
399#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
400 do { \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800401 unsigned long __tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400402 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 case 1: \
404 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400405 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 _op"b %"_bx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400407 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408 : "=m" (_eflags), "=m" ((_dst).val), \
Harvey Harrison77cd3372008-02-19 10:43:11 -0800409 "=&r" (__tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400410 : _by ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 break; \
412 default: \
413 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
414 _wx, _wy, _lx, _ly, _qx, _qy); \
415 break; \
416 } \
417 } while (0)
418
419/* Source operand is byte-sized and may be restricted to just %cl. */
420#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
421 __emulate_2op(_op, _src, _dst, _eflags, \
422 "b", "c", "b", "c", "b", "c", "b", "c")
423
424/* Source operand is byte, word, long or quad sized. */
425#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
426 __emulate_2op(_op, _src, _dst, _eflags, \
427 "b", "q", "w", "r", _LO32, "r", "", "r")
428
429/* Source operand is word, long or quad sized. */
430#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
431 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
432 "w", "r", _LO32, "r", "", "r")
433
Avi Kivitydda96d82008-11-26 15:14:10 +0200434#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 do { \
436 unsigned long _tmp; \
437 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200438 __asm__ __volatile__ ( \
439 _PRE_EFLAGS("0", "3", "2") \
440 _op _suffix " %1; " \
441 _POST_EFLAGS("0", "3", "2") \
442 : "=m" (_eflags), "+m" ((_dst).val), \
443 "=&r" (_tmp) \
444 : "i" (EFLAGS_MASK)); \
445 } while (0)
446
447/* Instruction has only one explicit operand (no source operand). */
448#define emulate_1op(_op, _dst, _eflags) \
449 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400450 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200451 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
452 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
453 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
454 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 } \
456 } while (0)
457
458/* Emulate an instruction with quadword operands (x86/64 only). */
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800459#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
461 do { \
462 __asm__ __volatile__ ( \
Mike Dayd77c26f2007-10-08 09:02:08 -0400463 _PRE_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 _op"q %"_qx"3,%1; " \
Mike Dayd77c26f2007-10-08 09:02:08 -0400465 _POST_EFLAGS("0", "4", "2") \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400467 : _qy ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 } while (0)
469
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470#elif defined(__i386__)
471#define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472#endif /* __i386__ */
473
474/* Fetch next part of the instruction being emulated. */
475#define insn_fetch(_type, _size, _eip) \
476({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200477 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400478 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 goto done; \
480 (_eip) += (_size); \
481 (_type)_x; \
482})
483
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800484static inline unsigned long ad_mask(struct decode_cache *c)
485{
486 return (1UL << (c->ad_bytes << 3)) - 1;
487}
488
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800490static inline unsigned long
491address_mask(struct decode_cache *c, unsigned long reg)
492{
493 if (c->ad_bytes == sizeof(unsigned long))
494 return reg;
495 else
496 return reg & ad_mask(c);
497}
498
499static inline unsigned long
500register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
501{
502 return base + address_mask(c, reg);
503}
504
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505static inline void
506register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
507{
508 if (c->ad_bytes == sizeof(unsigned long))
509 *reg += inc;
510 else
511 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
512}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513
Harvey Harrison7a9572752008-02-19 07:40:41 -0800514static inline void jmp_rel(struct decode_cache *c, int rel)
515{
516 register_address_increment(c, &c->eip, rel);
517}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300518
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300519static void set_seg_override(struct decode_cache *c, int seg)
520{
521 c->has_seg_override = true;
522 c->seg_override = seg;
523}
524
525static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
526{
527 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
528 return 0;
529
530 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
531}
532
533static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
534 struct decode_cache *c)
535{
536 if (!c->has_seg_override)
537 return 0;
538
539 return seg_base(ctxt, c->seg_override);
540}
541
542static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
543{
544 return seg_base(ctxt, VCPU_SREG_ES);
545}
546
547static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
548{
549 return seg_base(ctxt, VCPU_SREG_SS);
550}
551
Avi Kivity62266862007-11-20 13:15:52 +0200552static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
553 struct x86_emulate_ops *ops,
554 unsigned long linear, u8 *dest)
555{
556 struct fetch_cache *fc = &ctxt->decode.fetch;
557 int rc;
558 int size;
559
560 if (linear < fc->start || linear >= fc->end) {
561 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
562 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
563 if (rc)
564 return rc;
565 fc->start = linear;
566 fc->end = linear + size;
567 }
568 *dest = fc->data[linear - fc->start];
569 return 0;
570}
571
572static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
573 struct x86_emulate_ops *ops,
574 unsigned long eip, void *dest, unsigned size)
575{
576 int rc = 0;
577
578 eip += ctxt->cs_base;
579 while (size--) {
580 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
581 if (rc)
582 return rc;
583 }
584 return 0;
585}
586
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000587/*
588 * Given the 'reg' portion of a ModRM byte, and a register block, return a
589 * pointer into the block that addresses the relevant register.
590 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
591 */
592static void *decode_register(u8 modrm_reg, unsigned long *regs,
593 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594{
595 void *p;
596
597 p = &regs[modrm_reg];
598 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
599 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
600 return p;
601}
602
603static int read_descriptor(struct x86_emulate_ctxt *ctxt,
604 struct x86_emulate_ops *ops,
605 void *ptr,
606 u16 *size, unsigned long *address, int op_bytes)
607{
608 int rc;
609
610 if (op_bytes == 2)
611 op_bytes = 3;
612 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300613 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
614 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 if (rc)
616 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300617 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
618 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 return rc;
620}
621
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300622static int test_cc(unsigned int condition, unsigned int flags)
623{
624 int rc = 0;
625
626 switch ((condition & 15) >> 1) {
627 case 0: /* o */
628 rc |= (flags & EFLG_OF);
629 break;
630 case 1: /* b/c/nae */
631 rc |= (flags & EFLG_CF);
632 break;
633 case 2: /* z/e */
634 rc |= (flags & EFLG_ZF);
635 break;
636 case 3: /* be/na */
637 rc |= (flags & (EFLG_CF|EFLG_ZF));
638 break;
639 case 4: /* s */
640 rc |= (flags & EFLG_SF);
641 break;
642 case 5: /* p/pe */
643 rc |= (flags & EFLG_PF);
644 break;
645 case 7: /* le/ng */
646 rc |= (flags & EFLG_ZF);
647 /* fall through */
648 case 6: /* l/nge */
649 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
650 break;
651 }
652
653 /* Odd condition identifiers (lsb == 1) have inverted sense. */
654 return (!!rc ^ (condition & 1));
655}
656
Avi Kivity3c118e22007-10-31 10:27:04 +0200657static void decode_register_operand(struct operand *op,
658 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200659 int inhibit_bytereg)
660{
Avi Kivity33615aa2007-10-31 11:15:56 +0200661 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200662 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200663
664 if (!(c->d & ModRM))
665 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200666 op->type = OP_REG;
667 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200668 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200669 op->val = *(u8 *)op->ptr;
670 op->bytes = 1;
671 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200672 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200673 op->bytes = c->op_bytes;
674 switch (op->bytes) {
675 case 2:
676 op->val = *(u16 *)op->ptr;
677 break;
678 case 4:
679 op->val = *(u32 *)op->ptr;
680 break;
681 case 8:
682 op->val = *(u64 *) op->ptr;
683 break;
684 }
685 }
686 op->orig_val = op->val;
687}
688
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200689static int decode_modrm(struct x86_emulate_ctxt *ctxt,
690 struct x86_emulate_ops *ops)
691{
692 struct decode_cache *c = &ctxt->decode;
693 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700694 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200695 int rc = 0;
696
697 if (c->rex_prefix) {
698 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
699 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
700 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
701 }
702
703 c->modrm = insn_fetch(u8, 1, c->eip);
704 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
705 c->modrm_reg |= (c->modrm & 0x38) >> 3;
706 c->modrm_rm |= (c->modrm & 0x07);
707 c->modrm_ea = 0;
708 c->use_modrm_ea = 1;
709
710 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300711 c->modrm_ptr = decode_register(c->modrm_rm,
712 c->regs, c->d & ByteOp);
713 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 return rc;
715 }
716
717 if (c->ad_bytes == 2) {
718 unsigned bx = c->regs[VCPU_REGS_RBX];
719 unsigned bp = c->regs[VCPU_REGS_RBP];
720 unsigned si = c->regs[VCPU_REGS_RSI];
721 unsigned di = c->regs[VCPU_REGS_RDI];
722
723 /* 16-bit ModR/M decode. */
724 switch (c->modrm_mod) {
725 case 0:
726 if (c->modrm_rm == 6)
727 c->modrm_ea += insn_fetch(u16, 2, c->eip);
728 break;
729 case 1:
730 c->modrm_ea += insn_fetch(s8, 1, c->eip);
731 break;
732 case 2:
733 c->modrm_ea += insn_fetch(u16, 2, c->eip);
734 break;
735 }
736 switch (c->modrm_rm) {
737 case 0:
738 c->modrm_ea += bx + si;
739 break;
740 case 1:
741 c->modrm_ea += bx + di;
742 break;
743 case 2:
744 c->modrm_ea += bp + si;
745 break;
746 case 3:
747 c->modrm_ea += bp + di;
748 break;
749 case 4:
750 c->modrm_ea += si;
751 break;
752 case 5:
753 c->modrm_ea += di;
754 break;
755 case 6:
756 if (c->modrm_mod != 0)
757 c->modrm_ea += bp;
758 break;
759 case 7:
760 c->modrm_ea += bx;
761 break;
762 }
763 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
764 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300765 if (!c->has_seg_override)
766 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200767 c->modrm_ea = (u16)c->modrm_ea;
768 } else {
769 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700770 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200771 sib = insn_fetch(u8, 1, c->eip);
772 index_reg |= (sib >> 3) & 7;
773 base_reg |= sib & 7;
774 scale = sib >> 6;
775
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700776 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
777 c->modrm_ea += insn_fetch(s32, 4, c->eip);
778 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200779 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700780 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200781 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700782 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
783 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700784 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700785 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200787 switch (c->modrm_mod) {
788 case 0:
789 if (c->modrm_rm == 5)
790 c->modrm_ea += insn_fetch(s32, 4, c->eip);
791 break;
792 case 1:
793 c->modrm_ea += insn_fetch(s8, 1, c->eip);
794 break;
795 case 2:
796 c->modrm_ea += insn_fetch(s32, 4, c->eip);
797 break;
798 }
799 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200800done:
801 return rc;
802}
803
804static int decode_abs(struct x86_emulate_ctxt *ctxt,
805 struct x86_emulate_ops *ops)
806{
807 struct decode_cache *c = &ctxt->decode;
808 int rc = 0;
809
810 switch (c->ad_bytes) {
811 case 2:
812 c->modrm_ea = insn_fetch(u16, 2, c->eip);
813 break;
814 case 4:
815 c->modrm_ea = insn_fetch(u32, 4, c->eip);
816 break;
817 case 8:
818 c->modrm_ea = insn_fetch(u64, 8, c->eip);
819 break;
820 }
821done:
822 return rc;
823}
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200826x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200828 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200831 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
833 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834
Laurent Viviere4e03de2007-09-18 11:52:50 +0200835 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300836 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300837 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800838 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
840 switch (mode) {
841 case X86EMUL_MODE_REAL:
842 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200843 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 break;
845 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200846 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800848#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200850 def_op_bytes = 4;
851 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 break;
853#endif
854 default:
855 return -1;
856 }
857
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200858 c->op_bytes = def_op_bytes;
859 c->ad_bytes = def_ad_bytes;
860
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200862 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200863 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200865 /* switch between 2/4 bytes */
866 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 break;
868 case 0x67: /* address-size override */
869 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200870 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200871 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200873 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200874 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300877 case 0x2e: /* CS override */
878 case 0x36: /* SS override */
879 case 0x3e: /* DS override */
880 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
882 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300884 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200886 case 0x40 ... 0x4f: /* REX */
887 if (mode != X86EMUL_MODE_PROT64)
888 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200889 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200890 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200894 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100895 c->rep_prefix = REPNE_PREFIX;
896 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100898 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 default:
901 goto done_prefixes;
902 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200903
904 /* Any legacy prefix after a REX prefix nullifies its effect. */
905
Avi Kivity33615aa2007-10-31 11:15:56 +0200906 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 }
908
909done_prefixes:
910
911 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200912 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200913 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200914 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
916 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200917 c->d = opcode_table[c->b];
918 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200920 if (c->b == 0x0f) {
921 c->twobyte = 1;
922 c->b = insn_fetch(u8, 1, c->eip);
923 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200925 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
Avi Kivitye09d0822008-01-18 12:38:59 +0200927 if (c->d & Group) {
928 group = c->d & GroupMask;
929 c->modrm = insn_fetch(u8, 1, c->eip);
930 --c->eip;
931
932 group = (group << 3) + ((c->modrm >> 3) & 7);
933 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
934 c->d = group2_table[group];
935 else
936 c->d = group_table[group];
937 }
938
939 /* Unrecognised? */
940 if (c->d == 0) {
941 DPRINTF("Cannot emulate %02x\n", c->b);
942 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 }
944
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200945 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
946 c->op_bytes = 8;
947
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 if (c->d & ModRM)
950 rc = decode_modrm(ctxt, ops);
951 else if (c->d & MemAbs)
952 rc = decode_abs(ctxt, ops);
953 if (rc)
954 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300956 if (!c->has_seg_override)
957 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200958
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300959 if (!(!c->twobyte && c->b == 0x8d))
960 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200961
962 if (c->ad_bytes != 8)
963 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 /*
965 * Decode and fetch the source operand: register, memory
966 * or immediate.
967 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 case SrcNone:
970 break;
971 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200972 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 break;
974 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200975 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 goto srcmem_common;
977 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200978 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 goto srcmem_common;
980 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200981 c->src.bytes = (c->d & ByteOp) ? 1 :
982 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300983 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400984 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300985 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400986 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200987 /*
988 * For instructions with a ModR/M byte, switch to register
989 * access if Mod = 3.
990 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200991 if ((c->d & ModRM) && c->modrm_mod == 3) {
992 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +0300993 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +0300994 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200995 break;
996 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200997 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 break;
999 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001000 c->src.type = OP_IMM;
1001 c->src.ptr = (unsigned long *)c->eip;
1002 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1003 if (c->src.bytes == 8)
1004 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001006 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 break;
1010 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 break;
1013 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 break;
1016 }
1017 break;
1018 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001019 c->src.type = OP_IMM;
1020 c->src.ptr = (unsigned long *)c->eip;
1021 c->src.bytes = 1;
1022 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 break;
1024 }
1025
Avi Kivity038e51d2007-01-22 20:40:40 -08001026 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001027 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001028 case ImplicitOps:
1029 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001030 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001031 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001032 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001033 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001034 break;
1035 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001036 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001037 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001038 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001039 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001040 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001041 break;
1042 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001043 c->dst.type = OP_MEM;
1044 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001045 case DstAcc:
1046 c->dst.type = OP_REG;
1047 c->dst.bytes = c->op_bytes;
1048 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1049 switch (c->op_bytes) {
1050 case 1:
1051 c->dst.val = *(u8 *)c->dst.ptr;
1052 break;
1053 case 2:
1054 c->dst.val = *(u16 *)c->dst.ptr;
1055 break;
1056 case 4:
1057 c->dst.val = *(u32 *)c->dst.ptr;
1058 break;
1059 }
1060 c->dst.orig_val = c->dst.val;
1061 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001062 }
1063
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001064 if (c->rip_relative)
1065 c->modrm_ea += c->eip;
1066
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001067done:
1068 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1069}
1070
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001071static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1072{
1073 struct decode_cache *c = &ctxt->decode;
1074
1075 c->dst.type = OP_MEM;
1076 c->dst.bytes = c->op_bytes;
1077 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001078 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001079 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001080 c->regs[VCPU_REGS_RSP]);
1081}
1082
1083static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1084 struct x86_emulate_ops *ops)
1085{
1086 struct decode_cache *c = &ctxt->decode;
1087 int rc;
1088
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001089 rc = ops->read_std(register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001090 c->regs[VCPU_REGS_RSP]),
1091 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1092 if (rc != 0)
1093 return rc;
1094
Harvey Harrison7a9572752008-02-19 07:40:41 -08001095 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096
1097 return 0;
1098}
1099
Laurent Vivier05f086f2007-09-24 11:10:55 +02001100static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001101{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001102 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001103 switch (c->modrm_reg) {
1104 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001105 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001106 break;
1107 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001108 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109 break;
1110 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001111 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001112 break;
1113 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001114 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115 break;
1116 case 4: /* sal/shl */
1117 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001118 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001119 break;
1120 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001121 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001122 break;
1123 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001124 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001125 break;
1126 }
1127}
1128
1129static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001130 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001131{
1132 struct decode_cache *c = &ctxt->decode;
1133 int rc = 0;
1134
1135 switch (c->modrm_reg) {
1136 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001137 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001138 break;
1139 case 2: /* not */
1140 c->dst.val = ~c->dst.val;
1141 break;
1142 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001143 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001144 break;
1145 default:
1146 DPRINTF("Cannot emulate %02x\n", c->b);
1147 rc = X86EMUL_UNHANDLEABLE;
1148 break;
1149 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150 return rc;
1151}
1152
1153static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001154 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155{
1156 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001157
1158 switch (c->modrm_reg) {
1159 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001160 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001161 break;
1162 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001163 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001165 case 2: /* call near abs */ {
1166 long int old_eip;
1167 old_eip = c->eip;
1168 c->eip = c->src.val;
1169 c->src.val = old_eip;
1170 emulate_push(ctxt);
1171 break;
1172 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001173 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001174 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175 break;
1176 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001177 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001179 }
1180 return 0;
1181}
1182
1183static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1184 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001185 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001186{
1187 struct decode_cache *c = &ctxt->decode;
1188 u64 old, new;
1189 int rc;
1190
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001191 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001192 if (rc != 0)
1193 return rc;
1194
1195 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1196 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1197
1198 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1199 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001200 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001201
1202 } else {
1203 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1204 (u32) c->regs[VCPU_REGS_RBX];
1205
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001206 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001207 if (rc != 0)
1208 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001209 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001210 }
1211 return 0;
1212}
1213
1214static inline int writeback(struct x86_emulate_ctxt *ctxt,
1215 struct x86_emulate_ops *ops)
1216{
1217 int rc;
1218 struct decode_cache *c = &ctxt->decode;
1219
1220 switch (c->dst.type) {
1221 case OP_REG:
1222 /* The 4-byte case *is* correct:
1223 * in 64-bit mode we zero-extend.
1224 */
1225 switch (c->dst.bytes) {
1226 case 1:
1227 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1228 break;
1229 case 2:
1230 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1231 break;
1232 case 4:
1233 *c->dst.ptr = (u32)c->dst.val;
1234 break; /* 64b: zero-ext */
1235 case 8:
1236 *c->dst.ptr = c->dst.val;
1237 break;
1238 }
1239 break;
1240 case OP_MEM:
1241 if (c->lock_prefix)
1242 rc = ops->cmpxchg_emulated(
1243 (unsigned long)c->dst.ptr,
1244 &c->dst.orig_val,
1245 &c->dst.val,
1246 c->dst.bytes,
1247 ctxt->vcpu);
1248 else
1249 rc = ops->write_emulated(
1250 (unsigned long)c->dst.ptr,
1251 &c->dst.val,
1252 c->dst.bytes,
1253 ctxt->vcpu);
1254 if (rc != 0)
1255 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001256 break;
1257 case OP_NONE:
1258 /* no writeback */
1259 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001260 default:
1261 break;
1262 }
1263 return 0;
1264}
1265
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001266int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001267x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001268{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001269 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001270 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001271 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001272 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001273 unsigned int port;
1274 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001275 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001276
Laurent Vivier34273182007-09-18 11:27:37 +02001277 /* Shadow copy of register state. Committed on successful emulation.
1278 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1279 * modify them.
1280 */
1281
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001282 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001283 saved_eip = c->eip;
1284
Avi Kivityc7e75a32007-10-28 16:34:25 +02001285 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001286 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001287
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001288 if (c->rep_prefix && (c->d & String)) {
1289 /* All REP prefixes have the same first termination condition */
1290 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001291 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001292 goto done;
1293 }
1294 /* The second termination condition only applies for REPE
1295 * and REPNE. Test if the repeat string operation prefix is
1296 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1297 * corresponding termination condition according to:
1298 * - if REPE/REPZ and ZF = 0 then done
1299 * - if REPNE/REPNZ and ZF = 1 then done
1300 */
1301 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1302 (c->b == 0xae) || (c->b == 0xaf)) {
1303 if ((c->rep_prefix == REPE_PREFIX) &&
1304 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001305 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001306 goto done;
1307 }
1308 if ((c->rep_prefix == REPNE_PREFIX) &&
1309 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001310 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001311 goto done;
1312 }
1313 }
1314 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001315 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001316 }
1317
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001318 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001319 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001320 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001321 rc = ops->read_emulated((unsigned long)c->src.ptr,
1322 &c->src.val,
1323 c->src.bytes,
1324 ctxt->vcpu);
1325 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001326 goto done;
1327 c->src.orig_val = c->src.val;
1328 }
1329
1330 if ((c->d & DstMask) == ImplicitOps)
1331 goto special_insn;
1332
1333
1334 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001335 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001336 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1337 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001338 if (c->d & BitOp) {
1339 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001340
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 c->dst.ptr = (void *)c->dst.ptr +
1342 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001343 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001344 if (!(c->d & Mov) &&
1345 /* optimisation - avoid slow emulated read */
1346 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1347 &c->dst.val,
1348 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001349 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001350 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001351 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001352
Avi Kivity018a98d2007-11-27 19:30:56 +02001353special_insn:
1354
Laurent Viviere4e03de2007-09-18 11:52:50 +02001355 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 goto twobyte_insn;
1357
Laurent Viviere4e03de2007-09-18 11:52:50 +02001358 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 case 0x00 ... 0x05:
1360 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001361 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 break;
1363 case 0x08 ... 0x0d:
1364 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001365 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 break;
1367 case 0x10 ... 0x15:
1368 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001369 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 break;
1371 case 0x18 ... 0x1d:
1372 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001373 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001375 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001377 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 break;
1379 case 0x28 ... 0x2d:
1380 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001381 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 break;
1383 case 0x30 ... 0x35:
1384 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 break;
1387 case 0x38 ... 0x3d:
1388 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001391 case 0x40 ... 0x47: /* inc r16/r32 */
1392 emulate_1op("inc", c->dst, ctxt->eflags);
1393 break;
1394 case 0x48 ... 0x4f: /* dec r16/r32 */
1395 emulate_1op("dec", c->dst, ctxt->eflags);
1396 break;
1397 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001398 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001399 break;
1400 case 0x58 ... 0x5f: /* pop reg */
1401 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001402 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001403 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1404 c->op_bytes, ctxt->vcpu)) != 0)
1405 goto done;
1406
Harvey Harrison7a9572752008-02-19 07:40:41 -08001407 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001408 c->op_bytes);
1409 c->dst.type = OP_NONE; /* Disable writeback. */
1410 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001412 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001414 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001416 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001417 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001418 emulate_push(ctxt);
1419 break;
1420 case 0x6c: /* insb */
1421 case 0x6d: /* insw/insd */
1422 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1423 1,
1424 (c->d & ByteOp) ? 1 : c->op_bytes,
1425 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001426 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001427 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001428 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001429 c->regs[VCPU_REGS_RDI]),
1430 c->rep_prefix,
1431 c->regs[VCPU_REGS_RDX]) == 0) {
1432 c->eip = saved_eip;
1433 return -1;
1434 }
1435 return 0;
1436 case 0x6e: /* outsb */
1437 case 0x6f: /* outsw/outsd */
1438 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1439 0,
1440 (c->d & ByteOp) ? 1 : c->op_bytes,
1441 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001442 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001443 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001444 register_address(c,
1445 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001446 c->regs[VCPU_REGS_RSI]),
1447 c->rep_prefix,
1448 c->regs[VCPU_REGS_RDX]) == 0) {
1449 c->eip = saved_eip;
1450 return -1;
1451 }
1452 return 0;
1453 case 0x70 ... 0x7f: /* jcc (short) */ {
1454 int rel = insn_fetch(s8, 1, c->eip);
1455
1456 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001457 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001458 break;
1459 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001461 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 case 0:
1463 goto add;
1464 case 1:
1465 goto or;
1466 case 2:
1467 goto adc;
1468 case 3:
1469 goto sbb;
1470 case 4:
1471 goto and;
1472 case 5:
1473 goto sub;
1474 case 6:
1475 goto xor;
1476 case 7:
1477 goto cmp;
1478 }
1479 break;
1480 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001481 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 break;
1483 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001484 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001486 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001488 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 break;
1490 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001491 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 break;
1493 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001494 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 break; /* 64b reg: zero-extend */
1496 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001497 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 break;
1499 }
1500 /*
1501 * Write back the memory destination with implicit LOCK
1502 * prefix.
1503 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001504 c->dst.val = c->src.val;
1505 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001508 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001509 case 0x8c: { /* mov r/m, sreg */
1510 struct kvm_segment segreg;
1511
1512 if (c->modrm_reg <= 5)
1513 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1514 else {
1515 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1516 c->modrm);
1517 goto cannot_emulate;
1518 }
1519 c->dst.val = segreg.selector;
1520 break;
1521 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001522 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001523 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001524 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001525 case 0x8e: { /* mov seg, r/m16 */
1526 uint16_t sel;
1527 int type_bits;
1528 int err;
1529
1530 sel = c->src.val;
1531 if (c->modrm_reg <= 5) {
1532 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1533 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1534 type_bits, c->modrm_reg);
1535 } else {
1536 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1537 c->modrm);
1538 goto cannot_emulate;
1539 }
1540
1541 if (err < 0)
1542 goto cannot_emulate;
1543
1544 c->dst.type = OP_NONE; /* Disable writeback. */
1545 break;
1546 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001548 rc = emulate_grp1a(ctxt, ops);
1549 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001552 case 0x90: /* nop / xchg r8,rax */
1553 if (!(c->rex_prefix & 1)) { /* nop */
1554 c->dst.type = OP_NONE;
1555 break;
1556 }
1557 case 0x91 ... 0x97: /* xchg reg,rax */
1558 c->src.type = c->dst.type = OP_REG;
1559 c->src.bytes = c->dst.bytes = c->op_bytes;
1560 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1561 c->src.val = *(c->src.ptr);
1562 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001563 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001564 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001565 emulate_push(ctxt);
1566 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001567 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001568 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001569 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001570 case 0xa0 ... 0xa1: /* mov */
1571 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1572 c->dst.val = c->src.val;
1573 break;
1574 case 0xa2 ... 0xa3: /* mov */
1575 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1576 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001578 c->dst.type = OP_MEM;
1579 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001580 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001581 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001582 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001583 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001584 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 c->regs[VCPU_REGS_RSI]),
1586 &c->dst.val,
1587 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001589 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001590 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001591 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001592 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001593 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001594 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 break;
1596 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001597 c->src.type = OP_NONE; /* Disable writeback. */
1598 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001599 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001600 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001601 c->regs[VCPU_REGS_RSI]);
1602 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1603 &c->src.val,
1604 c->src.bytes,
1605 ctxt->vcpu)) != 0)
1606 goto done;
1607
1608 c->dst.type = OP_NONE; /* Disable writeback. */
1609 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001610 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001611 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001612 c->regs[VCPU_REGS_RDI]);
1613 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1614 &c->dst.val,
1615 c->dst.bytes,
1616 ctxt->vcpu)) != 0)
1617 goto done;
1618
1619 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1620
1621 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1622
Harvey Harrison7a9572752008-02-19 07:40:41 -08001623 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001624 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1625 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001626 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001627 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1628 : c->dst.bytes);
1629
1630 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001632 c->dst.type = OP_MEM;
1633 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001634 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001635 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001636 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001637 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001638 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001639 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001640 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641 break;
1642 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001643 c->dst.type = OP_REG;
1644 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1645 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001646 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001647 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001648 c->regs[VCPU_REGS_RSI]),
1649 &c->dst.val,
1650 c->dst.bytes,
1651 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001653 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001654 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001655 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 break;
1657 case 0xae ... 0xaf: /* scas */
1658 DPRINTF("Urk! I don't handle SCAS.\n");
1659 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001660 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001661 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001662 case 0xc0 ... 0xc1:
1663 emulate_grp2(ctxt);
1664 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001665 case 0xc3: /* ret */
1666 c->dst.ptr = &c->eip;
1667 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001668 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1669 mov:
1670 c->dst.val = c->src.val;
1671 break;
1672 case 0xd0 ... 0xd1: /* Grp2 */
1673 c->src.val = 1;
1674 emulate_grp2(ctxt);
1675 break;
1676 case 0xd2 ... 0xd3: /* Grp2 */
1677 c->src.val = c->regs[VCPU_REGS_RCX];
1678 emulate_grp2(ctxt);
1679 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001680 case 0xe4: /* inb */
1681 case 0xe5: /* in */
1682 port = insn_fetch(u8, 1, c->eip);
1683 io_dir_in = 1;
1684 goto do_io;
1685 case 0xe6: /* outb */
1686 case 0xe7: /* out */
1687 port = insn_fetch(u8, 1, c->eip);
1688 io_dir_in = 0;
1689 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001690 case 0xe8: /* call (near) */ {
1691 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001692 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001693 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001694 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001695 break;
1696 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001697 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001698 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001699 default:
1700 DPRINTF("Call: Invalid op_bytes\n");
1701 goto cannot_emulate;
1702 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001703 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001704 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001705 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706 emulate_push(ctxt);
1707 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001708 }
1709 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001710 goto jmp;
1711 case 0xea: /* jmp far */ {
1712 uint32_t eip;
1713 uint16_t sel;
1714
1715 switch (c->op_bytes) {
1716 case 2:
1717 eip = insn_fetch(u16, 2, c->eip);
1718 break;
1719 case 4:
1720 eip = insn_fetch(u32, 4, c->eip);
1721 break;
1722 default:
1723 DPRINTF("jmp far: Invalid op_bytes\n");
1724 goto cannot_emulate;
1725 }
1726 sel = insn_fetch(u16, 2, c->eip);
1727 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1728 DPRINTF("jmp far: Failed to load CS descriptor\n");
1729 goto cannot_emulate;
1730 }
1731
1732 c->eip = eip;
1733 break;
1734 }
1735 case 0xeb:
1736 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001737 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001738 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001739 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001740 case 0xec: /* in al,dx */
1741 case 0xed: /* in (e/r)ax,dx */
1742 port = c->regs[VCPU_REGS_RDX];
1743 io_dir_in = 1;
1744 goto do_io;
1745 case 0xee: /* out al,dx */
1746 case 0xef: /* out (e/r)ax,dx */
1747 port = c->regs[VCPU_REGS_RDX];
1748 io_dir_in = 0;
1749 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1750 (c->d & ByteOp) ? 1 : c->op_bytes,
1751 port) != 0) {
1752 c->eip = saved_eip;
1753 goto cannot_emulate;
1754 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001755 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001756 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001757 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001758 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001759 case 0xf5: /* cmc */
1760 /* complement carry flag from eflags reg */
1761 ctxt->eflags ^= EFLG_CF;
1762 c->dst.type = OP_NONE; /* Disable writeback. */
1763 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001764 case 0xf6 ... 0xf7: /* Grp3 */
1765 rc = emulate_grp3(ctxt, ops);
1766 if (rc != 0)
1767 goto done;
1768 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001769 case 0xf8: /* clc */
1770 ctxt->eflags &= ~EFLG_CF;
1771 c->dst.type = OP_NONE; /* Disable writeback. */
1772 break;
1773 case 0xfa: /* cli */
1774 ctxt->eflags &= ~X86_EFLAGS_IF;
1775 c->dst.type = OP_NONE; /* Disable writeback. */
1776 break;
1777 case 0xfb: /* sti */
1778 ctxt->eflags |= X86_EFLAGS_IF;
1779 c->dst.type = OP_NONE; /* Disable writeback. */
1780 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001781 case 0xfc: /* cld */
1782 ctxt->eflags &= ~EFLG_DF;
1783 c->dst.type = OP_NONE; /* Disable writeback. */
1784 break;
1785 case 0xfd: /* std */
1786 ctxt->eflags |= EFLG_DF;
1787 c->dst.type = OP_NONE; /* Disable writeback. */
1788 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001789 case 0xfe ... 0xff: /* Grp4/Grp5 */
1790 rc = emulate_grp45(ctxt, ops);
1791 if (rc != 0)
1792 goto done;
1793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001795
1796writeback:
1797 rc = writeback(ctxt, ops);
1798 if (rc != 0)
1799 goto done;
1800
1801 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001802 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001803 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001804
1805done:
1806 if (rc == X86EMUL_UNHANDLEABLE) {
1807 c->eip = saved_eip;
1808 return -1;
1809 }
1810 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811
1812twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001813 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001815 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 u16 size;
1817 unsigned long address;
1818
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001819 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001820 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001821 goto cannot_emulate;
1822
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001823 rc = kvm_fix_hypercall(ctxt->vcpu);
1824 if (rc)
1825 goto done;
1826
Avi Kivity33e38852008-05-21 15:34:25 +03001827 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001828 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001829 /* Disable writeback. */
1830 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001831 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001833 rc = read_descriptor(ctxt, ops, c->src.ptr,
1834 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 if (rc)
1836 goto done;
1837 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001838 /* Disable writeback. */
1839 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001841 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001842 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001843 rc = kvm_fix_hypercall(ctxt->vcpu);
1844 if (rc)
1845 goto done;
1846 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001847 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001848 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001849 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001850 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001851 if (rc)
1852 goto done;
1853 realmode_lidt(ctxt->vcpu, size, address);
1854 }
Avi Kivity16286d02008-04-14 14:40:50 +03001855 /* Disable writeback. */
1856 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 break;
1858 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001859 c->dst.bytes = 2;
1860 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 break;
1862 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001863 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1864 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001865 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 break;
1867 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001868 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001869 /* Disable writeback. */
1870 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871 break;
1872 default:
1873 goto cannot_emulate;
1874 }
1875 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001876 case 0x06:
1877 emulate_clts(ctxt->vcpu);
1878 c->dst.type = OP_NONE;
1879 break;
1880 case 0x08: /* invd */
1881 case 0x09: /* wbinvd */
1882 case 0x0d: /* GrpP (prefetch) */
1883 case 0x18: /* Grp16 (prefetch/nop) */
1884 c->dst.type = OP_NONE;
1885 break;
1886 case 0x20: /* mov cr, reg */
1887 if (c->modrm_mod != 3)
1888 goto cannot_emulate;
1889 c->regs[c->modrm_rm] =
1890 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1891 c->dst.type = OP_NONE; /* no writeback */
1892 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001894 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001896 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001897 if (rc)
1898 goto cannot_emulate;
1899 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001901 case 0x22: /* mov reg, cr */
1902 if (c->modrm_mod != 3)
1903 goto cannot_emulate;
1904 realmode_set_cr(ctxt->vcpu,
1905 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1906 c->dst.type = OP_NONE;
1907 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001909 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001911 rc = emulator_set_dr(ctxt, c->modrm_reg,
1912 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001913 if (rc)
1914 goto cannot_emulate;
1915 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001917 case 0x30:
1918 /* wrmsr */
1919 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1920 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1921 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1922 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001923 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001924 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001925 }
1926 rc = X86EMUL_CONTINUE;
1927 c->dst.type = OP_NONE;
1928 break;
1929 case 0x32:
1930 /* rdmsr */
1931 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1932 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001933 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001934 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001935 } else {
1936 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1937 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1938 }
1939 rc = X86EMUL_CONTINUE;
1940 c->dst.type = OP_NONE;
1941 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001943 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001944 if (!test_cc(c->b, ctxt->eflags))
1945 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001947 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1948 long int rel;
1949
1950 switch (c->op_bytes) {
1951 case 2:
1952 rel = insn_fetch(s16, 2, c->eip);
1953 break;
1954 case 4:
1955 rel = insn_fetch(s32, 4, c->eip);
1956 break;
1957 case 8:
1958 rel = insn_fetch(s64, 8, c->eip);
1959 break;
1960 default:
1961 DPRINTF("jnz: Invalid op_bytes\n");
1962 goto cannot_emulate;
1963 }
1964 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001965 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001966 c->dst.type = OP_NONE;
1967 break;
1968 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001969 case 0xa3:
1970 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001971 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001972 /* only subword offset */
1973 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001974 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001975 break;
1976 case 0xab:
1977 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001978 /* only subword offset */
1979 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001980 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001981 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03001982 case 0xae: /* clflush */
1983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 case 0xb0 ... 0xb1: /* cmpxchg */
1985 /*
1986 * Save real source value, then compare EAX against
1987 * destination.
1988 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001989 c->src.orig_val = c->src.val;
1990 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001991 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1992 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001994 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 } else {
1996 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001997 c->dst.type = OP_REG;
1998 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999 }
2000 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001 case 0xb3:
2002 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002003 /* only subword offset */
2004 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002005 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002008 c->dst.bytes = c->op_bytes;
2009 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2010 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002013 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 case 0:
2015 goto bt;
2016 case 1:
2017 goto bts;
2018 case 2:
2019 goto btr;
2020 case 3:
2021 goto btc;
2022 }
2023 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002024 case 0xbb:
2025 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002026 /* only subword offset */
2027 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002028 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002029 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002031 c->dst.bytes = c->op_bytes;
2032 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2033 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002035 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002036 c->dst.bytes = c->op_bytes;
2037 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2038 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002039 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002041 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002042 if (rc != 0)
2043 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002044 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002045 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046 }
2047 goto writeback;
2048
2049cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002050 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002051 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052 return -1;
2053}