blob: 71b4bee2e221f31d229bef5ad418790b8ba89b77 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.c
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
12 *
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
20 */
21
22#ifndef __KERNEL__
23#include <stdio.h>
24#include <stdint.h>
25#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040026#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#else
Avi Kivityedf88412007-12-16 11:02:48 +020028#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#define DPRINTF(x...) do {} while (0)
31#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/module.h>
Avi Kivityedf88412007-12-16 11:02:48 +020033#include <asm/kvm_x86_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
35/*
36 * Opcode effective-address decode tables.
37 * Note that we only emulate instructions that have at least one memory
38 * operand (excluding implicit stack references). We assume that stack
39 * references and instruction fetches will never occur in special memory
40 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
41 * not be handled.
42 */
43
44/* Operand sizes: 8-bit operands or specified/overridden size. */
45#define ByteOp (1<<0) /* 8-bit operands. */
46/* Destination operand type. */
47#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
48#define DstReg (2<<1) /* Register operand. */
49#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020050#define DstAcc (4<<1) /* Destination Accumulator */
51#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080052/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcNone (0<<4) /* No source operand. */
54#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
55#define SrcReg (1<<4) /* Register operand. */
56#define SrcMem (2<<4) /* Memory operand. */
57#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
58#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
59#define SrcImm (5<<4) /* Immediate operand. */
60#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010061#define SrcOne (7<<4) /* Implied '1' */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020062#define SrcMask (7<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080063/* Generic ModRM decode. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020064#define ModRM (1<<7)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065/* Destination is only written; never read. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020066#define Mov (1<<8)
67#define BitOp (1<<9)
68#define MemAbs (1<<10) /* Memory operand is absolute displacement */
69#define String (1<<12) /* String instruction (rep capable) */
70#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020071#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
72#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
73#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010074/* Source 2 operand type */
75#define Src2None (0<<29)
76#define Src2CL (1<<29)
77#define Src2ImmByte (2<<29)
78#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030079#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010080#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivity43bb19c2008-01-18 12:46:50 +020082enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020083 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020084 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020085};
86
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010087static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080088 /* 0x00 - 0x07 */
89 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
90 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020091 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 /* 0x08 - 0x0F */
93 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
94 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
95 0, 0, 0, 0,
96 /* 0x10 - 0x17 */
97 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
98 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
99 0, 0, 0, 0,
100 /* 0x18 - 0x1F */
101 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
103 0, 0, 0, 0,
104 /* 0x20 - 0x27 */
105 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
106 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200107 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 /* 0x28 - 0x2F */
109 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
111 0, 0, 0, 0,
112 /* 0x30 - 0x37 */
113 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
114 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
115 0, 0, 0, 0,
116 /* 0x38 - 0x3F */
117 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
118 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200119 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
120 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700121 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200122 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700123 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200124 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300125 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200126 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
127 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300128 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200129 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
130 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700131 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700133 0, 0, 0, 0,
134 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300135 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300136 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
137 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300138 /* 0x70 - 0x77 */
139 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
140 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
141 /* 0x78 - 0x7F */
142 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
143 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200145 Group | Group1_80, Group | Group1_81,
146 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
148 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
149 /* 0x88 - 0x8F */
150 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
151 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200152 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200153 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300154 /* 0x90 - 0x97 */
155 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
156 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300157 0, 0, SrcImm | Src2Imm16, 0,
158 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200160 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
161 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200162 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
163 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200165 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
166 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
167 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300168 /* 0xB0 - 0xB7 */
169 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
170 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
171 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
172 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
173 /* 0xB8 - 0xBF */
174 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
175 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
176 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
177 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300179 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200180 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300181 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xC8 - 0xCF */
Avi Kivitya77ab5e2009-01-05 13:27:34 +0200183 0, 0, 0, ImplicitOps | Stack, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 /* 0xD0 - 0xD7 */
185 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
186 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
187 0, 0, 0, 0,
188 /* 0xD8 - 0xDF */
189 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300190 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300191 0, 0, 0, 0,
192 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
193 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300194 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200195 ImplicitOps | Stack, SrcImm | ImplicitOps,
196 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300197 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
198 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0xF0 - 0xF7 */
200 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200201 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700203 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300204 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205};
206
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100207static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200209 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200210 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211 /* 0x10 - 0x1F */
212 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
213 /* 0x20 - 0x2F */
214 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
215 0, 0, 0, 0, 0, 0, 0, 0,
216 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300217 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0x40 - 0x47 */
219 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
220 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
221 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
222 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
223 /* 0x48 - 0x4F */
224 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
225 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
226 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
227 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
228 /* 0x50 - 0x5F */
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
230 /* 0x60 - 0x6F */
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 /* 0x70 - 0x7F */
233 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
234 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300235 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
236 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
237 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
238 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 /* 0x90 - 0x9F */
240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100242 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
243 DstMem | SrcReg | Src2ImmByte | ModRM,
244 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100246 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
247 DstMem | SrcReg | Src2ImmByte | ModRM,
248 DstMem | SrcReg | Src2CL | ModRM,
249 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 /* 0xB0 - 0xB7 */
251 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800252 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
254 DstReg | SrcMem16 | ModRM | Mov,
255 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800256 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800257 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
258 DstReg | SrcMem16 | ModRM | Mov,
259 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800260 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
261 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xD0 - 0xDF */
263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264 /* 0xE0 - 0xEF */
265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266 /* 0xF0 - 0xFF */
267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
268};
269
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100270static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200271 [Group1_80*8] =
272 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
273 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
274 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
275 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
276 [Group1_81*8] =
277 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
278 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
279 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
280 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
281 [Group1_82*8] =
282 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
283 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
286 [Group1_83*8] =
287 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
288 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
289 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
290 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200291 [Group1A*8] =
292 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200293 [Group3_Byte*8] =
294 ByteOp | SrcImm | DstMem | ModRM, 0,
295 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
296 0, 0, 0, 0,
297 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400298 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300299 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200300 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200301 [Group4*8] =
302 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
303 0, 0, 0, 0, 0, 0,
304 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300305 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
306 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300307 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200308 [Group7*8] =
309 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300310 SrcNone | ModRM | DstMem | Mov, 0,
311 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200312};
313
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100314static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200315 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000316 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300317 SrcNone | ModRM | DstMem | Mov, 0,
318 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200319};
320
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321/* EFLAGS bit definitions. */
322#define EFLG_OF (1<<11)
323#define EFLG_DF (1<<10)
324#define EFLG_SF (1<<7)
325#define EFLG_ZF (1<<6)
326#define EFLG_AF (1<<4)
327#define EFLG_PF (1<<2)
328#define EFLG_CF (1<<0)
329
330/*
331 * Instruction emulation:
332 * Most instructions are emulated directly via a fragment of inline assembly
333 * code. This allows us to save/restore EFLAGS and thus very easily pick up
334 * any modified flags.
335 */
336
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800337#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338#define _LO32 "k" /* force 32-bit operand */
339#define _STK "%%rsp" /* stack pointer */
340#elif defined(__i386__)
341#define _LO32 "" /* force 32-bit operand */
342#define _STK "%%esp" /* stack pointer */
343#endif
344
345/*
346 * These EFLAGS bits are restored from saved value during emulation, and
347 * any changes are written back to the saved value after emulation.
348 */
349#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
350
351/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200352#define _PRE_EFLAGS(_sav, _msk, _tmp) \
353 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
354 "movl %"_sav",%"_LO32 _tmp"; " \
355 "push %"_tmp"; " \
356 "push %"_tmp"; " \
357 "movl %"_msk",%"_LO32 _tmp"; " \
358 "andl %"_LO32 _tmp",("_STK"); " \
359 "pushf; " \
360 "notl %"_LO32 _tmp"; " \
361 "andl %"_LO32 _tmp",("_STK"); " \
362 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
363 "pop %"_tmp"; " \
364 "orl %"_LO32 _tmp",("_STK"); " \
365 "popf; " \
366 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367
368/* After executing instruction: write-back necessary bits in EFLAGS. */
369#define _POST_EFLAGS(_sav, _msk, _tmp) \
370 /* _sav |= EFLAGS & _msk; */ \
371 "pushf; " \
372 "pop %"_tmp"; " \
373 "andl %"_msk",%"_LO32 _tmp"; " \
374 "orl %"_LO32 _tmp",%"_sav"; "
375
Avi Kivitydda96d82008-11-26 15:14:10 +0200376#ifdef CONFIG_X86_64
377#define ON64(x) x
378#else
379#define ON64(x)
380#endif
381
Avi Kivity6b7ad612008-11-26 15:30:45 +0200382#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
383 do { \
384 __asm__ __volatile__ ( \
385 _PRE_EFLAGS("0", "4", "2") \
386 _op _suffix " %"_x"3,%1; " \
387 _POST_EFLAGS("0", "4", "2") \
388 : "=m" (_eflags), "=m" ((_dst).val), \
389 "=&r" (_tmp) \
390 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200391 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200392
393
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394/* Raw emulation: instruction has two explicit operands. */
395#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200396 do { \
397 unsigned long _tmp; \
398 \
399 switch ((_dst).bytes) { \
400 case 2: \
401 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
402 break; \
403 case 4: \
404 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
405 break; \
406 case 8: \
407 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
408 break; \
409 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410 } while (0)
411
412#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
413 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200414 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400415 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200417 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 break; \
419 default: \
420 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
421 _wx, _wy, _lx, _ly, _qx, _qy); \
422 break; \
423 } \
424 } while (0)
425
426/* Source operand is byte-sized and may be restricted to just %cl. */
427#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
428 __emulate_2op(_op, _src, _dst, _eflags, \
429 "b", "c", "b", "c", "b", "c", "b", "c")
430
431/* Source operand is byte, word, long or quad sized. */
432#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
433 __emulate_2op(_op, _src, _dst, _eflags, \
434 "b", "q", "w", "r", _LO32, "r", "", "r")
435
436/* Source operand is word, long or quad sized. */
437#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
438 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
439 "w", "r", _LO32, "r", "", "r")
440
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100441/* Instruction has three operands and one operand is stored in ECX register */
442#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
443 do { \
444 unsigned long _tmp; \
445 _type _clv = (_cl).val; \
446 _type _srcv = (_src).val; \
447 _type _dstv = (_dst).val; \
448 \
449 __asm__ __volatile__ ( \
450 _PRE_EFLAGS("0", "5", "2") \
451 _op _suffix " %4,%1 \n" \
452 _POST_EFLAGS("0", "5", "2") \
453 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
454 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
455 ); \
456 \
457 (_cl).val = (unsigned long) _clv; \
458 (_src).val = (unsigned long) _srcv; \
459 (_dst).val = (unsigned long) _dstv; \
460 } while (0)
461
462#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
463 do { \
464 switch ((_dst).bytes) { \
465 case 2: \
466 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
467 "w", unsigned short); \
468 break; \
469 case 4: \
470 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
471 "l", unsigned int); \
472 break; \
473 case 8: \
474 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
475 "q", unsigned long)); \
476 break; \
477 } \
478 } while (0)
479
Avi Kivitydda96d82008-11-26 15:14:10 +0200480#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 do { \
482 unsigned long _tmp; \
483 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200484 __asm__ __volatile__ ( \
485 _PRE_EFLAGS("0", "3", "2") \
486 _op _suffix " %1; " \
487 _POST_EFLAGS("0", "3", "2") \
488 : "=m" (_eflags), "+m" ((_dst).val), \
489 "=&r" (_tmp) \
490 : "i" (EFLAGS_MASK)); \
491 } while (0)
492
493/* Instruction has only one explicit operand (no source operand). */
494#define emulate_1op(_op, _dst, _eflags) \
495 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400496 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200497 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
498 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
499 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
500 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 } \
502 } while (0)
503
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504/* Fetch next part of the instruction being emulated. */
505#define insn_fetch(_type, _size, _eip) \
506({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200507 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400508 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 goto done; \
510 (_eip) += (_size); \
511 (_type)_x; \
512})
513
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800514static inline unsigned long ad_mask(struct decode_cache *c)
515{
516 return (1UL << (c->ad_bytes << 3)) - 1;
517}
518
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800520static inline unsigned long
521address_mask(struct decode_cache *c, unsigned long reg)
522{
523 if (c->ad_bytes == sizeof(unsigned long))
524 return reg;
525 else
526 return reg & ad_mask(c);
527}
528
529static inline unsigned long
530register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
531{
532 return base + address_mask(c, reg);
533}
534
Harvey Harrison7a9572752008-02-19 07:40:41 -0800535static inline void
536register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
537{
538 if (c->ad_bytes == sizeof(unsigned long))
539 *reg += inc;
540 else
541 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
542}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543
Harvey Harrison7a9572752008-02-19 07:40:41 -0800544static inline void jmp_rel(struct decode_cache *c, int rel)
545{
546 register_address_increment(c, &c->eip, rel);
547}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300548
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300549static void set_seg_override(struct decode_cache *c, int seg)
550{
551 c->has_seg_override = true;
552 c->seg_override = seg;
553}
554
555static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
556{
557 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
558 return 0;
559
560 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
561}
562
563static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
564 struct decode_cache *c)
565{
566 if (!c->has_seg_override)
567 return 0;
568
569 return seg_base(ctxt, c->seg_override);
570}
571
572static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
573{
574 return seg_base(ctxt, VCPU_SREG_ES);
575}
576
577static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
578{
579 return seg_base(ctxt, VCPU_SREG_SS);
580}
581
Avi Kivity62266862007-11-20 13:15:52 +0200582static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
583 struct x86_emulate_ops *ops,
584 unsigned long linear, u8 *dest)
585{
586 struct fetch_cache *fc = &ctxt->decode.fetch;
587 int rc;
588 int size;
589
590 if (linear < fc->start || linear >= fc->end) {
591 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
592 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
593 if (rc)
594 return rc;
595 fc->start = linear;
596 fc->end = linear + size;
597 }
598 *dest = fc->data[linear - fc->start];
599 return 0;
600}
601
602static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
603 struct x86_emulate_ops *ops,
604 unsigned long eip, void *dest, unsigned size)
605{
606 int rc = 0;
607
608 eip += ctxt->cs_base;
609 while (size--) {
610 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
611 if (rc)
612 return rc;
613 }
614 return 0;
615}
616
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000617/*
618 * Given the 'reg' portion of a ModRM byte, and a register block, return a
619 * pointer into the block that addresses the relevant register.
620 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
621 */
622static void *decode_register(u8 modrm_reg, unsigned long *regs,
623 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624{
625 void *p;
626
627 p = &regs[modrm_reg];
628 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
629 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
630 return p;
631}
632
633static int read_descriptor(struct x86_emulate_ctxt *ctxt,
634 struct x86_emulate_ops *ops,
635 void *ptr,
636 u16 *size, unsigned long *address, int op_bytes)
637{
638 int rc;
639
640 if (op_bytes == 2)
641 op_bytes = 3;
642 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300643 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
644 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 if (rc)
646 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300647 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
648 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 return rc;
650}
651
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300652static int test_cc(unsigned int condition, unsigned int flags)
653{
654 int rc = 0;
655
656 switch ((condition & 15) >> 1) {
657 case 0: /* o */
658 rc |= (flags & EFLG_OF);
659 break;
660 case 1: /* b/c/nae */
661 rc |= (flags & EFLG_CF);
662 break;
663 case 2: /* z/e */
664 rc |= (flags & EFLG_ZF);
665 break;
666 case 3: /* be/na */
667 rc |= (flags & (EFLG_CF|EFLG_ZF));
668 break;
669 case 4: /* s */
670 rc |= (flags & EFLG_SF);
671 break;
672 case 5: /* p/pe */
673 rc |= (flags & EFLG_PF);
674 break;
675 case 7: /* le/ng */
676 rc |= (flags & EFLG_ZF);
677 /* fall through */
678 case 6: /* l/nge */
679 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
680 break;
681 }
682
683 /* Odd condition identifiers (lsb == 1) have inverted sense. */
684 return (!!rc ^ (condition & 1));
685}
686
Avi Kivity3c118e22007-10-31 10:27:04 +0200687static void decode_register_operand(struct operand *op,
688 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200689 int inhibit_bytereg)
690{
Avi Kivity33615aa2007-10-31 11:15:56 +0200691 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200692 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200693
694 if (!(c->d & ModRM))
695 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200696 op->type = OP_REG;
697 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200698 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200699 op->val = *(u8 *)op->ptr;
700 op->bytes = 1;
701 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200702 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200703 op->bytes = c->op_bytes;
704 switch (op->bytes) {
705 case 2:
706 op->val = *(u16 *)op->ptr;
707 break;
708 case 4:
709 op->val = *(u32 *)op->ptr;
710 break;
711 case 8:
712 op->val = *(u64 *) op->ptr;
713 break;
714 }
715 }
716 op->orig_val = op->val;
717}
718
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200719static int decode_modrm(struct x86_emulate_ctxt *ctxt,
720 struct x86_emulate_ops *ops)
721{
722 struct decode_cache *c = &ctxt->decode;
723 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700724 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200725 int rc = 0;
726
727 if (c->rex_prefix) {
728 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
729 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
730 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
731 }
732
733 c->modrm = insn_fetch(u8, 1, c->eip);
734 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
735 c->modrm_reg |= (c->modrm & 0x38) >> 3;
736 c->modrm_rm |= (c->modrm & 0x07);
737 c->modrm_ea = 0;
738 c->use_modrm_ea = 1;
739
740 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300741 c->modrm_ptr = decode_register(c->modrm_rm,
742 c->regs, c->d & ByteOp);
743 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200744 return rc;
745 }
746
747 if (c->ad_bytes == 2) {
748 unsigned bx = c->regs[VCPU_REGS_RBX];
749 unsigned bp = c->regs[VCPU_REGS_RBP];
750 unsigned si = c->regs[VCPU_REGS_RSI];
751 unsigned di = c->regs[VCPU_REGS_RDI];
752
753 /* 16-bit ModR/M decode. */
754 switch (c->modrm_mod) {
755 case 0:
756 if (c->modrm_rm == 6)
757 c->modrm_ea += insn_fetch(u16, 2, c->eip);
758 break;
759 case 1:
760 c->modrm_ea += insn_fetch(s8, 1, c->eip);
761 break;
762 case 2:
763 c->modrm_ea += insn_fetch(u16, 2, c->eip);
764 break;
765 }
766 switch (c->modrm_rm) {
767 case 0:
768 c->modrm_ea += bx + si;
769 break;
770 case 1:
771 c->modrm_ea += bx + di;
772 break;
773 case 2:
774 c->modrm_ea += bp + si;
775 break;
776 case 3:
777 c->modrm_ea += bp + di;
778 break;
779 case 4:
780 c->modrm_ea += si;
781 break;
782 case 5:
783 c->modrm_ea += di;
784 break;
785 case 6:
786 if (c->modrm_mod != 0)
787 c->modrm_ea += bp;
788 break;
789 case 7:
790 c->modrm_ea += bx;
791 break;
792 }
793 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
794 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300795 if (!c->has_seg_override)
796 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200797 c->modrm_ea = (u16)c->modrm_ea;
798 } else {
799 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700800 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200801 sib = insn_fetch(u8, 1, c->eip);
802 index_reg |= (sib >> 3) & 7;
803 base_reg |= sib & 7;
804 scale = sib >> 6;
805
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700806 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
807 c->modrm_ea += insn_fetch(s32, 4, c->eip);
808 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700810 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200811 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700812 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
813 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700814 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700815 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200816 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817 switch (c->modrm_mod) {
818 case 0:
819 if (c->modrm_rm == 5)
820 c->modrm_ea += insn_fetch(s32, 4, c->eip);
821 break;
822 case 1:
823 c->modrm_ea += insn_fetch(s8, 1, c->eip);
824 break;
825 case 2:
826 c->modrm_ea += insn_fetch(s32, 4, c->eip);
827 break;
828 }
829 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200830done:
831 return rc;
832}
833
834static int decode_abs(struct x86_emulate_ctxt *ctxt,
835 struct x86_emulate_ops *ops)
836{
837 struct decode_cache *c = &ctxt->decode;
838 int rc = 0;
839
840 switch (c->ad_bytes) {
841 case 2:
842 c->modrm_ea = insn_fetch(u16, 2, c->eip);
843 break;
844 case 4:
845 c->modrm_ea = insn_fetch(u32, 4, c->eip);
846 break;
847 case 8:
848 c->modrm_ea = insn_fetch(u64, 8, c->eip);
849 break;
850 }
851done:
852 return rc;
853}
854
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200856x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200858 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200861 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862
863 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864
Laurent Viviere4e03de2007-09-18 11:52:50 +0200865 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300866 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300867 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800868 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869
870 switch (mode) {
871 case X86EMUL_MODE_REAL:
872 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200873 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 break;
875 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200876 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800878#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200880 def_op_bytes = 4;
881 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 break;
883#endif
884 default:
885 return -1;
886 }
887
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200888 c->op_bytes = def_op_bytes;
889 c->ad_bytes = def_ad_bytes;
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200892 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200893 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200895 /* switch between 2/4 bytes */
896 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 break;
898 case 0x67: /* address-size override */
899 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200900 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200901 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200903 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200904 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300907 case 0x2e: /* CS override */
908 case 0x36: /* SS override */
909 case 0x3e: /* DS override */
910 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 break;
912 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300914 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200916 case 0x40 ... 0x4f: /* REX */
917 if (mode != X86EMUL_MODE_PROT64)
918 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200919 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200920 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200922 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200924 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100925 c->rep_prefix = REPNE_PREFIX;
926 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100928 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 default:
931 goto done_prefixes;
932 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200933
934 /* Any legacy prefix after a REX prefix nullifies its effect. */
935
Avi Kivity33615aa2007-10-31 11:15:56 +0200936 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 }
938
939done_prefixes:
940
941 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200943 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200944 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
946 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200947 c->d = opcode_table[c->b];
948 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200950 if (c->b == 0x0f) {
951 c->twobyte = 1;
952 c->b = insn_fetch(u8, 1, c->eip);
953 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200955 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956
Avi Kivitye09d0822008-01-18 12:38:59 +0200957 if (c->d & Group) {
958 group = c->d & GroupMask;
959 c->modrm = insn_fetch(u8, 1, c->eip);
960 --c->eip;
961
962 group = (group << 3) + ((c->modrm >> 3) & 7);
963 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
964 c->d = group2_table[group];
965 else
966 c->d = group_table[group];
967 }
968
969 /* Unrecognised? */
970 if (c->d == 0) {
971 DPRINTF("Cannot emulate %02x\n", c->b);
972 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 }
974
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200975 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
976 c->op_bytes = 8;
977
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200979 if (c->d & ModRM)
980 rc = decode_modrm(ctxt, ops);
981 else if (c->d & MemAbs)
982 rc = decode_abs(ctxt, ops);
983 if (rc)
984 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300986 if (!c->has_seg_override)
987 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200988
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300989 if (!(!c->twobyte && c->b == 0x8d))
990 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200991
992 if (c->ad_bytes != 8)
993 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 /*
995 * Decode and fetch the source operand: register, memory
996 * or immediate.
997 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200998 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 case SrcNone:
1000 break;
1001 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001002 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 break;
1004 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001005 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 goto srcmem_common;
1007 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001008 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 goto srcmem_common;
1010 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001011 c->src.bytes = (c->d & ByteOp) ? 1 :
1012 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001013 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001014 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001015 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001016 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001017 /*
1018 * For instructions with a ModR/M byte, switch to register
1019 * access if Mod = 3.
1020 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001021 if ((c->d & ModRM) && c->modrm_mod == 3) {
1022 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001023 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001024 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001025 break;
1026 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001027 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 break;
1029 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001030 c->src.type = OP_IMM;
1031 c->src.ptr = (unsigned long *)c->eip;
1032 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1033 if (c->src.bytes == 8)
1034 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001036 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001038 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 break;
1040 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 break;
1043 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001044 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 break;
1046 }
1047 break;
1048 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001049 c->src.type = OP_IMM;
1050 c->src.ptr = (unsigned long *)c->eip;
1051 c->src.bytes = 1;
1052 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001054 case SrcOne:
1055 c->src.bytes = 1;
1056 c->src.val = 1;
1057 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 }
1059
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001060 /*
1061 * Decode and fetch the second source operand: register, memory
1062 * or immediate.
1063 */
1064 switch (c->d & Src2Mask) {
1065 case Src2None:
1066 break;
1067 case Src2CL:
1068 c->src2.bytes = 1;
1069 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1070 break;
1071 case Src2ImmByte:
1072 c->src2.type = OP_IMM;
1073 c->src2.ptr = (unsigned long *)c->eip;
1074 c->src2.bytes = 1;
1075 c->src2.val = insn_fetch(u8, 1, c->eip);
1076 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001077 case Src2Imm16:
1078 c->src2.type = OP_IMM;
1079 c->src2.ptr = (unsigned long *)c->eip;
1080 c->src2.bytes = 2;
1081 c->src2.val = insn_fetch(u16, 2, c->eip);
1082 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001083 case Src2One:
1084 c->src2.bytes = 1;
1085 c->src2.val = 1;
1086 break;
1087 }
1088
Avi Kivity038e51d2007-01-22 20:40:40 -08001089 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001090 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001091 case ImplicitOps:
1092 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001093 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001094 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001095 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001096 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001097 break;
1098 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001099 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001100 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001101 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001102 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001103 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001104 break;
1105 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001106 c->dst.type = OP_MEM;
1107 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001108 case DstAcc:
1109 c->dst.type = OP_REG;
1110 c->dst.bytes = c->op_bytes;
1111 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1112 switch (c->op_bytes) {
1113 case 1:
1114 c->dst.val = *(u8 *)c->dst.ptr;
1115 break;
1116 case 2:
1117 c->dst.val = *(u16 *)c->dst.ptr;
1118 break;
1119 case 4:
1120 c->dst.val = *(u32 *)c->dst.ptr;
1121 break;
1122 }
1123 c->dst.orig_val = c->dst.val;
1124 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001125 }
1126
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001127 if (c->rip_relative)
1128 c->modrm_ea += c->eip;
1129
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001130done:
1131 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1132}
1133
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1135{
1136 struct decode_cache *c = &ctxt->decode;
1137
1138 c->dst.type = OP_MEM;
1139 c->dst.bytes = c->op_bytes;
1140 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001141 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001142 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001143 c->regs[VCPU_REGS_RSP]);
1144}
1145
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001146static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001147 struct x86_emulate_ops *ops,
1148 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001149{
1150 struct decode_cache *c = &ctxt->decode;
1151 int rc;
1152
Avi Kivity781d0ed2008-11-27 18:00:28 +02001153 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1154 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001155 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001156 if (rc != 0)
1157 return rc;
1158
Avi Kivity350f69d2009-01-05 11:12:40 +02001159 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001160 return rc;
1161}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001162
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001163static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1164 struct x86_emulate_ops *ops)
1165{
1166 struct decode_cache *c = &ctxt->decode;
1167 int rc;
1168
Avi Kivity350f69d2009-01-05 11:12:40 +02001169 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001170 if (rc != 0)
1171 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001172 return 0;
1173}
1174
Laurent Vivier05f086f2007-09-24 11:10:55 +02001175static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001176{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001177 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178 switch (c->modrm_reg) {
1179 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001180 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001181 break;
1182 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001183 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184 break;
1185 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001186 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187 break;
1188 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001189 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001190 break;
1191 case 4: /* sal/shl */
1192 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001193 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194 break;
1195 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001196 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 break;
1198 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 break;
1201 }
1202}
1203
1204static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001205 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001206{
1207 struct decode_cache *c = &ctxt->decode;
1208 int rc = 0;
1209
1210 switch (c->modrm_reg) {
1211 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001212 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213 break;
1214 case 2: /* not */
1215 c->dst.val = ~c->dst.val;
1216 break;
1217 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001218 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001219 break;
1220 default:
1221 DPRINTF("Cannot emulate %02x\n", c->b);
1222 rc = X86EMUL_UNHANDLEABLE;
1223 break;
1224 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001225 return rc;
1226}
1227
1228static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001229 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001230{
1231 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001232
1233 switch (c->modrm_reg) {
1234 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001235 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001236 break;
1237 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001238 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001240 case 2: /* call near abs */ {
1241 long int old_eip;
1242 old_eip = c->eip;
1243 c->eip = c->src.val;
1244 c->src.val = old_eip;
1245 emulate_push(ctxt);
1246 break;
1247 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001249 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001250 break;
1251 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001252 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001254 }
1255 return 0;
1256}
1257
1258static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1259 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001260 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261{
1262 struct decode_cache *c = &ctxt->decode;
1263 u64 old, new;
1264 int rc;
1265
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001266 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001267 if (rc != 0)
1268 return rc;
1269
1270 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1271 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1272
1273 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1274 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001275 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001276
1277 } else {
1278 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1279 (u32) c->regs[VCPU_REGS_RBX];
1280
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001281 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001282 if (rc != 0)
1283 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001284 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001285 }
1286 return 0;
1287}
1288
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001289static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1290 struct x86_emulate_ops *ops)
1291{
1292 struct decode_cache *c = &ctxt->decode;
1293 int rc;
1294 unsigned long cs;
1295
1296 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1297 if (rc)
1298 return rc;
1299 if (c->op_bytes == 4)
1300 c->eip = (u32)c->eip;
1301 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1302 if (rc)
1303 return rc;
1304 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1305 return rc;
1306}
1307
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001308static inline int writeback(struct x86_emulate_ctxt *ctxt,
1309 struct x86_emulate_ops *ops)
1310{
1311 int rc;
1312 struct decode_cache *c = &ctxt->decode;
1313
1314 switch (c->dst.type) {
1315 case OP_REG:
1316 /* The 4-byte case *is* correct:
1317 * in 64-bit mode we zero-extend.
1318 */
1319 switch (c->dst.bytes) {
1320 case 1:
1321 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1322 break;
1323 case 2:
1324 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1325 break;
1326 case 4:
1327 *c->dst.ptr = (u32)c->dst.val;
1328 break; /* 64b: zero-ext */
1329 case 8:
1330 *c->dst.ptr = c->dst.val;
1331 break;
1332 }
1333 break;
1334 case OP_MEM:
1335 if (c->lock_prefix)
1336 rc = ops->cmpxchg_emulated(
1337 (unsigned long)c->dst.ptr,
1338 &c->dst.orig_val,
1339 &c->dst.val,
1340 c->dst.bytes,
1341 ctxt->vcpu);
1342 else
1343 rc = ops->write_emulated(
1344 (unsigned long)c->dst.ptr,
1345 &c->dst.val,
1346 c->dst.bytes,
1347 ctxt->vcpu);
1348 if (rc != 0)
1349 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001350 break;
1351 case OP_NONE:
1352 /* no writeback */
1353 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001354 default:
1355 break;
1356 }
1357 return 0;
1358}
1359
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001360int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001361x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001362{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001363 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001364 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001365 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001366 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001367 unsigned int port;
1368 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001369 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001370
Laurent Vivier34273182007-09-18 11:27:37 +02001371 /* Shadow copy of register state. Committed on successful emulation.
1372 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1373 * modify them.
1374 */
1375
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001376 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001377 saved_eip = c->eip;
1378
Avi Kivityc7e75a32007-10-28 16:34:25 +02001379 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001380 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001381
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001382 if (c->rep_prefix && (c->d & String)) {
1383 /* All REP prefixes have the same first termination condition */
1384 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001385 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001386 goto done;
1387 }
1388 /* The second termination condition only applies for REPE
1389 * and REPNE. Test if the repeat string operation prefix is
1390 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1391 * corresponding termination condition according to:
1392 * - if REPE/REPZ and ZF = 0 then done
1393 * - if REPNE/REPNZ and ZF = 1 then done
1394 */
1395 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1396 (c->b == 0xae) || (c->b == 0xaf)) {
1397 if ((c->rep_prefix == REPE_PREFIX) &&
1398 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001399 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001400 goto done;
1401 }
1402 if ((c->rep_prefix == REPNE_PREFIX) &&
1403 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001404 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001405 goto done;
1406 }
1407 }
1408 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001409 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001410 }
1411
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001412 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001413 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001414 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001415 rc = ops->read_emulated((unsigned long)c->src.ptr,
1416 &c->src.val,
1417 c->src.bytes,
1418 ctxt->vcpu);
1419 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001420 goto done;
1421 c->src.orig_val = c->src.val;
1422 }
1423
1424 if ((c->d & DstMask) == ImplicitOps)
1425 goto special_insn;
1426
1427
1428 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001429 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001430 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1431 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001432 if (c->d & BitOp) {
1433 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001434
Laurent Viviere4e03de2007-09-18 11:52:50 +02001435 c->dst.ptr = (void *)c->dst.ptr +
1436 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001437 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001438 if (!(c->d & Mov) &&
1439 /* optimisation - avoid slow emulated read */
1440 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1441 &c->dst.val,
1442 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001443 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001444 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001445 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001446
Avi Kivity018a98d2007-11-27 19:30:56 +02001447special_insn:
1448
Laurent Viviere4e03de2007-09-18 11:52:50 +02001449 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 goto twobyte_insn;
1451
Laurent Viviere4e03de2007-09-18 11:52:50 +02001452 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 case 0x00 ... 0x05:
1454 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001455 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 break;
1457 case 0x08 ... 0x0d:
1458 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001459 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 break;
1461 case 0x10 ... 0x15:
1462 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001463 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 break;
1465 case 0x18 ... 0x1d:
1466 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001467 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001469 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001471 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 break;
1473 case 0x28 ... 0x2d:
1474 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001475 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 break;
1477 case 0x30 ... 0x35:
1478 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001479 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 break;
1481 case 0x38 ... 0x3d:
1482 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001483 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001485 case 0x40 ... 0x47: /* inc r16/r32 */
1486 emulate_1op("inc", c->dst, ctxt->eflags);
1487 break;
1488 case 0x48 ... 0x4f: /* dec r16/r32 */
1489 emulate_1op("dec", c->dst, ctxt->eflags);
1490 break;
1491 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001492 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001493 break;
1494 case 0x58 ... 0x5f: /* pop reg */
1495 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001496 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001497 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001498 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001499 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001501 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001503 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001505 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001506 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001507 emulate_push(ctxt);
1508 break;
1509 case 0x6c: /* insb */
1510 case 0x6d: /* insw/insd */
1511 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1512 1,
1513 (c->d & ByteOp) ? 1 : c->op_bytes,
1514 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001515 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001516 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001517 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001518 c->regs[VCPU_REGS_RDI]),
1519 c->rep_prefix,
1520 c->regs[VCPU_REGS_RDX]) == 0) {
1521 c->eip = saved_eip;
1522 return -1;
1523 }
1524 return 0;
1525 case 0x6e: /* outsb */
1526 case 0x6f: /* outsw/outsd */
1527 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1528 0,
1529 (c->d & ByteOp) ? 1 : c->op_bytes,
1530 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001531 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001532 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001533 register_address(c,
1534 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001535 c->regs[VCPU_REGS_RSI]),
1536 c->rep_prefix,
1537 c->regs[VCPU_REGS_RDX]) == 0) {
1538 c->eip = saved_eip;
1539 return -1;
1540 }
1541 return 0;
1542 case 0x70 ... 0x7f: /* jcc (short) */ {
1543 int rel = insn_fetch(s8, 1, c->eip);
1544
1545 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001546 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001547 break;
1548 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001550 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 case 0:
1552 goto add;
1553 case 1:
1554 goto or;
1555 case 2:
1556 goto adc;
1557 case 3:
1558 goto sbb;
1559 case 4:
1560 goto and;
1561 case 5:
1562 goto sub;
1563 case 6:
1564 goto xor;
1565 case 7:
1566 goto cmp;
1567 }
1568 break;
1569 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001570 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 break;
1572 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001573 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001577 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 break;
1579 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001580 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 break;
1582 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001583 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 break; /* 64b reg: zero-extend */
1585 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001586 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 break;
1588 }
1589 /*
1590 * Write back the memory destination with implicit LOCK
1591 * prefix.
1592 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001593 c->dst.val = c->src.val;
1594 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001597 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001598 case 0x8c: { /* mov r/m, sreg */
1599 struct kvm_segment segreg;
1600
1601 if (c->modrm_reg <= 5)
1602 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1603 else {
1604 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1605 c->modrm);
1606 goto cannot_emulate;
1607 }
1608 c->dst.val = segreg.selector;
1609 break;
1610 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001611 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001612 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001613 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001614 case 0x8e: { /* mov seg, r/m16 */
1615 uint16_t sel;
1616 int type_bits;
1617 int err;
1618
1619 sel = c->src.val;
1620 if (c->modrm_reg <= 5) {
1621 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1622 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1623 type_bits, c->modrm_reg);
1624 } else {
1625 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1626 c->modrm);
1627 goto cannot_emulate;
1628 }
1629
1630 if (err < 0)
1631 goto cannot_emulate;
1632
1633 c->dst.type = OP_NONE; /* Disable writeback. */
1634 break;
1635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637 rc = emulate_grp1a(ctxt, ops);
1638 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001641 case 0x90: /* nop / xchg r8,rax */
1642 if (!(c->rex_prefix & 1)) { /* nop */
1643 c->dst.type = OP_NONE;
1644 break;
1645 }
1646 case 0x91 ... 0x97: /* xchg reg,rax */
1647 c->src.type = c->dst.type = OP_REG;
1648 c->src.bytes = c->dst.bytes = c->op_bytes;
1649 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1650 c->src.val = *(c->src.ptr);
1651 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001652 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001653 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001654 emulate_push(ctxt);
1655 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001656 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001657 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001658 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001659 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001660 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001661 case 0xa0 ... 0xa1: /* mov */
1662 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1663 c->dst.val = c->src.val;
1664 break;
1665 case 0xa2 ... 0xa3: /* mov */
1666 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1667 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001669 c->dst.type = OP_MEM;
1670 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001671 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001672 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001673 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001674 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001675 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001676 c->regs[VCPU_REGS_RSI]),
1677 &c->dst.val,
1678 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001680 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001681 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001683 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001684 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 break;
1687 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001688 c->src.type = OP_NONE; /* Disable writeback. */
1689 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001690 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001691 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001692 c->regs[VCPU_REGS_RSI]);
1693 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1694 &c->src.val,
1695 c->src.bytes,
1696 ctxt->vcpu)) != 0)
1697 goto done;
1698
1699 c->dst.type = OP_NONE; /* Disable writeback. */
1700 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001701 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001702 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001703 c->regs[VCPU_REGS_RDI]);
1704 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1705 &c->dst.val,
1706 c->dst.bytes,
1707 ctxt->vcpu)) != 0)
1708 goto done;
1709
1710 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1711
1712 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1713
Harvey Harrison7a9572752008-02-19 07:40:41 -08001714 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001715 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1716 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001717 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001718 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1719 : c->dst.bytes);
1720
1721 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 c->dst.type = OP_MEM;
1724 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001725 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001726 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001727 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001728 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001729 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001730 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001731 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 break;
1733 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001734 c->dst.type = OP_REG;
1735 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1736 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001737 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001738 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001739 c->regs[VCPU_REGS_RSI]),
1740 &c->dst.val,
1741 c->dst.bytes,
1742 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001744 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001745 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001746 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 break;
1748 case 0xae ... 0xaf: /* scas */
1749 DPRINTF("Urk! I don't handle SCAS.\n");
1750 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001751 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001752 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001753 case 0xc0 ... 0xc1:
1754 emulate_grp2(ctxt);
1755 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001756 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001757 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02001758 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02001759 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02001760 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001761 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1762 mov:
1763 c->dst.val = c->src.val;
1764 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001765 case 0xcb: /* ret far */
1766 rc = emulate_ret_far(ctxt, ops);
1767 if (rc)
1768 goto done;
1769 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001770 case 0xd0 ... 0xd1: /* Grp2 */
1771 c->src.val = 1;
1772 emulate_grp2(ctxt);
1773 break;
1774 case 0xd2 ... 0xd3: /* Grp2 */
1775 c->src.val = c->regs[VCPU_REGS_RCX];
1776 emulate_grp2(ctxt);
1777 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001778 case 0xe4: /* inb */
1779 case 0xe5: /* in */
1780 port = insn_fetch(u8, 1, c->eip);
1781 io_dir_in = 1;
1782 goto do_io;
1783 case 0xe6: /* outb */
1784 case 0xe7: /* out */
1785 port = insn_fetch(u8, 1, c->eip);
1786 io_dir_in = 0;
1787 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001788 case 0xe8: /* call (near) */ {
1789 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001790 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001791 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001792 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001793 break;
1794 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001795 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001796 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001797 default:
1798 DPRINTF("Call: Invalid op_bytes\n");
1799 goto cannot_emulate;
1800 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001801 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001802 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803 emulate_push(ctxt);
1804 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001805 }
1806 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001807 goto jmp;
1808 case 0xea: /* jmp far */ {
1809 uint32_t eip;
1810 uint16_t sel;
1811
1812 switch (c->op_bytes) {
1813 case 2:
1814 eip = insn_fetch(u16, 2, c->eip);
1815 break;
1816 case 4:
1817 eip = insn_fetch(u32, 4, c->eip);
1818 break;
1819 default:
1820 DPRINTF("jmp far: Invalid op_bytes\n");
1821 goto cannot_emulate;
1822 }
1823 sel = insn_fetch(u16, 2, c->eip);
1824 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1825 DPRINTF("jmp far: Failed to load CS descriptor\n");
1826 goto cannot_emulate;
1827 }
1828
1829 c->eip = eip;
1830 break;
1831 }
1832 case 0xeb:
1833 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001834 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001835 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001836 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001837 case 0xec: /* in al,dx */
1838 case 0xed: /* in (e/r)ax,dx */
1839 port = c->regs[VCPU_REGS_RDX];
1840 io_dir_in = 1;
1841 goto do_io;
1842 case 0xee: /* out al,dx */
1843 case 0xef: /* out (e/r)ax,dx */
1844 port = c->regs[VCPU_REGS_RDX];
1845 io_dir_in = 0;
1846 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1847 (c->d & ByteOp) ? 1 : c->op_bytes,
1848 port) != 0) {
1849 c->eip = saved_eip;
1850 goto cannot_emulate;
1851 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001852 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001853 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001854 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001855 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001856 case 0xf5: /* cmc */
1857 /* complement carry flag from eflags reg */
1858 ctxt->eflags ^= EFLG_CF;
1859 c->dst.type = OP_NONE; /* Disable writeback. */
1860 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001861 case 0xf6 ... 0xf7: /* Grp3 */
1862 rc = emulate_grp3(ctxt, ops);
1863 if (rc != 0)
1864 goto done;
1865 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001866 case 0xf8: /* clc */
1867 ctxt->eflags &= ~EFLG_CF;
1868 c->dst.type = OP_NONE; /* Disable writeback. */
1869 break;
1870 case 0xfa: /* cli */
1871 ctxt->eflags &= ~X86_EFLAGS_IF;
1872 c->dst.type = OP_NONE; /* Disable writeback. */
1873 break;
1874 case 0xfb: /* sti */
1875 ctxt->eflags |= X86_EFLAGS_IF;
1876 c->dst.type = OP_NONE; /* Disable writeback. */
1877 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001878 case 0xfc: /* cld */
1879 ctxt->eflags &= ~EFLG_DF;
1880 c->dst.type = OP_NONE; /* Disable writeback. */
1881 break;
1882 case 0xfd: /* std */
1883 ctxt->eflags |= EFLG_DF;
1884 c->dst.type = OP_NONE; /* Disable writeback. */
1885 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001886 case 0xfe ... 0xff: /* Grp4/Grp5 */
1887 rc = emulate_grp45(ctxt, ops);
1888 if (rc != 0)
1889 goto done;
1890 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001892
1893writeback:
1894 rc = writeback(ctxt, ops);
1895 if (rc != 0)
1896 goto done;
1897
1898 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001899 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001900 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001901
1902done:
1903 if (rc == X86EMUL_UNHANDLEABLE) {
1904 c->eip = saved_eip;
1905 return -1;
1906 }
1907 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908
1909twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001910 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001912 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 u16 size;
1914 unsigned long address;
1915
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001916 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001917 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001918 goto cannot_emulate;
1919
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001920 rc = kvm_fix_hypercall(ctxt->vcpu);
1921 if (rc)
1922 goto done;
1923
Avi Kivity33e38852008-05-21 15:34:25 +03001924 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001925 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001926 /* Disable writeback. */
1927 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001928 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001930 rc = read_descriptor(ctxt, ops, c->src.ptr,
1931 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 if (rc)
1933 goto done;
1934 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001935 /* Disable writeback. */
1936 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001938 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02001939 if (c->modrm_mod == 3) {
1940 switch (c->modrm_rm) {
1941 case 1:
1942 rc = kvm_fix_hypercall(ctxt->vcpu);
1943 if (rc)
1944 goto done;
1945 break;
1946 default:
1947 goto cannot_emulate;
1948 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001949 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001950 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001951 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001952 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001953 if (rc)
1954 goto done;
1955 realmode_lidt(ctxt->vcpu, size, address);
1956 }
Avi Kivity16286d02008-04-14 14:40:50 +03001957 /* Disable writeback. */
1958 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 break;
1960 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001961 c->dst.bytes = 2;
1962 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 break;
1964 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001965 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1966 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001967 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968 break;
1969 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001970 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001971 /* Disable writeback. */
1972 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 break;
1974 default:
1975 goto cannot_emulate;
1976 }
1977 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001978 case 0x06:
1979 emulate_clts(ctxt->vcpu);
1980 c->dst.type = OP_NONE;
1981 break;
1982 case 0x08: /* invd */
1983 case 0x09: /* wbinvd */
1984 case 0x0d: /* GrpP (prefetch) */
1985 case 0x18: /* Grp16 (prefetch/nop) */
1986 c->dst.type = OP_NONE;
1987 break;
1988 case 0x20: /* mov cr, reg */
1989 if (c->modrm_mod != 3)
1990 goto cannot_emulate;
1991 c->regs[c->modrm_rm] =
1992 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1993 c->dst.type = OP_NONE; /* no writeback */
1994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001996 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001998 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001999 if (rc)
2000 goto cannot_emulate;
2001 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002003 case 0x22: /* mov reg, cr */
2004 if (c->modrm_mod != 3)
2005 goto cannot_emulate;
2006 realmode_set_cr(ctxt->vcpu,
2007 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2008 c->dst.type = OP_NONE;
2009 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002011 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002013 rc = emulator_set_dr(ctxt, c->modrm_reg,
2014 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002015 if (rc)
2016 goto cannot_emulate;
2017 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002019 case 0x30:
2020 /* wrmsr */
2021 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2022 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2023 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2024 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002025 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002026 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002027 }
2028 rc = X86EMUL_CONTINUE;
2029 c->dst.type = OP_NONE;
2030 break;
2031 case 0x32:
2032 /* rdmsr */
2033 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2034 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002035 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002036 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002037 } else {
2038 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2039 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2040 }
2041 rc = X86EMUL_CONTINUE;
2042 c->dst.type = OP_NONE;
2043 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002045 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002046 if (!test_cc(c->b, ctxt->eflags))
2047 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002049 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
2050 long int rel;
2051
2052 switch (c->op_bytes) {
2053 case 2:
2054 rel = insn_fetch(s16, 2, c->eip);
2055 break;
2056 case 4:
2057 rel = insn_fetch(s32, 4, c->eip);
2058 break;
2059 case 8:
2060 rel = insn_fetch(s64, 8, c->eip);
2061 break;
2062 default:
2063 DPRINTF("jnz: Invalid op_bytes\n");
2064 goto cannot_emulate;
2065 }
2066 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08002067 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02002068 c->dst.type = OP_NONE;
2069 break;
2070 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03002071 case 0xa3:
2072 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002073 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002074 /* only subword offset */
2075 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002076 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002077 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002078 case 0xa4: /* shld imm8, r, r/m */
2079 case 0xa5: /* shld cl, r, r/m */
2080 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2081 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002082 case 0xab:
2083 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002084 /* only subword offset */
2085 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002086 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002087 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002088 case 0xac: /* shrd imm8, r, r/m */
2089 case 0xad: /* shrd cl, r, r/m */
2090 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2091 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002092 case 0xae: /* clflush */
2093 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 case 0xb0 ... 0xb1: /* cmpxchg */
2095 /*
2096 * Save real source value, then compare EAX against
2097 * destination.
2098 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002099 c->src.orig_val = c->src.val;
2100 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002101 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2102 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002104 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 } else {
2106 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002107 c->dst.type = OP_REG;
2108 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 }
2110 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 case 0xb3:
2112 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002113 /* only subword offset */
2114 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002115 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002118 c->dst.bytes = c->op_bytes;
2119 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2120 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002123 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 case 0:
2125 goto bt;
2126 case 1:
2127 goto bts;
2128 case 2:
2129 goto btr;
2130 case 3:
2131 goto btc;
2132 }
2133 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002134 case 0xbb:
2135 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002136 /* only subword offset */
2137 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002138 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002139 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002141 c->dst.bytes = c->op_bytes;
2142 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2143 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002145 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002146 c->dst.bytes = c->op_bytes;
2147 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2148 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002151 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002152 if (rc != 0)
2153 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002154 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002155 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 }
2157 goto writeback;
2158
2159cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002160 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002161 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162 return -1;
2163}