blob: 9ae6d5b3e962663ec28c891377a96dc31f431deb [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 */
181 0, 0, 0, 0, 0, 0, 0, 0,
182 /* 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 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800240 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 /* 0xA8 - 0xAF */
Glauber Costa2a7c5b82008-07-10 17:08:15 -0300242 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* 0xB0 - 0xB7 */
244 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800245 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem16 | ModRM | Mov,
248 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800249 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem16 | ModRM | Mov,
252 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800253 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
254 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 /* 0xD0 - 0xDF */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257 /* 0xE0 - 0xEF */
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259 /* 0xF0 - 0xFF */
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
261};
262
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100263static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200264 [Group1_80*8] =
265 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
266 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
267 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
268 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
269 [Group1_81*8] =
270 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
271 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
272 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
273 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
274 [Group1_82*8] =
275 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
276 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
277 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
278 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
279 [Group1_83*8] =
280 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
281 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
282 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
283 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200284 [Group1A*8] =
285 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200286 [Group3_Byte*8] =
287 ByteOp | SrcImm | DstMem | ModRM, 0,
288 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
289 0, 0, 0, 0,
290 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400291 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300292 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200293 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200294 [Group4*8] =
295 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
296 0, 0, 0, 0, 0, 0,
297 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300298 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
299 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300300 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200301 [Group7*8] =
302 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300303 SrcNone | ModRM | DstMem | Mov, 0,
304 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200305};
306
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100307static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200308 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300309 SrcNone | ModRM, 0, 0, 0,
310 SrcNone | ModRM | DstMem | Mov, 0,
311 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200312};
313
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314/* EFLAGS bit definitions. */
315#define EFLG_OF (1<<11)
316#define EFLG_DF (1<<10)
317#define EFLG_SF (1<<7)
318#define EFLG_ZF (1<<6)
319#define EFLG_AF (1<<4)
320#define EFLG_PF (1<<2)
321#define EFLG_CF (1<<0)
322
323/*
324 * Instruction emulation:
325 * Most instructions are emulated directly via a fragment of inline assembly
326 * code. This allows us to save/restore EFLAGS and thus very easily pick up
327 * any modified flags.
328 */
329
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800330#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331#define _LO32 "k" /* force 32-bit operand */
332#define _STK "%%rsp" /* stack pointer */
333#elif defined(__i386__)
334#define _LO32 "" /* force 32-bit operand */
335#define _STK "%%esp" /* stack pointer */
336#endif
337
338/*
339 * These EFLAGS bits are restored from saved value during emulation, and
340 * any changes are written back to the saved value after emulation.
341 */
342#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
343
344/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200345#define _PRE_EFLAGS(_sav, _msk, _tmp) \
346 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
347 "movl %"_sav",%"_LO32 _tmp"; " \
348 "push %"_tmp"; " \
349 "push %"_tmp"; " \
350 "movl %"_msk",%"_LO32 _tmp"; " \
351 "andl %"_LO32 _tmp",("_STK"); " \
352 "pushf; " \
353 "notl %"_LO32 _tmp"; " \
354 "andl %"_LO32 _tmp",("_STK"); " \
355 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
356 "pop %"_tmp"; " \
357 "orl %"_LO32 _tmp",("_STK"); " \
358 "popf; " \
359 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360
361/* After executing instruction: write-back necessary bits in EFLAGS. */
362#define _POST_EFLAGS(_sav, _msk, _tmp) \
363 /* _sav |= EFLAGS & _msk; */ \
364 "pushf; " \
365 "pop %"_tmp"; " \
366 "andl %"_msk",%"_LO32 _tmp"; " \
367 "orl %"_LO32 _tmp",%"_sav"; "
368
Avi Kivitydda96d82008-11-26 15:14:10 +0200369#ifdef CONFIG_X86_64
370#define ON64(x) x
371#else
372#define ON64(x)
373#endif
374
Avi Kivity6b7ad612008-11-26 15:30:45 +0200375#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
376 do { \
377 __asm__ __volatile__ ( \
378 _PRE_EFLAGS("0", "4", "2") \
379 _op _suffix " %"_x"3,%1; " \
380 _POST_EFLAGS("0", "4", "2") \
381 : "=m" (_eflags), "=m" ((_dst).val), \
382 "=&r" (_tmp) \
383 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200384 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200385
386
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387/* Raw emulation: instruction has two explicit operands. */
388#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200389 do { \
390 unsigned long _tmp; \
391 \
392 switch ((_dst).bytes) { \
393 case 2: \
394 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
395 break; \
396 case 4: \
397 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
398 break; \
399 case 8: \
400 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
401 break; \
402 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 } while (0)
404
405#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
406 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200407 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400408 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200410 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 break; \
412 default: \
413 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
414 _wx, _wy, _lx, _ly, _qx, _qy); \
415 break; \
416 } \
417 } while (0)
418
419/* Source operand is byte-sized and may be restricted to just %cl. */
420#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
421 __emulate_2op(_op, _src, _dst, _eflags, \
422 "b", "c", "b", "c", "b", "c", "b", "c")
423
424/* Source operand is byte, word, long or quad sized. */
425#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
426 __emulate_2op(_op, _src, _dst, _eflags, \
427 "b", "q", "w", "r", _LO32, "r", "", "r")
428
429/* Source operand is word, long or quad sized. */
430#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
431 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
432 "w", "r", _LO32, "r", "", "r")
433
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100434/* Instruction has three operands and one operand is stored in ECX register */
435#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
436 do { \
437 unsigned long _tmp; \
438 _type _clv = (_cl).val; \
439 _type _srcv = (_src).val; \
440 _type _dstv = (_dst).val; \
441 \
442 __asm__ __volatile__ ( \
443 _PRE_EFLAGS("0", "5", "2") \
444 _op _suffix " %4,%1 \n" \
445 _POST_EFLAGS("0", "5", "2") \
446 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
447 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
448 ); \
449 \
450 (_cl).val = (unsigned long) _clv; \
451 (_src).val = (unsigned long) _srcv; \
452 (_dst).val = (unsigned long) _dstv; \
453 } while (0)
454
455#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
456 do { \
457 switch ((_dst).bytes) { \
458 case 2: \
459 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
460 "w", unsigned short); \
461 break; \
462 case 4: \
463 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
464 "l", unsigned int); \
465 break; \
466 case 8: \
467 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
468 "q", unsigned long)); \
469 break; \
470 } \
471 } while (0)
472
Avi Kivitydda96d82008-11-26 15:14:10 +0200473#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 do { \
475 unsigned long _tmp; \
476 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200477 __asm__ __volatile__ ( \
478 _PRE_EFLAGS("0", "3", "2") \
479 _op _suffix " %1; " \
480 _POST_EFLAGS("0", "3", "2") \
481 : "=m" (_eflags), "+m" ((_dst).val), \
482 "=&r" (_tmp) \
483 : "i" (EFLAGS_MASK)); \
484 } while (0)
485
486/* Instruction has only one explicit operand (no source operand). */
487#define emulate_1op(_op, _dst, _eflags) \
488 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400489 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200490 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
491 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
492 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
493 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 } \
495 } while (0)
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497/* Fetch next part of the instruction being emulated. */
498#define insn_fetch(_type, _size, _eip) \
499({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200500 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400501 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 goto done; \
503 (_eip) += (_size); \
504 (_type)_x; \
505})
506
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800507static inline unsigned long ad_mask(struct decode_cache *c)
508{
509 return (1UL << (c->ad_bytes << 3)) - 1;
510}
511
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800513static inline unsigned long
514address_mask(struct decode_cache *c, unsigned long reg)
515{
516 if (c->ad_bytes == sizeof(unsigned long))
517 return reg;
518 else
519 return reg & ad_mask(c);
520}
521
522static inline unsigned long
523register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
524{
525 return base + address_mask(c, reg);
526}
527
Harvey Harrison7a9572752008-02-19 07:40:41 -0800528static inline void
529register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
530{
531 if (c->ad_bytes == sizeof(unsigned long))
532 *reg += inc;
533 else
534 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
535}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536
Harvey Harrison7a9572752008-02-19 07:40:41 -0800537static inline void jmp_rel(struct decode_cache *c, int rel)
538{
539 register_address_increment(c, &c->eip, rel);
540}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300541
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300542static void set_seg_override(struct decode_cache *c, int seg)
543{
544 c->has_seg_override = true;
545 c->seg_override = seg;
546}
547
548static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
549{
550 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
551 return 0;
552
553 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
554}
555
556static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
557 struct decode_cache *c)
558{
559 if (!c->has_seg_override)
560 return 0;
561
562 return seg_base(ctxt, c->seg_override);
563}
564
565static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
566{
567 return seg_base(ctxt, VCPU_SREG_ES);
568}
569
570static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
571{
572 return seg_base(ctxt, VCPU_SREG_SS);
573}
574
Avi Kivity62266862007-11-20 13:15:52 +0200575static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
576 struct x86_emulate_ops *ops,
577 unsigned long linear, u8 *dest)
578{
579 struct fetch_cache *fc = &ctxt->decode.fetch;
580 int rc;
581 int size;
582
583 if (linear < fc->start || linear >= fc->end) {
584 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
585 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
586 if (rc)
587 return rc;
588 fc->start = linear;
589 fc->end = linear + size;
590 }
591 *dest = fc->data[linear - fc->start];
592 return 0;
593}
594
595static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
596 struct x86_emulate_ops *ops,
597 unsigned long eip, void *dest, unsigned size)
598{
599 int rc = 0;
600
601 eip += ctxt->cs_base;
602 while (size--) {
603 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
604 if (rc)
605 return rc;
606 }
607 return 0;
608}
609
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000610/*
611 * Given the 'reg' portion of a ModRM byte, and a register block, return a
612 * pointer into the block that addresses the relevant register.
613 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
614 */
615static void *decode_register(u8 modrm_reg, unsigned long *regs,
616 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617{
618 void *p;
619
620 p = &regs[modrm_reg];
621 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
622 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
623 return p;
624}
625
626static int read_descriptor(struct x86_emulate_ctxt *ctxt,
627 struct x86_emulate_ops *ops,
628 void *ptr,
629 u16 *size, unsigned long *address, int op_bytes)
630{
631 int rc;
632
633 if (op_bytes == 2)
634 op_bytes = 3;
635 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300636 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
637 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 if (rc)
639 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300640 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
641 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642 return rc;
643}
644
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300645static int test_cc(unsigned int condition, unsigned int flags)
646{
647 int rc = 0;
648
649 switch ((condition & 15) >> 1) {
650 case 0: /* o */
651 rc |= (flags & EFLG_OF);
652 break;
653 case 1: /* b/c/nae */
654 rc |= (flags & EFLG_CF);
655 break;
656 case 2: /* z/e */
657 rc |= (flags & EFLG_ZF);
658 break;
659 case 3: /* be/na */
660 rc |= (flags & (EFLG_CF|EFLG_ZF));
661 break;
662 case 4: /* s */
663 rc |= (flags & EFLG_SF);
664 break;
665 case 5: /* p/pe */
666 rc |= (flags & EFLG_PF);
667 break;
668 case 7: /* le/ng */
669 rc |= (flags & EFLG_ZF);
670 /* fall through */
671 case 6: /* l/nge */
672 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
673 break;
674 }
675
676 /* Odd condition identifiers (lsb == 1) have inverted sense. */
677 return (!!rc ^ (condition & 1));
678}
679
Avi Kivity3c118e22007-10-31 10:27:04 +0200680static void decode_register_operand(struct operand *op,
681 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200682 int inhibit_bytereg)
683{
Avi Kivity33615aa2007-10-31 11:15:56 +0200684 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200685 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200686
687 if (!(c->d & ModRM))
688 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200689 op->type = OP_REG;
690 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200691 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200692 op->val = *(u8 *)op->ptr;
693 op->bytes = 1;
694 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200695 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200696 op->bytes = c->op_bytes;
697 switch (op->bytes) {
698 case 2:
699 op->val = *(u16 *)op->ptr;
700 break;
701 case 4:
702 op->val = *(u32 *)op->ptr;
703 break;
704 case 8:
705 op->val = *(u64 *) op->ptr;
706 break;
707 }
708 }
709 op->orig_val = op->val;
710}
711
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200712static int decode_modrm(struct x86_emulate_ctxt *ctxt,
713 struct x86_emulate_ops *ops)
714{
715 struct decode_cache *c = &ctxt->decode;
716 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700717 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200718 int rc = 0;
719
720 if (c->rex_prefix) {
721 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
722 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
723 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
724 }
725
726 c->modrm = insn_fetch(u8, 1, c->eip);
727 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
728 c->modrm_reg |= (c->modrm & 0x38) >> 3;
729 c->modrm_rm |= (c->modrm & 0x07);
730 c->modrm_ea = 0;
731 c->use_modrm_ea = 1;
732
733 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300734 c->modrm_ptr = decode_register(c->modrm_rm,
735 c->regs, c->d & ByteOp);
736 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200737 return rc;
738 }
739
740 if (c->ad_bytes == 2) {
741 unsigned bx = c->regs[VCPU_REGS_RBX];
742 unsigned bp = c->regs[VCPU_REGS_RBP];
743 unsigned si = c->regs[VCPU_REGS_RSI];
744 unsigned di = c->regs[VCPU_REGS_RDI];
745
746 /* 16-bit ModR/M decode. */
747 switch (c->modrm_mod) {
748 case 0:
749 if (c->modrm_rm == 6)
750 c->modrm_ea += insn_fetch(u16, 2, c->eip);
751 break;
752 case 1:
753 c->modrm_ea += insn_fetch(s8, 1, c->eip);
754 break;
755 case 2:
756 c->modrm_ea += insn_fetch(u16, 2, c->eip);
757 break;
758 }
759 switch (c->modrm_rm) {
760 case 0:
761 c->modrm_ea += bx + si;
762 break;
763 case 1:
764 c->modrm_ea += bx + di;
765 break;
766 case 2:
767 c->modrm_ea += bp + si;
768 break;
769 case 3:
770 c->modrm_ea += bp + di;
771 break;
772 case 4:
773 c->modrm_ea += si;
774 break;
775 case 5:
776 c->modrm_ea += di;
777 break;
778 case 6:
779 if (c->modrm_mod != 0)
780 c->modrm_ea += bp;
781 break;
782 case 7:
783 c->modrm_ea += bx;
784 break;
785 }
786 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
787 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300788 if (!c->has_seg_override)
789 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790 c->modrm_ea = (u16)c->modrm_ea;
791 } else {
792 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700793 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200794 sib = insn_fetch(u8, 1, c->eip);
795 index_reg |= (sib >> 3) & 7;
796 base_reg |= sib & 7;
797 scale = sib >> 6;
798
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700799 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
800 c->modrm_ea += insn_fetch(s32, 4, c->eip);
801 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200802 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700803 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700805 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
806 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700807 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700808 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 switch (c->modrm_mod) {
811 case 0:
812 if (c->modrm_rm == 5)
813 c->modrm_ea += insn_fetch(s32, 4, c->eip);
814 break;
815 case 1:
816 c->modrm_ea += insn_fetch(s8, 1, c->eip);
817 break;
818 case 2:
819 c->modrm_ea += insn_fetch(s32, 4, c->eip);
820 break;
821 }
822 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200823done:
824 return rc;
825}
826
827static int decode_abs(struct x86_emulate_ctxt *ctxt,
828 struct x86_emulate_ops *ops)
829{
830 struct decode_cache *c = &ctxt->decode;
831 int rc = 0;
832
833 switch (c->ad_bytes) {
834 case 2:
835 c->modrm_ea = insn_fetch(u16, 2, c->eip);
836 break;
837 case 4:
838 c->modrm_ea = insn_fetch(u32, 4, c->eip);
839 break;
840 case 8:
841 c->modrm_ea = insn_fetch(u64, 8, c->eip);
842 break;
843 }
844done:
845 return rc;
846}
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200849x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200851 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200854 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855
856 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857
Laurent Viviere4e03de2007-09-18 11:52:50 +0200858 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300859 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300860 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800861 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862
863 switch (mode) {
864 case X86EMUL_MODE_REAL:
865 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200866 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 break;
868 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200869 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800871#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200873 def_op_bytes = 4;
874 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
876#endif
877 default:
878 return -1;
879 }
880
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200881 c->op_bytes = def_op_bytes;
882 c->ad_bytes = def_ad_bytes;
883
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200885 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200886 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200888 /* switch between 2/4 bytes */
889 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
891 case 0x67: /* address-size override */
892 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200894 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200896 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200897 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300900 case 0x2e: /* CS override */
901 case 0x36: /* SS override */
902 case 0x3e: /* DS override */
903 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 break;
905 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300907 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200909 case 0x40 ... 0x4f: /* REX */
910 if (mode != X86EMUL_MODE_PROT64)
911 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200912 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200913 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200915 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200917 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100918 c->rep_prefix = REPNE_PREFIX;
919 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100921 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 default:
924 goto done_prefixes;
925 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200926
927 /* Any legacy prefix after a REX prefix nullifies its effect. */
928
Avi Kivity33615aa2007-10-31 11:15:56 +0200929 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 }
931
932done_prefixes:
933
934 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200936 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200937 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938
939 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200940 c->d = opcode_table[c->b];
941 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200943 if (c->b == 0x0f) {
944 c->twobyte = 1;
945 c->b = insn_fetch(u8, 1, c->eip);
946 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200948 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949
Avi Kivitye09d0822008-01-18 12:38:59 +0200950 if (c->d & Group) {
951 group = c->d & GroupMask;
952 c->modrm = insn_fetch(u8, 1, c->eip);
953 --c->eip;
954
955 group = (group << 3) + ((c->modrm >> 3) & 7);
956 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
957 c->d = group2_table[group];
958 else
959 c->d = group_table[group];
960 }
961
962 /* Unrecognised? */
963 if (c->d == 0) {
964 DPRINTF("Cannot emulate %02x\n", c->b);
965 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 }
967
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200968 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
969 c->op_bytes = 8;
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972 if (c->d & ModRM)
973 rc = decode_modrm(ctxt, ops);
974 else if (c->d & MemAbs)
975 rc = decode_abs(ctxt, ops);
976 if (rc)
977 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300979 if (!c->has_seg_override)
980 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200981
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300982 if (!(!c->twobyte && c->b == 0x8d))
983 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200984
985 if (c->ad_bytes != 8)
986 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 /*
988 * Decode and fetch the source operand: register, memory
989 * or immediate.
990 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200991 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 case SrcNone:
993 break;
994 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200995 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 break;
997 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200998 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 goto srcmem_common;
1000 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001001 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 goto srcmem_common;
1003 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001004 c->src.bytes = (c->d & ByteOp) ? 1 :
1005 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001006 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001007 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001008 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001009 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001010 /*
1011 * For instructions with a ModR/M byte, switch to register
1012 * access if Mod = 3.
1013 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001014 if ((c->d & ModRM) && c->modrm_mod == 3) {
1015 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001016 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001017 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001018 break;
1019 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001020 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 break;
1022 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001023 c->src.type = OP_IMM;
1024 c->src.ptr = (unsigned long *)c->eip;
1025 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1026 if (c->src.bytes == 8)
1027 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001034 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 break;
1036 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 break;
1039 }
1040 break;
1041 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001042 c->src.type = OP_IMM;
1043 c->src.ptr = (unsigned long *)c->eip;
1044 c->src.bytes = 1;
1045 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001047 case SrcOne:
1048 c->src.bytes = 1;
1049 c->src.val = 1;
1050 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 }
1052
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001053 /*
1054 * Decode and fetch the second source operand: register, memory
1055 * or immediate.
1056 */
1057 switch (c->d & Src2Mask) {
1058 case Src2None:
1059 break;
1060 case Src2CL:
1061 c->src2.bytes = 1;
1062 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1063 break;
1064 case Src2ImmByte:
1065 c->src2.type = OP_IMM;
1066 c->src2.ptr = (unsigned long *)c->eip;
1067 c->src2.bytes = 1;
1068 c->src2.val = insn_fetch(u8, 1, c->eip);
1069 break;
1070 case Src2One:
1071 c->src2.bytes = 1;
1072 c->src2.val = 1;
1073 break;
1074 }
1075
Avi Kivity038e51d2007-01-22 20:40:40 -08001076 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001078 case ImplicitOps:
1079 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001080 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001081 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001082 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001083 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001084 break;
1085 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001086 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001087 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001088 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001089 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001090 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001091 break;
1092 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001093 c->dst.type = OP_MEM;
1094 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001095 case DstAcc:
1096 c->dst.type = OP_REG;
1097 c->dst.bytes = c->op_bytes;
1098 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1099 switch (c->op_bytes) {
1100 case 1:
1101 c->dst.val = *(u8 *)c->dst.ptr;
1102 break;
1103 case 2:
1104 c->dst.val = *(u16 *)c->dst.ptr;
1105 break;
1106 case 4:
1107 c->dst.val = *(u32 *)c->dst.ptr;
1108 break;
1109 }
1110 c->dst.orig_val = c->dst.val;
1111 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001112 }
1113
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001114 if (c->rip_relative)
1115 c->modrm_ea += c->eip;
1116
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001117done:
1118 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1119}
1120
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1122{
1123 struct decode_cache *c = &ctxt->decode;
1124
1125 c->dst.type = OP_MEM;
1126 c->dst.bytes = c->op_bytes;
1127 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001128 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001129 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001130 c->regs[VCPU_REGS_RSP]);
1131}
1132
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001133static int emulate_pop(struct x86_emulate_ctxt *ctxt,
1134 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001135{
1136 struct decode_cache *c = &ctxt->decode;
1137 int rc;
1138
Avi Kivity781d0ed2008-11-27 18:00:28 +02001139 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1140 c->regs[VCPU_REGS_RSP]),
1141 &c->src.val, c->src.bytes, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001142 if (rc != 0)
1143 return rc;
1144
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001145 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.bytes);
1146 return rc;
1147}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001148
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001149static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1150 struct x86_emulate_ops *ops)
1151{
1152 struct decode_cache *c = &ctxt->decode;
1153 int rc;
1154
1155 c->src.bytes = c->dst.bytes;
1156 rc = emulate_pop(ctxt, ops);
1157 if (rc != 0)
1158 return rc;
1159 c->dst.val = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001160 return 0;
1161}
1162
Laurent Vivier05f086f2007-09-24 11:10:55 +02001163static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001164{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001165 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001166 switch (c->modrm_reg) {
1167 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001168 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 break;
1170 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001171 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001172 break;
1173 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001174 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175 break;
1176 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001177 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178 break;
1179 case 4: /* sal/shl */
1180 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001181 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001182 break;
1183 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001184 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001185 break;
1186 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001187 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001188 break;
1189 }
1190}
1191
1192static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001193 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194{
1195 struct decode_cache *c = &ctxt->decode;
1196 int rc = 0;
1197
1198 switch (c->modrm_reg) {
1199 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001200 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001201 break;
1202 case 2: /* not */
1203 c->dst.val = ~c->dst.val;
1204 break;
1205 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001206 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001207 break;
1208 default:
1209 DPRINTF("Cannot emulate %02x\n", c->b);
1210 rc = X86EMUL_UNHANDLEABLE;
1211 break;
1212 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213 return rc;
1214}
1215
1216static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001217 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001218{
1219 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001220
1221 switch (c->modrm_reg) {
1222 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001223 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001224 break;
1225 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001226 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001227 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001228 case 2: /* call near abs */ {
1229 long int old_eip;
1230 old_eip = c->eip;
1231 c->eip = c->src.val;
1232 c->src.val = old_eip;
1233 emulate_push(ctxt);
1234 break;
1235 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001236 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001237 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001238 break;
1239 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001240 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001241 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001242 }
1243 return 0;
1244}
1245
1246static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1247 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001248 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001249{
1250 struct decode_cache *c = &ctxt->decode;
1251 u64 old, new;
1252 int rc;
1253
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001254 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001255 if (rc != 0)
1256 return rc;
1257
1258 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1259 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1260
1261 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1262 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001263 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001264
1265 } else {
1266 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1267 (u32) c->regs[VCPU_REGS_RBX];
1268
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001269 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270 if (rc != 0)
1271 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001272 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001273 }
1274 return 0;
1275}
1276
1277static inline int writeback(struct x86_emulate_ctxt *ctxt,
1278 struct x86_emulate_ops *ops)
1279{
1280 int rc;
1281 struct decode_cache *c = &ctxt->decode;
1282
1283 switch (c->dst.type) {
1284 case OP_REG:
1285 /* The 4-byte case *is* correct:
1286 * in 64-bit mode we zero-extend.
1287 */
1288 switch (c->dst.bytes) {
1289 case 1:
1290 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1291 break;
1292 case 2:
1293 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1294 break;
1295 case 4:
1296 *c->dst.ptr = (u32)c->dst.val;
1297 break; /* 64b: zero-ext */
1298 case 8:
1299 *c->dst.ptr = c->dst.val;
1300 break;
1301 }
1302 break;
1303 case OP_MEM:
1304 if (c->lock_prefix)
1305 rc = ops->cmpxchg_emulated(
1306 (unsigned long)c->dst.ptr,
1307 &c->dst.orig_val,
1308 &c->dst.val,
1309 c->dst.bytes,
1310 ctxt->vcpu);
1311 else
1312 rc = ops->write_emulated(
1313 (unsigned long)c->dst.ptr,
1314 &c->dst.val,
1315 c->dst.bytes,
1316 ctxt->vcpu);
1317 if (rc != 0)
1318 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001319 break;
1320 case OP_NONE:
1321 /* no writeback */
1322 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001323 default:
1324 break;
1325 }
1326 return 0;
1327}
1328
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001329int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001330x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001331{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001332 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001333 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001334 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001335 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001336 unsigned int port;
1337 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001338 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001339
Laurent Vivier34273182007-09-18 11:27:37 +02001340 /* Shadow copy of register state. Committed on successful emulation.
1341 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1342 * modify them.
1343 */
1344
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001345 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001346 saved_eip = c->eip;
1347
Avi Kivityc7e75a32007-10-28 16:34:25 +02001348 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001349 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001350
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001351 if (c->rep_prefix && (c->d & String)) {
1352 /* All REP prefixes have the same first termination condition */
1353 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001354 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001355 goto done;
1356 }
1357 /* The second termination condition only applies for REPE
1358 * and REPNE. Test if the repeat string operation prefix is
1359 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1360 * corresponding termination condition according to:
1361 * - if REPE/REPZ and ZF = 0 then done
1362 * - if REPNE/REPNZ and ZF = 1 then done
1363 */
1364 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1365 (c->b == 0xae) || (c->b == 0xaf)) {
1366 if ((c->rep_prefix == REPE_PREFIX) &&
1367 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001368 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001369 goto done;
1370 }
1371 if ((c->rep_prefix == REPNE_PREFIX) &&
1372 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001373 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001374 goto done;
1375 }
1376 }
1377 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001378 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001379 }
1380
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001381 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001382 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001383 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001384 rc = ops->read_emulated((unsigned long)c->src.ptr,
1385 &c->src.val,
1386 c->src.bytes,
1387 ctxt->vcpu);
1388 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001389 goto done;
1390 c->src.orig_val = c->src.val;
1391 }
1392
1393 if ((c->d & DstMask) == ImplicitOps)
1394 goto special_insn;
1395
1396
1397 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001398 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001399 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1400 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001401 if (c->d & BitOp) {
1402 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001403
Laurent Viviere4e03de2007-09-18 11:52:50 +02001404 c->dst.ptr = (void *)c->dst.ptr +
1405 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001406 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001407 if (!(c->d & Mov) &&
1408 /* optimisation - avoid slow emulated read */
1409 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1410 &c->dst.val,
1411 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001412 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001413 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001414 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001415
Avi Kivity018a98d2007-11-27 19:30:56 +02001416special_insn:
1417
Laurent Viviere4e03de2007-09-18 11:52:50 +02001418 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 goto twobyte_insn;
1420
Laurent Viviere4e03de2007-09-18 11:52:50 +02001421 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 case 0x00 ... 0x05:
1423 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001424 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 break;
1426 case 0x08 ... 0x0d:
1427 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 break;
1430 case 0x10 ... 0x15:
1431 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 break;
1434 case 0x18 ... 0x1d:
1435 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001436 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001438 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001440 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 break;
1442 case 0x28 ... 0x2d:
1443 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001444 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 break;
1446 case 0x30 ... 0x35:
1447 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001448 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 break;
1450 case 0x38 ... 0x3d:
1451 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001452 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001454 case 0x40 ... 0x47: /* inc r16/r32 */
1455 emulate_1op("inc", c->dst, ctxt->eflags);
1456 break;
1457 case 0x48 ... 0x4f: /* dec r16/r32 */
1458 emulate_1op("dec", c->dst, ctxt->eflags);
1459 break;
1460 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001461 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001462 break;
1463 case 0x58 ... 0x5f: /* pop reg */
1464 pop_instruction:
Avi Kivity8a09b682008-11-27 18:06:33 +02001465 c->src.bytes = c->op_bytes;
1466 rc = emulate_pop(ctxt, ops);
1467 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001468 goto done;
Avi Kivity8a09b682008-11-27 18:06:33 +02001469 c->dst.val = c->src.val;
Avi Kivity33615aa2007-10-31 11:15:56 +02001470 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001472 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001476 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001477 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001478 emulate_push(ctxt);
1479 break;
1480 case 0x6c: /* insb */
1481 case 0x6d: /* insw/insd */
1482 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1483 1,
1484 (c->d & ByteOp) ? 1 : c->op_bytes,
1485 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001486 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001487 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001488 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001489 c->regs[VCPU_REGS_RDI]),
1490 c->rep_prefix,
1491 c->regs[VCPU_REGS_RDX]) == 0) {
1492 c->eip = saved_eip;
1493 return -1;
1494 }
1495 return 0;
1496 case 0x6e: /* outsb */
1497 case 0x6f: /* outsw/outsd */
1498 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1499 0,
1500 (c->d & ByteOp) ? 1 : c->op_bytes,
1501 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001502 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001503 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001504 register_address(c,
1505 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001506 c->regs[VCPU_REGS_RSI]),
1507 c->rep_prefix,
1508 c->regs[VCPU_REGS_RDX]) == 0) {
1509 c->eip = saved_eip;
1510 return -1;
1511 }
1512 return 0;
1513 case 0x70 ... 0x7f: /* jcc (short) */ {
1514 int rel = insn_fetch(s8, 1, c->eip);
1515
1516 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001517 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001518 break;
1519 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001521 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 case 0:
1523 goto add;
1524 case 1:
1525 goto or;
1526 case 2:
1527 goto adc;
1528 case 3:
1529 goto sbb;
1530 case 4:
1531 goto and;
1532 case 5:
1533 goto sub;
1534 case 6:
1535 goto xor;
1536 case 7:
1537 goto cmp;
1538 }
1539 break;
1540 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001541 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542 break;
1543 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001544 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001546 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001548 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 break;
1550 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001551 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552 break;
1553 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001554 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555 break; /* 64b reg: zero-extend */
1556 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001557 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558 break;
1559 }
1560 /*
1561 * Write back the memory destination with implicit LOCK
1562 * prefix.
1563 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001564 c->dst.val = c->src.val;
1565 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001568 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001569 case 0x8c: { /* mov r/m, sreg */
1570 struct kvm_segment segreg;
1571
1572 if (c->modrm_reg <= 5)
1573 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1574 else {
1575 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1576 c->modrm);
1577 goto cannot_emulate;
1578 }
1579 c->dst.val = segreg.selector;
1580 break;
1581 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001582 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001583 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001584 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001585 case 0x8e: { /* mov seg, r/m16 */
1586 uint16_t sel;
1587 int type_bits;
1588 int err;
1589
1590 sel = c->src.val;
1591 if (c->modrm_reg <= 5) {
1592 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1593 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1594 type_bits, c->modrm_reg);
1595 } else {
1596 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1597 c->modrm);
1598 goto cannot_emulate;
1599 }
1600
1601 if (err < 0)
1602 goto cannot_emulate;
1603
1604 c->dst.type = OP_NONE; /* Disable writeback. */
1605 break;
1606 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001608 rc = emulate_grp1a(ctxt, ops);
1609 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001612 case 0x90: /* nop / xchg r8,rax */
1613 if (!(c->rex_prefix & 1)) { /* nop */
1614 c->dst.type = OP_NONE;
1615 break;
1616 }
1617 case 0x91 ... 0x97: /* xchg reg,rax */
1618 c->src.type = c->dst.type = OP_REG;
1619 c->src.bytes = c->dst.bytes = c->op_bytes;
1620 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1621 c->src.val = *(c->src.ptr);
1622 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001623 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001624 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001625 emulate_push(ctxt);
1626 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001627 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001628 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001629 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001630 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001631 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001632 case 0xa0 ... 0xa1: /* mov */
1633 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1634 c->dst.val = c->src.val;
1635 break;
1636 case 0xa2 ... 0xa3: /* mov */
1637 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1638 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001640 c->dst.type = OP_MEM;
1641 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001642 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001643 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001644 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001645 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001646 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001647 c->regs[VCPU_REGS_RSI]),
1648 &c->dst.val,
1649 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001651 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001652 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001653 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001654 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001655 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001656 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 break;
1658 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001659 c->src.type = OP_NONE; /* Disable writeback. */
1660 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001661 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001662 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001663 c->regs[VCPU_REGS_RSI]);
1664 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1665 &c->src.val,
1666 c->src.bytes,
1667 ctxt->vcpu)) != 0)
1668 goto done;
1669
1670 c->dst.type = OP_NONE; /* Disable writeback. */
1671 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001672 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001673 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001674 c->regs[VCPU_REGS_RDI]);
1675 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1676 &c->dst.val,
1677 c->dst.bytes,
1678 ctxt->vcpu)) != 0)
1679 goto done;
1680
1681 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1682
1683 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1684
Harvey Harrison7a9572752008-02-19 07:40:41 -08001685 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001686 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1687 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001688 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001689 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1690 : c->dst.bytes);
1691
1692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001694 c->dst.type = OP_MEM;
1695 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001696 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001697 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001698 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001699 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001700 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001701 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001702 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 break;
1704 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001705 c->dst.type = OP_REG;
1706 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1707 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001708 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001709 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001710 c->regs[VCPU_REGS_RSI]),
1711 &c->dst.val,
1712 c->dst.bytes,
1713 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001715 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001716 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001717 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 break;
1719 case 0xae ... 0xaf: /* scas */
1720 DPRINTF("Urk! I don't handle SCAS.\n");
1721 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001722 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001723 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001724 case 0xc0 ... 0xc1:
1725 emulate_grp2(ctxt);
1726 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001727 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001728 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001729 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001730 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001731 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001732 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1733 mov:
1734 c->dst.val = c->src.val;
1735 break;
1736 case 0xd0 ... 0xd1: /* Grp2 */
1737 c->src.val = 1;
1738 emulate_grp2(ctxt);
1739 break;
1740 case 0xd2 ... 0xd3: /* Grp2 */
1741 c->src.val = c->regs[VCPU_REGS_RCX];
1742 emulate_grp2(ctxt);
1743 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001744 case 0xe4: /* inb */
1745 case 0xe5: /* in */
1746 port = insn_fetch(u8, 1, c->eip);
1747 io_dir_in = 1;
1748 goto do_io;
1749 case 0xe6: /* outb */
1750 case 0xe7: /* out */
1751 port = insn_fetch(u8, 1, c->eip);
1752 io_dir_in = 0;
1753 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001754 case 0xe8: /* call (near) */ {
1755 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001756 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001757 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001758 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001759 break;
1760 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001761 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001762 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001763 default:
1764 DPRINTF("Call: Invalid op_bytes\n");
1765 goto cannot_emulate;
1766 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001767 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001768 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001769 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 emulate_push(ctxt);
1771 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001772 }
1773 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001774 goto jmp;
1775 case 0xea: /* jmp far */ {
1776 uint32_t eip;
1777 uint16_t sel;
1778
1779 switch (c->op_bytes) {
1780 case 2:
1781 eip = insn_fetch(u16, 2, c->eip);
1782 break;
1783 case 4:
1784 eip = insn_fetch(u32, 4, c->eip);
1785 break;
1786 default:
1787 DPRINTF("jmp far: Invalid op_bytes\n");
1788 goto cannot_emulate;
1789 }
1790 sel = insn_fetch(u16, 2, c->eip);
1791 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1792 DPRINTF("jmp far: Failed to load CS descriptor\n");
1793 goto cannot_emulate;
1794 }
1795
1796 c->eip = eip;
1797 break;
1798 }
1799 case 0xeb:
1800 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001801 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001802 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001803 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001804 case 0xec: /* in al,dx */
1805 case 0xed: /* in (e/r)ax,dx */
1806 port = c->regs[VCPU_REGS_RDX];
1807 io_dir_in = 1;
1808 goto do_io;
1809 case 0xee: /* out al,dx */
1810 case 0xef: /* out (e/r)ax,dx */
1811 port = c->regs[VCPU_REGS_RDX];
1812 io_dir_in = 0;
1813 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1814 (c->d & ByteOp) ? 1 : c->op_bytes,
1815 port) != 0) {
1816 c->eip = saved_eip;
1817 goto cannot_emulate;
1818 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001819 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001820 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001821 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001822 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001823 case 0xf5: /* cmc */
1824 /* complement carry flag from eflags reg */
1825 ctxt->eflags ^= EFLG_CF;
1826 c->dst.type = OP_NONE; /* Disable writeback. */
1827 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001828 case 0xf6 ... 0xf7: /* Grp3 */
1829 rc = emulate_grp3(ctxt, ops);
1830 if (rc != 0)
1831 goto done;
1832 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001833 case 0xf8: /* clc */
1834 ctxt->eflags &= ~EFLG_CF;
1835 c->dst.type = OP_NONE; /* Disable writeback. */
1836 break;
1837 case 0xfa: /* cli */
1838 ctxt->eflags &= ~X86_EFLAGS_IF;
1839 c->dst.type = OP_NONE; /* Disable writeback. */
1840 break;
1841 case 0xfb: /* sti */
1842 ctxt->eflags |= X86_EFLAGS_IF;
1843 c->dst.type = OP_NONE; /* Disable writeback. */
1844 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001845 case 0xfc: /* cld */
1846 ctxt->eflags &= ~EFLG_DF;
1847 c->dst.type = OP_NONE; /* Disable writeback. */
1848 break;
1849 case 0xfd: /* std */
1850 ctxt->eflags |= EFLG_DF;
1851 c->dst.type = OP_NONE; /* Disable writeback. */
1852 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001853 case 0xfe ... 0xff: /* Grp4/Grp5 */
1854 rc = emulate_grp45(ctxt, ops);
1855 if (rc != 0)
1856 goto done;
1857 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001859
1860writeback:
1861 rc = writeback(ctxt, ops);
1862 if (rc != 0)
1863 goto done;
1864
1865 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001866 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001867 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001868
1869done:
1870 if (rc == X86EMUL_UNHANDLEABLE) {
1871 c->eip = saved_eip;
1872 return -1;
1873 }
1874 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875
1876twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001877 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001879 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 u16 size;
1881 unsigned long address;
1882
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001883 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001884 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001885 goto cannot_emulate;
1886
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001887 rc = kvm_fix_hypercall(ctxt->vcpu);
1888 if (rc)
1889 goto done;
1890
Avi Kivity33e38852008-05-21 15:34:25 +03001891 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001892 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001893 /* Disable writeback. */
1894 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001895 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001897 rc = read_descriptor(ctxt, ops, c->src.ptr,
1898 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 if (rc)
1900 goto done;
1901 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001902 /* Disable writeback. */
1903 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001905 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001906 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001907 rc = kvm_fix_hypercall(ctxt->vcpu);
1908 if (rc)
1909 goto done;
1910 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001911 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001913 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001914 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001915 if (rc)
1916 goto done;
1917 realmode_lidt(ctxt->vcpu, size, address);
1918 }
Avi Kivity16286d02008-04-14 14:40:50 +03001919 /* Disable writeback. */
1920 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 break;
1922 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001923 c->dst.bytes = 2;
1924 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 break;
1926 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001927 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1928 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001929 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 break;
1931 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001932 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001933 /* Disable writeback. */
1934 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 break;
1936 default:
1937 goto cannot_emulate;
1938 }
1939 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001940 case 0x06:
1941 emulate_clts(ctxt->vcpu);
1942 c->dst.type = OP_NONE;
1943 break;
1944 case 0x08: /* invd */
1945 case 0x09: /* wbinvd */
1946 case 0x0d: /* GrpP (prefetch) */
1947 case 0x18: /* Grp16 (prefetch/nop) */
1948 c->dst.type = OP_NONE;
1949 break;
1950 case 0x20: /* mov cr, reg */
1951 if (c->modrm_mod != 3)
1952 goto cannot_emulate;
1953 c->regs[c->modrm_rm] =
1954 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1955 c->dst.type = OP_NONE; /* no writeback */
1956 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001958 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001960 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001961 if (rc)
1962 goto cannot_emulate;
1963 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001965 case 0x22: /* mov reg, cr */
1966 if (c->modrm_mod != 3)
1967 goto cannot_emulate;
1968 realmode_set_cr(ctxt->vcpu,
1969 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1970 c->dst.type = OP_NONE;
1971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001973 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001975 rc = emulator_set_dr(ctxt, c->modrm_reg,
1976 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001977 if (rc)
1978 goto cannot_emulate;
1979 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001981 case 0x30:
1982 /* wrmsr */
1983 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1984 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1985 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1986 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001987 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001988 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001989 }
1990 rc = X86EMUL_CONTINUE;
1991 c->dst.type = OP_NONE;
1992 break;
1993 case 0x32:
1994 /* rdmsr */
1995 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1996 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001997 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001998 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001999 } else {
2000 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2001 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2002 }
2003 rc = X86EMUL_CONTINUE;
2004 c->dst.type = OP_NONE;
2005 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002007 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002008 if (!test_cc(c->b, ctxt->eflags))
2009 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002011 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
2012 long int rel;
2013
2014 switch (c->op_bytes) {
2015 case 2:
2016 rel = insn_fetch(s16, 2, c->eip);
2017 break;
2018 case 4:
2019 rel = insn_fetch(s32, 4, c->eip);
2020 break;
2021 case 8:
2022 rel = insn_fetch(s64, 8, c->eip);
2023 break;
2024 default:
2025 DPRINTF("jnz: Invalid op_bytes\n");
2026 goto cannot_emulate;
2027 }
2028 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08002029 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02002030 c->dst.type = OP_NONE;
2031 break;
2032 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03002033 case 0xa3:
2034 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002035 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002036 /* only subword offset */
2037 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002038 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002039 break;
2040 case 0xab:
2041 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002042 /* only subword offset */
2043 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002044 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002045 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002046 case 0xae: /* clflush */
2047 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 case 0xb0 ... 0xb1: /* cmpxchg */
2049 /*
2050 * Save real source value, then compare EAX against
2051 * destination.
2052 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002053 c->src.orig_val = c->src.val;
2054 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002055 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2056 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002058 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 } else {
2060 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002061 c->dst.type = OP_REG;
2062 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 }
2064 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 case 0xb3:
2066 btr: /* btr */
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("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002072 c->dst.bytes = c->op_bytes;
2073 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2074 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002077 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 case 0:
2079 goto bt;
2080 case 1:
2081 goto bts;
2082 case 2:
2083 goto btr;
2084 case 3:
2085 goto btc;
2086 }
2087 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002088 case 0xbb:
2089 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002090 /* only subword offset */
2091 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002092 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002093 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002095 c->dst.bytes = c->op_bytes;
2096 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2097 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002099 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002100 c->dst.bytes = c->op_bytes;
2101 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2102 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002103 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002105 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002106 if (rc != 0)
2107 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002108 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002109 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 }
2111 goto writeback;
2112
2113cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002114 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002115 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 return -1;
2117}