blob: ca91749d2083ff71a44b66f4253bda00dff7f4b3 [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. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010061#define SrcOne (7<<4) /* Implied '1' */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020062#define SrcMask (7<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080063/* Generic ModRM decode. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020064#define ModRM (1<<7)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065/* Destination is only written; never read. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020066#define Mov (1<<8)
67#define BitOp (1<<9)
68#define MemAbs (1<<10) /* Memory operand is absolute displacement */
69#define String (1<<12) /* String instruction (rep capable) */
70#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020071#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
72#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
73#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010074/* Source 2 operand type */
75#define Src2None (0<<29)
76#define Src2CL (1<<29)
77#define Src2ImmByte (2<<29)
78#define Src2One (3<<29)
79#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080080
Avi Kivity43bb19c2008-01-18 12:46:50 +020081enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020082 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020083 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020084};
85
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010086static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080087 /* 0x00 - 0x07 */
88 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
89 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020090 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080091 /* 0x08 - 0x0F */
92 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
93 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
94 0, 0, 0, 0,
95 /* 0x10 - 0x17 */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
98 0, 0, 0, 0,
99 /* 0x18 - 0x1F */
100 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
101 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
102 0, 0, 0, 0,
103 /* 0x20 - 0x27 */
104 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
105 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200106 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 /* 0x28 - 0x2F */
108 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
109 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
110 0, 0, 0, 0,
111 /* 0x30 - 0x37 */
112 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
113 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
114 0, 0, 0, 0,
115 /* 0x38 - 0x3F */
116 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
117 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200118 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
119 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700120 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200121 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700122 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200123 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300124 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200125 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
126 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300127 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200128 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
129 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700130 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700132 0, 0, 0, 0,
133 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300134 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300135 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
136 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300137 /* 0x70 - 0x77 */
138 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
139 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
140 /* 0x78 - 0x7F */
141 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
142 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200144 Group | Group1_80, Group | Group1_81,
145 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
147 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
148 /* 0x88 - 0x8F */
149 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
150 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200151 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200152 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300153 /* 0x90 - 0x97 */
154 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
155 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200156 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800157 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200158 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
159 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200160 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
161 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200163 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
164 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
165 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300166 /* 0xB0 - 0xB7 */
167 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
168 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
169 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
170 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
171 /* 0xB8 - 0xBF */
172 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
173 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
174 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
175 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300177 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200178 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300179 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0xC8 - 0xCF */
Avi Kivitya77ab5e2009-01-05 13:27:34 +0200181 0, 0, 0, ImplicitOps | Stack, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xD0 - 0xD7 */
183 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
184 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
185 0, 0, 0, 0,
186 /* 0xD8 - 0xDF */
187 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300188 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300189 0, 0, 0, 0,
190 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
191 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300192 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200193 ImplicitOps | Stack, SrcImm | ImplicitOps,
194 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300195 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
196 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 /* 0xF0 - 0xF7 */
198 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200199 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700201 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300202 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203};
204
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100205static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200207 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200208 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0x10 - 0x1F */
210 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
211 /* 0x20 - 0x2F */
212 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
213 0, 0, 0, 0, 0, 0, 0, 0,
214 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300215 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216 /* 0x40 - 0x47 */
217 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
218 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
219 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
220 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
221 /* 0x48 - 0x4F */
222 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
223 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
224 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
225 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
226 /* 0x50 - 0x5F */
227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228 /* 0x60 - 0x6F */
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
230 /* 0x70 - 0x7F */
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300233 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
234 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
235 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
236 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 /* 0x90 - 0x9F */
238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100240 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
241 DstMem | SrcReg | Src2ImmByte | ModRM,
242 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100244 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
245 DstMem | SrcReg | Src2ImmByte | ModRM,
246 DstMem | SrcReg | Src2CL | ModRM,
247 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* 0xB0 - 0xB7 */
249 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800250 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem16 | ModRM | Mov,
253 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800254 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
256 DstReg | SrcMem16 | ModRM | Mov,
257 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800258 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
259 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 /* 0xD0 - 0xDF */
261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262 /* 0xE0 - 0xEF */
263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264 /* 0xF0 - 0xFF */
265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
266};
267
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100268static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200269 [Group1_80*8] =
270 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
271 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
272 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
273 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
274 [Group1_81*8] =
275 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
276 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
277 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
278 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
279 [Group1_82*8] =
280 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
281 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
282 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 [Group1_83*8] =
285 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
286 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
287 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
288 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200289 [Group1A*8] =
290 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200291 [Group3_Byte*8] =
292 ByteOp | SrcImm | DstMem | ModRM, 0,
293 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
294 0, 0, 0, 0,
295 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400296 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300297 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200298 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200299 [Group4*8] =
300 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
301 0, 0, 0, 0, 0, 0,
302 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300303 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
304 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300305 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200306 [Group7*8] =
307 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300308 SrcNone | ModRM | DstMem | Mov, 0,
309 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200310};
311
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100312static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200313 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000314 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300315 SrcNone | ModRM | DstMem | Mov, 0,
316 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200317};
318
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319/* EFLAGS bit definitions. */
320#define EFLG_OF (1<<11)
321#define EFLG_DF (1<<10)
322#define EFLG_SF (1<<7)
323#define EFLG_ZF (1<<6)
324#define EFLG_AF (1<<4)
325#define EFLG_PF (1<<2)
326#define EFLG_CF (1<<0)
327
328/*
329 * Instruction emulation:
330 * Most instructions are emulated directly via a fragment of inline assembly
331 * code. This allows us to save/restore EFLAGS and thus very easily pick up
332 * any modified flags.
333 */
334
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800335#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336#define _LO32 "k" /* force 32-bit operand */
337#define _STK "%%rsp" /* stack pointer */
338#elif defined(__i386__)
339#define _LO32 "" /* force 32-bit operand */
340#define _STK "%%esp" /* stack pointer */
341#endif
342
343/*
344 * These EFLAGS bits are restored from saved value during emulation, and
345 * any changes are written back to the saved value after emulation.
346 */
347#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
348
349/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200350#define _PRE_EFLAGS(_sav, _msk, _tmp) \
351 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
352 "movl %"_sav",%"_LO32 _tmp"; " \
353 "push %"_tmp"; " \
354 "push %"_tmp"; " \
355 "movl %"_msk",%"_LO32 _tmp"; " \
356 "andl %"_LO32 _tmp",("_STK"); " \
357 "pushf; " \
358 "notl %"_LO32 _tmp"; " \
359 "andl %"_LO32 _tmp",("_STK"); " \
360 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
361 "pop %"_tmp"; " \
362 "orl %"_LO32 _tmp",("_STK"); " \
363 "popf; " \
364 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365
366/* After executing instruction: write-back necessary bits in EFLAGS. */
367#define _POST_EFLAGS(_sav, _msk, _tmp) \
368 /* _sav |= EFLAGS & _msk; */ \
369 "pushf; " \
370 "pop %"_tmp"; " \
371 "andl %"_msk",%"_LO32 _tmp"; " \
372 "orl %"_LO32 _tmp",%"_sav"; "
373
Avi Kivitydda96d82008-11-26 15:14:10 +0200374#ifdef CONFIG_X86_64
375#define ON64(x) x
376#else
377#define ON64(x)
378#endif
379
Avi Kivity6b7ad612008-11-26 15:30:45 +0200380#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
381 do { \
382 __asm__ __volatile__ ( \
383 _PRE_EFLAGS("0", "4", "2") \
384 _op _suffix " %"_x"3,%1; " \
385 _POST_EFLAGS("0", "4", "2") \
386 : "=m" (_eflags), "=m" ((_dst).val), \
387 "=&r" (_tmp) \
388 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200389 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200390
391
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392/* Raw emulation: instruction has two explicit operands. */
393#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200394 do { \
395 unsigned long _tmp; \
396 \
397 switch ((_dst).bytes) { \
398 case 2: \
399 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
400 break; \
401 case 4: \
402 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
403 break; \
404 case 8: \
405 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
406 break; \
407 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408 } while (0)
409
410#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
411 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200412 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400413 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200415 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 break; \
417 default: \
418 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
419 _wx, _wy, _lx, _ly, _qx, _qy); \
420 break; \
421 } \
422 } while (0)
423
424/* Source operand is byte-sized and may be restricted to just %cl. */
425#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
426 __emulate_2op(_op, _src, _dst, _eflags, \
427 "b", "c", "b", "c", "b", "c", "b", "c")
428
429/* Source operand is byte, word, long or quad sized. */
430#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
431 __emulate_2op(_op, _src, _dst, _eflags, \
432 "b", "q", "w", "r", _LO32, "r", "", "r")
433
434/* Source operand is word, long or quad sized. */
435#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
436 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
437 "w", "r", _LO32, "r", "", "r")
438
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100439/* Instruction has three operands and one operand is stored in ECX register */
440#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
441 do { \
442 unsigned long _tmp; \
443 _type _clv = (_cl).val; \
444 _type _srcv = (_src).val; \
445 _type _dstv = (_dst).val; \
446 \
447 __asm__ __volatile__ ( \
448 _PRE_EFLAGS("0", "5", "2") \
449 _op _suffix " %4,%1 \n" \
450 _POST_EFLAGS("0", "5", "2") \
451 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
452 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
453 ); \
454 \
455 (_cl).val = (unsigned long) _clv; \
456 (_src).val = (unsigned long) _srcv; \
457 (_dst).val = (unsigned long) _dstv; \
458 } while (0)
459
460#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
461 do { \
462 switch ((_dst).bytes) { \
463 case 2: \
464 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
465 "w", unsigned short); \
466 break; \
467 case 4: \
468 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
469 "l", unsigned int); \
470 break; \
471 case 8: \
472 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
473 "q", unsigned long)); \
474 break; \
475 } \
476 } while (0)
477
Avi Kivitydda96d82008-11-26 15:14:10 +0200478#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 do { \
480 unsigned long _tmp; \
481 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200482 __asm__ __volatile__ ( \
483 _PRE_EFLAGS("0", "3", "2") \
484 _op _suffix " %1; " \
485 _POST_EFLAGS("0", "3", "2") \
486 : "=m" (_eflags), "+m" ((_dst).val), \
487 "=&r" (_tmp) \
488 : "i" (EFLAGS_MASK)); \
489 } while (0)
490
491/* Instruction has only one explicit operand (no source operand). */
492#define emulate_1op(_op, _dst, _eflags) \
493 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400494 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200495 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
496 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
497 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
498 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 } \
500 } while (0)
501
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502/* Fetch next part of the instruction being emulated. */
503#define insn_fetch(_type, _size, _eip) \
504({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200505 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400506 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507 goto done; \
508 (_eip) += (_size); \
509 (_type)_x; \
510})
511
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800512static inline unsigned long ad_mask(struct decode_cache *c)
513{
514 return (1UL << (c->ad_bytes << 3)) - 1;
515}
516
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800518static inline unsigned long
519address_mask(struct decode_cache *c, unsigned long reg)
520{
521 if (c->ad_bytes == sizeof(unsigned long))
522 return reg;
523 else
524 return reg & ad_mask(c);
525}
526
527static inline unsigned long
528register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
529{
530 return base + address_mask(c, reg);
531}
532
Harvey Harrison7a9572752008-02-19 07:40:41 -0800533static inline void
534register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
535{
536 if (c->ad_bytes == sizeof(unsigned long))
537 *reg += inc;
538 else
539 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
540}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541
Harvey Harrison7a9572752008-02-19 07:40:41 -0800542static inline void jmp_rel(struct decode_cache *c, int rel)
543{
544 register_address_increment(c, &c->eip, rel);
545}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300546
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300547static void set_seg_override(struct decode_cache *c, int seg)
548{
549 c->has_seg_override = true;
550 c->seg_override = seg;
551}
552
553static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
554{
555 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
556 return 0;
557
558 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
559}
560
561static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
562 struct decode_cache *c)
563{
564 if (!c->has_seg_override)
565 return 0;
566
567 return seg_base(ctxt, c->seg_override);
568}
569
570static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
571{
572 return seg_base(ctxt, VCPU_SREG_ES);
573}
574
575static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
576{
577 return seg_base(ctxt, VCPU_SREG_SS);
578}
579
Avi Kivity62266862007-11-20 13:15:52 +0200580static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
581 struct x86_emulate_ops *ops,
582 unsigned long linear, u8 *dest)
583{
584 struct fetch_cache *fc = &ctxt->decode.fetch;
585 int rc;
586 int size;
587
588 if (linear < fc->start || linear >= fc->end) {
589 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
590 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
591 if (rc)
592 return rc;
593 fc->start = linear;
594 fc->end = linear + size;
595 }
596 *dest = fc->data[linear - fc->start];
597 return 0;
598}
599
600static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
601 struct x86_emulate_ops *ops,
602 unsigned long eip, void *dest, unsigned size)
603{
604 int rc = 0;
605
606 eip += ctxt->cs_base;
607 while (size--) {
608 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
609 if (rc)
610 return rc;
611 }
612 return 0;
613}
614
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000615/*
616 * Given the 'reg' portion of a ModRM byte, and a register block, return a
617 * pointer into the block that addresses the relevant register.
618 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
619 */
620static void *decode_register(u8 modrm_reg, unsigned long *regs,
621 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622{
623 void *p;
624
625 p = &regs[modrm_reg];
626 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
627 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
628 return p;
629}
630
631static int read_descriptor(struct x86_emulate_ctxt *ctxt,
632 struct x86_emulate_ops *ops,
633 void *ptr,
634 u16 *size, unsigned long *address, int op_bytes)
635{
636 int rc;
637
638 if (op_bytes == 2)
639 op_bytes = 3;
640 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300641 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
642 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 if (rc)
644 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300645 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
646 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 return rc;
648}
649
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300650static int test_cc(unsigned int condition, unsigned int flags)
651{
652 int rc = 0;
653
654 switch ((condition & 15) >> 1) {
655 case 0: /* o */
656 rc |= (flags & EFLG_OF);
657 break;
658 case 1: /* b/c/nae */
659 rc |= (flags & EFLG_CF);
660 break;
661 case 2: /* z/e */
662 rc |= (flags & EFLG_ZF);
663 break;
664 case 3: /* be/na */
665 rc |= (flags & (EFLG_CF|EFLG_ZF));
666 break;
667 case 4: /* s */
668 rc |= (flags & EFLG_SF);
669 break;
670 case 5: /* p/pe */
671 rc |= (flags & EFLG_PF);
672 break;
673 case 7: /* le/ng */
674 rc |= (flags & EFLG_ZF);
675 /* fall through */
676 case 6: /* l/nge */
677 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
678 break;
679 }
680
681 /* Odd condition identifiers (lsb == 1) have inverted sense. */
682 return (!!rc ^ (condition & 1));
683}
684
Avi Kivity3c118e22007-10-31 10:27:04 +0200685static void decode_register_operand(struct operand *op,
686 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200687 int inhibit_bytereg)
688{
Avi Kivity33615aa2007-10-31 11:15:56 +0200689 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200690 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200691
692 if (!(c->d & ModRM))
693 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200694 op->type = OP_REG;
695 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200696 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200697 op->val = *(u8 *)op->ptr;
698 op->bytes = 1;
699 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200700 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200701 op->bytes = c->op_bytes;
702 switch (op->bytes) {
703 case 2:
704 op->val = *(u16 *)op->ptr;
705 break;
706 case 4:
707 op->val = *(u32 *)op->ptr;
708 break;
709 case 8:
710 op->val = *(u64 *) op->ptr;
711 break;
712 }
713 }
714 op->orig_val = op->val;
715}
716
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717static int decode_modrm(struct x86_emulate_ctxt *ctxt,
718 struct x86_emulate_ops *ops)
719{
720 struct decode_cache *c = &ctxt->decode;
721 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700722 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200723 int rc = 0;
724
725 if (c->rex_prefix) {
726 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
727 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
728 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
729 }
730
731 c->modrm = insn_fetch(u8, 1, c->eip);
732 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
733 c->modrm_reg |= (c->modrm & 0x38) >> 3;
734 c->modrm_rm |= (c->modrm & 0x07);
735 c->modrm_ea = 0;
736 c->use_modrm_ea = 1;
737
738 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300739 c->modrm_ptr = decode_register(c->modrm_rm,
740 c->regs, c->d & ByteOp);
741 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200742 return rc;
743 }
744
745 if (c->ad_bytes == 2) {
746 unsigned bx = c->regs[VCPU_REGS_RBX];
747 unsigned bp = c->regs[VCPU_REGS_RBP];
748 unsigned si = c->regs[VCPU_REGS_RSI];
749 unsigned di = c->regs[VCPU_REGS_RDI];
750
751 /* 16-bit ModR/M decode. */
752 switch (c->modrm_mod) {
753 case 0:
754 if (c->modrm_rm == 6)
755 c->modrm_ea += insn_fetch(u16, 2, c->eip);
756 break;
757 case 1:
758 c->modrm_ea += insn_fetch(s8, 1, c->eip);
759 break;
760 case 2:
761 c->modrm_ea += insn_fetch(u16, 2, c->eip);
762 break;
763 }
764 switch (c->modrm_rm) {
765 case 0:
766 c->modrm_ea += bx + si;
767 break;
768 case 1:
769 c->modrm_ea += bx + di;
770 break;
771 case 2:
772 c->modrm_ea += bp + si;
773 break;
774 case 3:
775 c->modrm_ea += bp + di;
776 break;
777 case 4:
778 c->modrm_ea += si;
779 break;
780 case 5:
781 c->modrm_ea += di;
782 break;
783 case 6:
784 if (c->modrm_mod != 0)
785 c->modrm_ea += bp;
786 break;
787 case 7:
788 c->modrm_ea += bx;
789 break;
790 }
791 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
792 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300793 if (!c->has_seg_override)
794 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200795 c->modrm_ea = (u16)c->modrm_ea;
796 } else {
797 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700798 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200799 sib = insn_fetch(u8, 1, c->eip);
800 index_reg |= (sib >> 3) & 7;
801 base_reg |= sib & 7;
802 scale = sib >> 6;
803
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700804 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
805 c->modrm_ea += insn_fetch(s32, 4, c->eip);
806 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200807 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700808 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700810 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
811 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700812 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700813 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200814 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200815 switch (c->modrm_mod) {
816 case 0:
817 if (c->modrm_rm == 5)
818 c->modrm_ea += insn_fetch(s32, 4, c->eip);
819 break;
820 case 1:
821 c->modrm_ea += insn_fetch(s8, 1, c->eip);
822 break;
823 case 2:
824 c->modrm_ea += insn_fetch(s32, 4, c->eip);
825 break;
826 }
827 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200828done:
829 return rc;
830}
831
832static int decode_abs(struct x86_emulate_ctxt *ctxt,
833 struct x86_emulate_ops *ops)
834{
835 struct decode_cache *c = &ctxt->decode;
836 int rc = 0;
837
838 switch (c->ad_bytes) {
839 case 2:
840 c->modrm_ea = insn_fetch(u16, 2, c->eip);
841 break;
842 case 4:
843 c->modrm_ea = insn_fetch(u32, 4, c->eip);
844 break;
845 case 8:
846 c->modrm_ea = insn_fetch(u64, 8, c->eip);
847 break;
848 }
849done:
850 return rc;
851}
852
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200854x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200856 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200859 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
861 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862
Laurent Viviere4e03de2007-09-18 11:52:50 +0200863 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300864 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300865 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800866 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867
868 switch (mode) {
869 case X86EMUL_MODE_REAL:
870 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200871 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 break;
873 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200874 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800876#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200878 def_op_bytes = 4;
879 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 break;
881#endif
882 default:
883 return -1;
884 }
885
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200886 c->op_bytes = def_op_bytes;
887 c->ad_bytes = def_ad_bytes;
888
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200890 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200891 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200893 /* switch between 2/4 bytes */
894 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 break;
896 case 0x67: /* address-size override */
897 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200898 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200899 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200902 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300905 case 0x2e: /* CS override */
906 case 0x36: /* SS override */
907 case 0x3e: /* DS override */
908 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 break;
910 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300912 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200914 case 0x40 ... 0x4f: /* REX */
915 if (mode != X86EMUL_MODE_PROT64)
916 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200917 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200918 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200920 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200922 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100923 c->rep_prefix = REPNE_PREFIX;
924 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100926 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 default:
929 goto done_prefixes;
930 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200931
932 /* Any legacy prefix after a REX prefix nullifies its effect. */
933
Avi Kivity33615aa2007-10-31 11:15:56 +0200934 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 }
936
937done_prefixes:
938
939 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200940 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200941 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200942 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
944 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200945 c->d = opcode_table[c->b];
946 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200948 if (c->b == 0x0f) {
949 c->twobyte = 1;
950 c->b = insn_fetch(u8, 1, c->eip);
951 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954
Avi Kivitye09d0822008-01-18 12:38:59 +0200955 if (c->d & Group) {
956 group = c->d & GroupMask;
957 c->modrm = insn_fetch(u8, 1, c->eip);
958 --c->eip;
959
960 group = (group << 3) + ((c->modrm >> 3) & 7);
961 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
962 c->d = group2_table[group];
963 else
964 c->d = group_table[group];
965 }
966
967 /* Unrecognised? */
968 if (c->d == 0) {
969 DPRINTF("Cannot emulate %02x\n", c->b);
970 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 }
972
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200973 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
974 c->op_bytes = 8;
975
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 if (c->d & ModRM)
978 rc = decode_modrm(ctxt, ops);
979 else if (c->d & MemAbs)
980 rc = decode_abs(ctxt, ops);
981 if (rc)
982 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300984 if (!c->has_seg_override)
985 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200986
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300987 if (!(!c->twobyte && c->b == 0x8d))
988 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200989
990 if (c->ad_bytes != 8)
991 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 /*
993 * Decode and fetch the source operand: register, memory
994 * or immediate.
995 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 case SrcNone:
998 break;
999 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001000 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 break;
1002 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001003 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 goto srcmem_common;
1005 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001006 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 goto srcmem_common;
1008 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->src.bytes = (c->d & ByteOp) ? 1 :
1010 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001011 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001012 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001013 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001014 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001015 /*
1016 * For instructions with a ModR/M byte, switch to register
1017 * access if Mod = 3.
1018 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001019 if ((c->d & ModRM) && c->modrm_mod == 3) {
1020 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001021 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001022 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001023 break;
1024 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001025 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 break;
1027 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001028 c->src.type = OP_IMM;
1029 c->src.ptr = (unsigned long *)c->eip;
1030 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1031 if (c->src.bytes == 8)
1032 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001034 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001036 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 break;
1038 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001039 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 break;
1041 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001042 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 break;
1044 }
1045 break;
1046 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001047 c->src.type = OP_IMM;
1048 c->src.ptr = (unsigned long *)c->eip;
1049 c->src.bytes = 1;
1050 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001052 case SrcOne:
1053 c->src.bytes = 1;
1054 c->src.val = 1;
1055 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 }
1057
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001058 /*
1059 * Decode and fetch the second source operand: register, memory
1060 * or immediate.
1061 */
1062 switch (c->d & Src2Mask) {
1063 case Src2None:
1064 break;
1065 case Src2CL:
1066 c->src2.bytes = 1;
1067 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1068 break;
1069 case Src2ImmByte:
1070 c->src2.type = OP_IMM;
1071 c->src2.ptr = (unsigned long *)c->eip;
1072 c->src2.bytes = 1;
1073 c->src2.val = insn_fetch(u8, 1, c->eip);
1074 break;
1075 case Src2One:
1076 c->src2.bytes = 1;
1077 c->src2.val = 1;
1078 break;
1079 }
1080
Avi Kivity038e51d2007-01-22 20:40:40 -08001081 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001082 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001083 case ImplicitOps:
1084 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001085 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001086 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001087 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001088 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001089 break;
1090 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001092 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001094 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001095 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001096 break;
1097 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001098 c->dst.type = OP_MEM;
1099 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001100 case DstAcc:
1101 c->dst.type = OP_REG;
1102 c->dst.bytes = c->op_bytes;
1103 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1104 switch (c->op_bytes) {
1105 case 1:
1106 c->dst.val = *(u8 *)c->dst.ptr;
1107 break;
1108 case 2:
1109 c->dst.val = *(u16 *)c->dst.ptr;
1110 break;
1111 case 4:
1112 c->dst.val = *(u32 *)c->dst.ptr;
1113 break;
1114 }
1115 c->dst.orig_val = c->dst.val;
1116 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001117 }
1118
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001119 if (c->rip_relative)
1120 c->modrm_ea += c->eip;
1121
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001122done:
1123 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1124}
1125
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001126static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1127{
1128 struct decode_cache *c = &ctxt->decode;
1129
1130 c->dst.type = OP_MEM;
1131 c->dst.bytes = c->op_bytes;
1132 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001133 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001134 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001135 c->regs[VCPU_REGS_RSP]);
1136}
1137
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001138static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001139 struct x86_emulate_ops *ops,
1140 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001141{
1142 struct decode_cache *c = &ctxt->decode;
1143 int rc;
1144
Avi Kivity781d0ed2008-11-27 18:00:28 +02001145 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1146 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001147 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001148 if (rc != 0)
1149 return rc;
1150
Avi Kivity350f69d2009-01-05 11:12:40 +02001151 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001152 return rc;
1153}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001154
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001155static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1156 struct x86_emulate_ops *ops)
1157{
1158 struct decode_cache *c = &ctxt->decode;
1159 int rc;
1160
Avi Kivity350f69d2009-01-05 11:12:40 +02001161 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001162 if (rc != 0)
1163 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164 return 0;
1165}
1166
Laurent Vivier05f086f2007-09-24 11:10:55 +02001167static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001168{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001169 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001170 switch (c->modrm_reg) {
1171 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001172 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001173 break;
1174 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001175 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001176 break;
1177 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001178 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001179 break;
1180 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001181 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001182 break;
1183 case 4: /* sal/shl */
1184 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001185 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001186 break;
1187 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001188 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001189 break;
1190 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001191 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001192 break;
1193 }
1194}
1195
1196static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001197 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198{
1199 struct decode_cache *c = &ctxt->decode;
1200 int rc = 0;
1201
1202 switch (c->modrm_reg) {
1203 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001204 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001205 break;
1206 case 2: /* not */
1207 c->dst.val = ~c->dst.val;
1208 break;
1209 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001210 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001211 break;
1212 default:
1213 DPRINTF("Cannot emulate %02x\n", c->b);
1214 rc = X86EMUL_UNHANDLEABLE;
1215 break;
1216 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001217 return rc;
1218}
1219
1220static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001221 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001222{
1223 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001224
1225 switch (c->modrm_reg) {
1226 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001227 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001228 break;
1229 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001230 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001231 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001232 case 2: /* call near abs */ {
1233 long int old_eip;
1234 old_eip = c->eip;
1235 c->eip = c->src.val;
1236 c->src.val = old_eip;
1237 emulate_push(ctxt);
1238 break;
1239 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001240 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001241 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001242 break;
1243 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001244 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001245 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001246 }
1247 return 0;
1248}
1249
1250static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1251 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001252 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253{
1254 struct decode_cache *c = &ctxt->decode;
1255 u64 old, new;
1256 int rc;
1257
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001258 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001259 if (rc != 0)
1260 return rc;
1261
1262 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1263 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1264
1265 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1266 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001267 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001268
1269 } else {
1270 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1271 (u32) c->regs[VCPU_REGS_RBX];
1272
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001273 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001274 if (rc != 0)
1275 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001276 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001277 }
1278 return 0;
1279}
1280
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001281static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1282 struct x86_emulate_ops *ops)
1283{
1284 struct decode_cache *c = &ctxt->decode;
1285 int rc;
1286 unsigned long cs;
1287
1288 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1289 if (rc)
1290 return rc;
1291 if (c->op_bytes == 4)
1292 c->eip = (u32)c->eip;
1293 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1294 if (rc)
1295 return rc;
1296 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1297 return rc;
1298}
1299
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300static inline int writeback(struct x86_emulate_ctxt *ctxt,
1301 struct x86_emulate_ops *ops)
1302{
1303 int rc;
1304 struct decode_cache *c = &ctxt->decode;
1305
1306 switch (c->dst.type) {
1307 case OP_REG:
1308 /* The 4-byte case *is* correct:
1309 * in 64-bit mode we zero-extend.
1310 */
1311 switch (c->dst.bytes) {
1312 case 1:
1313 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1314 break;
1315 case 2:
1316 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1317 break;
1318 case 4:
1319 *c->dst.ptr = (u32)c->dst.val;
1320 break; /* 64b: zero-ext */
1321 case 8:
1322 *c->dst.ptr = c->dst.val;
1323 break;
1324 }
1325 break;
1326 case OP_MEM:
1327 if (c->lock_prefix)
1328 rc = ops->cmpxchg_emulated(
1329 (unsigned long)c->dst.ptr,
1330 &c->dst.orig_val,
1331 &c->dst.val,
1332 c->dst.bytes,
1333 ctxt->vcpu);
1334 else
1335 rc = ops->write_emulated(
1336 (unsigned long)c->dst.ptr,
1337 &c->dst.val,
1338 c->dst.bytes,
1339 ctxt->vcpu);
1340 if (rc != 0)
1341 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001342 break;
1343 case OP_NONE:
1344 /* no writeback */
1345 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 default:
1347 break;
1348 }
1349 return 0;
1350}
1351
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001352int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001353x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001354{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001355 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001356 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001357 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001358 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001359 unsigned int port;
1360 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001361 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001362
Laurent Vivier34273182007-09-18 11:27:37 +02001363 /* Shadow copy of register state. Committed on successful emulation.
1364 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1365 * modify them.
1366 */
1367
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001368 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001369 saved_eip = c->eip;
1370
Avi Kivityc7e75a32007-10-28 16:34:25 +02001371 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001372 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001373
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001374 if (c->rep_prefix && (c->d & String)) {
1375 /* All REP prefixes have the same first termination condition */
1376 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001377 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001378 goto done;
1379 }
1380 /* The second termination condition only applies for REPE
1381 * and REPNE. Test if the repeat string operation prefix is
1382 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1383 * corresponding termination condition according to:
1384 * - if REPE/REPZ and ZF = 0 then done
1385 * - if REPNE/REPNZ and ZF = 1 then done
1386 */
1387 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1388 (c->b == 0xae) || (c->b == 0xaf)) {
1389 if ((c->rep_prefix == REPE_PREFIX) &&
1390 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001391 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001392 goto done;
1393 }
1394 if ((c->rep_prefix == REPNE_PREFIX) &&
1395 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001396 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001397 goto done;
1398 }
1399 }
1400 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001401 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001402 }
1403
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001404 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001405 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001406 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001407 rc = ops->read_emulated((unsigned long)c->src.ptr,
1408 &c->src.val,
1409 c->src.bytes,
1410 ctxt->vcpu);
1411 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001412 goto done;
1413 c->src.orig_val = c->src.val;
1414 }
1415
1416 if ((c->d & DstMask) == ImplicitOps)
1417 goto special_insn;
1418
1419
1420 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001421 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001422 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1423 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001424 if (c->d & BitOp) {
1425 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001426
Laurent Viviere4e03de2007-09-18 11:52:50 +02001427 c->dst.ptr = (void *)c->dst.ptr +
1428 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001429 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001430 if (!(c->d & Mov) &&
1431 /* optimisation - avoid slow emulated read */
1432 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1433 &c->dst.val,
1434 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001435 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001436 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001437 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001438
Avi Kivity018a98d2007-11-27 19:30:56 +02001439special_insn:
1440
Laurent Viviere4e03de2007-09-18 11:52:50 +02001441 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 goto twobyte_insn;
1443
Laurent Viviere4e03de2007-09-18 11:52:50 +02001444 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 case 0x00 ... 0x05:
1446 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001447 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448 break;
1449 case 0x08 ... 0x0d:
1450 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001451 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452 break;
1453 case 0x10 ... 0x15:
1454 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001455 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 break;
1457 case 0x18 ... 0x1d:
1458 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001459 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001461 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001463 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 break;
1465 case 0x28 ... 0x2d:
1466 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001467 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 break;
1469 case 0x30 ... 0x35:
1470 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001471 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 break;
1473 case 0x38 ... 0x3d:
1474 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001475 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001477 case 0x40 ... 0x47: /* inc r16/r32 */
1478 emulate_1op("inc", c->dst, ctxt->eflags);
1479 break;
1480 case 0x48 ... 0x4f: /* dec r16/r32 */
1481 emulate_1op("dec", c->dst, ctxt->eflags);
1482 break;
1483 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001484 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001485 break;
1486 case 0x58 ... 0x5f: /* pop reg */
1487 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001488 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001489 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001490 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001491 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001493 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001495 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001497 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001498 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001499 emulate_push(ctxt);
1500 break;
1501 case 0x6c: /* insb */
1502 case 0x6d: /* insw/insd */
1503 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1504 1,
1505 (c->d & ByteOp) ? 1 : c->op_bytes,
1506 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001507 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001508 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001509 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001510 c->regs[VCPU_REGS_RDI]),
1511 c->rep_prefix,
1512 c->regs[VCPU_REGS_RDX]) == 0) {
1513 c->eip = saved_eip;
1514 return -1;
1515 }
1516 return 0;
1517 case 0x6e: /* outsb */
1518 case 0x6f: /* outsw/outsd */
1519 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1520 0,
1521 (c->d & ByteOp) ? 1 : c->op_bytes,
1522 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001523 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001524 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001525 register_address(c,
1526 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001527 c->regs[VCPU_REGS_RSI]),
1528 c->rep_prefix,
1529 c->regs[VCPU_REGS_RDX]) == 0) {
1530 c->eip = saved_eip;
1531 return -1;
1532 }
1533 return 0;
1534 case 0x70 ... 0x7f: /* jcc (short) */ {
1535 int rel = insn_fetch(s8, 1, c->eip);
1536
1537 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001538 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001539 break;
1540 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001542 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543 case 0:
1544 goto add;
1545 case 1:
1546 goto or;
1547 case 2:
1548 goto adc;
1549 case 3:
1550 goto sbb;
1551 case 4:
1552 goto and;
1553 case 5:
1554 goto sub;
1555 case 6:
1556 goto xor;
1557 case 7:
1558 goto cmp;
1559 }
1560 break;
1561 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001562 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563 break;
1564 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001565 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001567 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001569 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570 break;
1571 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001572 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 break;
1574 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 break; /* 64b reg: zero-extend */
1577 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001578 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 break;
1580 }
1581 /*
1582 * Write back the memory destination with implicit LOCK
1583 * prefix.
1584 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 c->dst.val = c->src.val;
1586 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001589 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001590 case 0x8c: { /* mov r/m, sreg */
1591 struct kvm_segment segreg;
1592
1593 if (c->modrm_reg <= 5)
1594 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1595 else {
1596 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1597 c->modrm);
1598 goto cannot_emulate;
1599 }
1600 c->dst.val = segreg.selector;
1601 break;
1602 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001603 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001604 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001605 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001606 case 0x8e: { /* mov seg, r/m16 */
1607 uint16_t sel;
1608 int type_bits;
1609 int err;
1610
1611 sel = c->src.val;
1612 if (c->modrm_reg <= 5) {
1613 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1614 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1615 type_bits, c->modrm_reg);
1616 } else {
1617 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1618 c->modrm);
1619 goto cannot_emulate;
1620 }
1621
1622 if (err < 0)
1623 goto cannot_emulate;
1624
1625 c->dst.type = OP_NONE; /* Disable writeback. */
1626 break;
1627 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001629 rc = emulate_grp1a(ctxt, ops);
1630 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001633 case 0x90: /* nop / xchg r8,rax */
1634 if (!(c->rex_prefix & 1)) { /* nop */
1635 c->dst.type = OP_NONE;
1636 break;
1637 }
1638 case 0x91 ... 0x97: /* xchg reg,rax */
1639 c->src.type = c->dst.type = OP_REG;
1640 c->src.bytes = c->dst.bytes = c->op_bytes;
1641 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1642 c->src.val = *(c->src.ptr);
1643 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001644 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001645 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646 emulate_push(ctxt);
1647 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001648 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001649 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001650 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001651 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001652 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001653 case 0xa0 ... 0xa1: /* mov */
1654 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1655 c->dst.val = c->src.val;
1656 break;
1657 case 0xa2 ... 0xa3: /* mov */
1658 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1659 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001661 c->dst.type = OP_MEM;
1662 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001663 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001664 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001665 c->regs[VCPU_REGS_RDI]);
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),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001668 c->regs[VCPU_REGS_RSI]),
1669 &c->dst.val,
1670 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001672 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001673 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001674 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001675 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001676 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001677 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 break;
1679 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001680 c->src.type = OP_NONE; /* Disable writeback. */
1681 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001682 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001683 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001684 c->regs[VCPU_REGS_RSI]);
1685 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1686 &c->src.val,
1687 c->src.bytes,
1688 ctxt->vcpu)) != 0)
1689 goto done;
1690
1691 c->dst.type = OP_NONE; /* Disable writeback. */
1692 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001693 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001694 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001695 c->regs[VCPU_REGS_RDI]);
1696 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1697 &c->dst.val,
1698 c->dst.bytes,
1699 ctxt->vcpu)) != 0)
1700 goto done;
1701
1702 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1703
1704 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1705
Harvey Harrison7a9572752008-02-19 07:40:41 -08001706 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001707 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1708 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001709 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001710 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1711 : c->dst.bytes);
1712
1713 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001715 c->dst.type = OP_MEM;
1716 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001717 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001718 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001719 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001720 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001721 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001722 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 break;
1725 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001726 c->dst.type = OP_REG;
1727 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1728 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001729 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001730 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001731 c->regs[VCPU_REGS_RSI]),
1732 &c->dst.val,
1733 c->dst.bytes,
1734 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001736 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001737 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001738 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 break;
1740 case 0xae ... 0xaf: /* scas */
1741 DPRINTF("Urk! I don't handle SCAS.\n");
1742 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001743 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001744 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001745 case 0xc0 ... 0xc1:
1746 emulate_grp2(ctxt);
1747 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001748 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001749 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001750 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001751 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001752 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001753 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1754 mov:
1755 c->dst.val = c->src.val;
1756 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001757 case 0xcb: /* ret far */
1758 rc = emulate_ret_far(ctxt, ops);
1759 if (rc)
1760 goto done;
1761 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001762 case 0xd0 ... 0xd1: /* Grp2 */
1763 c->src.val = 1;
1764 emulate_grp2(ctxt);
1765 break;
1766 case 0xd2 ... 0xd3: /* Grp2 */
1767 c->src.val = c->regs[VCPU_REGS_RCX];
1768 emulate_grp2(ctxt);
1769 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001770 case 0xe4: /* inb */
1771 case 0xe5: /* in */
1772 port = insn_fetch(u8, 1, c->eip);
1773 io_dir_in = 1;
1774 goto do_io;
1775 case 0xe6: /* outb */
1776 case 0xe7: /* out */
1777 port = insn_fetch(u8, 1, c->eip);
1778 io_dir_in = 0;
1779 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001780 case 0xe8: /* call (near) */ {
1781 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001782 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001783 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001784 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001785 break;
1786 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001787 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001788 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001789 default:
1790 DPRINTF("Call: Invalid op_bytes\n");
1791 goto cannot_emulate;
1792 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001793 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001794 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001795 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796 emulate_push(ctxt);
1797 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001798 }
1799 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001800 goto jmp;
1801 case 0xea: /* jmp far */ {
1802 uint32_t eip;
1803 uint16_t sel;
1804
1805 switch (c->op_bytes) {
1806 case 2:
1807 eip = insn_fetch(u16, 2, c->eip);
1808 break;
1809 case 4:
1810 eip = insn_fetch(u32, 4, c->eip);
1811 break;
1812 default:
1813 DPRINTF("jmp far: Invalid op_bytes\n");
1814 goto cannot_emulate;
1815 }
1816 sel = insn_fetch(u16, 2, c->eip);
1817 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1818 DPRINTF("jmp far: Failed to load CS descriptor\n");
1819 goto cannot_emulate;
1820 }
1821
1822 c->eip = eip;
1823 break;
1824 }
1825 case 0xeb:
1826 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001827 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001828 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001829 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001830 case 0xec: /* in al,dx */
1831 case 0xed: /* in (e/r)ax,dx */
1832 port = c->regs[VCPU_REGS_RDX];
1833 io_dir_in = 1;
1834 goto do_io;
1835 case 0xee: /* out al,dx */
1836 case 0xef: /* out (e/r)ax,dx */
1837 port = c->regs[VCPU_REGS_RDX];
1838 io_dir_in = 0;
1839 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1840 (c->d & ByteOp) ? 1 : c->op_bytes,
1841 port) != 0) {
1842 c->eip = saved_eip;
1843 goto cannot_emulate;
1844 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001845 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001846 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001847 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001848 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001849 case 0xf5: /* cmc */
1850 /* complement carry flag from eflags reg */
1851 ctxt->eflags ^= EFLG_CF;
1852 c->dst.type = OP_NONE; /* Disable writeback. */
1853 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001854 case 0xf6 ... 0xf7: /* Grp3 */
1855 rc = emulate_grp3(ctxt, ops);
1856 if (rc != 0)
1857 goto done;
1858 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001859 case 0xf8: /* clc */
1860 ctxt->eflags &= ~EFLG_CF;
1861 c->dst.type = OP_NONE; /* Disable writeback. */
1862 break;
1863 case 0xfa: /* cli */
1864 ctxt->eflags &= ~X86_EFLAGS_IF;
1865 c->dst.type = OP_NONE; /* Disable writeback. */
1866 break;
1867 case 0xfb: /* sti */
1868 ctxt->eflags |= X86_EFLAGS_IF;
1869 c->dst.type = OP_NONE; /* Disable writeback. */
1870 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001871 case 0xfc: /* cld */
1872 ctxt->eflags &= ~EFLG_DF;
1873 c->dst.type = OP_NONE; /* Disable writeback. */
1874 break;
1875 case 0xfd: /* std */
1876 ctxt->eflags |= EFLG_DF;
1877 c->dst.type = OP_NONE; /* Disable writeback. */
1878 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001879 case 0xfe ... 0xff: /* Grp4/Grp5 */
1880 rc = emulate_grp45(ctxt, ops);
1881 if (rc != 0)
1882 goto done;
1883 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001885
1886writeback:
1887 rc = writeback(ctxt, ops);
1888 if (rc != 0)
1889 goto done;
1890
1891 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001892 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001893 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001894
1895done:
1896 if (rc == X86EMUL_UNHANDLEABLE) {
1897 c->eip = saved_eip;
1898 return -1;
1899 }
1900 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901
1902twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001903 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001905 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 u16 size;
1907 unsigned long address;
1908
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001909 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001910 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001911 goto cannot_emulate;
1912
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001913 rc = kvm_fix_hypercall(ctxt->vcpu);
1914 if (rc)
1915 goto done;
1916
Avi Kivity33e38852008-05-21 15:34:25 +03001917 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001918 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001919 /* Disable writeback. */
1920 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001921 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001923 rc = read_descriptor(ctxt, ops, c->src.ptr,
1924 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 if (rc)
1926 goto done;
1927 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001928 /* Disable writeback. */
1929 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001931 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02001932 if (c->modrm_mod == 3) {
1933 switch (c->modrm_rm) {
1934 case 1:
1935 rc = kvm_fix_hypercall(ctxt->vcpu);
1936 if (rc)
1937 goto done;
1938 break;
1939 default:
1940 goto cannot_emulate;
1941 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001942 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001943 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001944 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001945 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001946 if (rc)
1947 goto done;
1948 realmode_lidt(ctxt->vcpu, size, address);
1949 }
Avi Kivity16286d02008-04-14 14:40:50 +03001950 /* Disable writeback. */
1951 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 break;
1953 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001954 c->dst.bytes = 2;
1955 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 break;
1957 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001958 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1959 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001960 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 break;
1962 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001963 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001964 /* Disable writeback. */
1965 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 break;
1967 default:
1968 goto cannot_emulate;
1969 }
1970 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001971 case 0x06:
1972 emulate_clts(ctxt->vcpu);
1973 c->dst.type = OP_NONE;
1974 break;
1975 case 0x08: /* invd */
1976 case 0x09: /* wbinvd */
1977 case 0x0d: /* GrpP (prefetch) */
1978 case 0x18: /* Grp16 (prefetch/nop) */
1979 c->dst.type = OP_NONE;
1980 break;
1981 case 0x20: /* mov cr, reg */
1982 if (c->modrm_mod != 3)
1983 goto cannot_emulate;
1984 c->regs[c->modrm_rm] =
1985 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1986 c->dst.type = OP_NONE; /* no writeback */
1987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001989 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001992 if (rc)
1993 goto cannot_emulate;
1994 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001996 case 0x22: /* mov reg, cr */
1997 if (c->modrm_mod != 3)
1998 goto cannot_emulate;
1999 realmode_set_cr(ctxt->vcpu,
2000 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2001 c->dst.type = OP_NONE;
2002 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002004 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002006 rc = emulator_set_dr(ctxt, c->modrm_reg,
2007 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002008 if (rc)
2009 goto cannot_emulate;
2010 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002012 case 0x30:
2013 /* wrmsr */
2014 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2015 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2016 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2017 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002018 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002019 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002020 }
2021 rc = X86EMUL_CONTINUE;
2022 c->dst.type = OP_NONE;
2023 break;
2024 case 0x32:
2025 /* rdmsr */
2026 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2027 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002028 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002029 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002030 } else {
2031 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2032 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2033 }
2034 rc = X86EMUL_CONTINUE;
2035 c->dst.type = OP_NONE;
2036 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002038 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002039 if (!test_cc(c->b, ctxt->eflags))
2040 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002042 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
2043 long int rel;
2044
2045 switch (c->op_bytes) {
2046 case 2:
2047 rel = insn_fetch(s16, 2, c->eip);
2048 break;
2049 case 4:
2050 rel = insn_fetch(s32, 4, c->eip);
2051 break;
2052 case 8:
2053 rel = insn_fetch(s64, 8, c->eip);
2054 break;
2055 default:
2056 DPRINTF("jnz: Invalid op_bytes\n");
2057 goto cannot_emulate;
2058 }
2059 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08002060 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02002061 c->dst.type = OP_NONE;
2062 break;
2063 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03002064 case 0xa3:
2065 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002066 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002067 /* only subword offset */
2068 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002069 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002070 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002071 case 0xa4: /* shld imm8, r, r/m */
2072 case 0xa5: /* shld cl, r, r/m */
2073 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2074 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002075 case 0xab:
2076 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002077 /* only subword offset */
2078 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002079 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002080 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002081 case 0xac: /* shrd imm8, r, r/m */
2082 case 0xad: /* shrd cl, r, r/m */
2083 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2084 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002085 case 0xae: /* clflush */
2086 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 case 0xb0 ... 0xb1: /* cmpxchg */
2088 /*
2089 * Save real source value, then compare EAX against
2090 * destination.
2091 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002092 c->src.orig_val = c->src.val;
2093 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002094 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2095 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002097 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 } else {
2099 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002100 c->dst.type = OP_REG;
2101 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 }
2103 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 case 0xb3:
2105 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002106 /* only subword offset */
2107 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002108 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002111 c->dst.bytes = c->op_bytes;
2112 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2113 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 case 0:
2118 goto bt;
2119 case 1:
2120 goto bts;
2121 case 2:
2122 goto btr;
2123 case 3:
2124 goto btc;
2125 }
2126 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002127 case 0xbb:
2128 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002129 /* only subword offset */
2130 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002131 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002134 c->dst.bytes = c->op_bytes;
2135 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2136 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002138 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002139 c->dst.bytes = c->op_bytes;
2140 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2141 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002142 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002144 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145 if (rc != 0)
2146 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002147 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 }
2150 goto writeback;
2151
2152cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002153 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002154 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 return -1;
2156}