blob: 67af33aeb3f3d19d95f5abeea957a270b100cd8c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivityedf88412007-12-16 11:02:48 +020033#include <asm/kvm_x86_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
35/*
36 * Opcode effective-address decode tables.
37 * Note that we only emulate instructions that have at least one memory
38 * operand (excluding implicit stack references). We assume that stack
39 * references and instruction fetches will never occur in special memory
40 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
41 * not be handled.
42 */
43
44/* Operand sizes: 8-bit operands or specified/overridden size. */
45#define ByteOp (1<<0) /* 8-bit operands. */
46/* Destination operand type. */
47#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
48#define DstReg (2<<1) /* Register operand. */
49#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020050#define DstAcc (4<<1) /* Destination Accumulator */
51#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080052/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcNone (0<<4) /* No source operand. */
54#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
55#define SrcReg (1<<4) /* Register operand. */
56#define SrcMem (2<<4) /* Memory operand. */
57#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
58#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
59#define SrcImm (5<<4) /* Immediate operand. */
60#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010061#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030062#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030063#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define Mov (1<<9)
69#define BitOp (1<<10)
70#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020071#define String (1<<12) /* String instruction (rep capable) */
72#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020073#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
74#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
75#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010076/* Source 2 operand type */
77#define Src2None (0<<29)
78#define Src2CL (1<<29)
79#define Src2ImmByte (2<<29)
80#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030081#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010082#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080083
Avi Kivity43bb19c2008-01-18 12:46:50 +020084enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020085 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020086 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020087};
88
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010089static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080090 /* 0x00 - 0x07 */
91 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
92 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020093 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080094 /* 0x08 - 0x0F */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
97 0, 0, 0, 0,
98 /* 0x10 - 0x17 */
99 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
100 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
101 0, 0, 0, 0,
102 /* 0x18 - 0x1F */
103 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
105 0, 0, 0, 0,
106 /* 0x20 - 0x27 */
107 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
108 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200109 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 /* 0x28 - 0x2F */
111 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
112 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
113 0, 0, 0, 0,
114 /* 0x30 - 0x37 */
115 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
116 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
117 0, 0, 0, 0,
118 /* 0x38 - 0x3F */
119 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
120 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200121 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
122 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700123 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200124 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700125 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200126 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300127 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200128 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
129 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300130 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200131 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
132 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700133 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700135 0, 0, 0, 0,
136 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300137 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300138 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
139 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300140 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300141 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
142 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300143 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300144 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
145 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200147 Group | Group1_80, Group | Group1_81,
148 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
150 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
151 /* 0x88 - 0x8F */
152 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
153 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200154 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200155 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300156 /* 0x90 - 0x97 */
157 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
158 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300159 0, 0, SrcImm | Src2Imm16, 0,
160 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200162 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
163 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200164 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
165 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200167 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
168 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
169 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300170 /* 0xB0 - 0xB7 */
171 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
172 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
173 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
174 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
175 /* 0xB8 - 0xBF */
176 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
177 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
178 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
179 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300181 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200182 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300183 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300185 0, 0, 0, ImplicitOps | Stack,
186 ImplicitOps, SrcImmByte, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 /* 0xD0 - 0xD7 */
188 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
189 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
190 0, 0, 0, 0,
191 /* 0xD8 - 0xDF */
192 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300193 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300194 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300195 ByteOp | SrcImmUByte, SrcImmUByte,
196 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300197 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300198 SrcImm | Stack, SrcImm | ImplicitOps,
Avi Kivityee3d29e2009-05-18 16:15:20 +0300199 SrcImmU | Src2Imm16, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300200 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
201 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xF0 - 0xF7 */
203 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200204 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700206 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300207 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208};
209
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100210static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200212 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200213 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 /* 0x10 - 0x1F */
215 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
216 /* 0x20 - 0x2F */
217 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0,
219 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300220 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0x40 - 0x47 */
222 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
223 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
224 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
225 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
226 /* 0x48 - 0x4F */
227 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
228 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
229 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
230 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
231 /* 0x50 - 0x5F */
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 /* 0x60 - 0x6F */
234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x70 - 0x7F */
236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300238 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
239 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 /* 0x90 - 0x9F */
241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100243 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
244 DstMem | SrcReg | Src2ImmByte | ModRM,
245 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100247 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
248 DstMem | SrcReg | Src2ImmByte | ModRM,
249 DstMem | SrcReg | Src2CL | ModRM,
250 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 /* 0xB0 - 0xB7 */
252 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800253 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
255 DstReg | SrcMem16 | ModRM | Mov,
256 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800257 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
259 DstReg | SrcMem16 | ModRM | Mov,
260 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800261 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
262 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 /* 0xD0 - 0xDF */
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265 /* 0xE0 - 0xEF */
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267 /* 0xF0 - 0xFF */
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
269};
270
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100271static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200272 [Group1_80*8] =
273 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
274 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
275 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
276 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
277 [Group1_81*8] =
278 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
279 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
280 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
281 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
282 [Group1_82*8] =
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
286 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
287 [Group1_83*8] =
288 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
289 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
290 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
291 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200292 [Group1A*8] =
293 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200294 [Group3_Byte*8] =
295 ByteOp | SrcImm | DstMem | ModRM, 0,
296 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
297 0, 0, 0, 0,
298 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400299 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300300 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200301 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200302 [Group4*8] =
303 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
304 0, 0, 0, 0, 0, 0,
305 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300306 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
307 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300308 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200309 [Group7*8] =
310 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300311 SrcNone | ModRM | DstMem | Mov, 0,
312 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200313};
314
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100315static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200316 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000317 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300318 SrcNone | ModRM | DstMem | Mov, 0,
319 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200320};
321
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200323#define EFLG_VM (1<<17)
324#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325#define EFLG_OF (1<<11)
326#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200327#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328#define EFLG_SF (1<<7)
329#define EFLG_ZF (1<<6)
330#define EFLG_AF (1<<4)
331#define EFLG_PF (1<<2)
332#define EFLG_CF (1<<0)
333
334/*
335 * Instruction emulation:
336 * Most instructions are emulated directly via a fragment of inline assembly
337 * code. This allows us to save/restore EFLAGS and thus very easily pick up
338 * any modified flags.
339 */
340
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800341#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342#define _LO32 "k" /* force 32-bit operand */
343#define _STK "%%rsp" /* stack pointer */
344#elif defined(__i386__)
345#define _LO32 "" /* force 32-bit operand */
346#define _STK "%%esp" /* stack pointer */
347#endif
348
349/*
350 * These EFLAGS bits are restored from saved value during emulation, and
351 * any changes are written back to the saved value after emulation.
352 */
353#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
354
355/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200356#define _PRE_EFLAGS(_sav, _msk, _tmp) \
357 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
358 "movl %"_sav",%"_LO32 _tmp"; " \
359 "push %"_tmp"; " \
360 "push %"_tmp"; " \
361 "movl %"_msk",%"_LO32 _tmp"; " \
362 "andl %"_LO32 _tmp",("_STK"); " \
363 "pushf; " \
364 "notl %"_LO32 _tmp"; " \
365 "andl %"_LO32 _tmp",("_STK"); " \
366 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
367 "pop %"_tmp"; " \
368 "orl %"_LO32 _tmp",("_STK"); " \
369 "popf; " \
370 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371
372/* After executing instruction: write-back necessary bits in EFLAGS. */
373#define _POST_EFLAGS(_sav, _msk, _tmp) \
374 /* _sav |= EFLAGS & _msk; */ \
375 "pushf; " \
376 "pop %"_tmp"; " \
377 "andl %"_msk",%"_LO32 _tmp"; " \
378 "orl %"_LO32 _tmp",%"_sav"; "
379
Avi Kivitydda96d82008-11-26 15:14:10 +0200380#ifdef CONFIG_X86_64
381#define ON64(x) x
382#else
383#define ON64(x)
384#endif
385
Avi Kivity6b7ad612008-11-26 15:30:45 +0200386#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
387 do { \
388 __asm__ __volatile__ ( \
389 _PRE_EFLAGS("0", "4", "2") \
390 _op _suffix " %"_x"3,%1; " \
391 _POST_EFLAGS("0", "4", "2") \
392 : "=m" (_eflags), "=m" ((_dst).val), \
393 "=&r" (_tmp) \
394 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200395 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200396
397
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398/* Raw emulation: instruction has two explicit operands. */
399#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200400 do { \
401 unsigned long _tmp; \
402 \
403 switch ((_dst).bytes) { \
404 case 2: \
405 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
406 break; \
407 case 4: \
408 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
409 break; \
410 case 8: \
411 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
412 break; \
413 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 } while (0)
415
416#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
417 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200418 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400419 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200421 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 break; \
423 default: \
424 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
425 _wx, _wy, _lx, _ly, _qx, _qy); \
426 break; \
427 } \
428 } while (0)
429
430/* Source operand is byte-sized and may be restricted to just %cl. */
431#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
432 __emulate_2op(_op, _src, _dst, _eflags, \
433 "b", "c", "b", "c", "b", "c", "b", "c")
434
435/* Source operand is byte, word, long or quad sized. */
436#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
437 __emulate_2op(_op, _src, _dst, _eflags, \
438 "b", "q", "w", "r", _LO32, "r", "", "r")
439
440/* Source operand is word, long or quad sized. */
441#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
442 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
443 "w", "r", _LO32, "r", "", "r")
444
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100445/* Instruction has three operands and one operand is stored in ECX register */
446#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
447 do { \
448 unsigned long _tmp; \
449 _type _clv = (_cl).val; \
450 _type _srcv = (_src).val; \
451 _type _dstv = (_dst).val; \
452 \
453 __asm__ __volatile__ ( \
454 _PRE_EFLAGS("0", "5", "2") \
455 _op _suffix " %4,%1 \n" \
456 _POST_EFLAGS("0", "5", "2") \
457 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
458 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
459 ); \
460 \
461 (_cl).val = (unsigned long) _clv; \
462 (_src).val = (unsigned long) _srcv; \
463 (_dst).val = (unsigned long) _dstv; \
464 } while (0)
465
466#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
467 do { \
468 switch ((_dst).bytes) { \
469 case 2: \
470 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
471 "w", unsigned short); \
472 break; \
473 case 4: \
474 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
475 "l", unsigned int); \
476 break; \
477 case 8: \
478 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
479 "q", unsigned long)); \
480 break; \
481 } \
482 } while (0)
483
Avi Kivitydda96d82008-11-26 15:14:10 +0200484#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 do { \
486 unsigned long _tmp; \
487 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200488 __asm__ __volatile__ ( \
489 _PRE_EFLAGS("0", "3", "2") \
490 _op _suffix " %1; " \
491 _POST_EFLAGS("0", "3", "2") \
492 : "=m" (_eflags), "+m" ((_dst).val), \
493 "=&r" (_tmp) \
494 : "i" (EFLAGS_MASK)); \
495 } while (0)
496
497/* Instruction has only one explicit operand (no source operand). */
498#define emulate_1op(_op, _dst, _eflags) \
499 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400500 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200501 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
502 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
503 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
504 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 } \
506 } while (0)
507
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508/* Fetch next part of the instruction being emulated. */
509#define insn_fetch(_type, _size, _eip) \
510({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200511 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400512 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 goto done; \
514 (_eip) += (_size); \
515 (_type)_x; \
516})
517
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800518static inline unsigned long ad_mask(struct decode_cache *c)
519{
520 return (1UL << (c->ad_bytes << 3)) - 1;
521}
522
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800524static inline unsigned long
525address_mask(struct decode_cache *c, unsigned long reg)
526{
527 if (c->ad_bytes == sizeof(unsigned long))
528 return reg;
529 else
530 return reg & ad_mask(c);
531}
532
533static inline unsigned long
534register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
535{
536 return base + address_mask(c, reg);
537}
538
Harvey Harrison7a9572752008-02-19 07:40:41 -0800539static inline void
540register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
541{
542 if (c->ad_bytes == sizeof(unsigned long))
543 *reg += inc;
544 else
545 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
546}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547
Harvey Harrison7a9572752008-02-19 07:40:41 -0800548static inline void jmp_rel(struct decode_cache *c, int rel)
549{
550 register_address_increment(c, &c->eip, rel);
551}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300552
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300553static void set_seg_override(struct decode_cache *c, int seg)
554{
555 c->has_seg_override = true;
556 c->seg_override = seg;
557}
558
559static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
560{
561 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
562 return 0;
563
564 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
565}
566
567static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
568 struct decode_cache *c)
569{
570 if (!c->has_seg_override)
571 return 0;
572
573 return seg_base(ctxt, c->seg_override);
574}
575
576static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
577{
578 return seg_base(ctxt, VCPU_SREG_ES);
579}
580
581static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
582{
583 return seg_base(ctxt, VCPU_SREG_SS);
584}
585
Avi Kivity62266862007-11-20 13:15:52 +0200586static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
587 struct x86_emulate_ops *ops,
588 unsigned long linear, u8 *dest)
589{
590 struct fetch_cache *fc = &ctxt->decode.fetch;
591 int rc;
592 int size;
593
594 if (linear < fc->start || linear >= fc->end) {
595 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
596 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
597 if (rc)
598 return rc;
599 fc->start = linear;
600 fc->end = linear + size;
601 }
602 *dest = fc->data[linear - fc->start];
603 return 0;
604}
605
606static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
607 struct x86_emulate_ops *ops,
608 unsigned long eip, void *dest, unsigned size)
609{
610 int rc = 0;
611
612 eip += ctxt->cs_base;
613 while (size--) {
614 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
615 if (rc)
616 return rc;
617 }
618 return 0;
619}
620
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000621/*
622 * Given the 'reg' portion of a ModRM byte, and a register block, return a
623 * pointer into the block that addresses the relevant register.
624 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
625 */
626static void *decode_register(u8 modrm_reg, unsigned long *regs,
627 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628{
629 void *p;
630
631 p = &regs[modrm_reg];
632 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
633 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
634 return p;
635}
636
637static int read_descriptor(struct x86_emulate_ctxt *ctxt,
638 struct x86_emulate_ops *ops,
639 void *ptr,
640 u16 *size, unsigned long *address, int op_bytes)
641{
642 int rc;
643
644 if (op_bytes == 2)
645 op_bytes = 3;
646 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300647 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
648 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 if (rc)
650 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300651 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
652 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 return rc;
654}
655
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300656static int test_cc(unsigned int condition, unsigned int flags)
657{
658 int rc = 0;
659
660 switch ((condition & 15) >> 1) {
661 case 0: /* o */
662 rc |= (flags & EFLG_OF);
663 break;
664 case 1: /* b/c/nae */
665 rc |= (flags & EFLG_CF);
666 break;
667 case 2: /* z/e */
668 rc |= (flags & EFLG_ZF);
669 break;
670 case 3: /* be/na */
671 rc |= (flags & (EFLG_CF|EFLG_ZF));
672 break;
673 case 4: /* s */
674 rc |= (flags & EFLG_SF);
675 break;
676 case 5: /* p/pe */
677 rc |= (flags & EFLG_PF);
678 break;
679 case 7: /* le/ng */
680 rc |= (flags & EFLG_ZF);
681 /* fall through */
682 case 6: /* l/nge */
683 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
684 break;
685 }
686
687 /* Odd condition identifiers (lsb == 1) have inverted sense. */
688 return (!!rc ^ (condition & 1));
689}
690
Avi Kivity3c118e22007-10-31 10:27:04 +0200691static void decode_register_operand(struct operand *op,
692 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200693 int inhibit_bytereg)
694{
Avi Kivity33615aa2007-10-31 11:15:56 +0200695 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200696 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200697
698 if (!(c->d & ModRM))
699 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200700 op->type = OP_REG;
701 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200702 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200703 op->val = *(u8 *)op->ptr;
704 op->bytes = 1;
705 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200706 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200707 op->bytes = c->op_bytes;
708 switch (op->bytes) {
709 case 2:
710 op->val = *(u16 *)op->ptr;
711 break;
712 case 4:
713 op->val = *(u32 *)op->ptr;
714 break;
715 case 8:
716 op->val = *(u64 *) op->ptr;
717 break;
718 }
719 }
720 op->orig_val = op->val;
721}
722
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200723static int decode_modrm(struct x86_emulate_ctxt *ctxt,
724 struct x86_emulate_ops *ops)
725{
726 struct decode_cache *c = &ctxt->decode;
727 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700728 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200729 int rc = 0;
730
731 if (c->rex_prefix) {
732 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
733 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
734 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
735 }
736
737 c->modrm = insn_fetch(u8, 1, c->eip);
738 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
739 c->modrm_reg |= (c->modrm & 0x38) >> 3;
740 c->modrm_rm |= (c->modrm & 0x07);
741 c->modrm_ea = 0;
742 c->use_modrm_ea = 1;
743
744 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300745 c->modrm_ptr = decode_register(c->modrm_rm,
746 c->regs, c->d & ByteOp);
747 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200748 return rc;
749 }
750
751 if (c->ad_bytes == 2) {
752 unsigned bx = c->regs[VCPU_REGS_RBX];
753 unsigned bp = c->regs[VCPU_REGS_RBP];
754 unsigned si = c->regs[VCPU_REGS_RSI];
755 unsigned di = c->regs[VCPU_REGS_RDI];
756
757 /* 16-bit ModR/M decode. */
758 switch (c->modrm_mod) {
759 case 0:
760 if (c->modrm_rm == 6)
761 c->modrm_ea += insn_fetch(u16, 2, c->eip);
762 break;
763 case 1:
764 c->modrm_ea += insn_fetch(s8, 1, c->eip);
765 break;
766 case 2:
767 c->modrm_ea += insn_fetch(u16, 2, c->eip);
768 break;
769 }
770 switch (c->modrm_rm) {
771 case 0:
772 c->modrm_ea += bx + si;
773 break;
774 case 1:
775 c->modrm_ea += bx + di;
776 break;
777 case 2:
778 c->modrm_ea += bp + si;
779 break;
780 case 3:
781 c->modrm_ea += bp + di;
782 break;
783 case 4:
784 c->modrm_ea += si;
785 break;
786 case 5:
787 c->modrm_ea += di;
788 break;
789 case 6:
790 if (c->modrm_mod != 0)
791 c->modrm_ea += bp;
792 break;
793 case 7:
794 c->modrm_ea += bx;
795 break;
796 }
797 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
798 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300799 if (!c->has_seg_override)
800 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200801 c->modrm_ea = (u16)c->modrm_ea;
802 } else {
803 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700804 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200805 sib = insn_fetch(u8, 1, c->eip);
806 index_reg |= (sib >> 3) & 7;
807 base_reg |= sib & 7;
808 scale = sib >> 6;
809
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700810 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
811 c->modrm_ea += insn_fetch(s32, 4, c->eip);
812 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200813 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700814 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200815 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700816 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
817 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700818 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700819 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200820 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200821 switch (c->modrm_mod) {
822 case 0:
823 if (c->modrm_rm == 5)
824 c->modrm_ea += insn_fetch(s32, 4, c->eip);
825 break;
826 case 1:
827 c->modrm_ea += insn_fetch(s8, 1, c->eip);
828 break;
829 case 2:
830 c->modrm_ea += insn_fetch(s32, 4, c->eip);
831 break;
832 }
833 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200834done:
835 return rc;
836}
837
838static int decode_abs(struct x86_emulate_ctxt *ctxt,
839 struct x86_emulate_ops *ops)
840{
841 struct decode_cache *c = &ctxt->decode;
842 int rc = 0;
843
844 switch (c->ad_bytes) {
845 case 2:
846 c->modrm_ea = insn_fetch(u16, 2, c->eip);
847 break;
848 case 4:
849 c->modrm_ea = insn_fetch(u32, 4, c->eip);
850 break;
851 case 8:
852 c->modrm_ea = insn_fetch(u64, 8, c->eip);
853 break;
854 }
855done:
856 return rc;
857}
858
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200860x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200862 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200865 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866
867 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
Laurent Viviere4e03de2007-09-18 11:52:50 +0200869 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300870 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300871 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800872 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873
874 switch (mode) {
875 case X86EMUL_MODE_REAL:
876 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200877 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 break;
879 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200880 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800882#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200884 def_op_bytes = 4;
885 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 break;
887#endif
888 default:
889 return -1;
890 }
891
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200892 c->op_bytes = def_op_bytes;
893 c->ad_bytes = def_ad_bytes;
894
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200896 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200897 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200899 /* switch between 2/4 bytes */
900 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 break;
902 case 0x67: /* address-size override */
903 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200904 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200905 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200907 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200908 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300911 case 0x2e: /* CS override */
912 case 0x36: /* SS override */
913 case 0x3e: /* DS override */
914 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 break;
916 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300918 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200920 case 0x40 ... 0x4f: /* REX */
921 if (mode != X86EMUL_MODE_PROT64)
922 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200923 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200924 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200926 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200928 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100929 c->rep_prefix = REPNE_PREFIX;
930 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100932 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 default:
935 goto done_prefixes;
936 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200937
938 /* Any legacy prefix after a REX prefix nullifies its effect. */
939
Avi Kivity33615aa2007-10-31 11:15:56 +0200940 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 }
942
943done_prefixes:
944
945 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200946 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200947 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200948 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949
950 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200951 c->d = opcode_table[c->b];
952 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200954 if (c->b == 0x0f) {
955 c->twobyte = 1;
956 c->b = insn_fetch(u8, 1, c->eip);
957 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200959 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960
Avi Kivitye09d0822008-01-18 12:38:59 +0200961 if (c->d & Group) {
962 group = c->d & GroupMask;
963 c->modrm = insn_fetch(u8, 1, c->eip);
964 --c->eip;
965
966 group = (group << 3) + ((c->modrm >> 3) & 7);
967 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
968 c->d = group2_table[group];
969 else
970 c->d = group_table[group];
971 }
972
973 /* Unrecognised? */
974 if (c->d == 0) {
975 DPRINTF("Cannot emulate %02x\n", c->b);
976 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 }
978
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200979 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
980 c->op_bytes = 8;
981
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983 if (c->d & ModRM)
984 rc = decode_modrm(ctxt, ops);
985 else if (c->d & MemAbs)
986 rc = decode_abs(ctxt, ops);
987 if (rc)
988 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300990 if (!c->has_seg_override)
991 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200992
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300993 if (!(!c->twobyte && c->b == 0x8d))
994 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200995
996 if (c->ad_bytes != 8)
997 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 /*
999 * Decode and fetch the source operand: register, memory
1000 * or immediate.
1001 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001002 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 case SrcNone:
1004 break;
1005 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001006 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 break;
1008 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001009 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 goto srcmem_common;
1011 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001012 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 goto srcmem_common;
1014 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->src.bytes = (c->d & ByteOp) ? 1 :
1016 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001017 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001018 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001019 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001020 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001021 /*
1022 * For instructions with a ModR/M byte, switch to register
1023 * access if Mod = 3.
1024 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001025 if ((c->d & ModRM) && c->modrm_mod == 3) {
1026 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001027 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001028 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001029 break;
1030 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001034 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 c->src.type = OP_IMM;
1036 c->src.ptr = (unsigned long *)c->eip;
1037 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1038 if (c->src.bytes == 8)
1039 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 break;
1045 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 break;
1048 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001049 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 break;
1051 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001052 if ((c->d & SrcMask) == SrcImmU) {
1053 switch (c->src.bytes) {
1054 case 1:
1055 c->src.val &= 0xff;
1056 break;
1057 case 2:
1058 c->src.val &= 0xffff;
1059 break;
1060 case 4:
1061 c->src.val &= 0xffffffff;
1062 break;
1063 }
1064 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 break;
1066 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001067 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001068 c->src.type = OP_IMM;
1069 c->src.ptr = (unsigned long *)c->eip;
1070 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001071 if ((c->d & SrcMask) == SrcImmByte)
1072 c->src.val = insn_fetch(s8, 1, c->eip);
1073 else
1074 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001076 case SrcOne:
1077 c->src.bytes = 1;
1078 c->src.val = 1;
1079 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 }
1081
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001082 /*
1083 * Decode and fetch the second source operand: register, memory
1084 * or immediate.
1085 */
1086 switch (c->d & Src2Mask) {
1087 case Src2None:
1088 break;
1089 case Src2CL:
1090 c->src2.bytes = 1;
1091 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1092 break;
1093 case Src2ImmByte:
1094 c->src2.type = OP_IMM;
1095 c->src2.ptr = (unsigned long *)c->eip;
1096 c->src2.bytes = 1;
1097 c->src2.val = insn_fetch(u8, 1, c->eip);
1098 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001099 case Src2Imm16:
1100 c->src2.type = OP_IMM;
1101 c->src2.ptr = (unsigned long *)c->eip;
1102 c->src2.bytes = 2;
1103 c->src2.val = insn_fetch(u16, 2, c->eip);
1104 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001105 case Src2One:
1106 c->src2.bytes = 1;
1107 c->src2.val = 1;
1108 break;
1109 }
1110
Avi Kivity038e51d2007-01-22 20:40:40 -08001111 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001112 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001113 case ImplicitOps:
1114 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001115 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001116 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001117 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001118 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001119 break;
1120 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001121 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001122 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001123 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001124 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001125 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001126 break;
1127 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001128 c->dst.type = OP_MEM;
1129 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001130 case DstAcc:
1131 c->dst.type = OP_REG;
1132 c->dst.bytes = c->op_bytes;
1133 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1134 switch (c->op_bytes) {
1135 case 1:
1136 c->dst.val = *(u8 *)c->dst.ptr;
1137 break;
1138 case 2:
1139 c->dst.val = *(u16 *)c->dst.ptr;
1140 break;
1141 case 4:
1142 c->dst.val = *(u32 *)c->dst.ptr;
1143 break;
1144 }
1145 c->dst.orig_val = c->dst.val;
1146 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001147 }
1148
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001149 if (c->rip_relative)
1150 c->modrm_ea += c->eip;
1151
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001152done:
1153 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1154}
1155
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001156static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1157{
1158 struct decode_cache *c = &ctxt->decode;
1159
1160 c->dst.type = OP_MEM;
1161 c->dst.bytes = c->op_bytes;
1162 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001163 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001164 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165 c->regs[VCPU_REGS_RSP]);
1166}
1167
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001168static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001169 struct x86_emulate_ops *ops,
1170 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001171{
1172 struct decode_cache *c = &ctxt->decode;
1173 int rc;
1174
Avi Kivity781d0ed2008-11-27 18:00:28 +02001175 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1176 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001177 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178 if (rc != 0)
1179 return rc;
1180
Avi Kivity350f69d2009-01-05 11:12:40 +02001181 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001182 return rc;
1183}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001185static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1186 struct x86_emulate_ops *ops)
1187{
1188 struct decode_cache *c = &ctxt->decode;
1189 int rc;
1190
Avi Kivity350f69d2009-01-05 11:12:40 +02001191 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001192 if (rc != 0)
1193 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194 return 0;
1195}
1196
Laurent Vivier05f086f2007-09-24 11:10:55 +02001197static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 switch (c->modrm_reg) {
1201 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001202 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001203 break;
1204 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001205 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001206 break;
1207 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001208 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001209 break;
1210 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001211 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001212 break;
1213 case 4: /* sal/shl */
1214 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001215 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001216 break;
1217 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001218 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001219 break;
1220 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001221 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001222 break;
1223 }
1224}
1225
1226static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001227 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001228{
1229 struct decode_cache *c = &ctxt->decode;
1230 int rc = 0;
1231
1232 switch (c->modrm_reg) {
1233 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001234 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001235 break;
1236 case 2: /* not */
1237 c->dst.val = ~c->dst.val;
1238 break;
1239 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001240 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001241 break;
1242 default:
1243 DPRINTF("Cannot emulate %02x\n", c->b);
1244 rc = X86EMUL_UNHANDLEABLE;
1245 break;
1246 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001247 return rc;
1248}
1249
1250static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001251 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001252{
1253 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001254
1255 switch (c->modrm_reg) {
1256 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001257 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258 break;
1259 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001260 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001262 case 2: /* call near abs */ {
1263 long int old_eip;
1264 old_eip = c->eip;
1265 c->eip = c->src.val;
1266 c->src.val = old_eip;
1267 emulate_push(ctxt);
1268 break;
1269 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001271 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001272 break;
1273 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001274 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001275 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001276 }
1277 return 0;
1278}
1279
1280static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1281 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001282 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001283{
1284 struct decode_cache *c = &ctxt->decode;
1285 u64 old, new;
1286 int rc;
1287
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001288 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001289 if (rc != 0)
1290 return rc;
1291
1292 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1293 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1294
1295 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1296 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001297 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001298
1299 } else {
1300 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1301 (u32) c->regs[VCPU_REGS_RBX];
1302
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001303 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001304 if (rc != 0)
1305 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001306 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001307 }
1308 return 0;
1309}
1310
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001311static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1312 struct x86_emulate_ops *ops)
1313{
1314 struct decode_cache *c = &ctxt->decode;
1315 int rc;
1316 unsigned long cs;
1317
1318 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1319 if (rc)
1320 return rc;
1321 if (c->op_bytes == 4)
1322 c->eip = (u32)c->eip;
1323 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1324 if (rc)
1325 return rc;
1326 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1327 return rc;
1328}
1329
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001330static inline int writeback(struct x86_emulate_ctxt *ctxt,
1331 struct x86_emulate_ops *ops)
1332{
1333 int rc;
1334 struct decode_cache *c = &ctxt->decode;
1335
1336 switch (c->dst.type) {
1337 case OP_REG:
1338 /* The 4-byte case *is* correct:
1339 * in 64-bit mode we zero-extend.
1340 */
1341 switch (c->dst.bytes) {
1342 case 1:
1343 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1344 break;
1345 case 2:
1346 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1347 break;
1348 case 4:
1349 *c->dst.ptr = (u32)c->dst.val;
1350 break; /* 64b: zero-ext */
1351 case 8:
1352 *c->dst.ptr = c->dst.val;
1353 break;
1354 }
1355 break;
1356 case OP_MEM:
1357 if (c->lock_prefix)
1358 rc = ops->cmpxchg_emulated(
1359 (unsigned long)c->dst.ptr,
1360 &c->dst.orig_val,
1361 &c->dst.val,
1362 c->dst.bytes,
1363 ctxt->vcpu);
1364 else
1365 rc = ops->write_emulated(
1366 (unsigned long)c->dst.ptr,
1367 &c->dst.val,
1368 c->dst.bytes,
1369 ctxt->vcpu);
1370 if (rc != 0)
1371 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001372 break;
1373 case OP_NONE:
1374 /* no writeback */
1375 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376 default:
1377 break;
1378 }
1379 return 0;
1380}
1381
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301382static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001383{
1384 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1385 /*
1386 * an sti; sti; sequence only disable interrupts for the first
1387 * instruction. So, if the last instruction, be it emulated or
1388 * not, left the system with the INT_STI flag enabled, it
1389 * means that the last instruction is an sti. We should not
1390 * leave the flag on in this case. The same goes for mov ss
1391 */
1392 if (!(int_shadow & mask))
1393 ctxt->interruptibility = mask;
1394}
1395
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001396int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001397x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001398{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001399 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001400 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001401 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001402 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001403 unsigned int port;
1404 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001405 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001406
Glauber Costa310b5d32009-05-12 16:21:06 -04001407 ctxt->interruptibility = 0;
1408
Laurent Vivier34273182007-09-18 11:27:37 +02001409 /* Shadow copy of register state. Committed on successful emulation.
1410 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1411 * modify them.
1412 */
1413
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001414 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001415 saved_eip = c->eip;
1416
Avi Kivityc7e75a32007-10-28 16:34:25 +02001417 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001418 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001419
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001420 if (c->rep_prefix && (c->d & String)) {
1421 /* All REP prefixes have the same first termination condition */
1422 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001423 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001424 goto done;
1425 }
1426 /* The second termination condition only applies for REPE
1427 * and REPNE. Test if the repeat string operation prefix is
1428 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1429 * corresponding termination condition according to:
1430 * - if REPE/REPZ and ZF = 0 then done
1431 * - if REPNE/REPNZ and ZF = 1 then done
1432 */
1433 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1434 (c->b == 0xae) || (c->b == 0xaf)) {
1435 if ((c->rep_prefix == REPE_PREFIX) &&
1436 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001437 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001438 goto done;
1439 }
1440 if ((c->rep_prefix == REPNE_PREFIX) &&
1441 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001442 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001443 goto done;
1444 }
1445 }
1446 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001447 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001448 }
1449
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001450 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001451 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001452 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001453 rc = ops->read_emulated((unsigned long)c->src.ptr,
1454 &c->src.val,
1455 c->src.bytes,
1456 ctxt->vcpu);
1457 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001458 goto done;
1459 c->src.orig_val = c->src.val;
1460 }
1461
1462 if ((c->d & DstMask) == ImplicitOps)
1463 goto special_insn;
1464
1465
1466 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001467 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001468 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1469 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001470 if (c->d & BitOp) {
1471 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001472
Laurent Viviere4e03de2007-09-18 11:52:50 +02001473 c->dst.ptr = (void *)c->dst.ptr +
1474 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001475 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001476 if (!(c->d & Mov) &&
1477 /* optimisation - avoid slow emulated read */
1478 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1479 &c->dst.val,
1480 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001481 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001482 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001483 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001484
Avi Kivity018a98d2007-11-27 19:30:56 +02001485special_insn:
1486
Laurent Viviere4e03de2007-09-18 11:52:50 +02001487 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 goto twobyte_insn;
1489
Laurent Viviere4e03de2007-09-18 11:52:50 +02001490 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 case 0x00 ... 0x05:
1492 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001493 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 break;
1495 case 0x08 ... 0x0d:
1496 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001497 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 break;
1499 case 0x10 ... 0x15:
1500 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001501 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 break;
1503 case 0x18 ... 0x1d:
1504 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001505 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001507 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001509 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 break;
1511 case 0x28 ... 0x2d:
1512 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001513 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 break;
1515 case 0x30 ... 0x35:
1516 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001517 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 break;
1519 case 0x38 ... 0x3d:
1520 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001521 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001523 case 0x40 ... 0x47: /* inc r16/r32 */
1524 emulate_1op("inc", c->dst, ctxt->eflags);
1525 break;
1526 case 0x48 ... 0x4f: /* dec r16/r32 */
1527 emulate_1op("dec", c->dst, ctxt->eflags);
1528 break;
1529 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001530 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001531 break;
1532 case 0x58 ... 0x5f: /* pop reg */
1533 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001534 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001535 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001536 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001537 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001539 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001541 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001543 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001544 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001545 emulate_push(ctxt);
1546 break;
1547 case 0x6c: /* insb */
1548 case 0x6d: /* insw/insd */
1549 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1550 1,
1551 (c->d & ByteOp) ? 1 : c->op_bytes,
1552 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001553 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001554 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001555 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001556 c->regs[VCPU_REGS_RDI]),
1557 c->rep_prefix,
1558 c->regs[VCPU_REGS_RDX]) == 0) {
1559 c->eip = saved_eip;
1560 return -1;
1561 }
1562 return 0;
1563 case 0x6e: /* outsb */
1564 case 0x6f: /* outsw/outsd */
1565 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1566 0,
1567 (c->d & ByteOp) ? 1 : c->op_bytes,
1568 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001569 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001570 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001571 register_address(c,
1572 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001573 c->regs[VCPU_REGS_RSI]),
1574 c->rep_prefix,
1575 c->regs[VCPU_REGS_RDX]) == 0) {
1576 c->eip = saved_eip;
1577 return -1;
1578 }
1579 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001580 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001581 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001582 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001585 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 case 0:
1587 goto add;
1588 case 1:
1589 goto or;
1590 case 2:
1591 goto adc;
1592 case 3:
1593 goto sbb;
1594 case 4:
1595 goto and;
1596 case 5:
1597 goto sub;
1598 case 6:
1599 goto xor;
1600 case 7:
1601 goto cmp;
1602 }
1603 break;
1604 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001605 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 break;
1607 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001608 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001610 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001612 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 break;
1614 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001615 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 break;
1617 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001618 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 break; /* 64b reg: zero-extend */
1620 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001621 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 break;
1623 }
1624 /*
1625 * Write back the memory destination with implicit LOCK
1626 * prefix.
1627 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001628 c->dst.val = c->src.val;
1629 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001632 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001633 case 0x8c: { /* mov r/m, sreg */
1634 struct kvm_segment segreg;
1635
1636 if (c->modrm_reg <= 5)
1637 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1638 else {
1639 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1640 c->modrm);
1641 goto cannot_emulate;
1642 }
1643 c->dst.val = segreg.selector;
1644 break;
1645 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001646 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001647 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001648 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001649 case 0x8e: { /* mov seg, r/m16 */
1650 uint16_t sel;
1651 int type_bits;
1652 int err;
1653
1654 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001655 if (c->modrm_reg == VCPU_SREG_SS)
1656 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1657
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001658 if (c->modrm_reg <= 5) {
1659 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1660 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1661 type_bits, c->modrm_reg);
1662 } else {
1663 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1664 c->modrm);
1665 goto cannot_emulate;
1666 }
1667
1668 if (err < 0)
1669 goto cannot_emulate;
1670
1671 c->dst.type = OP_NONE; /* Disable writeback. */
1672 break;
1673 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001675 rc = emulate_grp1a(ctxt, ops);
1676 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001679 case 0x90: /* nop / xchg r8,rax */
1680 if (!(c->rex_prefix & 1)) { /* nop */
1681 c->dst.type = OP_NONE;
1682 break;
1683 }
1684 case 0x91 ... 0x97: /* xchg reg,rax */
1685 c->src.type = c->dst.type = OP_REG;
1686 c->src.bytes = c->dst.bytes = c->op_bytes;
1687 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1688 c->src.val = *(c->src.ptr);
1689 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001690 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001691 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692 emulate_push(ctxt);
1693 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001694 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001695 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001696 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001697 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001698 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001699 case 0xa0 ... 0xa1: /* mov */
1700 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1701 c->dst.val = c->src.val;
1702 break;
1703 case 0xa2 ... 0xa3: /* mov */
1704 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1705 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001707 c->dst.type = OP_MEM;
1708 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001709 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001710 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001711 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001712 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001713 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001714 c->regs[VCPU_REGS_RSI]),
1715 &c->dst.val,
1716 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001718 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001719 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001720 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001721 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001722 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 break;
1725 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001726 c->src.type = OP_NONE; /* Disable writeback. */
1727 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001728 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001729 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001730 c->regs[VCPU_REGS_RSI]);
1731 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1732 &c->src.val,
1733 c->src.bytes,
1734 ctxt->vcpu)) != 0)
1735 goto done;
1736
1737 c->dst.type = OP_NONE; /* Disable writeback. */
1738 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001739 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001740 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001741 c->regs[VCPU_REGS_RDI]);
1742 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1743 &c->dst.val,
1744 c->dst.bytes,
1745 ctxt->vcpu)) != 0)
1746 goto done;
1747
1748 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1749
1750 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1751
Harvey Harrison7a9572752008-02-19 07:40:41 -08001752 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001753 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1754 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001755 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001756 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1757 : c->dst.bytes);
1758
1759 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001761 c->dst.type = OP_MEM;
1762 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001763 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001764 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001765 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001766 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001767 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001768 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001769 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770 break;
1771 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001772 c->dst.type = OP_REG;
1773 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1774 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001775 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001776 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001777 c->regs[VCPU_REGS_RSI]),
1778 &c->dst.val,
1779 c->dst.bytes,
1780 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001782 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001783 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001784 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785 break;
1786 case 0xae ... 0xaf: /* scas */
1787 DPRINTF("Urk! I don't handle SCAS.\n");
1788 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001789 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001790 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001791 case 0xc0 ... 0xc1:
1792 emulate_grp2(ctxt);
1793 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001794 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001795 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001796 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001797 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001798 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001799 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1800 mov:
1801 c->dst.val = c->src.val;
1802 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001803 case 0xcb: /* ret far */
1804 rc = emulate_ret_far(ctxt, ops);
1805 if (rc)
1806 goto done;
1807 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001808 case 0xd0 ... 0xd1: /* Grp2 */
1809 c->src.val = 1;
1810 emulate_grp2(ctxt);
1811 break;
1812 case 0xd2 ... 0xd3: /* Grp2 */
1813 c->src.val = c->regs[VCPU_REGS_RCX];
1814 emulate_grp2(ctxt);
1815 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001816 case 0xe4: /* inb */
1817 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03001818 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001819 io_dir_in = 1;
1820 goto do_io;
1821 case 0xe6: /* outb */
1822 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03001823 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001824 io_dir_in = 0;
1825 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001826 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03001827 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001828 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001829 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830 emulate_push(ctxt);
1831 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001832 }
1833 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001834 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03001835 case 0xea: /* jmp far */
1836 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
1837 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001838 DPRINTF("jmp far: Failed to load CS descriptor\n");
1839 goto cannot_emulate;
1840 }
1841
Gleb Natapov782b8772009-04-12 13:36:25 +03001842 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001843 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001844 case 0xeb:
1845 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001846 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001847 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001848 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001849 case 0xec: /* in al,dx */
1850 case 0xed: /* in (e/r)ax,dx */
1851 port = c->regs[VCPU_REGS_RDX];
1852 io_dir_in = 1;
1853 goto do_io;
1854 case 0xee: /* out al,dx */
1855 case 0xef: /* out (e/r)ax,dx */
1856 port = c->regs[VCPU_REGS_RDX];
1857 io_dir_in = 0;
1858 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1859 (c->d & ByteOp) ? 1 : c->op_bytes,
1860 port) != 0) {
1861 c->eip = saved_eip;
1862 goto cannot_emulate;
1863 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001864 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001865 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001866 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001867 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001868 case 0xf5: /* cmc */
1869 /* complement carry flag from eflags reg */
1870 ctxt->eflags ^= EFLG_CF;
1871 c->dst.type = OP_NONE; /* Disable writeback. */
1872 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001873 case 0xf6 ... 0xf7: /* Grp3 */
1874 rc = emulate_grp3(ctxt, ops);
1875 if (rc != 0)
1876 goto done;
1877 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001878 case 0xf8: /* clc */
1879 ctxt->eflags &= ~EFLG_CF;
1880 c->dst.type = OP_NONE; /* Disable writeback. */
1881 break;
1882 case 0xfa: /* cli */
1883 ctxt->eflags &= ~X86_EFLAGS_IF;
1884 c->dst.type = OP_NONE; /* Disable writeback. */
1885 break;
1886 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04001887 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02001888 ctxt->eflags |= X86_EFLAGS_IF;
1889 c->dst.type = OP_NONE; /* Disable writeback. */
1890 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001891 case 0xfc: /* cld */
1892 ctxt->eflags &= ~EFLG_DF;
1893 c->dst.type = OP_NONE; /* Disable writeback. */
1894 break;
1895 case 0xfd: /* std */
1896 ctxt->eflags |= EFLG_DF;
1897 c->dst.type = OP_NONE; /* Disable writeback. */
1898 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001899 case 0xfe ... 0xff: /* Grp4/Grp5 */
1900 rc = emulate_grp45(ctxt, ops);
1901 if (rc != 0)
1902 goto done;
1903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001905
1906writeback:
1907 rc = writeback(ctxt, ops);
1908 if (rc != 0)
1909 goto done;
1910
1911 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001912 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001913 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001914
1915done:
1916 if (rc == X86EMUL_UNHANDLEABLE) {
1917 c->eip = saved_eip;
1918 return -1;
1919 }
1920 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
1922twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001923 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001925 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 u16 size;
1927 unsigned long address;
1928
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001929 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001930 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001931 goto cannot_emulate;
1932
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001933 rc = kvm_fix_hypercall(ctxt->vcpu);
1934 if (rc)
1935 goto done;
1936
Avi Kivity33e38852008-05-21 15:34:25 +03001937 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001938 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001939 /* Disable writeback. */
1940 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001941 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001943 rc = read_descriptor(ctxt, ops, c->src.ptr,
1944 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 if (rc)
1946 goto done;
1947 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001948 /* Disable writeback. */
1949 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001951 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02001952 if (c->modrm_mod == 3) {
1953 switch (c->modrm_rm) {
1954 case 1:
1955 rc = kvm_fix_hypercall(ctxt->vcpu);
1956 if (rc)
1957 goto done;
1958 break;
1959 default:
1960 goto cannot_emulate;
1961 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001962 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001963 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001964 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001965 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001966 if (rc)
1967 goto done;
1968 realmode_lidt(ctxt->vcpu, size, address);
1969 }
Avi Kivity16286d02008-04-14 14:40:50 +03001970 /* Disable writeback. */
1971 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 break;
1973 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001974 c->dst.bytes = 2;
1975 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 break;
1977 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001978 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1979 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001980 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 break;
1982 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001983 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001984 /* Disable writeback. */
1985 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 break;
1987 default:
1988 goto cannot_emulate;
1989 }
1990 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001991 case 0x06:
1992 emulate_clts(ctxt->vcpu);
1993 c->dst.type = OP_NONE;
1994 break;
1995 case 0x08: /* invd */
1996 case 0x09: /* wbinvd */
1997 case 0x0d: /* GrpP (prefetch) */
1998 case 0x18: /* Grp16 (prefetch/nop) */
1999 c->dst.type = OP_NONE;
2000 break;
2001 case 0x20: /* mov cr, reg */
2002 if (c->modrm_mod != 3)
2003 goto cannot_emulate;
2004 c->regs[c->modrm_rm] =
2005 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2006 c->dst.type = OP_NONE; /* no writeback */
2007 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002009 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002011 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002012 if (rc)
2013 goto cannot_emulate;
2014 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002016 case 0x22: /* mov reg, cr */
2017 if (c->modrm_mod != 3)
2018 goto cannot_emulate;
2019 realmode_set_cr(ctxt->vcpu,
2020 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2021 c->dst.type = OP_NONE;
2022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002024 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002026 rc = emulator_set_dr(ctxt, c->modrm_reg,
2027 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002028 if (rc)
2029 goto cannot_emulate;
2030 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002032 case 0x30:
2033 /* wrmsr */
2034 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2035 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2036 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2037 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002038 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002039 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002040 }
2041 rc = X86EMUL_CONTINUE;
2042 c->dst.type = OP_NONE;
2043 break;
2044 case 0x32:
2045 /* rdmsr */
2046 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2047 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002048 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002049 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002050 } else {
2051 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2052 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2053 }
2054 rc = X86EMUL_CONTINUE;
2055 c->dst.type = OP_NONE;
2056 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002058 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002059 if (!test_cc(c->b, ctxt->eflags))
2060 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002062 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002063 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002064 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002065 c->dst.type = OP_NONE;
2066 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002067 case 0xa3:
2068 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002069 c->dst.type = OP_NONE;
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("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002073 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002074 case 0xa4: /* shld imm8, r, r/m */
2075 case 0xa5: /* shld cl, r, r/m */
2076 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2077 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002078 case 0xab:
2079 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002080 /* only subword offset */
2081 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002082 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002083 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002084 case 0xac: /* shrd imm8, r, r/m */
2085 case 0xad: /* shrd cl, r, r/m */
2086 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2087 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002088 case 0xae: /* clflush */
2089 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 case 0xb0 ... 0xb1: /* cmpxchg */
2091 /*
2092 * Save real source value, then compare EAX against
2093 * destination.
2094 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002095 c->src.orig_val = c->src.val;
2096 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002097 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2098 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002100 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 } else {
2102 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002103 c->dst.type = OP_REG;
2104 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 }
2106 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 case 0xb3:
2108 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002109 /* only subword offset */
2110 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002111 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002114 c->dst.bytes = c->op_bytes;
2115 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2116 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002119 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 case 0:
2121 goto bt;
2122 case 1:
2123 goto bts;
2124 case 2:
2125 goto btr;
2126 case 3:
2127 goto btc;
2128 }
2129 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002130 case 0xbb:
2131 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002132 /* only subword offset */
2133 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002134 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002137 c->dst.bytes = c->op_bytes;
2138 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2139 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002141 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002142 c->dst.bytes = c->op_bytes;
2143 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2144 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002147 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148 if (rc != 0)
2149 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002150 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152 }
2153 goto writeback;
2154
2155cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002156 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002157 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158 return -1;
2159}