blob: 70242f5f0964ab0744fd3ba3e57af84a98b5b2ae [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. */
61#define SrcMask (7<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080062/* Generic ModRM decode. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020063#define ModRM (1<<7)
Avi Kivity6aa8b732006-12-10 02:21:36 -080064/* Destination is only written; never read. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020065#define Mov (1<<8)
66#define BitOp (1<<9)
67#define MemAbs (1<<10) /* Memory operand is absolute displacement */
68#define String (1<<12) /* String instruction (rep capable) */
69#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020070#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
71#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
72#define GroupMask 0xff /* Group number stored in bits 0:7 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
Avi Kivity43bb19c2008-01-18 12:46:50 +020074enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020075 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020076 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020077};
78
Avi Kivityc7e75a32007-10-28 16:34:25 +020079static u16 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080080 /* 0x00 - 0x07 */
81 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
82 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020083 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080084 /* 0x08 - 0x0F */
85 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
86 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
87 0, 0, 0, 0,
88 /* 0x10 - 0x17 */
89 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
90 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
91 0, 0, 0, 0,
92 /* 0x18 - 0x1F */
93 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
94 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
95 0, 0, 0, 0,
96 /* 0x20 - 0x27 */
97 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
98 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +020099 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 /* 0x28 - 0x2F */
101 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
103 0, 0, 0, 0,
104 /* 0x30 - 0x37 */
105 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
106 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
107 0, 0, 0, 0,
108 /* 0x38 - 0x3F */
109 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200111 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
112 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700113 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200114 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700115 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200116 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300117 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200118 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
119 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300120 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200121 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
122 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700123 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700125 0, 0, 0, 0,
126 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300127 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300128 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
129 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300130 /* 0x70 - 0x77 */
131 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
132 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
133 /* 0x78 - 0x7F */
134 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
135 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200137 Group | Group1_80, Group | Group1_81,
138 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
140 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
141 /* 0x88 - 0x8F */
142 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
143 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200144 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200145 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300146 /* 0x90 - 0x97 */
147 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
148 /* 0x98 - 0x9F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200149 0, 0, 0, 0, ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200151 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
152 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200153 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
154 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200156 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
157 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
158 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300159 /* 0xB0 - 0xB7 */
160 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
161 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
162 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
163 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
164 /* 0xB8 - 0xBF */
165 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
166 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
167 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
168 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300170 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200171 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300172 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0xC8 - 0xCF */
174 0, 0, 0, 0, 0, 0, 0, 0,
175 /* 0xD0 - 0xD7 */
176 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
177 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
178 0, 0, 0, 0,
179 /* 0xD8 - 0xDF */
180 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300181 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300182 0, 0, 0, 0,
183 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
184 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300185 /* 0xE8 - 0xEF */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +0200186 ImplicitOps | Stack, SrcImm | ImplicitOps,
187 ImplicitOps, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300188 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
189 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190 /* 0xF0 - 0xF7 */
191 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200192 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700194 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300195 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196};
197
Avi Kivity038e51d2007-01-22 20:40:40 -0800198static u16 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 /* 0x00 - 0x0F */
Avi Kivityd95058a2008-01-18 13:36:50 +0200200 0, Group | GroupDual | Group7, 0, 0, 0, 0, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200201 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 /* 0x10 - 0x1F */
203 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
204 /* 0x20 - 0x2F */
205 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
206 0, 0, 0, 0, 0, 0, 0, 0,
207 /* 0x30 - 0x3F */
Avi Kivity35f3f282007-07-17 14:20:30 +0300208 ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0x40 - 0x47 */
210 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
211 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
212 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
213 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
214 /* 0x48 - 0x4F */
215 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
216 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
217 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
218 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
219 /* 0x50 - 0x5F */
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0x60 - 0x6F */
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 /* 0x70 - 0x7F */
224 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225 /* 0x80 - 0x8F */
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300226 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
227 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
228 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
229 ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 /* 0x90 - 0x9F */
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 /* 0xA0 - 0xA7 */
Avi Kivity038e51d2007-01-22 20:40:40 -0800233 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 /* 0xA8 - 0xAF */
Glauber Costa2a7c5b82008-07-10 17:08:15 -0300235 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 /* 0xB0 - 0xB7 */
237 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800238 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
240 DstReg | SrcMem16 | ModRM | Mov,
241 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800242 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
244 DstReg | SrcMem16 | ModRM | Mov,
245 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800246 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
247 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* 0xD0 - 0xDF */
249 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /* 0xE0 - 0xEF */
251 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 /* 0xF0 - 0xFF */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
254};
255
Avi Kivitye09d0822008-01-18 12:38:59 +0200256static u16 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200257 [Group1_80*8] =
258 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
259 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
260 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
261 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
262 [Group1_81*8] =
263 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
264 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
265 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
266 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
267 [Group1_82*8] =
268 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
269 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
270 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
271 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
272 [Group1_83*8] =
273 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
274 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
275 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
276 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200277 [Group1A*8] =
278 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200279 [Group3_Byte*8] =
280 ByteOp | SrcImm | DstMem | ModRM, 0,
281 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
282 0, 0, 0, 0,
283 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400284 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300285 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200286 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200287 [Group4*8] =
288 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
289 0, 0, 0, 0, 0, 0,
290 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300291 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
292 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300293 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200294 [Group7*8] =
295 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300296 SrcNone | ModRM | DstMem | Mov, 0,
297 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200298};
299
300static u16 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200301 [Group7*8] =
Avi Kivity16286d02008-04-14 14:40:50 +0300302 SrcNone | ModRM, 0, 0, 0,
303 SrcNone | ModRM | DstMem | Mov, 0,
304 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200305};
306
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307/* EFLAGS bit definitions. */
308#define EFLG_OF (1<<11)
309#define EFLG_DF (1<<10)
310#define EFLG_SF (1<<7)
311#define EFLG_ZF (1<<6)
312#define EFLG_AF (1<<4)
313#define EFLG_PF (1<<2)
314#define EFLG_CF (1<<0)
315
316/*
317 * Instruction emulation:
318 * Most instructions are emulated directly via a fragment of inline assembly
319 * code. This allows us to save/restore EFLAGS and thus very easily pick up
320 * any modified flags.
321 */
322
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800323#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324#define _LO32 "k" /* force 32-bit operand */
325#define _STK "%%rsp" /* stack pointer */
326#elif defined(__i386__)
327#define _LO32 "" /* force 32-bit operand */
328#define _STK "%%esp" /* stack pointer */
329#endif
330
331/*
332 * These EFLAGS bits are restored from saved value during emulation, and
333 * any changes are written back to the saved value after emulation.
334 */
335#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
336
337/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200338#define _PRE_EFLAGS(_sav, _msk, _tmp) \
339 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
340 "movl %"_sav",%"_LO32 _tmp"; " \
341 "push %"_tmp"; " \
342 "push %"_tmp"; " \
343 "movl %"_msk",%"_LO32 _tmp"; " \
344 "andl %"_LO32 _tmp",("_STK"); " \
345 "pushf; " \
346 "notl %"_LO32 _tmp"; " \
347 "andl %"_LO32 _tmp",("_STK"); " \
348 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
349 "pop %"_tmp"; " \
350 "orl %"_LO32 _tmp",("_STK"); " \
351 "popf; " \
352 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353
354/* After executing instruction: write-back necessary bits in EFLAGS. */
355#define _POST_EFLAGS(_sav, _msk, _tmp) \
356 /* _sav |= EFLAGS & _msk; */ \
357 "pushf; " \
358 "pop %"_tmp"; " \
359 "andl %"_msk",%"_LO32 _tmp"; " \
360 "orl %"_LO32 _tmp",%"_sav"; "
361
Avi Kivitydda96d82008-11-26 15:14:10 +0200362#ifdef CONFIG_X86_64
363#define ON64(x) x
364#else
365#define ON64(x)
366#endif
367
Avi Kivity6b7ad612008-11-26 15:30:45 +0200368#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
369 do { \
370 __asm__ __volatile__ ( \
371 _PRE_EFLAGS("0", "4", "2") \
372 _op _suffix " %"_x"3,%1; " \
373 _POST_EFLAGS("0", "4", "2") \
374 : "=m" (_eflags), "=m" ((_dst).val), \
375 "=&r" (_tmp) \
376 : _y ((_src).val), "i" (EFLAGS_MASK)); \
377 } while (0);
378
379
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380/* Raw emulation: instruction has two explicit operands. */
381#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200382 do { \
383 unsigned long _tmp; \
384 \
385 switch ((_dst).bytes) { \
386 case 2: \
387 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
388 break; \
389 case 4: \
390 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
391 break; \
392 case 8: \
393 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
394 break; \
395 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 } while (0)
397
398#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
399 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200400 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400401 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200403 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 break; \
405 default: \
406 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
407 _wx, _wy, _lx, _ly, _qx, _qy); \
408 break; \
409 } \
410 } while (0)
411
412/* Source operand is byte-sized and may be restricted to just %cl. */
413#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
414 __emulate_2op(_op, _src, _dst, _eflags, \
415 "b", "c", "b", "c", "b", "c", "b", "c")
416
417/* Source operand is byte, word, long or quad sized. */
418#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
419 __emulate_2op(_op, _src, _dst, _eflags, \
420 "b", "q", "w", "r", _LO32, "r", "", "r")
421
422/* Source operand is word, long or quad sized. */
423#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
424 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
425 "w", "r", _LO32, "r", "", "r")
426
Avi Kivitydda96d82008-11-26 15:14:10 +0200427#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 do { \
429 unsigned long _tmp; \
430 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200431 __asm__ __volatile__ ( \
432 _PRE_EFLAGS("0", "3", "2") \
433 _op _suffix " %1; " \
434 _POST_EFLAGS("0", "3", "2") \
435 : "=m" (_eflags), "+m" ((_dst).val), \
436 "=&r" (_tmp) \
437 : "i" (EFLAGS_MASK)); \
438 } while (0)
439
440/* Instruction has only one explicit operand (no source operand). */
441#define emulate_1op(_op, _dst, _eflags) \
442 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400443 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200444 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
445 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
446 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
447 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 } \
449 } while (0)
450
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451/* Fetch next part of the instruction being emulated. */
452#define insn_fetch(_type, _size, _eip) \
453({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200454 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400455 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 goto done; \
457 (_eip) += (_size); \
458 (_type)_x; \
459})
460
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800461static inline unsigned long ad_mask(struct decode_cache *c)
462{
463 return (1UL << (c->ad_bytes << 3)) - 1;
464}
465
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800467static inline unsigned long
468address_mask(struct decode_cache *c, unsigned long reg)
469{
470 if (c->ad_bytes == sizeof(unsigned long))
471 return reg;
472 else
473 return reg & ad_mask(c);
474}
475
476static inline unsigned long
477register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
478{
479 return base + address_mask(c, reg);
480}
481
Harvey Harrison7a9572752008-02-19 07:40:41 -0800482static inline void
483register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
484{
485 if (c->ad_bytes == sizeof(unsigned long))
486 *reg += inc;
487 else
488 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
489}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490
Harvey Harrison7a9572752008-02-19 07:40:41 -0800491static inline void jmp_rel(struct decode_cache *c, int rel)
492{
493 register_address_increment(c, &c->eip, rel);
494}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300495
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300496static void set_seg_override(struct decode_cache *c, int seg)
497{
498 c->has_seg_override = true;
499 c->seg_override = seg;
500}
501
502static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
503{
504 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
505 return 0;
506
507 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
508}
509
510static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
511 struct decode_cache *c)
512{
513 if (!c->has_seg_override)
514 return 0;
515
516 return seg_base(ctxt, c->seg_override);
517}
518
519static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
520{
521 return seg_base(ctxt, VCPU_SREG_ES);
522}
523
524static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
525{
526 return seg_base(ctxt, VCPU_SREG_SS);
527}
528
Avi Kivity62266862007-11-20 13:15:52 +0200529static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
530 struct x86_emulate_ops *ops,
531 unsigned long linear, u8 *dest)
532{
533 struct fetch_cache *fc = &ctxt->decode.fetch;
534 int rc;
535 int size;
536
537 if (linear < fc->start || linear >= fc->end) {
538 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
539 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
540 if (rc)
541 return rc;
542 fc->start = linear;
543 fc->end = linear + size;
544 }
545 *dest = fc->data[linear - fc->start];
546 return 0;
547}
548
549static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
550 struct x86_emulate_ops *ops,
551 unsigned long eip, void *dest, unsigned size)
552{
553 int rc = 0;
554
555 eip += ctxt->cs_base;
556 while (size--) {
557 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
558 if (rc)
559 return rc;
560 }
561 return 0;
562}
563
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000564/*
565 * Given the 'reg' portion of a ModRM byte, and a register block, return a
566 * pointer into the block that addresses the relevant register.
567 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
568 */
569static void *decode_register(u8 modrm_reg, unsigned long *regs,
570 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571{
572 void *p;
573
574 p = &regs[modrm_reg];
575 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
576 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
577 return p;
578}
579
580static int read_descriptor(struct x86_emulate_ctxt *ctxt,
581 struct x86_emulate_ops *ops,
582 void *ptr,
583 u16 *size, unsigned long *address, int op_bytes)
584{
585 int rc;
586
587 if (op_bytes == 2)
588 op_bytes = 3;
589 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300590 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
591 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 if (rc)
593 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300594 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
595 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 return rc;
597}
598
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300599static int test_cc(unsigned int condition, unsigned int flags)
600{
601 int rc = 0;
602
603 switch ((condition & 15) >> 1) {
604 case 0: /* o */
605 rc |= (flags & EFLG_OF);
606 break;
607 case 1: /* b/c/nae */
608 rc |= (flags & EFLG_CF);
609 break;
610 case 2: /* z/e */
611 rc |= (flags & EFLG_ZF);
612 break;
613 case 3: /* be/na */
614 rc |= (flags & (EFLG_CF|EFLG_ZF));
615 break;
616 case 4: /* s */
617 rc |= (flags & EFLG_SF);
618 break;
619 case 5: /* p/pe */
620 rc |= (flags & EFLG_PF);
621 break;
622 case 7: /* le/ng */
623 rc |= (flags & EFLG_ZF);
624 /* fall through */
625 case 6: /* l/nge */
626 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
627 break;
628 }
629
630 /* Odd condition identifiers (lsb == 1) have inverted sense. */
631 return (!!rc ^ (condition & 1));
632}
633
Avi Kivity3c118e22007-10-31 10:27:04 +0200634static void decode_register_operand(struct operand *op,
635 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200636 int inhibit_bytereg)
637{
Avi Kivity33615aa2007-10-31 11:15:56 +0200638 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200639 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200640
641 if (!(c->d & ModRM))
642 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200643 op->type = OP_REG;
644 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200645 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200646 op->val = *(u8 *)op->ptr;
647 op->bytes = 1;
648 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200649 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200650 op->bytes = c->op_bytes;
651 switch (op->bytes) {
652 case 2:
653 op->val = *(u16 *)op->ptr;
654 break;
655 case 4:
656 op->val = *(u32 *)op->ptr;
657 break;
658 case 8:
659 op->val = *(u64 *) op->ptr;
660 break;
661 }
662 }
663 op->orig_val = op->val;
664}
665
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666static int decode_modrm(struct x86_emulate_ctxt *ctxt,
667 struct x86_emulate_ops *ops)
668{
669 struct decode_cache *c = &ctxt->decode;
670 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700671 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200672 int rc = 0;
673
674 if (c->rex_prefix) {
675 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
676 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
677 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
678 }
679
680 c->modrm = insn_fetch(u8, 1, c->eip);
681 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
682 c->modrm_reg |= (c->modrm & 0x38) >> 3;
683 c->modrm_rm |= (c->modrm & 0x07);
684 c->modrm_ea = 0;
685 c->use_modrm_ea = 1;
686
687 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300688 c->modrm_ptr = decode_register(c->modrm_rm,
689 c->regs, c->d & ByteOp);
690 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200691 return rc;
692 }
693
694 if (c->ad_bytes == 2) {
695 unsigned bx = c->regs[VCPU_REGS_RBX];
696 unsigned bp = c->regs[VCPU_REGS_RBP];
697 unsigned si = c->regs[VCPU_REGS_RSI];
698 unsigned di = c->regs[VCPU_REGS_RDI];
699
700 /* 16-bit ModR/M decode. */
701 switch (c->modrm_mod) {
702 case 0:
703 if (c->modrm_rm == 6)
704 c->modrm_ea += insn_fetch(u16, 2, c->eip);
705 break;
706 case 1:
707 c->modrm_ea += insn_fetch(s8, 1, c->eip);
708 break;
709 case 2:
710 c->modrm_ea += insn_fetch(u16, 2, c->eip);
711 break;
712 }
713 switch (c->modrm_rm) {
714 case 0:
715 c->modrm_ea += bx + si;
716 break;
717 case 1:
718 c->modrm_ea += bx + di;
719 break;
720 case 2:
721 c->modrm_ea += bp + si;
722 break;
723 case 3:
724 c->modrm_ea += bp + di;
725 break;
726 case 4:
727 c->modrm_ea += si;
728 break;
729 case 5:
730 c->modrm_ea += di;
731 break;
732 case 6:
733 if (c->modrm_mod != 0)
734 c->modrm_ea += bp;
735 break;
736 case 7:
737 c->modrm_ea += bx;
738 break;
739 }
740 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
741 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300742 if (!c->has_seg_override)
743 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200744 c->modrm_ea = (u16)c->modrm_ea;
745 } else {
746 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700747 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200748 sib = insn_fetch(u8, 1, c->eip);
749 index_reg |= (sib >> 3) & 7;
750 base_reg |= sib & 7;
751 scale = sib >> 6;
752
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700753 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
754 c->modrm_ea += insn_fetch(s32, 4, c->eip);
755 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200756 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700757 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200758 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700759 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
760 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700761 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700762 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200763 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200764 switch (c->modrm_mod) {
765 case 0:
766 if (c->modrm_rm == 5)
767 c->modrm_ea += insn_fetch(s32, 4, c->eip);
768 break;
769 case 1:
770 c->modrm_ea += insn_fetch(s8, 1, c->eip);
771 break;
772 case 2:
773 c->modrm_ea += insn_fetch(s32, 4, c->eip);
774 break;
775 }
776 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200777done:
778 return rc;
779}
780
781static int decode_abs(struct x86_emulate_ctxt *ctxt,
782 struct x86_emulate_ops *ops)
783{
784 struct decode_cache *c = &ctxt->decode;
785 int rc = 0;
786
787 switch (c->ad_bytes) {
788 case 2:
789 c->modrm_ea = insn_fetch(u16, 2, c->eip);
790 break;
791 case 4:
792 c->modrm_ea = insn_fetch(u32, 4, c->eip);
793 break;
794 case 8:
795 c->modrm_ea = insn_fetch(u64, 8, c->eip);
796 break;
797 }
798done:
799 return rc;
800}
801
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200803x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200805 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200808 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809
810 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Laurent Viviere4e03de2007-09-18 11:52:50 +0200812 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300813 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300814 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800815 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816
817 switch (mode) {
818 case X86EMUL_MODE_REAL:
819 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200820 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 break;
822 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200823 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800825#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200827 def_op_bytes = 4;
828 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 break;
830#endif
831 default:
832 return -1;
833 }
834
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200835 c->op_bytes = def_op_bytes;
836 c->ad_bytes = def_ad_bytes;
837
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200839 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200840 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200842 /* switch between 2/4 bytes */
843 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 break;
845 case 0x67: /* address-size override */
846 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200847 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200848 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200850 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200851 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300854 case 0x2e: /* CS override */
855 case 0x36: /* SS override */
856 case 0x3e: /* DS override */
857 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 break;
859 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300861 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200863 case 0x40 ... 0x4f: /* REX */
864 if (mode != X86EMUL_MODE_PROT64)
865 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200866 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200867 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200869 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200871 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100872 c->rep_prefix = REPNE_PREFIX;
873 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100875 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 default:
878 goto done_prefixes;
879 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200880
881 /* Any legacy prefix after a REX prefix nullifies its effect. */
882
Avi Kivity33615aa2007-10-31 11:15:56 +0200883 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 }
885
886done_prefixes:
887
888 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200890 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200891 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892
893 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200894 c->d = opcode_table[c->b];
895 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200897 if (c->b == 0x0f) {
898 c->twobyte = 1;
899 c->b = insn_fetch(u8, 1, c->eip);
900 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200902 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903
Avi Kivitye09d0822008-01-18 12:38:59 +0200904 if (c->d & Group) {
905 group = c->d & GroupMask;
906 c->modrm = insn_fetch(u8, 1, c->eip);
907 --c->eip;
908
909 group = (group << 3) + ((c->modrm >> 3) & 7);
910 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
911 c->d = group2_table[group];
912 else
913 c->d = group_table[group];
914 }
915
916 /* Unrecognised? */
917 if (c->d == 0) {
918 DPRINTF("Cannot emulate %02x\n", c->b);
919 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 }
921
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200922 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
923 c->op_bytes = 8;
924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926 if (c->d & ModRM)
927 rc = decode_modrm(ctxt, ops);
928 else if (c->d & MemAbs)
929 rc = decode_abs(ctxt, ops);
930 if (rc)
931 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300933 if (!c->has_seg_override)
934 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200935
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300936 if (!(!c->twobyte && c->b == 0x8d))
937 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200938
939 if (c->ad_bytes != 8)
940 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 /*
942 * Decode and fetch the source operand: register, memory
943 * or immediate.
944 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200945 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 case SrcNone:
947 break;
948 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200949 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 break;
951 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200952 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 goto srcmem_common;
954 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200955 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 goto srcmem_common;
957 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200958 c->src.bytes = (c->d & ByteOp) ? 1 :
959 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300960 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -0400961 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +0300962 break;
Mike Dayd77c26f2007-10-08 09:02:08 -0400963 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +0200964 /*
965 * For instructions with a ModR/M byte, switch to register
966 * access if Mod = 3.
967 */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200968 if ((c->d & ModRM) && c->modrm_mod == 3) {
969 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +0300970 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +0300971 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +0200972 break;
973 }
Laurent Viviere4e03de2007-09-18 11:52:50 +0200974 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 break;
976 case SrcImm:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200977 c->src.type = OP_IMM;
978 c->src.ptr = (unsigned long *)c->eip;
979 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
980 if (c->src.bytes == 8)
981 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200983 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200985 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 break;
987 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200988 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 break;
990 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200991 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 break;
993 }
994 break;
995 case SrcImmByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +0200996 c->src.type = OP_IMM;
997 c->src.ptr = (unsigned long *)c->eip;
998 c->src.bytes = 1;
999 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 break;
1001 }
1002
Avi Kivity038e51d2007-01-22 20:40:40 -08001003 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001004 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001005 case ImplicitOps:
1006 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001007 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001008 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001009 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001010 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001011 break;
1012 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001014 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001015 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001016 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001017 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001018 break;
1019 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001020 c->dst.type = OP_MEM;
1021 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001022 case DstAcc:
1023 c->dst.type = OP_REG;
1024 c->dst.bytes = c->op_bytes;
1025 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1026 switch (c->op_bytes) {
1027 case 1:
1028 c->dst.val = *(u8 *)c->dst.ptr;
1029 break;
1030 case 2:
1031 c->dst.val = *(u16 *)c->dst.ptr;
1032 break;
1033 case 4:
1034 c->dst.val = *(u32 *)c->dst.ptr;
1035 break;
1036 }
1037 c->dst.orig_val = c->dst.val;
1038 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001039 }
1040
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001041 if (c->rip_relative)
1042 c->modrm_ea += c->eip;
1043
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001044done:
1045 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1046}
1047
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001048static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1049{
1050 struct decode_cache *c = &ctxt->decode;
1051
1052 c->dst.type = OP_MEM;
1053 c->dst.bytes = c->op_bytes;
1054 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001055 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001056 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001057 c->regs[VCPU_REGS_RSP]);
1058}
1059
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001060static int emulate_pop(struct x86_emulate_ctxt *ctxt,
1061 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001062{
1063 struct decode_cache *c = &ctxt->decode;
1064 int rc;
1065
Avi Kivity781d0ed2008-11-27 18:00:28 +02001066 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1067 c->regs[VCPU_REGS_RSP]),
1068 &c->src.val, c->src.bytes, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001069 if (rc != 0)
1070 return rc;
1071
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001072 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.bytes);
1073 return rc;
1074}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001075
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001076static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1077 struct x86_emulate_ops *ops)
1078{
1079 struct decode_cache *c = &ctxt->decode;
1080 int rc;
1081
1082 c->src.bytes = c->dst.bytes;
1083 rc = emulate_pop(ctxt, ops);
1084 if (rc != 0)
1085 return rc;
1086 c->dst.val = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001087 return 0;
1088}
1089
Laurent Vivier05f086f2007-09-24 11:10:55 +02001090static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001091{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001092 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001093 switch (c->modrm_reg) {
1094 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001095 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096 break;
1097 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001098 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001099 break;
1100 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001101 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001102 break;
1103 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001104 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001105 break;
1106 case 4: /* sal/shl */
1107 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001108 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109 break;
1110 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001111 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001112 break;
1113 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001114 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115 break;
1116 }
1117}
1118
1119static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001120 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121{
1122 struct decode_cache *c = &ctxt->decode;
1123 int rc = 0;
1124
1125 switch (c->modrm_reg) {
1126 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001127 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001128 break;
1129 case 2: /* not */
1130 c->dst.val = ~c->dst.val;
1131 break;
1132 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001133 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134 break;
1135 default:
1136 DPRINTF("Cannot emulate %02x\n", c->b);
1137 rc = X86EMUL_UNHANDLEABLE;
1138 break;
1139 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001140 return rc;
1141}
1142
1143static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001144 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001145{
1146 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001147
1148 switch (c->modrm_reg) {
1149 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001150 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001151 break;
1152 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001153 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001154 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001155 case 2: /* call near abs */ {
1156 long int old_eip;
1157 old_eip = c->eip;
1158 c->eip = c->src.val;
1159 c->src.val = old_eip;
1160 emulate_push(ctxt);
1161 break;
1162 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001163 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001164 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001165 break;
1166 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001167 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001168 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 }
1170 return 0;
1171}
1172
1173static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1174 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001175 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001176{
1177 struct decode_cache *c = &ctxt->decode;
1178 u64 old, new;
1179 int rc;
1180
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001181 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001182 if (rc != 0)
1183 return rc;
1184
1185 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1186 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1187
1188 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1189 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001190 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001191
1192 } else {
1193 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1194 (u32) c->regs[VCPU_REGS_RBX];
1195
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001196 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 if (rc != 0)
1198 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001199 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200 }
1201 return 0;
1202}
1203
1204static inline int writeback(struct x86_emulate_ctxt *ctxt,
1205 struct x86_emulate_ops *ops)
1206{
1207 int rc;
1208 struct decode_cache *c = &ctxt->decode;
1209
1210 switch (c->dst.type) {
1211 case OP_REG:
1212 /* The 4-byte case *is* correct:
1213 * in 64-bit mode we zero-extend.
1214 */
1215 switch (c->dst.bytes) {
1216 case 1:
1217 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1218 break;
1219 case 2:
1220 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1221 break;
1222 case 4:
1223 *c->dst.ptr = (u32)c->dst.val;
1224 break; /* 64b: zero-ext */
1225 case 8:
1226 *c->dst.ptr = c->dst.val;
1227 break;
1228 }
1229 break;
1230 case OP_MEM:
1231 if (c->lock_prefix)
1232 rc = ops->cmpxchg_emulated(
1233 (unsigned long)c->dst.ptr,
1234 &c->dst.orig_val,
1235 &c->dst.val,
1236 c->dst.bytes,
1237 ctxt->vcpu);
1238 else
1239 rc = ops->write_emulated(
1240 (unsigned long)c->dst.ptr,
1241 &c->dst.val,
1242 c->dst.bytes,
1243 ctxt->vcpu);
1244 if (rc != 0)
1245 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001246 break;
1247 case OP_NONE:
1248 /* no writeback */
1249 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001250 default:
1251 break;
1252 }
1253 return 0;
1254}
1255
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001256int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001257x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001258{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001259 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001260 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001261 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001262 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001263 unsigned int port;
1264 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001265 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001266
Laurent Vivier34273182007-09-18 11:27:37 +02001267 /* Shadow copy of register state. Committed on successful emulation.
1268 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1269 * modify them.
1270 */
1271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001272 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001273 saved_eip = c->eip;
1274
Avi Kivityc7e75a32007-10-28 16:34:25 +02001275 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001276 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001277
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001278 if (c->rep_prefix && (c->d & String)) {
1279 /* All REP prefixes have the same first termination condition */
1280 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001281 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001282 goto done;
1283 }
1284 /* The second termination condition only applies for REPE
1285 * and REPNE. Test if the repeat string operation prefix is
1286 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1287 * corresponding termination condition according to:
1288 * - if REPE/REPZ and ZF = 0 then done
1289 * - if REPNE/REPNZ and ZF = 1 then done
1290 */
1291 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1292 (c->b == 0xae) || (c->b == 0xaf)) {
1293 if ((c->rep_prefix == REPE_PREFIX) &&
1294 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001295 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001296 goto done;
1297 }
1298 if ((c->rep_prefix == REPNE_PREFIX) &&
1299 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001300 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001301 goto done;
1302 }
1303 }
1304 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001305 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001306 }
1307
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001308 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001309 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001310 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001311 rc = ops->read_emulated((unsigned long)c->src.ptr,
1312 &c->src.val,
1313 c->src.bytes,
1314 ctxt->vcpu);
1315 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001316 goto done;
1317 c->src.orig_val = c->src.val;
1318 }
1319
1320 if ((c->d & DstMask) == ImplicitOps)
1321 goto special_insn;
1322
1323
1324 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001325 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001326 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1327 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001328 if (c->d & BitOp) {
1329 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001330
Laurent Viviere4e03de2007-09-18 11:52:50 +02001331 c->dst.ptr = (void *)c->dst.ptr +
1332 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001333 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001334 if (!(c->d & Mov) &&
1335 /* optimisation - avoid slow emulated read */
1336 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1337 &c->dst.val,
1338 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001339 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001340 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001341 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001342
Avi Kivity018a98d2007-11-27 19:30:56 +02001343special_insn:
1344
Laurent Viviere4e03de2007-09-18 11:52:50 +02001345 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 goto twobyte_insn;
1347
Laurent Viviere4e03de2007-09-18 11:52:50 +02001348 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 case 0x00 ... 0x05:
1350 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001351 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 break;
1353 case 0x08 ... 0x0d:
1354 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001355 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 break;
1357 case 0x10 ... 0x15:
1358 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001359 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 break;
1361 case 0x18 ... 0x1d:
1362 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001363 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001365 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001367 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 break;
1369 case 0x28 ... 0x2d:
1370 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001371 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 break;
1373 case 0x30 ... 0x35:
1374 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001375 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 break;
1377 case 0x38 ... 0x3d:
1378 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001379 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001381 case 0x40 ... 0x47: /* inc r16/r32 */
1382 emulate_1op("inc", c->dst, ctxt->eflags);
1383 break;
1384 case 0x48 ... 0x4f: /* dec r16/r32 */
1385 emulate_1op("dec", c->dst, ctxt->eflags);
1386 break;
1387 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001388 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001389 break;
1390 case 0x58 ... 0x5f: /* pop reg */
1391 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001392 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001393 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1394 c->op_bytes, ctxt->vcpu)) != 0)
1395 goto done;
1396
Harvey Harrison7a9572752008-02-19 07:40:41 -08001397 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001398 c->op_bytes);
1399 c->dst.type = OP_NONE; /* Disable writeback. */
1400 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001402 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001404 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001406 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001407 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001408 emulate_push(ctxt);
1409 break;
1410 case 0x6c: /* insb */
1411 case 0x6d: /* insw/insd */
1412 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1413 1,
1414 (c->d & ByteOp) ? 1 : c->op_bytes,
1415 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001416 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001417 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001418 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001419 c->regs[VCPU_REGS_RDI]),
1420 c->rep_prefix,
1421 c->regs[VCPU_REGS_RDX]) == 0) {
1422 c->eip = saved_eip;
1423 return -1;
1424 }
1425 return 0;
1426 case 0x6e: /* outsb */
1427 case 0x6f: /* outsw/outsd */
1428 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1429 0,
1430 (c->d & ByteOp) ? 1 : c->op_bytes,
1431 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001432 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001433 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001434 register_address(c,
1435 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001436 c->regs[VCPU_REGS_RSI]),
1437 c->rep_prefix,
1438 c->regs[VCPU_REGS_RDX]) == 0) {
1439 c->eip = saved_eip;
1440 return -1;
1441 }
1442 return 0;
1443 case 0x70 ... 0x7f: /* jcc (short) */ {
1444 int rel = insn_fetch(s8, 1, c->eip);
1445
1446 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001447 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001448 break;
1449 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001451 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452 case 0:
1453 goto add;
1454 case 1:
1455 goto or;
1456 case 2:
1457 goto adc;
1458 case 3:
1459 goto sbb;
1460 case 4:
1461 goto and;
1462 case 5:
1463 goto sub;
1464 case 6:
1465 goto xor;
1466 case 7:
1467 goto cmp;
1468 }
1469 break;
1470 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001471 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 break;
1473 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001474 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001476 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001478 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 break;
1480 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001481 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 break;
1483 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001484 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 break; /* 64b reg: zero-extend */
1486 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001487 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 break;
1489 }
1490 /*
1491 * Write back the memory destination with implicit LOCK
1492 * prefix.
1493 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001494 c->dst.val = c->src.val;
1495 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001498 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001499 case 0x8c: { /* mov r/m, sreg */
1500 struct kvm_segment segreg;
1501
1502 if (c->modrm_reg <= 5)
1503 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1504 else {
1505 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1506 c->modrm);
1507 goto cannot_emulate;
1508 }
1509 c->dst.val = segreg.selector;
1510 break;
1511 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001512 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001513 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001514 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001515 case 0x8e: { /* mov seg, r/m16 */
1516 uint16_t sel;
1517 int type_bits;
1518 int err;
1519
1520 sel = c->src.val;
1521 if (c->modrm_reg <= 5) {
1522 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1523 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1524 type_bits, c->modrm_reg);
1525 } else {
1526 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1527 c->modrm);
1528 goto cannot_emulate;
1529 }
1530
1531 if (err < 0)
1532 goto cannot_emulate;
1533
1534 c->dst.type = OP_NONE; /* Disable writeback. */
1535 break;
1536 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538 rc = emulate_grp1a(ctxt, ops);
1539 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001542 case 0x90: /* nop / xchg r8,rax */
1543 if (!(c->rex_prefix & 1)) { /* nop */
1544 c->dst.type = OP_NONE;
1545 break;
1546 }
1547 case 0x91 ... 0x97: /* xchg reg,rax */
1548 c->src.type = c->dst.type = OP_REG;
1549 c->src.bytes = c->dst.bytes = c->op_bytes;
1550 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1551 c->src.val = *(c->src.ptr);
1552 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001553 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001554 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001555 emulate_push(ctxt);
1556 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001557 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001558 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001559 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001560 case 0xa0 ... 0xa1: /* mov */
1561 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1562 c->dst.val = c->src.val;
1563 break;
1564 case 0xa2 ... 0xa3: /* mov */
1565 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001568 c->dst.type = OP_MEM;
1569 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001570 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001571 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001572 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001573 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001574 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001575 c->regs[VCPU_REGS_RSI]),
1576 &c->dst.val,
1577 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001579 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001580 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001581 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001582 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001583 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001584 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 break;
1586 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001587 c->src.type = OP_NONE; /* Disable writeback. */
1588 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001589 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001590 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001591 c->regs[VCPU_REGS_RSI]);
1592 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1593 &c->src.val,
1594 c->src.bytes,
1595 ctxt->vcpu)) != 0)
1596 goto done;
1597
1598 c->dst.type = OP_NONE; /* Disable writeback. */
1599 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001600 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001601 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001602 c->regs[VCPU_REGS_RDI]);
1603 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1604 &c->dst.val,
1605 c->dst.bytes,
1606 ctxt->vcpu)) != 0)
1607 goto done;
1608
1609 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1610
1611 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1612
Harvey Harrison7a9572752008-02-19 07:40:41 -08001613 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001614 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1615 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001616 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001617 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1618 : c->dst.bytes);
1619
1620 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001622 c->dst.type = OP_MEM;
1623 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001624 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001625 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001626 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001627 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001628 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001629 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001630 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 break;
1632 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001633 c->dst.type = OP_REG;
1634 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1635 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001636 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001637 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001638 c->regs[VCPU_REGS_RSI]),
1639 &c->dst.val,
1640 c->dst.bytes,
1641 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001643 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001644 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001645 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 break;
1647 case 0xae ... 0xaf: /* scas */
1648 DPRINTF("Urk! I don't handle SCAS.\n");
1649 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001650 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001651 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001652 case 0xc0 ... 0xc1:
1653 emulate_grp2(ctxt);
1654 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001655 case 0xc3: /* ret */
1656 c->dst.ptr = &c->eip;
1657 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001658 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1659 mov:
1660 c->dst.val = c->src.val;
1661 break;
1662 case 0xd0 ... 0xd1: /* Grp2 */
1663 c->src.val = 1;
1664 emulate_grp2(ctxt);
1665 break;
1666 case 0xd2 ... 0xd3: /* Grp2 */
1667 c->src.val = c->regs[VCPU_REGS_RCX];
1668 emulate_grp2(ctxt);
1669 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001670 case 0xe4: /* inb */
1671 case 0xe5: /* in */
1672 port = insn_fetch(u8, 1, c->eip);
1673 io_dir_in = 1;
1674 goto do_io;
1675 case 0xe6: /* outb */
1676 case 0xe7: /* out */
1677 port = insn_fetch(u8, 1, c->eip);
1678 io_dir_in = 0;
1679 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001680 case 0xe8: /* call (near) */ {
1681 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001683 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001684 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001685 break;
1686 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001687 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001688 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001689 default:
1690 DPRINTF("Call: Invalid op_bytes\n");
1691 goto cannot_emulate;
1692 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001693 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001694 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001695 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 emulate_push(ctxt);
1697 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001698 }
1699 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001700 goto jmp;
1701 case 0xea: /* jmp far */ {
1702 uint32_t eip;
1703 uint16_t sel;
1704
1705 switch (c->op_bytes) {
1706 case 2:
1707 eip = insn_fetch(u16, 2, c->eip);
1708 break;
1709 case 4:
1710 eip = insn_fetch(u32, 4, c->eip);
1711 break;
1712 default:
1713 DPRINTF("jmp far: Invalid op_bytes\n");
1714 goto cannot_emulate;
1715 }
1716 sel = insn_fetch(u16, 2, c->eip);
1717 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1718 DPRINTF("jmp far: Failed to load CS descriptor\n");
1719 goto cannot_emulate;
1720 }
1721
1722 c->eip = eip;
1723 break;
1724 }
1725 case 0xeb:
1726 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001727 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001728 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001729 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001730 case 0xec: /* in al,dx */
1731 case 0xed: /* in (e/r)ax,dx */
1732 port = c->regs[VCPU_REGS_RDX];
1733 io_dir_in = 1;
1734 goto do_io;
1735 case 0xee: /* out al,dx */
1736 case 0xef: /* out (e/r)ax,dx */
1737 port = c->regs[VCPU_REGS_RDX];
1738 io_dir_in = 0;
1739 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1740 (c->d & ByteOp) ? 1 : c->op_bytes,
1741 port) != 0) {
1742 c->eip = saved_eip;
1743 goto cannot_emulate;
1744 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001745 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001746 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001747 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001748 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001749 case 0xf5: /* cmc */
1750 /* complement carry flag from eflags reg */
1751 ctxt->eflags ^= EFLG_CF;
1752 c->dst.type = OP_NONE; /* Disable writeback. */
1753 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001754 case 0xf6 ... 0xf7: /* Grp3 */
1755 rc = emulate_grp3(ctxt, ops);
1756 if (rc != 0)
1757 goto done;
1758 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001759 case 0xf8: /* clc */
1760 ctxt->eflags &= ~EFLG_CF;
1761 c->dst.type = OP_NONE; /* Disable writeback. */
1762 break;
1763 case 0xfa: /* cli */
1764 ctxt->eflags &= ~X86_EFLAGS_IF;
1765 c->dst.type = OP_NONE; /* Disable writeback. */
1766 break;
1767 case 0xfb: /* sti */
1768 ctxt->eflags |= X86_EFLAGS_IF;
1769 c->dst.type = OP_NONE; /* Disable writeback. */
1770 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001771 case 0xfc: /* cld */
1772 ctxt->eflags &= ~EFLG_DF;
1773 c->dst.type = OP_NONE; /* Disable writeback. */
1774 break;
1775 case 0xfd: /* std */
1776 ctxt->eflags |= EFLG_DF;
1777 c->dst.type = OP_NONE; /* Disable writeback. */
1778 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001779 case 0xfe ... 0xff: /* Grp4/Grp5 */
1780 rc = emulate_grp45(ctxt, ops);
1781 if (rc != 0)
1782 goto done;
1783 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001785
1786writeback:
1787 rc = writeback(ctxt, ops);
1788 if (rc != 0)
1789 goto done;
1790
1791 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001792 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001793 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001794
1795done:
1796 if (rc == X86EMUL_UNHANDLEABLE) {
1797 c->eip = saved_eip;
1798 return -1;
1799 }
1800 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801
1802twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001803 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001805 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 u16 size;
1807 unsigned long address;
1808
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001809 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001810 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001811 goto cannot_emulate;
1812
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001813 rc = kvm_fix_hypercall(ctxt->vcpu);
1814 if (rc)
1815 goto done;
1816
Avi Kivity33e38852008-05-21 15:34:25 +03001817 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001818 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001819 /* Disable writeback. */
1820 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001821 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001823 rc = read_descriptor(ctxt, ops, c->src.ptr,
1824 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001825 if (rc)
1826 goto done;
1827 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001828 /* Disable writeback. */
1829 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001831 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001832 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001833 rc = kvm_fix_hypercall(ctxt->vcpu);
1834 if (rc)
1835 goto done;
1836 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001837 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001838 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001839 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001840 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001841 if (rc)
1842 goto done;
1843 realmode_lidt(ctxt->vcpu, size, address);
1844 }
Avi Kivity16286d02008-04-14 14:40:50 +03001845 /* Disable writeback. */
1846 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 break;
1848 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001849 c->dst.bytes = 2;
1850 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 break;
1852 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001853 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1854 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001855 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 break;
1857 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001858 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001859 /* Disable writeback. */
1860 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 break;
1862 default:
1863 goto cannot_emulate;
1864 }
1865 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001866 case 0x06:
1867 emulate_clts(ctxt->vcpu);
1868 c->dst.type = OP_NONE;
1869 break;
1870 case 0x08: /* invd */
1871 case 0x09: /* wbinvd */
1872 case 0x0d: /* GrpP (prefetch) */
1873 case 0x18: /* Grp16 (prefetch/nop) */
1874 c->dst.type = OP_NONE;
1875 break;
1876 case 0x20: /* mov cr, reg */
1877 if (c->modrm_mod != 3)
1878 goto cannot_emulate;
1879 c->regs[c->modrm_rm] =
1880 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1881 c->dst.type = OP_NONE; /* no writeback */
1882 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001884 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001887 if (rc)
1888 goto cannot_emulate;
1889 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001891 case 0x22: /* mov reg, cr */
1892 if (c->modrm_mod != 3)
1893 goto cannot_emulate;
1894 realmode_set_cr(ctxt->vcpu,
1895 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1896 c->dst.type = OP_NONE;
1897 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001899 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001901 rc = emulator_set_dr(ctxt, c->modrm_reg,
1902 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001903 if (rc)
1904 goto cannot_emulate;
1905 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001907 case 0x30:
1908 /* wrmsr */
1909 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1910 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1911 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1912 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001913 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001914 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001915 }
1916 rc = X86EMUL_CONTINUE;
1917 c->dst.type = OP_NONE;
1918 break;
1919 case 0x32:
1920 /* rdmsr */
1921 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1922 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001923 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001924 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001925 } else {
1926 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1927 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1928 }
1929 rc = X86EMUL_CONTINUE;
1930 c->dst.type = OP_NONE;
1931 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001933 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001934 if (!test_cc(c->b, ctxt->eflags))
1935 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001937 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1938 long int rel;
1939
1940 switch (c->op_bytes) {
1941 case 2:
1942 rel = insn_fetch(s16, 2, c->eip);
1943 break;
1944 case 4:
1945 rel = insn_fetch(s32, 4, c->eip);
1946 break;
1947 case 8:
1948 rel = insn_fetch(s64, 8, c->eip);
1949 break;
1950 default:
1951 DPRINTF("jnz: Invalid op_bytes\n");
1952 goto cannot_emulate;
1953 }
1954 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001955 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001956 c->dst.type = OP_NONE;
1957 break;
1958 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001959 case 0xa3:
1960 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001961 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001962 /* only subword offset */
1963 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001964 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001965 break;
1966 case 0xab:
1967 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001968 /* only subword offset */
1969 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001970 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001971 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03001972 case 0xae: /* clflush */
1973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 case 0xb0 ... 0xb1: /* cmpxchg */
1975 /*
1976 * Save real source value, then compare EAX against
1977 * destination.
1978 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001979 c->src.orig_val = c->src.val;
1980 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001981 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1982 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001984 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 } else {
1986 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001987 c->dst.type = OP_REG;
1988 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 }
1990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 case 0xb3:
1992 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001993 /* only subword offset */
1994 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001995 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001998 c->dst.bytes = c->op_bytes;
1999 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2000 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002003 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 case 0:
2005 goto bt;
2006 case 1:
2007 goto bts;
2008 case 2:
2009 goto btr;
2010 case 3:
2011 goto btc;
2012 }
2013 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002014 case 0xbb:
2015 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002016 /* only subword offset */
2017 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002018 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002019 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002021 c->dst.bytes = c->op_bytes;
2022 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2023 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002025 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002026 c->dst.bytes = c->op_bytes;
2027 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2028 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002029 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002031 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002032 if (rc != 0)
2033 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002034 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002035 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036 }
2037 goto writeback;
2038
2039cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002040 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002041 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042 return -1;
2043}