blob: 0988a13063dc9a8f6e0630da8a702a3c998a0c2e [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. */
63#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080064/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030065#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080066/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define Mov (1<<9)
68#define BitOp (1<<10)
69#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020070#define String (1<<12) /* String instruction (rep capable) */
71#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020072#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
73#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
74#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010075/* Source 2 operand type */
76#define Src2None (0<<29)
77#define Src2CL (1<<29)
78#define Src2ImmByte (2<<29)
79#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030080#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010081#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivity43bb19c2008-01-18 12:46:50 +020083enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020084 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020085 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020086};
87
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010088static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080089 /* 0x00 - 0x07 */
90 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
91 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020092 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080093 /* 0x08 - 0x0F */
94 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
95 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
96 0, 0, 0, 0,
97 /* 0x10 - 0x17 */
98 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
99 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
100 0, 0, 0, 0,
101 /* 0x18 - 0x1F */
102 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
103 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
104 0, 0, 0, 0,
105 /* 0x20 - 0x27 */
106 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
107 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200108 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109 /* 0x28 - 0x2F */
110 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
111 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
112 0, 0, 0, 0,
113 /* 0x30 - 0x37 */
114 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
115 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
116 0, 0, 0, 0,
117 /* 0x38 - 0x3F */
118 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
119 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200120 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
121 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700122 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200123 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700124 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200125 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300126 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200127 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
128 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300129 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200130 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
131 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700132 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700134 0, 0, 0, 0,
135 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300136 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300137 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
138 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300139 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300140 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
141 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300142 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300143 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
144 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200146 Group | Group1_80, Group | Group1_81,
147 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
149 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
150 /* 0x88 - 0x8F */
151 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
152 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200153 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200154 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300155 /* 0x90 - 0x97 */
156 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
157 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300158 0, 0, SrcImm | Src2Imm16, 0,
159 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200161 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
162 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200163 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
164 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200166 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
167 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
168 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300169 /* 0xB0 - 0xB7 */
170 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
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 /* 0xB8 - 0xBF */
175 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
176 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
177 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
178 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300180 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200181 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300182 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0xC8 - 0xCF */
Avi Kivitya77ab5e2009-01-05 13:27:34 +0200184 0, 0, 0, ImplicitOps | Stack, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185 /* 0xD0 - 0xD7 */
186 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
187 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
188 0, 0, 0, 0,
189 /* 0xD8 - 0xDF */
190 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300191 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300192 0, 0, 0, 0,
193 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
194 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300195 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300196 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov782b8772009-04-12 13:36:25 +0300197 SrcImm | Src2Imm16, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300198 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
199 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 /* 0xF0 - 0xF7 */
201 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200202 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700204 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300205 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206};
207
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100208static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200210 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200211 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 /* 0x10 - 0x1F */
213 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
214 /* 0x20 - 0x2F */
215 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
216 0, 0, 0, 0, 0, 0, 0, 0,
217 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300218 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 /* 0x40 - 0x47 */
220 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
221 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
222 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
223 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
224 /* 0x48 - 0x4F */
225 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
226 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
227 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
228 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
229 /* 0x50 - 0x5F */
230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
231 /* 0x60 - 0x6F */
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x70 - 0x7F */
234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300236 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
237 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 /* 0x90 - 0x9F */
239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100241 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
242 DstMem | SrcReg | Src2ImmByte | ModRM,
243 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100245 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
246 DstMem | SrcReg | Src2ImmByte | ModRM,
247 DstMem | SrcReg | Src2CL | ModRM,
248 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249 /* 0xB0 - 0xB7 */
250 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800251 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
253 DstReg | SrcMem16 | ModRM | Mov,
254 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800255 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
257 DstReg | SrcMem16 | ModRM | Mov,
258 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800259 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
260 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261 /* 0xD0 - 0xDF */
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
263 /* 0xE0 - 0xEF */
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265 /* 0xF0 - 0xFF */
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
267};
268
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100269static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200270 [Group1_80*8] =
271 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
272 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
273 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
274 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
275 [Group1_81*8] =
276 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
277 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
278 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
279 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
280 [Group1_82*8] =
281 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
282 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 [Group1_83*8] =
286 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
287 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
288 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
289 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200290 [Group1A*8] =
291 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200292 [Group3_Byte*8] =
293 ByteOp | SrcImm | DstMem | ModRM, 0,
294 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
295 0, 0, 0, 0,
296 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400297 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300298 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200299 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200300 [Group4*8] =
301 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
302 0, 0, 0, 0, 0, 0,
303 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300304 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
305 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300306 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200307 [Group7*8] =
308 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300309 SrcNone | ModRM | DstMem | Mov, 0,
310 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200311};
312
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100313static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200314 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000315 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300316 SrcNone | ModRM | DstMem | Mov, 0,
317 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200318};
319
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320/* EFLAGS bit definitions. */
321#define EFLG_OF (1<<11)
322#define EFLG_DF (1<<10)
323#define EFLG_SF (1<<7)
324#define EFLG_ZF (1<<6)
325#define EFLG_AF (1<<4)
326#define EFLG_PF (1<<2)
327#define EFLG_CF (1<<0)
328
329/*
330 * Instruction emulation:
331 * Most instructions are emulated directly via a fragment of inline assembly
332 * code. This allows us to save/restore EFLAGS and thus very easily pick up
333 * any modified flags.
334 */
335
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800336#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337#define _LO32 "k" /* force 32-bit operand */
338#define _STK "%%rsp" /* stack pointer */
339#elif defined(__i386__)
340#define _LO32 "" /* force 32-bit operand */
341#define _STK "%%esp" /* stack pointer */
342#endif
343
344/*
345 * These EFLAGS bits are restored from saved value during emulation, and
346 * any changes are written back to the saved value after emulation.
347 */
348#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
349
350/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200351#define _PRE_EFLAGS(_sav, _msk, _tmp) \
352 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
353 "movl %"_sav",%"_LO32 _tmp"; " \
354 "push %"_tmp"; " \
355 "push %"_tmp"; " \
356 "movl %"_msk",%"_LO32 _tmp"; " \
357 "andl %"_LO32 _tmp",("_STK"); " \
358 "pushf; " \
359 "notl %"_LO32 _tmp"; " \
360 "andl %"_LO32 _tmp",("_STK"); " \
361 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
362 "pop %"_tmp"; " \
363 "orl %"_LO32 _tmp",("_STK"); " \
364 "popf; " \
365 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366
367/* After executing instruction: write-back necessary bits in EFLAGS. */
368#define _POST_EFLAGS(_sav, _msk, _tmp) \
369 /* _sav |= EFLAGS & _msk; */ \
370 "pushf; " \
371 "pop %"_tmp"; " \
372 "andl %"_msk",%"_LO32 _tmp"; " \
373 "orl %"_LO32 _tmp",%"_sav"; "
374
Avi Kivitydda96d82008-11-26 15:14:10 +0200375#ifdef CONFIG_X86_64
376#define ON64(x) x
377#else
378#define ON64(x)
379#endif
380
Avi Kivity6b7ad612008-11-26 15:30:45 +0200381#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
382 do { \
383 __asm__ __volatile__ ( \
384 _PRE_EFLAGS("0", "4", "2") \
385 _op _suffix " %"_x"3,%1; " \
386 _POST_EFLAGS("0", "4", "2") \
387 : "=m" (_eflags), "=m" ((_dst).val), \
388 "=&r" (_tmp) \
389 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200390 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200391
392
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393/* Raw emulation: instruction has two explicit operands. */
394#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200395 do { \
396 unsigned long _tmp; \
397 \
398 switch ((_dst).bytes) { \
399 case 2: \
400 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
401 break; \
402 case 4: \
403 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
404 break; \
405 case 8: \
406 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
407 break; \
408 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 } while (0)
410
411#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
412 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200413 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400414 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200416 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 break; \
418 default: \
419 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
420 _wx, _wy, _lx, _ly, _qx, _qy); \
421 break; \
422 } \
423 } while (0)
424
425/* Source operand is byte-sized and may be restricted to just %cl. */
426#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
427 __emulate_2op(_op, _src, _dst, _eflags, \
428 "b", "c", "b", "c", "b", "c", "b", "c")
429
430/* Source operand is byte, word, long or quad sized. */
431#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
432 __emulate_2op(_op, _src, _dst, _eflags, \
433 "b", "q", "w", "r", _LO32, "r", "", "r")
434
435/* Source operand is word, long or quad sized. */
436#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
437 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
438 "w", "r", _LO32, "r", "", "r")
439
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100440/* Instruction has three operands and one operand is stored in ECX register */
441#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
442 do { \
443 unsigned long _tmp; \
444 _type _clv = (_cl).val; \
445 _type _srcv = (_src).val; \
446 _type _dstv = (_dst).val; \
447 \
448 __asm__ __volatile__ ( \
449 _PRE_EFLAGS("0", "5", "2") \
450 _op _suffix " %4,%1 \n" \
451 _POST_EFLAGS("0", "5", "2") \
452 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
453 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
454 ); \
455 \
456 (_cl).val = (unsigned long) _clv; \
457 (_src).val = (unsigned long) _srcv; \
458 (_dst).val = (unsigned long) _dstv; \
459 } while (0)
460
461#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
462 do { \
463 switch ((_dst).bytes) { \
464 case 2: \
465 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
466 "w", unsigned short); \
467 break; \
468 case 4: \
469 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
470 "l", unsigned int); \
471 break; \
472 case 8: \
473 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
474 "q", unsigned long)); \
475 break; \
476 } \
477 } while (0)
478
Avi Kivitydda96d82008-11-26 15:14:10 +0200479#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 do { \
481 unsigned long _tmp; \
482 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200483 __asm__ __volatile__ ( \
484 _PRE_EFLAGS("0", "3", "2") \
485 _op _suffix " %1; " \
486 _POST_EFLAGS("0", "3", "2") \
487 : "=m" (_eflags), "+m" ((_dst).val), \
488 "=&r" (_tmp) \
489 : "i" (EFLAGS_MASK)); \
490 } while (0)
491
492/* Instruction has only one explicit operand (no source operand). */
493#define emulate_1op(_op, _dst, _eflags) \
494 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400495 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200496 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
497 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
498 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
499 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 } \
501 } while (0)
502
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503/* Fetch next part of the instruction being emulated. */
504#define insn_fetch(_type, _size, _eip) \
505({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200506 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400507 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 goto done; \
509 (_eip) += (_size); \
510 (_type)_x; \
511})
512
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800513static inline unsigned long ad_mask(struct decode_cache *c)
514{
515 return (1UL << (c->ad_bytes << 3)) - 1;
516}
517
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800519static inline unsigned long
520address_mask(struct decode_cache *c, unsigned long reg)
521{
522 if (c->ad_bytes == sizeof(unsigned long))
523 return reg;
524 else
525 return reg & ad_mask(c);
526}
527
528static inline unsigned long
529register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
530{
531 return base + address_mask(c, reg);
532}
533
Harvey Harrison7a9572752008-02-19 07:40:41 -0800534static inline void
535register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
536{
537 if (c->ad_bytes == sizeof(unsigned long))
538 *reg += inc;
539 else
540 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
541}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542
Harvey Harrison7a9572752008-02-19 07:40:41 -0800543static inline void jmp_rel(struct decode_cache *c, int rel)
544{
545 register_address_increment(c, &c->eip, rel);
546}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300547
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300548static void set_seg_override(struct decode_cache *c, int seg)
549{
550 c->has_seg_override = true;
551 c->seg_override = seg;
552}
553
554static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
555{
556 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
557 return 0;
558
559 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
560}
561
562static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
563 struct decode_cache *c)
564{
565 if (!c->has_seg_override)
566 return 0;
567
568 return seg_base(ctxt, c->seg_override);
569}
570
571static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
572{
573 return seg_base(ctxt, VCPU_SREG_ES);
574}
575
576static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
577{
578 return seg_base(ctxt, VCPU_SREG_SS);
579}
580
Avi Kivity62266862007-11-20 13:15:52 +0200581static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
582 struct x86_emulate_ops *ops,
583 unsigned long linear, u8 *dest)
584{
585 struct fetch_cache *fc = &ctxt->decode.fetch;
586 int rc;
587 int size;
588
589 if (linear < fc->start || linear >= fc->end) {
590 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
591 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
592 if (rc)
593 return rc;
594 fc->start = linear;
595 fc->end = linear + size;
596 }
597 *dest = fc->data[linear - fc->start];
598 return 0;
599}
600
601static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
602 struct x86_emulate_ops *ops,
603 unsigned long eip, void *dest, unsigned size)
604{
605 int rc = 0;
606
607 eip += ctxt->cs_base;
608 while (size--) {
609 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
610 if (rc)
611 return rc;
612 }
613 return 0;
614}
615
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000616/*
617 * Given the 'reg' portion of a ModRM byte, and a register block, return a
618 * pointer into the block that addresses the relevant register.
619 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
620 */
621static void *decode_register(u8 modrm_reg, unsigned long *regs,
622 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623{
624 void *p;
625
626 p = &regs[modrm_reg];
627 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
628 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
629 return p;
630}
631
632static int read_descriptor(struct x86_emulate_ctxt *ctxt,
633 struct x86_emulate_ops *ops,
634 void *ptr,
635 u16 *size, unsigned long *address, int op_bytes)
636{
637 int rc;
638
639 if (op_bytes == 2)
640 op_bytes = 3;
641 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300642 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
643 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644 if (rc)
645 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300646 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
647 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 return rc;
649}
650
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300651static int test_cc(unsigned int condition, unsigned int flags)
652{
653 int rc = 0;
654
655 switch ((condition & 15) >> 1) {
656 case 0: /* o */
657 rc |= (flags & EFLG_OF);
658 break;
659 case 1: /* b/c/nae */
660 rc |= (flags & EFLG_CF);
661 break;
662 case 2: /* z/e */
663 rc |= (flags & EFLG_ZF);
664 break;
665 case 3: /* be/na */
666 rc |= (flags & (EFLG_CF|EFLG_ZF));
667 break;
668 case 4: /* s */
669 rc |= (flags & EFLG_SF);
670 break;
671 case 5: /* p/pe */
672 rc |= (flags & EFLG_PF);
673 break;
674 case 7: /* le/ng */
675 rc |= (flags & EFLG_ZF);
676 /* fall through */
677 case 6: /* l/nge */
678 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
679 break;
680 }
681
682 /* Odd condition identifiers (lsb == 1) have inverted sense. */
683 return (!!rc ^ (condition & 1));
684}
685
Avi Kivity3c118e22007-10-31 10:27:04 +0200686static void decode_register_operand(struct operand *op,
687 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200688 int inhibit_bytereg)
689{
Avi Kivity33615aa2007-10-31 11:15:56 +0200690 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200691 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200692
693 if (!(c->d & ModRM))
694 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200695 op->type = OP_REG;
696 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200697 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200698 op->val = *(u8 *)op->ptr;
699 op->bytes = 1;
700 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200701 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200702 op->bytes = c->op_bytes;
703 switch (op->bytes) {
704 case 2:
705 op->val = *(u16 *)op->ptr;
706 break;
707 case 4:
708 op->val = *(u32 *)op->ptr;
709 break;
710 case 8:
711 op->val = *(u64 *) op->ptr;
712 break;
713 }
714 }
715 op->orig_val = op->val;
716}
717
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200718static int decode_modrm(struct x86_emulate_ctxt *ctxt,
719 struct x86_emulate_ops *ops)
720{
721 struct decode_cache *c = &ctxt->decode;
722 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700723 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200724 int rc = 0;
725
726 if (c->rex_prefix) {
727 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
728 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
729 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
730 }
731
732 c->modrm = insn_fetch(u8, 1, c->eip);
733 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
734 c->modrm_reg |= (c->modrm & 0x38) >> 3;
735 c->modrm_rm |= (c->modrm & 0x07);
736 c->modrm_ea = 0;
737 c->use_modrm_ea = 1;
738
739 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300740 c->modrm_ptr = decode_register(c->modrm_rm,
741 c->regs, c->d & ByteOp);
742 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200743 return rc;
744 }
745
746 if (c->ad_bytes == 2) {
747 unsigned bx = c->regs[VCPU_REGS_RBX];
748 unsigned bp = c->regs[VCPU_REGS_RBP];
749 unsigned si = c->regs[VCPU_REGS_RSI];
750 unsigned di = c->regs[VCPU_REGS_RDI];
751
752 /* 16-bit ModR/M decode. */
753 switch (c->modrm_mod) {
754 case 0:
755 if (c->modrm_rm == 6)
756 c->modrm_ea += insn_fetch(u16, 2, c->eip);
757 break;
758 case 1:
759 c->modrm_ea += insn_fetch(s8, 1, c->eip);
760 break;
761 case 2:
762 c->modrm_ea += insn_fetch(u16, 2, c->eip);
763 break;
764 }
765 switch (c->modrm_rm) {
766 case 0:
767 c->modrm_ea += bx + si;
768 break;
769 case 1:
770 c->modrm_ea += bx + di;
771 break;
772 case 2:
773 c->modrm_ea += bp + si;
774 break;
775 case 3:
776 c->modrm_ea += bp + di;
777 break;
778 case 4:
779 c->modrm_ea += si;
780 break;
781 case 5:
782 c->modrm_ea += di;
783 break;
784 case 6:
785 if (c->modrm_mod != 0)
786 c->modrm_ea += bp;
787 break;
788 case 7:
789 c->modrm_ea += bx;
790 break;
791 }
792 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
793 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300794 if (!c->has_seg_override)
795 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200796 c->modrm_ea = (u16)c->modrm_ea;
797 } else {
798 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700799 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200800 sib = insn_fetch(u8, 1, c->eip);
801 index_reg |= (sib >> 3) & 7;
802 base_reg |= sib & 7;
803 scale = sib >> 6;
804
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700805 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
806 c->modrm_ea += insn_fetch(s32, 4, c->eip);
807 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200808 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700809 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700811 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
812 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700813 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700814 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200815 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200816 switch (c->modrm_mod) {
817 case 0:
818 if (c->modrm_rm == 5)
819 c->modrm_ea += insn_fetch(s32, 4, c->eip);
820 break;
821 case 1:
822 c->modrm_ea += insn_fetch(s8, 1, c->eip);
823 break;
824 case 2:
825 c->modrm_ea += insn_fetch(s32, 4, c->eip);
826 break;
827 }
828 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200829done:
830 return rc;
831}
832
833static int decode_abs(struct x86_emulate_ctxt *ctxt,
834 struct x86_emulate_ops *ops)
835{
836 struct decode_cache *c = &ctxt->decode;
837 int rc = 0;
838
839 switch (c->ad_bytes) {
840 case 2:
841 c->modrm_ea = insn_fetch(u16, 2, c->eip);
842 break;
843 case 4:
844 c->modrm_ea = insn_fetch(u32, 4, c->eip);
845 break;
846 case 8:
847 c->modrm_ea = insn_fetch(u64, 8, c->eip);
848 break;
849 }
850done:
851 return rc;
852}
853
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200855x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200857 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200860 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
862 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
Laurent Viviere4e03de2007-09-18 11:52:50 +0200864 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300865 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300866 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800867 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
869 switch (mode) {
870 case X86EMUL_MODE_REAL:
871 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200872 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 break;
874 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200875 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800877#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200879 def_op_bytes = 4;
880 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
882#endif
883 default:
884 return -1;
885 }
886
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200887 c->op_bytes = def_op_bytes;
888 c->ad_bytes = def_ad_bytes;
889
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200891 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200892 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200894 /* switch between 2/4 bytes */
895 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 break;
897 case 0x67: /* address-size override */
898 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200899 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200900 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200902 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200903 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300906 case 0x2e: /* CS override */
907 case 0x36: /* SS override */
908 case 0x3e: /* DS override */
909 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
911 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300913 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200915 case 0x40 ... 0x4f: /* REX */
916 if (mode != X86EMUL_MODE_PROT64)
917 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200918 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200919 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200921 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200923 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100924 c->rep_prefix = REPNE_PREFIX;
925 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100927 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 default:
930 goto done_prefixes;
931 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200932
933 /* Any legacy prefix after a REX prefix nullifies its effect. */
934
Avi Kivity33615aa2007-10-31 11:15:56 +0200935 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 }
937
938done_prefixes:
939
940 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200941 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200942 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200943 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
945 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200946 c->d = opcode_table[c->b];
947 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200949 if (c->b == 0x0f) {
950 c->twobyte = 1;
951 c->b = insn_fetch(u8, 1, c->eip);
952 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200954 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Avi Kivitye09d0822008-01-18 12:38:59 +0200956 if (c->d & Group) {
957 group = c->d & GroupMask;
958 c->modrm = insn_fetch(u8, 1, c->eip);
959 --c->eip;
960
961 group = (group << 3) + ((c->modrm >> 3) & 7);
962 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
963 c->d = group2_table[group];
964 else
965 c->d = group_table[group];
966 }
967
968 /* Unrecognised? */
969 if (c->d == 0) {
970 DPRINTF("Cannot emulate %02x\n", c->b);
971 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 }
973
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200974 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
975 c->op_bytes = 8;
976
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200978 if (c->d & ModRM)
979 rc = decode_modrm(ctxt, ops);
980 else if (c->d & MemAbs)
981 rc = decode_abs(ctxt, ops);
982 if (rc)
983 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300985 if (!c->has_seg_override)
986 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200987
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300988 if (!(!c->twobyte && c->b == 0x8d))
989 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200990
991 if (c->ad_bytes != 8)
992 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 /*
994 * Decode and fetch the source operand: register, memory
995 * or immediate.
996 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200997 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 case SrcNone:
999 break;
1000 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001001 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 break;
1003 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001004 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 goto srcmem_common;
1006 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001007 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 goto srcmem_common;
1009 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 c->src.bytes = (c->d & ByteOp) ? 1 :
1011 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001012 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001013 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001014 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001015 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001016 /*
1017 * For instructions with a ModR/M byte, switch to register
1018 * access if Mod = 3.
1019 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001020 if ((c->d & ModRM) && c->modrm_mod == 3) {
1021 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001022 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001023 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001024 break;
1025 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001026 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 break;
1028 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 c->src.type = OP_IMM;
1030 c->src.ptr = (unsigned long *)c->eip;
1031 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1032 if (c->src.bytes == 8)
1033 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001037 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 break;
1039 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001040 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 break;
1042 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
1045 }
1046 break;
1047 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001048 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001049 c->src.type = OP_IMM;
1050 c->src.ptr = (unsigned long *)c->eip;
1051 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001052 if ((c->d & SrcMask) == SrcImmByte)
1053 c->src.val = insn_fetch(s8, 1, c->eip);
1054 else
1055 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001057 case SrcOne:
1058 c->src.bytes = 1;
1059 c->src.val = 1;
1060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 }
1062
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001063 /*
1064 * Decode and fetch the second source operand: register, memory
1065 * or immediate.
1066 */
1067 switch (c->d & Src2Mask) {
1068 case Src2None:
1069 break;
1070 case Src2CL:
1071 c->src2.bytes = 1;
1072 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1073 break;
1074 case Src2ImmByte:
1075 c->src2.type = OP_IMM;
1076 c->src2.ptr = (unsigned long *)c->eip;
1077 c->src2.bytes = 1;
1078 c->src2.val = insn_fetch(u8, 1, c->eip);
1079 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001080 case Src2Imm16:
1081 c->src2.type = OP_IMM;
1082 c->src2.ptr = (unsigned long *)c->eip;
1083 c->src2.bytes = 2;
1084 c->src2.val = insn_fetch(u16, 2, c->eip);
1085 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001086 case Src2One:
1087 c->src2.bytes = 1;
1088 c->src2.val = 1;
1089 break;
1090 }
1091
Avi Kivity038e51d2007-01-22 20:40:40 -08001092 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001093 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001094 case ImplicitOps:
1095 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001096 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001097 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001098 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001099 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001100 break;
1101 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001102 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001103 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001104 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001105 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001106 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001107 break;
1108 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001109 c->dst.type = OP_MEM;
1110 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001111 case DstAcc:
1112 c->dst.type = OP_REG;
1113 c->dst.bytes = c->op_bytes;
1114 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1115 switch (c->op_bytes) {
1116 case 1:
1117 c->dst.val = *(u8 *)c->dst.ptr;
1118 break;
1119 case 2:
1120 c->dst.val = *(u16 *)c->dst.ptr;
1121 break;
1122 case 4:
1123 c->dst.val = *(u32 *)c->dst.ptr;
1124 break;
1125 }
1126 c->dst.orig_val = c->dst.val;
1127 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001128 }
1129
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001130 if (c->rip_relative)
1131 c->modrm_ea += c->eip;
1132
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001133done:
1134 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1135}
1136
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001137static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1138{
1139 struct decode_cache *c = &ctxt->decode;
1140
1141 c->dst.type = OP_MEM;
1142 c->dst.bytes = c->op_bytes;
1143 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001144 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001145 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001146 c->regs[VCPU_REGS_RSP]);
1147}
1148
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001149static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001150 struct x86_emulate_ops *ops,
1151 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001152{
1153 struct decode_cache *c = &ctxt->decode;
1154 int rc;
1155
Avi Kivity781d0ed2008-11-27 18:00:28 +02001156 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1157 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001158 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001159 if (rc != 0)
1160 return rc;
1161
Avi Kivity350f69d2009-01-05 11:12:40 +02001162 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001163 return rc;
1164}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001166static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1167 struct x86_emulate_ops *ops)
1168{
1169 struct decode_cache *c = &ctxt->decode;
1170 int rc;
1171
Avi Kivity350f69d2009-01-05 11:12:40 +02001172 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001173 if (rc != 0)
1174 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175 return 0;
1176}
1177
Laurent Vivier05f086f2007-09-24 11:10:55 +02001178static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001179{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001180 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001181 switch (c->modrm_reg) {
1182 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001183 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184 break;
1185 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001186 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187 break;
1188 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001189 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001190 break;
1191 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001192 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001193 break;
1194 case 4: /* sal/shl */
1195 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001196 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 break;
1198 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 break;
1201 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001202 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001203 break;
1204 }
1205}
1206
1207static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001208 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001209{
1210 struct decode_cache *c = &ctxt->decode;
1211 int rc = 0;
1212
1213 switch (c->modrm_reg) {
1214 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001215 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001216 break;
1217 case 2: /* not */
1218 c->dst.val = ~c->dst.val;
1219 break;
1220 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001221 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001222 break;
1223 default:
1224 DPRINTF("Cannot emulate %02x\n", c->b);
1225 rc = X86EMUL_UNHANDLEABLE;
1226 break;
1227 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001228 return rc;
1229}
1230
1231static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001232 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233{
1234 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001235
1236 switch (c->modrm_reg) {
1237 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001238 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239 break;
1240 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001241 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001242 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001243 case 2: /* call near abs */ {
1244 long int old_eip;
1245 old_eip = c->eip;
1246 c->eip = c->src.val;
1247 c->src.val = old_eip;
1248 emulate_push(ctxt);
1249 break;
1250 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001252 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 break;
1254 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001255 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001256 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001257 }
1258 return 0;
1259}
1260
1261static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1262 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001263 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001264{
1265 struct decode_cache *c = &ctxt->decode;
1266 u64 old, new;
1267 int rc;
1268
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001269 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270 if (rc != 0)
1271 return rc;
1272
1273 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1274 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1275
1276 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1277 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001278 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001279
1280 } else {
1281 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1282 (u32) c->regs[VCPU_REGS_RBX];
1283
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001284 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001285 if (rc != 0)
1286 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001287 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001288 }
1289 return 0;
1290}
1291
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001292static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1293 struct x86_emulate_ops *ops)
1294{
1295 struct decode_cache *c = &ctxt->decode;
1296 int rc;
1297 unsigned long cs;
1298
1299 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1300 if (rc)
1301 return rc;
1302 if (c->op_bytes == 4)
1303 c->eip = (u32)c->eip;
1304 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1305 if (rc)
1306 return rc;
1307 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1308 return rc;
1309}
1310
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311static inline int writeback(struct x86_emulate_ctxt *ctxt,
1312 struct x86_emulate_ops *ops)
1313{
1314 int rc;
1315 struct decode_cache *c = &ctxt->decode;
1316
1317 switch (c->dst.type) {
1318 case OP_REG:
1319 /* The 4-byte case *is* correct:
1320 * in 64-bit mode we zero-extend.
1321 */
1322 switch (c->dst.bytes) {
1323 case 1:
1324 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1325 break;
1326 case 2:
1327 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1328 break;
1329 case 4:
1330 *c->dst.ptr = (u32)c->dst.val;
1331 break; /* 64b: zero-ext */
1332 case 8:
1333 *c->dst.ptr = c->dst.val;
1334 break;
1335 }
1336 break;
1337 case OP_MEM:
1338 if (c->lock_prefix)
1339 rc = ops->cmpxchg_emulated(
1340 (unsigned long)c->dst.ptr,
1341 &c->dst.orig_val,
1342 &c->dst.val,
1343 c->dst.bytes,
1344 ctxt->vcpu);
1345 else
1346 rc = ops->write_emulated(
1347 (unsigned long)c->dst.ptr,
1348 &c->dst.val,
1349 c->dst.bytes,
1350 ctxt->vcpu);
1351 if (rc != 0)
1352 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001353 break;
1354 case OP_NONE:
1355 /* no writeback */
1356 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001357 default:
1358 break;
1359 }
1360 return 0;
1361}
1362
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001363int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001364x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001365{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001366 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001367 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001368 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001369 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001370 unsigned int port;
1371 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001372 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001373
Laurent Vivier34273182007-09-18 11:27:37 +02001374 /* Shadow copy of register state. Committed on successful emulation.
1375 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1376 * modify them.
1377 */
1378
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001379 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001380 saved_eip = c->eip;
1381
Avi Kivityc7e75a32007-10-28 16:34:25 +02001382 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001383 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001384
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001385 if (c->rep_prefix && (c->d & String)) {
1386 /* All REP prefixes have the same first termination condition */
1387 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001388 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001389 goto done;
1390 }
1391 /* The second termination condition only applies for REPE
1392 * and REPNE. Test if the repeat string operation prefix is
1393 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1394 * corresponding termination condition according to:
1395 * - if REPE/REPZ and ZF = 0 then done
1396 * - if REPNE/REPNZ and ZF = 1 then done
1397 */
1398 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1399 (c->b == 0xae) || (c->b == 0xaf)) {
1400 if ((c->rep_prefix == REPE_PREFIX) &&
1401 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001402 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001403 goto done;
1404 }
1405 if ((c->rep_prefix == REPNE_PREFIX) &&
1406 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001407 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001408 goto done;
1409 }
1410 }
1411 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001412 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001413 }
1414
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001415 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001416 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001417 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001418 rc = ops->read_emulated((unsigned long)c->src.ptr,
1419 &c->src.val,
1420 c->src.bytes,
1421 ctxt->vcpu);
1422 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001423 goto done;
1424 c->src.orig_val = c->src.val;
1425 }
1426
1427 if ((c->d & DstMask) == ImplicitOps)
1428 goto special_insn;
1429
1430
1431 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001432 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001433 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1434 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001435 if (c->d & BitOp) {
1436 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001437
Laurent Viviere4e03de2007-09-18 11:52:50 +02001438 c->dst.ptr = (void *)c->dst.ptr +
1439 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001440 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001441 if (!(c->d & Mov) &&
1442 /* optimisation - avoid slow emulated read */
1443 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1444 &c->dst.val,
1445 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001446 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001447 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001448 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001449
Avi Kivity018a98d2007-11-27 19:30:56 +02001450special_insn:
1451
Laurent Viviere4e03de2007-09-18 11:52:50 +02001452 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 goto twobyte_insn;
1454
Laurent Viviere4e03de2007-09-18 11:52:50 +02001455 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 case 0x00 ... 0x05:
1457 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001458 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 break;
1460 case 0x08 ... 0x0d:
1461 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001462 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463 break;
1464 case 0x10 ... 0x15:
1465 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001466 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 break;
1468 case 0x18 ... 0x1d:
1469 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001470 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001472 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001474 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 break;
1476 case 0x28 ... 0x2d:
1477 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001478 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 break;
1480 case 0x30 ... 0x35:
1481 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001482 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 break;
1484 case 0x38 ... 0x3d:
1485 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001486 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001488 case 0x40 ... 0x47: /* inc r16/r32 */
1489 emulate_1op("inc", c->dst, ctxt->eflags);
1490 break;
1491 case 0x48 ... 0x4f: /* dec r16/r32 */
1492 emulate_1op("dec", c->dst, ctxt->eflags);
1493 break;
1494 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001495 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001496 break;
1497 case 0x58 ... 0x5f: /* pop reg */
1498 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001499 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001500 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001501 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001502 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001504 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001506 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001508 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001509 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001510 emulate_push(ctxt);
1511 break;
1512 case 0x6c: /* insb */
1513 case 0x6d: /* insw/insd */
1514 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1515 1,
1516 (c->d & ByteOp) ? 1 : c->op_bytes,
1517 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001518 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001519 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001520 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001521 c->regs[VCPU_REGS_RDI]),
1522 c->rep_prefix,
1523 c->regs[VCPU_REGS_RDX]) == 0) {
1524 c->eip = saved_eip;
1525 return -1;
1526 }
1527 return 0;
1528 case 0x6e: /* outsb */
1529 case 0x6f: /* outsw/outsd */
1530 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1531 0,
1532 (c->d & ByteOp) ? 1 : c->op_bytes,
1533 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001534 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001535 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001536 register_address(c,
1537 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001538 c->regs[VCPU_REGS_RSI]),
1539 c->rep_prefix,
1540 c->regs[VCPU_REGS_RDX]) == 0) {
1541 c->eip = saved_eip;
1542 return -1;
1543 }
1544 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001545 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001546 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001547 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001548 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001550 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 case 0:
1552 goto add;
1553 case 1:
1554 goto or;
1555 case 2:
1556 goto adc;
1557 case 3:
1558 goto sbb;
1559 case 4:
1560 goto and;
1561 case 5:
1562 goto sub;
1563 case 6:
1564 goto xor;
1565 case 7:
1566 goto cmp;
1567 }
1568 break;
1569 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001570 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 break;
1572 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001573 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001577 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 break;
1579 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001580 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 break;
1582 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001583 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 break; /* 64b reg: zero-extend */
1585 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001586 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 break;
1588 }
1589 /*
1590 * Write back the memory destination with implicit LOCK
1591 * prefix.
1592 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001593 c->dst.val = c->src.val;
1594 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001597 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001598 case 0x8c: { /* mov r/m, sreg */
1599 struct kvm_segment segreg;
1600
1601 if (c->modrm_reg <= 5)
1602 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1603 else {
1604 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1605 c->modrm);
1606 goto cannot_emulate;
1607 }
1608 c->dst.val = segreg.selector;
1609 break;
1610 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001611 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001612 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001613 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001614 case 0x8e: { /* mov seg, r/m16 */
1615 uint16_t sel;
1616 int type_bits;
1617 int err;
1618
1619 sel = c->src.val;
1620 if (c->modrm_reg <= 5) {
1621 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1622 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1623 type_bits, c->modrm_reg);
1624 } else {
1625 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1626 c->modrm);
1627 goto cannot_emulate;
1628 }
1629
1630 if (err < 0)
1631 goto cannot_emulate;
1632
1633 c->dst.type = OP_NONE; /* Disable writeback. */
1634 break;
1635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637 rc = emulate_grp1a(ctxt, ops);
1638 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001641 case 0x90: /* nop / xchg r8,rax */
1642 if (!(c->rex_prefix & 1)) { /* nop */
1643 c->dst.type = OP_NONE;
1644 break;
1645 }
1646 case 0x91 ... 0x97: /* xchg reg,rax */
1647 c->src.type = c->dst.type = OP_REG;
1648 c->src.bytes = c->dst.bytes = c->op_bytes;
1649 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1650 c->src.val = *(c->src.ptr);
1651 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001652 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001653 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001654 emulate_push(ctxt);
1655 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001656 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001657 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001658 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001659 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001660 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001661 case 0xa0 ... 0xa1: /* mov */
1662 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1663 c->dst.val = c->src.val;
1664 break;
1665 case 0xa2 ... 0xa3: /* mov */
1666 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1667 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001669 c->dst.type = OP_MEM;
1670 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001671 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001672 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001673 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001674 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001675 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001676 c->regs[VCPU_REGS_RSI]),
1677 &c->dst.val,
1678 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001680 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001681 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001683 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001684 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 break;
1687 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001688 c->src.type = OP_NONE; /* Disable writeback. */
1689 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001690 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001691 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001692 c->regs[VCPU_REGS_RSI]);
1693 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1694 &c->src.val,
1695 c->src.bytes,
1696 ctxt->vcpu)) != 0)
1697 goto done;
1698
1699 c->dst.type = OP_NONE; /* Disable writeback. */
1700 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001701 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001702 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001703 c->regs[VCPU_REGS_RDI]);
1704 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1705 &c->dst.val,
1706 c->dst.bytes,
1707 ctxt->vcpu)) != 0)
1708 goto done;
1709
1710 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1711
1712 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1713
Harvey Harrison7a9572752008-02-19 07:40:41 -08001714 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001715 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1716 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001717 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001718 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1719 : c->dst.bytes);
1720
1721 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 c->dst.type = OP_MEM;
1724 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001725 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001726 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001727 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001728 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001729 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001730 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001731 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 break;
1733 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001734 c->dst.type = OP_REG;
1735 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1736 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001737 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001738 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001739 c->regs[VCPU_REGS_RSI]),
1740 &c->dst.val,
1741 c->dst.bytes,
1742 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001744 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001745 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001746 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 break;
1748 case 0xae ... 0xaf: /* scas */
1749 DPRINTF("Urk! I don't handle SCAS.\n");
1750 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001751 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001752 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001753 case 0xc0 ... 0xc1:
1754 emulate_grp2(ctxt);
1755 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001756 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001757 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001758 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001759 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001760 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001761 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1762 mov:
1763 c->dst.val = c->src.val;
1764 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001765 case 0xcb: /* ret far */
1766 rc = emulate_ret_far(ctxt, ops);
1767 if (rc)
1768 goto done;
1769 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001770 case 0xd0 ... 0xd1: /* Grp2 */
1771 c->src.val = 1;
1772 emulate_grp2(ctxt);
1773 break;
1774 case 0xd2 ... 0xd3: /* Grp2 */
1775 c->src.val = c->regs[VCPU_REGS_RCX];
1776 emulate_grp2(ctxt);
1777 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001778 case 0xe4: /* inb */
1779 case 0xe5: /* in */
1780 port = insn_fetch(u8, 1, c->eip);
1781 io_dir_in = 1;
1782 goto do_io;
1783 case 0xe6: /* outb */
1784 case 0xe7: /* out */
1785 port = insn_fetch(u8, 1, c->eip);
1786 io_dir_in = 0;
1787 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001788 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03001789 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001790 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001791 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792 emulate_push(ctxt);
1793 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001794 }
1795 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001796 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03001797 case 0xea: /* jmp far */
1798 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
1799 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001800 DPRINTF("jmp far: Failed to load CS descriptor\n");
1801 goto cannot_emulate;
1802 }
1803
Gleb Natapov782b8772009-04-12 13:36:25 +03001804 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001805 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001806 case 0xeb:
1807 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001808 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001809 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001810 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001811 case 0xec: /* in al,dx */
1812 case 0xed: /* in (e/r)ax,dx */
1813 port = c->regs[VCPU_REGS_RDX];
1814 io_dir_in = 1;
1815 goto do_io;
1816 case 0xee: /* out al,dx */
1817 case 0xef: /* out (e/r)ax,dx */
1818 port = c->regs[VCPU_REGS_RDX];
1819 io_dir_in = 0;
1820 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1821 (c->d & ByteOp) ? 1 : c->op_bytes,
1822 port) != 0) {
1823 c->eip = saved_eip;
1824 goto cannot_emulate;
1825 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001826 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001827 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001828 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001829 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001830 case 0xf5: /* cmc */
1831 /* complement carry flag from eflags reg */
1832 ctxt->eflags ^= EFLG_CF;
1833 c->dst.type = OP_NONE; /* Disable writeback. */
1834 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001835 case 0xf6 ... 0xf7: /* Grp3 */
1836 rc = emulate_grp3(ctxt, ops);
1837 if (rc != 0)
1838 goto done;
1839 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001840 case 0xf8: /* clc */
1841 ctxt->eflags &= ~EFLG_CF;
1842 c->dst.type = OP_NONE; /* Disable writeback. */
1843 break;
1844 case 0xfa: /* cli */
1845 ctxt->eflags &= ~X86_EFLAGS_IF;
1846 c->dst.type = OP_NONE; /* Disable writeback. */
1847 break;
1848 case 0xfb: /* sti */
1849 ctxt->eflags |= X86_EFLAGS_IF;
1850 c->dst.type = OP_NONE; /* Disable writeback. */
1851 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001852 case 0xfc: /* cld */
1853 ctxt->eflags &= ~EFLG_DF;
1854 c->dst.type = OP_NONE; /* Disable writeback. */
1855 break;
1856 case 0xfd: /* std */
1857 ctxt->eflags |= EFLG_DF;
1858 c->dst.type = OP_NONE; /* Disable writeback. */
1859 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001860 case 0xfe ... 0xff: /* Grp4/Grp5 */
1861 rc = emulate_grp45(ctxt, ops);
1862 if (rc != 0)
1863 goto done;
1864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001866
1867writeback:
1868 rc = writeback(ctxt, ops);
1869 if (rc != 0)
1870 goto done;
1871
1872 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001873 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001874 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001875
1876done:
1877 if (rc == X86EMUL_UNHANDLEABLE) {
1878 c->eip = saved_eip;
1879 return -1;
1880 }
1881 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882
1883twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001884 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001886 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 u16 size;
1888 unsigned long address;
1889
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001890 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001891 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001892 goto cannot_emulate;
1893
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001894 rc = kvm_fix_hypercall(ctxt->vcpu);
1895 if (rc)
1896 goto done;
1897
Avi Kivity33e38852008-05-21 15:34:25 +03001898 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001899 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001900 /* Disable writeback. */
1901 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001902 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001904 rc = read_descriptor(ctxt, ops, c->src.ptr,
1905 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 if (rc)
1907 goto done;
1908 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001909 /* Disable writeback. */
1910 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001912 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02001913 if (c->modrm_mod == 3) {
1914 switch (c->modrm_rm) {
1915 case 1:
1916 rc = kvm_fix_hypercall(ctxt->vcpu);
1917 if (rc)
1918 goto done;
1919 break;
1920 default:
1921 goto cannot_emulate;
1922 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001923 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001924 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001925 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001927 if (rc)
1928 goto done;
1929 realmode_lidt(ctxt->vcpu, size, address);
1930 }
Avi Kivity16286d02008-04-14 14:40:50 +03001931 /* Disable writeback. */
1932 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 break;
1934 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001935 c->dst.bytes = 2;
1936 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 break;
1938 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001939 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1940 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001941 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 break;
1943 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001944 emulate_invlpg(ctxt->vcpu, memop);
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;
1948 default:
1949 goto cannot_emulate;
1950 }
1951 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001952 case 0x06:
1953 emulate_clts(ctxt->vcpu);
1954 c->dst.type = OP_NONE;
1955 break;
1956 case 0x08: /* invd */
1957 case 0x09: /* wbinvd */
1958 case 0x0d: /* GrpP (prefetch) */
1959 case 0x18: /* Grp16 (prefetch/nop) */
1960 c->dst.type = OP_NONE;
1961 break;
1962 case 0x20: /* mov cr, reg */
1963 if (c->modrm_mod != 3)
1964 goto cannot_emulate;
1965 c->regs[c->modrm_rm] =
1966 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1967 c->dst.type = OP_NONE; /* no writeback */
1968 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001970 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001972 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001973 if (rc)
1974 goto cannot_emulate;
1975 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001977 case 0x22: /* mov reg, cr */
1978 if (c->modrm_mod != 3)
1979 goto cannot_emulate;
1980 realmode_set_cr(ctxt->vcpu,
1981 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1982 c->dst.type = OP_NONE;
1983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001985 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001987 rc = emulator_set_dr(ctxt, c->modrm_reg,
1988 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001989 if (rc)
1990 goto cannot_emulate;
1991 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001993 case 0x30:
1994 /* wrmsr */
1995 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1996 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1997 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1998 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001999 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002000 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002001 }
2002 rc = X86EMUL_CONTINUE;
2003 c->dst.type = OP_NONE;
2004 break;
2005 case 0x32:
2006 /* rdmsr */
2007 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2008 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002009 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002010 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002011 } else {
2012 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2013 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2014 }
2015 rc = X86EMUL_CONTINUE;
2016 c->dst.type = OP_NONE;
2017 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002019 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002020 if (!test_cc(c->b, ctxt->eflags))
2021 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002023 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002024 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002025 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002026 c->dst.type = OP_NONE;
2027 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002028 case 0xa3:
2029 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002030 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002031 /* only subword offset */
2032 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002033 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002034 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002035 case 0xa4: /* shld imm8, r, r/m */
2036 case 0xa5: /* shld cl, r, r/m */
2037 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2038 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002039 case 0xab:
2040 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002041 /* only subword offset */
2042 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002043 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002044 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002045 case 0xac: /* shrd imm8, r, r/m */
2046 case 0xad: /* shrd cl, r, r/m */
2047 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2048 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002049 case 0xae: /* clflush */
2050 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 case 0xb0 ... 0xb1: /* cmpxchg */
2052 /*
2053 * Save real source value, then compare EAX against
2054 * destination.
2055 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002056 c->src.orig_val = c->src.val;
2057 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002058 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2059 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002061 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 } else {
2063 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002064 c->dst.type = OP_REG;
2065 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 }
2067 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068 case 0xb3:
2069 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002070 /* only subword offset */
2071 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002072 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002075 c->dst.bytes = c->op_bytes;
2076 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2077 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002080 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 case 0:
2082 goto bt;
2083 case 1:
2084 goto bts;
2085 case 2:
2086 goto btr;
2087 case 3:
2088 goto btc;
2089 }
2090 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002091 case 0xbb:
2092 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002093 /* only subword offset */
2094 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002095 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002096 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002098 c->dst.bytes = c->op_bytes;
2099 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2100 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002102 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002103 c->dst.bytes = c->op_bytes;
2104 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2105 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002106 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002108 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002109 if (rc != 0)
2110 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002111 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002112 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 }
2114 goto writeback;
2115
2116cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002117 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002118 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 return -1;
2120}