blob: a30d5fc17eefd405fdea69d71dcc59fff692ab33 [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' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030062#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030063#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define Mov (1<<9)
69#define BitOp (1<<10)
70#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020071#define String (1<<12) /* String instruction (rep capable) */
72#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020073#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
74#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
75#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010076/* Source 2 operand type */
77#define Src2None (0<<29)
78#define Src2CL (1<<29)
79#define Src2ImmByte (2<<29)
80#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030081#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080083
Avi Kivity43bb19c2008-01-18 12:46:50 +020084enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020085 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020086 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020087};
88
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010089static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080090 /* 0x00 - 0x07 */
91 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020093 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080094 /* 0x08 - 0x0F */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
97 0, 0, 0, 0,
98 /* 0x10 - 0x17 */
99 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
101 0, 0, 0, 0,
102 /* 0x18 - 0x1F */
103 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
105 0, 0, 0, 0,
106 /* 0x20 - 0x27 */
107 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200109 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 /* 0x28 - 0x2F */
111 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
112 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
113 0, 0, 0, 0,
114 /* 0x30 - 0x37 */
115 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
116 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
117 0, 0, 0, 0,
118 /* 0x38 - 0x3F */
119 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
122 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700123 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200124 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700125 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200126 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300127 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200128 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
129 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300130 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200131 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
132 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700133 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700135 0, 0, 0, 0,
136 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300137 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300138 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
139 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300140 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300141 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
142 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300143 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300144 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
145 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200147 Group | Group1_80, Group | Group1_81,
148 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
150 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
151 /* 0x88 - 0x8F */
152 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
153 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200154 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200155 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300156 /* 0x90 - 0x97 */
157 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
158 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300159 0, 0, SrcImm | Src2Imm16, 0,
160 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200162 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
163 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200164 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
165 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200167 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
168 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
169 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300170 /* 0xB0 - 0xB7 */
171 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
172 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
173 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
174 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
175 /* 0xB8 - 0xBF */
176 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
177 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
178 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
179 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300181 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200182 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300183 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300185 0, 0, 0, ImplicitOps | Stack,
186 ImplicitOps, SrcImmByte, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 /* 0xD0 - 0xD7 */
188 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
189 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
190 0, 0, 0, 0,
191 /* 0xD8 - 0xDF */
192 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300193 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300194 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300195 ByteOp | SrcImmUByte, SrcImmUByte,
196 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300197 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300198 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov782b8772009-04-12 13:36:25 +0300199 SrcImm | Src2Imm16, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300200 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
201 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xF0 - 0xF7 */
203 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200204 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700206 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300207 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208};
209
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100210static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200212 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200213 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0x10 - 0x1F */
215 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
216 /* 0x20 - 0x2F */
217 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300220 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0x40 - 0x47 */
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 /* 0x48 - 0x4F */
227 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
228 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
229 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
230 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
231 /* 0x50 - 0x5F */
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x60 - 0x6F */
234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x70 - 0x7F */
236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300238 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
239 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 /* 0x90 - 0x9F */
241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100243 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
244 DstMem | SrcReg | Src2ImmByte | ModRM,
245 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100247 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
248 DstMem | SrcReg | Src2ImmByte | ModRM,
249 DstMem | SrcReg | Src2CL | ModRM,
250 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 /* 0xB0 - 0xB7 */
252 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800253 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
255 DstReg | SrcMem16 | ModRM | Mov,
256 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800257 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
259 DstReg | SrcMem16 | ModRM | Mov,
260 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800261 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
262 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 /* 0xD0 - 0xDF */
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265 /* 0xE0 - 0xEF */
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267 /* 0xF0 - 0xFF */
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
269};
270
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100271static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200272 [Group1_80*8] =
273 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
274 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
275 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
276 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
277 [Group1_81*8] =
278 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
279 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
280 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
281 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
282 [Group1_82*8] =
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
286 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
287 [Group1_83*8] =
288 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
289 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
290 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
291 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200292 [Group1A*8] =
293 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200294 [Group3_Byte*8] =
295 ByteOp | SrcImm | DstMem | ModRM, 0,
296 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
297 0, 0, 0, 0,
298 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400299 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300300 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200301 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200302 [Group4*8] =
303 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
304 0, 0, 0, 0, 0, 0,
305 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300306 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
307 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300308 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200309 [Group7*8] =
310 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300311 SrcNone | ModRM | DstMem | Mov, 0,
312 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200313};
314
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100315static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200316 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000317 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300318 SrcNone | ModRM | DstMem | Mov, 0,
319 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200320};
321
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322/* EFLAGS bit definitions. */
323#define EFLG_OF (1<<11)
324#define EFLG_DF (1<<10)
325#define EFLG_SF (1<<7)
326#define EFLG_ZF (1<<6)
327#define EFLG_AF (1<<4)
328#define EFLG_PF (1<<2)
329#define EFLG_CF (1<<0)
330
331/*
332 * Instruction emulation:
333 * Most instructions are emulated directly via a fragment of inline assembly
334 * code. This allows us to save/restore EFLAGS and thus very easily pick up
335 * any modified flags.
336 */
337
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800338#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339#define _LO32 "k" /* force 32-bit operand */
340#define _STK "%%rsp" /* stack pointer */
341#elif defined(__i386__)
342#define _LO32 "" /* force 32-bit operand */
343#define _STK "%%esp" /* stack pointer */
344#endif
345
346/*
347 * These EFLAGS bits are restored from saved value during emulation, and
348 * any changes are written back to the saved value after emulation.
349 */
350#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
351
352/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200353#define _PRE_EFLAGS(_sav, _msk, _tmp) \
354 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
355 "movl %"_sav",%"_LO32 _tmp"; " \
356 "push %"_tmp"; " \
357 "push %"_tmp"; " \
358 "movl %"_msk",%"_LO32 _tmp"; " \
359 "andl %"_LO32 _tmp",("_STK"); " \
360 "pushf; " \
361 "notl %"_LO32 _tmp"; " \
362 "andl %"_LO32 _tmp",("_STK"); " \
363 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
364 "pop %"_tmp"; " \
365 "orl %"_LO32 _tmp",("_STK"); " \
366 "popf; " \
367 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368
369/* After executing instruction: write-back necessary bits in EFLAGS. */
370#define _POST_EFLAGS(_sav, _msk, _tmp) \
371 /* _sav |= EFLAGS & _msk; */ \
372 "pushf; " \
373 "pop %"_tmp"; " \
374 "andl %"_msk",%"_LO32 _tmp"; " \
375 "orl %"_LO32 _tmp",%"_sav"; "
376
Avi Kivitydda96d82008-11-26 15:14:10 +0200377#ifdef CONFIG_X86_64
378#define ON64(x) x
379#else
380#define ON64(x)
381#endif
382
Avi Kivity6b7ad612008-11-26 15:30:45 +0200383#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
384 do { \
385 __asm__ __volatile__ ( \
386 _PRE_EFLAGS("0", "4", "2") \
387 _op _suffix " %"_x"3,%1; " \
388 _POST_EFLAGS("0", "4", "2") \
389 : "=m" (_eflags), "=m" ((_dst).val), \
390 "=&r" (_tmp) \
391 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200392 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200393
394
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395/* Raw emulation: instruction has two explicit operands. */
396#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200397 do { \
398 unsigned long _tmp; \
399 \
400 switch ((_dst).bytes) { \
401 case 2: \
402 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
403 break; \
404 case 4: \
405 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
406 break; \
407 case 8: \
408 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
409 break; \
410 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 } while (0)
412
413#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
414 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200415 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400416 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200418 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 break; \
420 default: \
421 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
422 _wx, _wy, _lx, _ly, _qx, _qy); \
423 break; \
424 } \
425 } while (0)
426
427/* Source operand is byte-sized and may be restricted to just %cl. */
428#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
429 __emulate_2op(_op, _src, _dst, _eflags, \
430 "b", "c", "b", "c", "b", "c", "b", "c")
431
432/* Source operand is byte, word, long or quad sized. */
433#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
434 __emulate_2op(_op, _src, _dst, _eflags, \
435 "b", "q", "w", "r", _LO32, "r", "", "r")
436
437/* Source operand is word, long or quad sized. */
438#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
439 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
440 "w", "r", _LO32, "r", "", "r")
441
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100442/* Instruction has three operands and one operand is stored in ECX register */
443#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
444 do { \
445 unsigned long _tmp; \
446 _type _clv = (_cl).val; \
447 _type _srcv = (_src).val; \
448 _type _dstv = (_dst).val; \
449 \
450 __asm__ __volatile__ ( \
451 _PRE_EFLAGS("0", "5", "2") \
452 _op _suffix " %4,%1 \n" \
453 _POST_EFLAGS("0", "5", "2") \
454 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
455 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
456 ); \
457 \
458 (_cl).val = (unsigned long) _clv; \
459 (_src).val = (unsigned long) _srcv; \
460 (_dst).val = (unsigned long) _dstv; \
461 } while (0)
462
463#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
464 do { \
465 switch ((_dst).bytes) { \
466 case 2: \
467 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
468 "w", unsigned short); \
469 break; \
470 case 4: \
471 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
472 "l", unsigned int); \
473 break; \
474 case 8: \
475 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
476 "q", unsigned long)); \
477 break; \
478 } \
479 } while (0)
480
Avi Kivitydda96d82008-11-26 15:14:10 +0200481#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 do { \
483 unsigned long _tmp; \
484 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200485 __asm__ __volatile__ ( \
486 _PRE_EFLAGS("0", "3", "2") \
487 _op _suffix " %1; " \
488 _POST_EFLAGS("0", "3", "2") \
489 : "=m" (_eflags), "+m" ((_dst).val), \
490 "=&r" (_tmp) \
491 : "i" (EFLAGS_MASK)); \
492 } while (0)
493
494/* Instruction has only one explicit operand (no source operand). */
495#define emulate_1op(_op, _dst, _eflags) \
496 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400497 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200498 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
499 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
500 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
501 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 } \
503 } while (0)
504
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505/* Fetch next part of the instruction being emulated. */
506#define insn_fetch(_type, _size, _eip) \
507({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200508 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400509 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 goto done; \
511 (_eip) += (_size); \
512 (_type)_x; \
513})
514
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800515static inline unsigned long ad_mask(struct decode_cache *c)
516{
517 return (1UL << (c->ad_bytes << 3)) - 1;
518}
519
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800521static inline unsigned long
522address_mask(struct decode_cache *c, unsigned long reg)
523{
524 if (c->ad_bytes == sizeof(unsigned long))
525 return reg;
526 else
527 return reg & ad_mask(c);
528}
529
530static inline unsigned long
531register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
532{
533 return base + address_mask(c, reg);
534}
535
Harvey Harrison7a9572752008-02-19 07:40:41 -0800536static inline void
537register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
538{
539 if (c->ad_bytes == sizeof(unsigned long))
540 *reg += inc;
541 else
542 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
543}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544
Harvey Harrison7a9572752008-02-19 07:40:41 -0800545static inline void jmp_rel(struct decode_cache *c, int rel)
546{
547 register_address_increment(c, &c->eip, rel);
548}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300549
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300550static void set_seg_override(struct decode_cache *c, int seg)
551{
552 c->has_seg_override = true;
553 c->seg_override = seg;
554}
555
556static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
557{
558 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
559 return 0;
560
561 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
562}
563
564static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
565 struct decode_cache *c)
566{
567 if (!c->has_seg_override)
568 return 0;
569
570 return seg_base(ctxt, c->seg_override);
571}
572
573static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
574{
575 return seg_base(ctxt, VCPU_SREG_ES);
576}
577
578static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
579{
580 return seg_base(ctxt, VCPU_SREG_SS);
581}
582
Avi Kivity62266862007-11-20 13:15:52 +0200583static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
584 struct x86_emulate_ops *ops,
585 unsigned long linear, u8 *dest)
586{
587 struct fetch_cache *fc = &ctxt->decode.fetch;
588 int rc;
589 int size;
590
591 if (linear < fc->start || linear >= fc->end) {
592 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
593 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
594 if (rc)
595 return rc;
596 fc->start = linear;
597 fc->end = linear + size;
598 }
599 *dest = fc->data[linear - fc->start];
600 return 0;
601}
602
603static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
604 struct x86_emulate_ops *ops,
605 unsigned long eip, void *dest, unsigned size)
606{
607 int rc = 0;
608
609 eip += ctxt->cs_base;
610 while (size--) {
611 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
612 if (rc)
613 return rc;
614 }
615 return 0;
616}
617
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000618/*
619 * Given the 'reg' portion of a ModRM byte, and a register block, return a
620 * pointer into the block that addresses the relevant register.
621 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
622 */
623static void *decode_register(u8 modrm_reg, unsigned long *regs,
624 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625{
626 void *p;
627
628 p = &regs[modrm_reg];
629 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
630 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
631 return p;
632}
633
634static int read_descriptor(struct x86_emulate_ctxt *ctxt,
635 struct x86_emulate_ops *ops,
636 void *ptr,
637 u16 *size, unsigned long *address, int op_bytes)
638{
639 int rc;
640
641 if (op_bytes == 2)
642 op_bytes = 3;
643 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300644 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
645 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 if (rc)
647 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300648 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
649 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 return rc;
651}
652
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300653static int test_cc(unsigned int condition, unsigned int flags)
654{
655 int rc = 0;
656
657 switch ((condition & 15) >> 1) {
658 case 0: /* o */
659 rc |= (flags & EFLG_OF);
660 break;
661 case 1: /* b/c/nae */
662 rc |= (flags & EFLG_CF);
663 break;
664 case 2: /* z/e */
665 rc |= (flags & EFLG_ZF);
666 break;
667 case 3: /* be/na */
668 rc |= (flags & (EFLG_CF|EFLG_ZF));
669 break;
670 case 4: /* s */
671 rc |= (flags & EFLG_SF);
672 break;
673 case 5: /* p/pe */
674 rc |= (flags & EFLG_PF);
675 break;
676 case 7: /* le/ng */
677 rc |= (flags & EFLG_ZF);
678 /* fall through */
679 case 6: /* l/nge */
680 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
681 break;
682 }
683
684 /* Odd condition identifiers (lsb == 1) have inverted sense. */
685 return (!!rc ^ (condition & 1));
686}
687
Avi Kivity3c118e22007-10-31 10:27:04 +0200688static void decode_register_operand(struct operand *op,
689 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200690 int inhibit_bytereg)
691{
Avi Kivity33615aa2007-10-31 11:15:56 +0200692 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200693 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200694
695 if (!(c->d & ModRM))
696 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200697 op->type = OP_REG;
698 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200699 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200700 op->val = *(u8 *)op->ptr;
701 op->bytes = 1;
702 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200703 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200704 op->bytes = c->op_bytes;
705 switch (op->bytes) {
706 case 2:
707 op->val = *(u16 *)op->ptr;
708 break;
709 case 4:
710 op->val = *(u32 *)op->ptr;
711 break;
712 case 8:
713 op->val = *(u64 *) op->ptr;
714 break;
715 }
716 }
717 op->orig_val = op->val;
718}
719
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720static int decode_modrm(struct x86_emulate_ctxt *ctxt,
721 struct x86_emulate_ops *ops)
722{
723 struct decode_cache *c = &ctxt->decode;
724 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700725 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200726 int rc = 0;
727
728 if (c->rex_prefix) {
729 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
730 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
731 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
732 }
733
734 c->modrm = insn_fetch(u8, 1, c->eip);
735 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
736 c->modrm_reg |= (c->modrm & 0x38) >> 3;
737 c->modrm_rm |= (c->modrm & 0x07);
738 c->modrm_ea = 0;
739 c->use_modrm_ea = 1;
740
741 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300742 c->modrm_ptr = decode_register(c->modrm_rm,
743 c->regs, c->d & ByteOp);
744 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200745 return rc;
746 }
747
748 if (c->ad_bytes == 2) {
749 unsigned bx = c->regs[VCPU_REGS_RBX];
750 unsigned bp = c->regs[VCPU_REGS_RBP];
751 unsigned si = c->regs[VCPU_REGS_RSI];
752 unsigned di = c->regs[VCPU_REGS_RDI];
753
754 /* 16-bit ModR/M decode. */
755 switch (c->modrm_mod) {
756 case 0:
757 if (c->modrm_rm == 6)
758 c->modrm_ea += insn_fetch(u16, 2, c->eip);
759 break;
760 case 1:
761 c->modrm_ea += insn_fetch(s8, 1, c->eip);
762 break;
763 case 2:
764 c->modrm_ea += insn_fetch(u16, 2, c->eip);
765 break;
766 }
767 switch (c->modrm_rm) {
768 case 0:
769 c->modrm_ea += bx + si;
770 break;
771 case 1:
772 c->modrm_ea += bx + di;
773 break;
774 case 2:
775 c->modrm_ea += bp + si;
776 break;
777 case 3:
778 c->modrm_ea += bp + di;
779 break;
780 case 4:
781 c->modrm_ea += si;
782 break;
783 case 5:
784 c->modrm_ea += di;
785 break;
786 case 6:
787 if (c->modrm_mod != 0)
788 c->modrm_ea += bp;
789 break;
790 case 7:
791 c->modrm_ea += bx;
792 break;
793 }
794 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
795 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300796 if (!c->has_seg_override)
797 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200798 c->modrm_ea = (u16)c->modrm_ea;
799 } else {
800 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700801 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200802 sib = insn_fetch(u8, 1, c->eip);
803 index_reg |= (sib >> 3) & 7;
804 base_reg |= sib & 7;
805 scale = sib >> 6;
806
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700807 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
808 c->modrm_ea += insn_fetch(s32, 4, c->eip);
809 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700811 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200812 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700813 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
814 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700815 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700816 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200818 switch (c->modrm_mod) {
819 case 0:
820 if (c->modrm_rm == 5)
821 c->modrm_ea += insn_fetch(s32, 4, c->eip);
822 break;
823 case 1:
824 c->modrm_ea += insn_fetch(s8, 1, c->eip);
825 break;
826 case 2:
827 c->modrm_ea += insn_fetch(s32, 4, c->eip);
828 break;
829 }
830 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200831done:
832 return rc;
833}
834
835static int decode_abs(struct x86_emulate_ctxt *ctxt,
836 struct x86_emulate_ops *ops)
837{
838 struct decode_cache *c = &ctxt->decode;
839 int rc = 0;
840
841 switch (c->ad_bytes) {
842 case 2:
843 c->modrm_ea = insn_fetch(u16, 2, c->eip);
844 break;
845 case 4:
846 c->modrm_ea = insn_fetch(u32, 4, c->eip);
847 break;
848 case 8:
849 c->modrm_ea = insn_fetch(u64, 8, c->eip);
850 break;
851 }
852done:
853 return rc;
854}
855
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200857x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200859 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200862 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
864 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865
Laurent Viviere4e03de2007-09-18 11:52:50 +0200866 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300867 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300868 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800869 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
871 switch (mode) {
872 case X86EMUL_MODE_REAL:
873 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200874 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 break;
876 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200877 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800879#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200881 def_op_bytes = 4;
882 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 break;
884#endif
885 default:
886 return -1;
887 }
888
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200889 c->op_bytes = def_op_bytes;
890 c->ad_bytes = def_ad_bytes;
891
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200893 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200894 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200896 /* switch between 2/4 bytes */
897 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 break;
899 case 0x67: /* address-size override */
900 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200902 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200904 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200905 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300908 case 0x2e: /* CS override */
909 case 0x36: /* SS override */
910 case 0x3e: /* DS override */
911 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 break;
913 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300915 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200917 case 0x40 ... 0x4f: /* REX */
918 if (mode != X86EMUL_MODE_PROT64)
919 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200920 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200921 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200923 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200925 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100926 c->rep_prefix = REPNE_PREFIX;
927 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100929 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 default:
932 goto done_prefixes;
933 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200934
935 /* Any legacy prefix after a REX prefix nullifies its effect. */
936
Avi Kivity33615aa2007-10-31 11:15:56 +0200937 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 }
939
940done_prefixes:
941
942 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200944 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200945 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
947 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200948 c->d = opcode_table[c->b];
949 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200951 if (c->b == 0x0f) {
952 c->twobyte = 1;
953 c->b = insn_fetch(u8, 1, c->eip);
954 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200956 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
Avi Kivitye09d0822008-01-18 12:38:59 +0200958 if (c->d & Group) {
959 group = c->d & GroupMask;
960 c->modrm = insn_fetch(u8, 1, c->eip);
961 --c->eip;
962
963 group = (group << 3) + ((c->modrm >> 3) & 7);
964 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
965 c->d = group2_table[group];
966 else
967 c->d = group_table[group];
968 }
969
970 /* Unrecognised? */
971 if (c->d == 0) {
972 DPRINTF("Cannot emulate %02x\n", c->b);
973 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 }
975
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200976 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
977 c->op_bytes = 8;
978
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 if (c->d & ModRM)
981 rc = decode_modrm(ctxt, ops);
982 else if (c->d & MemAbs)
983 rc = decode_abs(ctxt, ops);
984 if (rc)
985 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300987 if (!c->has_seg_override)
988 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200989
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300990 if (!(!c->twobyte && c->b == 0x8d))
991 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200992
993 if (c->ad_bytes != 8)
994 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 /*
996 * Decode and fetch the source operand: register, memory
997 * or immediate.
998 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200999 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 case SrcNone:
1001 break;
1002 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001003 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 break;
1005 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001006 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 goto srcmem_common;
1008 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 goto srcmem_common;
1011 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->src.bytes = (c->d & ByteOp) ? 1 :
1013 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001014 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001015 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001016 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001017 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001018 /*
1019 * For instructions with a ModR/M byte, switch to register
1020 * access if Mod = 3.
1021 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001022 if ((c->d & ModRM) && c->modrm_mod == 3) {
1023 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001024 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001025 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001026 break;
1027 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001028 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 break;
1030 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001031 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001032 c->src.type = OP_IMM;
1033 c->src.ptr = (unsigned long *)c->eip;
1034 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1035 if (c->src.bytes == 8)
1036 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001038 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001040 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 break;
1042 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
1045 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 break;
1048 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001049 if ((c->d & SrcMask) == SrcImmU) {
1050 switch (c->src.bytes) {
1051 case 1:
1052 c->src.val &= 0xff;
1053 break;
1054 case 2:
1055 c->src.val &= 0xffff;
1056 break;
1057 case 4:
1058 c->src.val &= 0xffffffff;
1059 break;
1060 }
1061 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 break;
1063 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001064 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001065 c->src.type = OP_IMM;
1066 c->src.ptr = (unsigned long *)c->eip;
1067 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001068 if ((c->d & SrcMask) == SrcImmByte)
1069 c->src.val = insn_fetch(s8, 1, c->eip);
1070 else
1071 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001073 case SrcOne:
1074 c->src.bytes = 1;
1075 c->src.val = 1;
1076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 }
1078
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001079 /*
1080 * Decode and fetch the second source operand: register, memory
1081 * or immediate.
1082 */
1083 switch (c->d & Src2Mask) {
1084 case Src2None:
1085 break;
1086 case Src2CL:
1087 c->src2.bytes = 1;
1088 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1089 break;
1090 case Src2ImmByte:
1091 c->src2.type = OP_IMM;
1092 c->src2.ptr = (unsigned long *)c->eip;
1093 c->src2.bytes = 1;
1094 c->src2.val = insn_fetch(u8, 1, c->eip);
1095 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001096 case Src2Imm16:
1097 c->src2.type = OP_IMM;
1098 c->src2.ptr = (unsigned long *)c->eip;
1099 c->src2.bytes = 2;
1100 c->src2.val = insn_fetch(u16, 2, c->eip);
1101 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001102 case Src2One:
1103 c->src2.bytes = 1;
1104 c->src2.val = 1;
1105 break;
1106 }
1107
Avi Kivity038e51d2007-01-22 20:40:40 -08001108 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001109 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001110 case ImplicitOps:
1111 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001112 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001113 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001114 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001115 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001116 break;
1117 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001118 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001119 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001120 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001121 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001122 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001123 break;
1124 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001125 c->dst.type = OP_MEM;
1126 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001127 case DstAcc:
1128 c->dst.type = OP_REG;
1129 c->dst.bytes = c->op_bytes;
1130 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1131 switch (c->op_bytes) {
1132 case 1:
1133 c->dst.val = *(u8 *)c->dst.ptr;
1134 break;
1135 case 2:
1136 c->dst.val = *(u16 *)c->dst.ptr;
1137 break;
1138 case 4:
1139 c->dst.val = *(u32 *)c->dst.ptr;
1140 break;
1141 }
1142 c->dst.orig_val = c->dst.val;
1143 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001144 }
1145
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001146 if (c->rip_relative)
1147 c->modrm_ea += c->eip;
1148
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001149done:
1150 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1151}
1152
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001153static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1154{
1155 struct decode_cache *c = &ctxt->decode;
1156
1157 c->dst.type = OP_MEM;
1158 c->dst.bytes = c->op_bytes;
1159 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001160 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001161 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001162 c->regs[VCPU_REGS_RSP]);
1163}
1164
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001165static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001166 struct x86_emulate_ops *ops,
1167 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001168{
1169 struct decode_cache *c = &ctxt->decode;
1170 int rc;
1171
Avi Kivity781d0ed2008-11-27 18:00:28 +02001172 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1173 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001174 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175 if (rc != 0)
1176 return rc;
1177
Avi Kivity350f69d2009-01-05 11:12:40 +02001178 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001179 return rc;
1180}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001181
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001182static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1183 struct x86_emulate_ops *ops)
1184{
1185 struct decode_cache *c = &ctxt->decode;
1186 int rc;
1187
Avi Kivity350f69d2009-01-05 11:12:40 +02001188 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001189 if (rc != 0)
1190 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001191 return 0;
1192}
1193
Laurent Vivier05f086f2007-09-24 11:10:55 +02001194static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001195{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001196 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 switch (c->modrm_reg) {
1198 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 break;
1201 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001202 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001203 break;
1204 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001205 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001206 break;
1207 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001208 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001209 break;
1210 case 4: /* sal/shl */
1211 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001212 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213 break;
1214 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001215 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001216 break;
1217 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001218 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001219 break;
1220 }
1221}
1222
1223static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001224 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001225{
1226 struct decode_cache *c = &ctxt->decode;
1227 int rc = 0;
1228
1229 switch (c->modrm_reg) {
1230 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001231 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001232 break;
1233 case 2: /* not */
1234 c->dst.val = ~c->dst.val;
1235 break;
1236 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001237 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001238 break;
1239 default:
1240 DPRINTF("Cannot emulate %02x\n", c->b);
1241 rc = X86EMUL_UNHANDLEABLE;
1242 break;
1243 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001244 return rc;
1245}
1246
1247static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001248 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001249{
1250 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251
1252 switch (c->modrm_reg) {
1253 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001254 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001255 break;
1256 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001257 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001259 case 2: /* call near abs */ {
1260 long int old_eip;
1261 old_eip = c->eip;
1262 c->eip = c->src.val;
1263 c->src.val = old_eip;
1264 emulate_push(ctxt);
1265 break;
1266 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001267 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001268 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 break;
1270 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001271 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001272 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001273 }
1274 return 0;
1275}
1276
1277static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1278 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001279 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001280{
1281 struct decode_cache *c = &ctxt->decode;
1282 u64 old, new;
1283 int rc;
1284
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001285 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001286 if (rc != 0)
1287 return rc;
1288
1289 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1290 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1291
1292 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1293 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001294 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001295
1296 } else {
1297 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1298 (u32) c->regs[VCPU_REGS_RBX];
1299
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001300 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001301 if (rc != 0)
1302 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001303 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001304 }
1305 return 0;
1306}
1307
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001308static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1309 struct x86_emulate_ops *ops)
1310{
1311 struct decode_cache *c = &ctxt->decode;
1312 int rc;
1313 unsigned long cs;
1314
1315 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1316 if (rc)
1317 return rc;
1318 if (c->op_bytes == 4)
1319 c->eip = (u32)c->eip;
1320 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1321 if (rc)
1322 return rc;
1323 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1324 return rc;
1325}
1326
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001327static inline int writeback(struct x86_emulate_ctxt *ctxt,
1328 struct x86_emulate_ops *ops)
1329{
1330 int rc;
1331 struct decode_cache *c = &ctxt->decode;
1332
1333 switch (c->dst.type) {
1334 case OP_REG:
1335 /* The 4-byte case *is* correct:
1336 * in 64-bit mode we zero-extend.
1337 */
1338 switch (c->dst.bytes) {
1339 case 1:
1340 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1341 break;
1342 case 2:
1343 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1344 break;
1345 case 4:
1346 *c->dst.ptr = (u32)c->dst.val;
1347 break; /* 64b: zero-ext */
1348 case 8:
1349 *c->dst.ptr = c->dst.val;
1350 break;
1351 }
1352 break;
1353 case OP_MEM:
1354 if (c->lock_prefix)
1355 rc = ops->cmpxchg_emulated(
1356 (unsigned long)c->dst.ptr,
1357 &c->dst.orig_val,
1358 &c->dst.val,
1359 c->dst.bytes,
1360 ctxt->vcpu);
1361 else
1362 rc = ops->write_emulated(
1363 (unsigned long)c->dst.ptr,
1364 &c->dst.val,
1365 c->dst.bytes,
1366 ctxt->vcpu);
1367 if (rc != 0)
1368 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001369 break;
1370 case OP_NONE:
1371 /* no writeback */
1372 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373 default:
1374 break;
1375 }
1376 return 0;
1377}
1378
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301379static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001380{
1381 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1382 /*
1383 * an sti; sti; sequence only disable interrupts for the first
1384 * instruction. So, if the last instruction, be it emulated or
1385 * not, left the system with the INT_STI flag enabled, it
1386 * means that the last instruction is an sti. We should not
1387 * leave the flag on in this case. The same goes for mov ss
1388 */
1389 if (!(int_shadow & mask))
1390 ctxt->interruptibility = mask;
1391}
1392
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001393int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001394x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001395{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001396 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001397 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001398 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001399 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001400 unsigned int port;
1401 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001402 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001403
Glauber Costa310b5d32009-05-12 16:21:06 -04001404 ctxt->interruptibility = 0;
1405
Laurent Vivier34273182007-09-18 11:27:37 +02001406 /* Shadow copy of register state. Committed on successful emulation.
1407 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1408 * modify them.
1409 */
1410
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001411 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001412 saved_eip = c->eip;
1413
Avi Kivityc7e75a32007-10-28 16:34:25 +02001414 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001415 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001416
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001417 if (c->rep_prefix && (c->d & String)) {
1418 /* All REP prefixes have the same first termination condition */
1419 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001420 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001421 goto done;
1422 }
1423 /* The second termination condition only applies for REPE
1424 * and REPNE. Test if the repeat string operation prefix is
1425 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1426 * corresponding termination condition according to:
1427 * - if REPE/REPZ and ZF = 0 then done
1428 * - if REPNE/REPNZ and ZF = 1 then done
1429 */
1430 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1431 (c->b == 0xae) || (c->b == 0xaf)) {
1432 if ((c->rep_prefix == REPE_PREFIX) &&
1433 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001434 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001435 goto done;
1436 }
1437 if ((c->rep_prefix == REPNE_PREFIX) &&
1438 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001439 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001440 goto done;
1441 }
1442 }
1443 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001444 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001445 }
1446
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001447 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001448 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001449 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001450 rc = ops->read_emulated((unsigned long)c->src.ptr,
1451 &c->src.val,
1452 c->src.bytes,
1453 ctxt->vcpu);
1454 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001455 goto done;
1456 c->src.orig_val = c->src.val;
1457 }
1458
1459 if ((c->d & DstMask) == ImplicitOps)
1460 goto special_insn;
1461
1462
1463 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001464 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001465 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1466 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001467 if (c->d & BitOp) {
1468 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001469
Laurent Viviere4e03de2007-09-18 11:52:50 +02001470 c->dst.ptr = (void *)c->dst.ptr +
1471 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001472 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001473 if (!(c->d & Mov) &&
1474 /* optimisation - avoid slow emulated read */
1475 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1476 &c->dst.val,
1477 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001478 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001479 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001480 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001481
Avi Kivity018a98d2007-11-27 19:30:56 +02001482special_insn:
1483
Laurent Viviere4e03de2007-09-18 11:52:50 +02001484 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 goto twobyte_insn;
1486
Laurent Viviere4e03de2007-09-18 11:52:50 +02001487 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 case 0x00 ... 0x05:
1489 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001490 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 break;
1492 case 0x08 ... 0x0d:
1493 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001494 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 break;
1496 case 0x10 ... 0x15:
1497 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001498 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 break;
1500 case 0x18 ... 0x1d:
1501 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001502 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001504 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001506 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 break;
1508 case 0x28 ... 0x2d:
1509 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001510 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 break;
1512 case 0x30 ... 0x35:
1513 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001514 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 break;
1516 case 0x38 ... 0x3d:
1517 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001518 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001520 case 0x40 ... 0x47: /* inc r16/r32 */
1521 emulate_1op("inc", c->dst, ctxt->eflags);
1522 break;
1523 case 0x48 ... 0x4f: /* dec r16/r32 */
1524 emulate_1op("dec", c->dst, ctxt->eflags);
1525 break;
1526 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001527 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001528 break;
1529 case 0x58 ... 0x5f: /* pop reg */
1530 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001531 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001532 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001533 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001534 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001536 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001538 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001540 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001541 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001542 emulate_push(ctxt);
1543 break;
1544 case 0x6c: /* insb */
1545 case 0x6d: /* insw/insd */
1546 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1547 1,
1548 (c->d & ByteOp) ? 1 : c->op_bytes,
1549 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001550 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001551 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001552 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001553 c->regs[VCPU_REGS_RDI]),
1554 c->rep_prefix,
1555 c->regs[VCPU_REGS_RDX]) == 0) {
1556 c->eip = saved_eip;
1557 return -1;
1558 }
1559 return 0;
1560 case 0x6e: /* outsb */
1561 case 0x6f: /* outsw/outsd */
1562 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1563 0,
1564 (c->d & ByteOp) ? 1 : c->op_bytes,
1565 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001566 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001567 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001568 register_address(c,
1569 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001570 c->regs[VCPU_REGS_RSI]),
1571 c->rep_prefix,
1572 c->regs[VCPU_REGS_RDX]) == 0) {
1573 c->eip = saved_eip;
1574 return -1;
1575 }
1576 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001577 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001578 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001579 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001580 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001582 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 case 0:
1584 goto add;
1585 case 1:
1586 goto or;
1587 case 2:
1588 goto adc;
1589 case 3:
1590 goto sbb;
1591 case 4:
1592 goto and;
1593 case 5:
1594 goto sub;
1595 case 6:
1596 goto xor;
1597 case 7:
1598 goto cmp;
1599 }
1600 break;
1601 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001602 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 break;
1604 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001605 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001607 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 break;
1611 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001612 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 break;
1614 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001615 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 break; /* 64b reg: zero-extend */
1617 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001618 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 break;
1620 }
1621 /*
1622 * Write back the memory destination with implicit LOCK
1623 * prefix.
1624 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001625 c->dst.val = c->src.val;
1626 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001629 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001630 case 0x8c: { /* mov r/m, sreg */
1631 struct kvm_segment segreg;
1632
1633 if (c->modrm_reg <= 5)
1634 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1635 else {
1636 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1637 c->modrm);
1638 goto cannot_emulate;
1639 }
1640 c->dst.val = segreg.selector;
1641 break;
1642 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001643 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001644 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001645 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001646 case 0x8e: { /* mov seg, r/m16 */
1647 uint16_t sel;
1648 int type_bits;
1649 int err;
1650
1651 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001652 if (c->modrm_reg == VCPU_SREG_SS)
1653 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1654
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001655 if (c->modrm_reg <= 5) {
1656 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1657 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1658 type_bits, c->modrm_reg);
1659 } else {
1660 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1661 c->modrm);
1662 goto cannot_emulate;
1663 }
1664
1665 if (err < 0)
1666 goto cannot_emulate;
1667
1668 c->dst.type = OP_NONE; /* Disable writeback. */
1669 break;
1670 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001672 rc = emulate_grp1a(ctxt, ops);
1673 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001676 case 0x90: /* nop / xchg r8,rax */
1677 if (!(c->rex_prefix & 1)) { /* nop */
1678 c->dst.type = OP_NONE;
1679 break;
1680 }
1681 case 0x91 ... 0x97: /* xchg reg,rax */
1682 c->src.type = c->dst.type = OP_REG;
1683 c->src.bytes = c->dst.bytes = c->op_bytes;
1684 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1685 c->src.val = *(c->src.ptr);
1686 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001687 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001688 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689 emulate_push(ctxt);
1690 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001691 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001692 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001693 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001694 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001695 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001696 case 0xa0 ... 0xa1: /* mov */
1697 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1698 c->dst.val = c->src.val;
1699 break;
1700 case 0xa2 ... 0xa3: /* mov */
1701 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1702 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001704 c->dst.type = OP_MEM;
1705 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001706 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001707 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001708 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001709 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001710 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001711 c->regs[VCPU_REGS_RSI]),
1712 &c->dst.val,
1713 c->dst.bytes, 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);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001718 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001719 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001720 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 break;
1722 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001723 c->src.type = OP_NONE; /* Disable writeback. */
1724 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001725 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001726 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001727 c->regs[VCPU_REGS_RSI]);
1728 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1729 &c->src.val,
1730 c->src.bytes,
1731 ctxt->vcpu)) != 0)
1732 goto done;
1733
1734 c->dst.type = OP_NONE; /* Disable writeback. */
1735 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001736 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001737 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001738 c->regs[VCPU_REGS_RDI]);
1739 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1740 &c->dst.val,
1741 c->dst.bytes,
1742 ctxt->vcpu)) != 0)
1743 goto done;
1744
1745 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1746
1747 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1748
Harvey Harrison7a9572752008-02-19 07:40:41 -08001749 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001750 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1751 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001752 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001753 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1754 : c->dst.bytes);
1755
1756 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001758 c->dst.type = OP_MEM;
1759 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001760 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001761 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001762 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001763 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001764 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001765 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001766 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 break;
1768 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001769 c->dst.type = OP_REG;
1770 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1771 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001772 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001773 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001774 c->regs[VCPU_REGS_RSI]),
1775 &c->dst.val,
1776 c->dst.bytes,
1777 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001779 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001780 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001781 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782 break;
1783 case 0xae ... 0xaf: /* scas */
1784 DPRINTF("Urk! I don't handle SCAS.\n");
1785 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001786 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001787 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001788 case 0xc0 ... 0xc1:
1789 emulate_grp2(ctxt);
1790 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001791 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001792 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001793 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001794 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001795 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001796 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1797 mov:
1798 c->dst.val = c->src.val;
1799 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001800 case 0xcb: /* ret far */
1801 rc = emulate_ret_far(ctxt, ops);
1802 if (rc)
1803 goto done;
1804 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001805 case 0xd0 ... 0xd1: /* Grp2 */
1806 c->src.val = 1;
1807 emulate_grp2(ctxt);
1808 break;
1809 case 0xd2 ... 0xd3: /* Grp2 */
1810 c->src.val = c->regs[VCPU_REGS_RCX];
1811 emulate_grp2(ctxt);
1812 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001813 case 0xe4: /* inb */
1814 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03001815 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001816 io_dir_in = 1;
1817 goto do_io;
1818 case 0xe6: /* outb */
1819 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03001820 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001821 io_dir_in = 0;
1822 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001823 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03001824 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001825 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001826 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 emulate_push(ctxt);
1828 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001829 }
1830 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001831 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03001832 case 0xea: /* jmp far */
1833 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
1834 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001835 DPRINTF("jmp far: Failed to load CS descriptor\n");
1836 goto cannot_emulate;
1837 }
1838
Gleb Natapov782b8772009-04-12 13:36:25 +03001839 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001840 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001841 case 0xeb:
1842 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001843 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001844 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001845 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001846 case 0xec: /* in al,dx */
1847 case 0xed: /* in (e/r)ax,dx */
1848 port = c->regs[VCPU_REGS_RDX];
1849 io_dir_in = 1;
1850 goto do_io;
1851 case 0xee: /* out al,dx */
1852 case 0xef: /* out (e/r)ax,dx */
1853 port = c->regs[VCPU_REGS_RDX];
1854 io_dir_in = 0;
1855 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1856 (c->d & ByteOp) ? 1 : c->op_bytes,
1857 port) != 0) {
1858 c->eip = saved_eip;
1859 goto cannot_emulate;
1860 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001861 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001862 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001863 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001864 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001865 case 0xf5: /* cmc */
1866 /* complement carry flag from eflags reg */
1867 ctxt->eflags ^= EFLG_CF;
1868 c->dst.type = OP_NONE; /* Disable writeback. */
1869 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001870 case 0xf6 ... 0xf7: /* Grp3 */
1871 rc = emulate_grp3(ctxt, ops);
1872 if (rc != 0)
1873 goto done;
1874 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001875 case 0xf8: /* clc */
1876 ctxt->eflags &= ~EFLG_CF;
1877 c->dst.type = OP_NONE; /* Disable writeback. */
1878 break;
1879 case 0xfa: /* cli */
1880 ctxt->eflags &= ~X86_EFLAGS_IF;
1881 c->dst.type = OP_NONE; /* Disable writeback. */
1882 break;
1883 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04001884 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02001885 ctxt->eflags |= X86_EFLAGS_IF;
1886 c->dst.type = OP_NONE; /* Disable writeback. */
1887 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001888 case 0xfc: /* cld */
1889 ctxt->eflags &= ~EFLG_DF;
1890 c->dst.type = OP_NONE; /* Disable writeback. */
1891 break;
1892 case 0xfd: /* std */
1893 ctxt->eflags |= EFLG_DF;
1894 c->dst.type = OP_NONE; /* Disable writeback. */
1895 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001896 case 0xfe ... 0xff: /* Grp4/Grp5 */
1897 rc = emulate_grp45(ctxt, ops);
1898 if (rc != 0)
1899 goto done;
1900 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001902
1903writeback:
1904 rc = writeback(ctxt, ops);
1905 if (rc != 0)
1906 goto done;
1907
1908 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001909 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001910 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001911
1912done:
1913 if (rc == X86EMUL_UNHANDLEABLE) {
1914 c->eip = saved_eip;
1915 return -1;
1916 }
1917 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918
1919twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001920 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001922 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 u16 size;
1924 unsigned long address;
1925
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001926 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001927 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001928 goto cannot_emulate;
1929
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001930 rc = kvm_fix_hypercall(ctxt->vcpu);
1931 if (rc)
1932 goto done;
1933
Avi Kivity33e38852008-05-21 15:34:25 +03001934 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001935 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001936 /* Disable writeback. */
1937 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001938 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001940 rc = read_descriptor(ctxt, ops, c->src.ptr,
1941 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 if (rc)
1943 goto done;
1944 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001945 /* Disable writeback. */
1946 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001948 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02001949 if (c->modrm_mod == 3) {
1950 switch (c->modrm_rm) {
1951 case 1:
1952 rc = kvm_fix_hypercall(ctxt->vcpu);
1953 if (rc)
1954 goto done;
1955 break;
1956 default:
1957 goto cannot_emulate;
1958 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001959 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001960 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001961 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001962 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001963 if (rc)
1964 goto done;
1965 realmode_lidt(ctxt->vcpu, size, address);
1966 }
Avi Kivity16286d02008-04-14 14:40:50 +03001967 /* Disable writeback. */
1968 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 break;
1970 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001971 c->dst.bytes = 2;
1972 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 break;
1974 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001975 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1976 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001977 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 break;
1979 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001980 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001981 /* Disable writeback. */
1982 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 break;
1984 default:
1985 goto cannot_emulate;
1986 }
1987 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001988 case 0x06:
1989 emulate_clts(ctxt->vcpu);
1990 c->dst.type = OP_NONE;
1991 break;
1992 case 0x08: /* invd */
1993 case 0x09: /* wbinvd */
1994 case 0x0d: /* GrpP (prefetch) */
1995 case 0x18: /* Grp16 (prefetch/nop) */
1996 c->dst.type = OP_NONE;
1997 break;
1998 case 0x20: /* mov cr, reg */
1999 if (c->modrm_mod != 3)
2000 goto cannot_emulate;
2001 c->regs[c->modrm_rm] =
2002 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2003 c->dst.type = OP_NONE; /* no writeback */
2004 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002006 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002008 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002009 if (rc)
2010 goto cannot_emulate;
2011 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002013 case 0x22: /* mov reg, cr */
2014 if (c->modrm_mod != 3)
2015 goto cannot_emulate;
2016 realmode_set_cr(ctxt->vcpu,
2017 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2018 c->dst.type = OP_NONE;
2019 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002021 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002023 rc = emulator_set_dr(ctxt, c->modrm_reg,
2024 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002025 if (rc)
2026 goto cannot_emulate;
2027 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002029 case 0x30:
2030 /* wrmsr */
2031 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2032 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2033 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2034 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002035 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002036 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002037 }
2038 rc = X86EMUL_CONTINUE;
2039 c->dst.type = OP_NONE;
2040 break;
2041 case 0x32:
2042 /* rdmsr */
2043 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2044 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002045 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002046 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002047 } else {
2048 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2049 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2050 }
2051 rc = X86EMUL_CONTINUE;
2052 c->dst.type = OP_NONE;
2053 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002055 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002056 if (!test_cc(c->b, ctxt->eflags))
2057 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002059 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002060 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002061 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002062 c->dst.type = OP_NONE;
2063 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002064 case 0xa3:
2065 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002066 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002067 /* only subword offset */
2068 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002069 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002070 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002071 case 0xa4: /* shld imm8, r, r/m */
2072 case 0xa5: /* shld cl, r, r/m */
2073 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2074 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002075 case 0xab:
2076 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002077 /* only subword offset */
2078 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002079 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002080 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002081 case 0xac: /* shrd imm8, r, r/m */
2082 case 0xad: /* shrd cl, r, r/m */
2083 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2084 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002085 case 0xae: /* clflush */
2086 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 case 0xb0 ... 0xb1: /* cmpxchg */
2088 /*
2089 * Save real source value, then compare EAX against
2090 * destination.
2091 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002092 c->src.orig_val = c->src.val;
2093 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002094 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2095 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002097 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 } else {
2099 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002100 c->dst.type = OP_REG;
2101 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 }
2103 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 case 0xb3:
2105 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002106 /* only subword offset */
2107 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002108 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002111 c->dst.bytes = c->op_bytes;
2112 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2113 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 case 0:
2118 goto bt;
2119 case 1:
2120 goto bts;
2121 case 2:
2122 goto btr;
2123 case 3:
2124 goto btc;
2125 }
2126 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002127 case 0xbb:
2128 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002129 /* only subword offset */
2130 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002131 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002134 c->dst.bytes = c->op_bytes;
2135 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2136 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002138 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002139 c->dst.bytes = c->op_bytes;
2140 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2141 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002142 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002144 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145 if (rc != 0)
2146 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002147 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 }
2150 goto writeback;
2151
2152cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002153 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002154 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 return -1;
2156}