blob: a11af6f74d68c273870f27620371a88fd3662f99 [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
1060static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1061 struct x86_emulate_ops *ops)
1062{
1063 struct decode_cache *c = &ctxt->decode;
1064 int rc;
1065
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001066 rc = ops->read_std(register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001067 c->regs[VCPU_REGS_RSP]),
1068 &c->dst.val, c->dst.bytes, ctxt->vcpu);
1069 if (rc != 0)
1070 return rc;
1071
Harvey Harrison7a9572752008-02-19 07:40:41 -08001072 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001073
1074 return 0;
1075}
1076
Laurent Vivier05f086f2007-09-24 11:10:55 +02001077static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001078{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001079 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001080 switch (c->modrm_reg) {
1081 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001082 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001083 break;
1084 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001085 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001086 break;
1087 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001088 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001089 break;
1090 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001091 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001092 break;
1093 case 4: /* sal/shl */
1094 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001095 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096 break;
1097 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001098 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001099 break;
1100 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001101 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001102 break;
1103 }
1104}
1105
1106static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001107 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001108{
1109 struct decode_cache *c = &ctxt->decode;
1110 int rc = 0;
1111
1112 switch (c->modrm_reg) {
1113 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001114 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001115 break;
1116 case 2: /* not */
1117 c->dst.val = ~c->dst.val;
1118 break;
1119 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001120 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121 break;
1122 default:
1123 DPRINTF("Cannot emulate %02x\n", c->b);
1124 rc = X86EMUL_UNHANDLEABLE;
1125 break;
1126 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001127 return rc;
1128}
1129
1130static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001131 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001132{
1133 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001134
1135 switch (c->modrm_reg) {
1136 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001137 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001138 break;
1139 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001140 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001141 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001142 case 2: /* call near abs */ {
1143 long int old_eip;
1144 old_eip = c->eip;
1145 c->eip = c->src.val;
1146 c->src.val = old_eip;
1147 emulate_push(ctxt);
1148 break;
1149 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001151 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001152 break;
1153 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001154 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001156 }
1157 return 0;
1158}
1159
1160static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1161 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001162 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001163{
1164 struct decode_cache *c = &ctxt->decode;
1165 u64 old, new;
1166 int rc;
1167
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001168 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 if (rc != 0)
1170 return rc;
1171
1172 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1173 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1174
1175 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1176 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001177 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001178
1179 } else {
1180 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1181 (u32) c->regs[VCPU_REGS_RBX];
1182
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001183 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001184 if (rc != 0)
1185 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001186 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187 }
1188 return 0;
1189}
1190
1191static inline int writeback(struct x86_emulate_ctxt *ctxt,
1192 struct x86_emulate_ops *ops)
1193{
1194 int rc;
1195 struct decode_cache *c = &ctxt->decode;
1196
1197 switch (c->dst.type) {
1198 case OP_REG:
1199 /* The 4-byte case *is* correct:
1200 * in 64-bit mode we zero-extend.
1201 */
1202 switch (c->dst.bytes) {
1203 case 1:
1204 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1205 break;
1206 case 2:
1207 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1208 break;
1209 case 4:
1210 *c->dst.ptr = (u32)c->dst.val;
1211 break; /* 64b: zero-ext */
1212 case 8:
1213 *c->dst.ptr = c->dst.val;
1214 break;
1215 }
1216 break;
1217 case OP_MEM:
1218 if (c->lock_prefix)
1219 rc = ops->cmpxchg_emulated(
1220 (unsigned long)c->dst.ptr,
1221 &c->dst.orig_val,
1222 &c->dst.val,
1223 c->dst.bytes,
1224 ctxt->vcpu);
1225 else
1226 rc = ops->write_emulated(
1227 (unsigned long)c->dst.ptr,
1228 &c->dst.val,
1229 c->dst.bytes,
1230 ctxt->vcpu);
1231 if (rc != 0)
1232 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001233 break;
1234 case OP_NONE:
1235 /* no writeback */
1236 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001237 default:
1238 break;
1239 }
1240 return 0;
1241}
1242
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001243int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001244x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001245{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001246 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001247 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001248 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001249 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001250 unsigned int port;
1251 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001252 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001253
Laurent Vivier34273182007-09-18 11:27:37 +02001254 /* Shadow copy of register state. Committed on successful emulation.
1255 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1256 * modify them.
1257 */
1258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001260 saved_eip = c->eip;
1261
Avi Kivityc7e75a32007-10-28 16:34:25 +02001262 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001263 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001264
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001265 if (c->rep_prefix && (c->d & String)) {
1266 /* All REP prefixes have the same first termination condition */
1267 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001268 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001269 goto done;
1270 }
1271 /* The second termination condition only applies for REPE
1272 * and REPNE. Test if the repeat string operation prefix is
1273 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1274 * corresponding termination condition according to:
1275 * - if REPE/REPZ and ZF = 0 then done
1276 * - if REPNE/REPNZ and ZF = 1 then done
1277 */
1278 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1279 (c->b == 0xae) || (c->b == 0xaf)) {
1280 if ((c->rep_prefix == REPE_PREFIX) &&
1281 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001282 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001283 goto done;
1284 }
1285 if ((c->rep_prefix == REPNE_PREFIX) &&
1286 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001287 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001288 goto done;
1289 }
1290 }
1291 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001292 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001293 }
1294
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001295 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001296 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001297 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001298 rc = ops->read_emulated((unsigned long)c->src.ptr,
1299 &c->src.val,
1300 c->src.bytes,
1301 ctxt->vcpu);
1302 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001303 goto done;
1304 c->src.orig_val = c->src.val;
1305 }
1306
1307 if ((c->d & DstMask) == ImplicitOps)
1308 goto special_insn;
1309
1310
1311 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001312 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001313 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1314 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001315 if (c->d & BitOp) {
1316 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001317
Laurent Viviere4e03de2007-09-18 11:52:50 +02001318 c->dst.ptr = (void *)c->dst.ptr +
1319 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001320 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001321 if (!(c->d & Mov) &&
1322 /* optimisation - avoid slow emulated read */
1323 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1324 &c->dst.val,
1325 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001326 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001327 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001328 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001329
Avi Kivity018a98d2007-11-27 19:30:56 +02001330special_insn:
1331
Laurent Viviere4e03de2007-09-18 11:52:50 +02001332 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 goto twobyte_insn;
1334
Laurent Viviere4e03de2007-09-18 11:52:50 +02001335 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 case 0x00 ... 0x05:
1337 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001338 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 break;
1340 case 0x08 ... 0x0d:
1341 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001342 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 break;
1344 case 0x10 ... 0x15:
1345 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001346 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 break;
1348 case 0x18 ... 0x1d:
1349 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001350 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001352 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001354 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 break;
1356 case 0x28 ... 0x2d:
1357 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001358 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 break;
1360 case 0x30 ... 0x35:
1361 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001362 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 break;
1364 case 0x38 ... 0x3d:
1365 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001366 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001368 case 0x40 ... 0x47: /* inc r16/r32 */
1369 emulate_1op("inc", c->dst, ctxt->eflags);
1370 break;
1371 case 0x48 ... 0x4f: /* dec r16/r32 */
1372 emulate_1op("dec", c->dst, ctxt->eflags);
1373 break;
1374 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001375 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001376 break;
1377 case 0x58 ... 0x5f: /* pop reg */
1378 pop_instruction:
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001379 if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
Avi Kivity33615aa2007-10-31 11:15:56 +02001380 c->regs[VCPU_REGS_RSP]), c->dst.ptr,
1381 c->op_bytes, ctxt->vcpu)) != 0)
1382 goto done;
1383
Harvey Harrison7a9572752008-02-19 07:40:41 -08001384 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
Avi Kivity33615aa2007-10-31 11:15:56 +02001385 c->op_bytes);
1386 c->dst.type = OP_NONE; /* Disable writeback. */
1387 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001389 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001391 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001393 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001394 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001395 emulate_push(ctxt);
1396 break;
1397 case 0x6c: /* insb */
1398 case 0x6d: /* insw/insd */
1399 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1400 1,
1401 (c->d & ByteOp) ? 1 : c->op_bytes,
1402 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001403 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001404 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001405 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001406 c->regs[VCPU_REGS_RDI]),
1407 c->rep_prefix,
1408 c->regs[VCPU_REGS_RDX]) == 0) {
1409 c->eip = saved_eip;
1410 return -1;
1411 }
1412 return 0;
1413 case 0x6e: /* outsb */
1414 case 0x6f: /* outsw/outsd */
1415 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1416 0,
1417 (c->d & ByteOp) ? 1 : c->op_bytes,
1418 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001419 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001420 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001421 register_address(c,
1422 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001423 c->regs[VCPU_REGS_RSI]),
1424 c->rep_prefix,
1425 c->regs[VCPU_REGS_RDX]) == 0) {
1426 c->eip = saved_eip;
1427 return -1;
1428 }
1429 return 0;
1430 case 0x70 ... 0x7f: /* jcc (short) */ {
1431 int rel = insn_fetch(s8, 1, c->eip);
1432
1433 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001434 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001435 break;
1436 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001438 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 case 0:
1440 goto add;
1441 case 1:
1442 goto or;
1443 case 2:
1444 goto adc;
1445 case 3:
1446 goto sbb;
1447 case 4:
1448 goto and;
1449 case 5:
1450 goto sub;
1451 case 6:
1452 goto xor;
1453 case 7:
1454 goto cmp;
1455 }
1456 break;
1457 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001458 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 break;
1460 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001461 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001463 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001465 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 break;
1467 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001468 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 break;
1470 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001471 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 break; /* 64b reg: zero-extend */
1473 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001474 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 break;
1476 }
1477 /*
1478 * Write back the memory destination with implicit LOCK
1479 * prefix.
1480 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001481 c->dst.val = c->src.val;
1482 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001485 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001486 case 0x8c: { /* mov r/m, sreg */
1487 struct kvm_segment segreg;
1488
1489 if (c->modrm_reg <= 5)
1490 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1491 else {
1492 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1493 c->modrm);
1494 goto cannot_emulate;
1495 }
1496 c->dst.val = segreg.selector;
1497 break;
1498 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001499 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001500 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001501 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001502 case 0x8e: { /* mov seg, r/m16 */
1503 uint16_t sel;
1504 int type_bits;
1505 int err;
1506
1507 sel = c->src.val;
1508 if (c->modrm_reg <= 5) {
1509 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1510 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1511 type_bits, c->modrm_reg);
1512 } else {
1513 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1514 c->modrm);
1515 goto cannot_emulate;
1516 }
1517
1518 if (err < 0)
1519 goto cannot_emulate;
1520
1521 c->dst.type = OP_NONE; /* Disable writeback. */
1522 break;
1523 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001525 rc = emulate_grp1a(ctxt, ops);
1526 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001529 case 0x90: /* nop / xchg r8,rax */
1530 if (!(c->rex_prefix & 1)) { /* nop */
1531 c->dst.type = OP_NONE;
1532 break;
1533 }
1534 case 0x91 ... 0x97: /* xchg reg,rax */
1535 c->src.type = c->dst.type = OP_REG;
1536 c->src.bytes = c->dst.bytes = c->op_bytes;
1537 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1538 c->src.val = *(c->src.ptr);
1539 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001540 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001541 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001542 emulate_push(ctxt);
1543 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001544 case 0x9d: /* popf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001545 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001546 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001547 case 0xa0 ... 0xa1: /* mov */
1548 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1549 c->dst.val = c->src.val;
1550 break;
1551 case 0xa2 ... 0xa3: /* mov */
1552 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1553 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001555 c->dst.type = OP_MEM;
1556 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001557 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001558 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001559 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001560 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001561 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001562 c->regs[VCPU_REGS_RSI]),
1563 &c->dst.val,
1564 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001566 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001567 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001568 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001569 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001570 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001571 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572 break;
1573 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001574 c->src.type = OP_NONE; /* Disable writeback. */
1575 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001576 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001577 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001578 c->regs[VCPU_REGS_RSI]);
1579 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1580 &c->src.val,
1581 c->src.bytes,
1582 ctxt->vcpu)) != 0)
1583 goto done;
1584
1585 c->dst.type = OP_NONE; /* Disable writeback. */
1586 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001587 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001588 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001589 c->regs[VCPU_REGS_RDI]);
1590 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1591 &c->dst.val,
1592 c->dst.bytes,
1593 ctxt->vcpu)) != 0)
1594 goto done;
1595
1596 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1597
1598 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1599
Harvey Harrison7a9572752008-02-19 07:40:41 -08001600 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001601 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1602 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001603 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001604 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1605 : c->dst.bytes);
1606
1607 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001609 c->dst.type = OP_MEM;
1610 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001611 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001612 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001613 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001614 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001615 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001616 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001617 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 break;
1619 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001620 c->dst.type = OP_REG;
1621 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1622 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001623 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001624 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001625 c->regs[VCPU_REGS_RSI]),
1626 &c->dst.val,
1627 c->dst.bytes,
1628 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001630 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001631 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001632 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 break;
1634 case 0xae ... 0xaf: /* scas */
1635 DPRINTF("Urk! I don't handle SCAS.\n");
1636 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03001637 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02001638 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02001639 case 0xc0 ... 0xc1:
1640 emulate_grp2(ctxt);
1641 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001642 case 0xc3: /* ret */
1643 c->dst.ptr = &c->eip;
1644 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001645 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
1646 mov:
1647 c->dst.val = c->src.val;
1648 break;
1649 case 0xd0 ... 0xd1: /* Grp2 */
1650 c->src.val = 1;
1651 emulate_grp2(ctxt);
1652 break;
1653 case 0xd2 ... 0xd3: /* Grp2 */
1654 c->src.val = c->regs[VCPU_REGS_RCX];
1655 emulate_grp2(ctxt);
1656 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001657 case 0xe4: /* inb */
1658 case 0xe5: /* in */
1659 port = insn_fetch(u8, 1, c->eip);
1660 io_dir_in = 1;
1661 goto do_io;
1662 case 0xe6: /* outb */
1663 case 0xe7: /* out */
1664 port = insn_fetch(u8, 1, c->eip);
1665 io_dir_in = 0;
1666 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001667 case 0xe8: /* call (near) */ {
1668 long int rel;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001669 switch (c->op_bytes) {
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001670 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001671 rel = insn_fetch(s16, 2, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001672 break;
1673 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001674 rel = insn_fetch(s32, 4, c->eip);
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001675 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001676 default:
1677 DPRINTF("Call: Invalid op_bytes\n");
1678 goto cannot_emulate;
1679 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001680 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001681 jmp_rel(c, rel);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001682 c->op_bytes = c->ad_bytes;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 emulate_push(ctxt);
1684 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001685 }
1686 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02001687 goto jmp;
1688 case 0xea: /* jmp far */ {
1689 uint32_t eip;
1690 uint16_t sel;
1691
1692 switch (c->op_bytes) {
1693 case 2:
1694 eip = insn_fetch(u16, 2, c->eip);
1695 break;
1696 case 4:
1697 eip = insn_fetch(u32, 4, c->eip);
1698 break;
1699 default:
1700 DPRINTF("jmp far: Invalid op_bytes\n");
1701 goto cannot_emulate;
1702 }
1703 sel = insn_fetch(u16, 2, c->eip);
1704 if (kvm_load_segment_descriptor(ctxt->vcpu, sel, 9, VCPU_SREG_CS) < 0) {
1705 DPRINTF("jmp far: Failed to load CS descriptor\n");
1706 goto cannot_emulate;
1707 }
1708
1709 c->eip = eip;
1710 break;
1711 }
1712 case 0xeb:
1713 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08001714 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001715 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07001716 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001717 case 0xec: /* in al,dx */
1718 case 0xed: /* in (e/r)ax,dx */
1719 port = c->regs[VCPU_REGS_RDX];
1720 io_dir_in = 1;
1721 goto do_io;
1722 case 0xee: /* out al,dx */
1723 case 0xef: /* out (e/r)ax,dx */
1724 port = c->regs[VCPU_REGS_RDX];
1725 io_dir_in = 0;
1726 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
1727 (c->d & ByteOp) ? 1 : c->op_bytes,
1728 port) != 0) {
1729 c->eip = saved_eip;
1730 goto cannot_emulate;
1731 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001732 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001733 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001734 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03001735 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001736 case 0xf5: /* cmc */
1737 /* complement carry flag from eflags reg */
1738 ctxt->eflags ^= EFLG_CF;
1739 c->dst.type = OP_NONE; /* Disable writeback. */
1740 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001741 case 0xf6 ... 0xf7: /* Grp3 */
1742 rc = emulate_grp3(ctxt, ops);
1743 if (rc != 0)
1744 goto done;
1745 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02001746 case 0xf8: /* clc */
1747 ctxt->eflags &= ~EFLG_CF;
1748 c->dst.type = OP_NONE; /* Disable writeback. */
1749 break;
1750 case 0xfa: /* cli */
1751 ctxt->eflags &= ~X86_EFLAGS_IF;
1752 c->dst.type = OP_NONE; /* Disable writeback. */
1753 break;
1754 case 0xfb: /* sti */
1755 ctxt->eflags |= X86_EFLAGS_IF;
1756 c->dst.type = OP_NONE; /* Disable writeback. */
1757 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03001758 case 0xfc: /* cld */
1759 ctxt->eflags &= ~EFLG_DF;
1760 c->dst.type = OP_NONE; /* Disable writeback. */
1761 break;
1762 case 0xfd: /* std */
1763 ctxt->eflags |= EFLG_DF;
1764 c->dst.type = OP_NONE; /* Disable writeback. */
1765 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001766 case 0xfe ... 0xff: /* Grp4/Grp5 */
1767 rc = emulate_grp45(ctxt, ops);
1768 if (rc != 0)
1769 goto done;
1770 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 }
Avi Kivity018a98d2007-11-27 19:30:56 +02001772
1773writeback:
1774 rc = writeback(ctxt, ops);
1775 if (rc != 0)
1776 goto done;
1777
1778 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001779 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001780 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02001781
1782done:
1783 if (rc == X86EMUL_UNHANDLEABLE) {
1784 c->eip = saved_eip;
1785 return -1;
1786 }
1787 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788
1789twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001790 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001792 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 u16 size;
1794 unsigned long address;
1795
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001796 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001797 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001798 goto cannot_emulate;
1799
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001800 rc = kvm_fix_hypercall(ctxt->vcpu);
1801 if (rc)
1802 goto done;
1803
Avi Kivity33e38852008-05-21 15:34:25 +03001804 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001805 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03001806 /* Disable writeback. */
1807 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001808 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001810 rc = read_descriptor(ctxt, ops, c->src.ptr,
1811 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812 if (rc)
1813 goto done;
1814 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03001815 /* Disable writeback. */
1816 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001818 case 3: /* lidt/vmmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001819 if (c->modrm_mod == 3 && c->modrm_rm == 1) {
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001820 rc = kvm_fix_hypercall(ctxt->vcpu);
1821 if (rc)
1822 goto done;
1823 kvm_emulate_hypercall(ctxt->vcpu);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001824 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001825 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001826 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02001827 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05001828 if (rc)
1829 goto done;
1830 realmode_lidt(ctxt->vcpu, size, address);
1831 }
Avi Kivity16286d02008-04-14 14:40:50 +03001832 /* Disable writeback. */
1833 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 break;
1835 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001836 c->dst.bytes = 2;
1837 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 break;
1839 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03001840 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
1841 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03001842 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 break;
1844 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001845 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03001846 /* Disable writeback. */
1847 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 break;
1849 default:
1850 goto cannot_emulate;
1851 }
1852 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001853 case 0x06:
1854 emulate_clts(ctxt->vcpu);
1855 c->dst.type = OP_NONE;
1856 break;
1857 case 0x08: /* invd */
1858 case 0x09: /* wbinvd */
1859 case 0x0d: /* GrpP (prefetch) */
1860 case 0x18: /* Grp16 (prefetch/nop) */
1861 c->dst.type = OP_NONE;
1862 break;
1863 case 0x20: /* mov cr, reg */
1864 if (c->modrm_mod != 3)
1865 goto cannot_emulate;
1866 c->regs[c->modrm_rm] =
1867 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
1868 c->dst.type = OP_NONE; /* no writeback */
1869 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001871 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001874 if (rc)
1875 goto cannot_emulate;
1876 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001878 case 0x22: /* mov reg, cr */
1879 if (c->modrm_mod != 3)
1880 goto cannot_emulate;
1881 realmode_set_cr(ctxt->vcpu,
1882 c->modrm_reg, c->modrm_val, &ctxt->eflags);
1883 c->dst.type = OP_NONE;
1884 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001886 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001888 rc = emulator_set_dr(ctxt, c->modrm_reg,
1889 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02001890 if (rc)
1891 goto cannot_emulate;
1892 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001894 case 0x30:
1895 /* wrmsr */
1896 msr_data = (u32)c->regs[VCPU_REGS_RAX]
1897 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
1898 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
1899 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001900 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001901 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001902 }
1903 rc = X86EMUL_CONTINUE;
1904 c->dst.type = OP_NONE;
1905 break;
1906 case 0x32:
1907 /* rdmsr */
1908 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
1909 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001910 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001911 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02001912 } else {
1913 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
1914 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
1915 }
1916 rc = X86EMUL_CONTINUE;
1917 c->dst.type = OP_NONE;
1918 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001920 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001921 if (!test_cc(c->b, ctxt->eflags))
1922 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02001924 case 0x80 ... 0x8f: /* jnz rel, etc*/ {
1925 long int rel;
1926
1927 switch (c->op_bytes) {
1928 case 2:
1929 rel = insn_fetch(s16, 2, c->eip);
1930 break;
1931 case 4:
1932 rel = insn_fetch(s32, 4, c->eip);
1933 break;
1934 case 8:
1935 rel = insn_fetch(s64, 8, c->eip);
1936 break;
1937 default:
1938 DPRINTF("jnz: Invalid op_bytes\n");
1939 goto cannot_emulate;
1940 }
1941 if (test_cc(c->b, ctxt->eflags))
Harvey Harrison7a9572752008-02-19 07:40:41 -08001942 jmp_rel(c, rel);
Avi Kivity018a98d2007-11-27 19:30:56 +02001943 c->dst.type = OP_NONE;
1944 break;
1945 }
Nitin A Kamble7de75242007-09-15 10:13:07 +03001946 case 0xa3:
1947 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08001948 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001949 /* only subword offset */
1950 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001951 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001952 break;
1953 case 0xab:
1954 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001955 /* only subword offset */
1956 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001957 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03001958 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03001959 case 0xae: /* clflush */
1960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 case 0xb0 ... 0xb1: /* cmpxchg */
1962 /*
1963 * Save real source value, then compare EAX against
1964 * destination.
1965 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001966 c->src.orig_val = c->src.val;
1967 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02001968 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1969 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001971 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 } else {
1973 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001974 c->dst.type = OP_REG;
1975 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 }
1977 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 case 0xb3:
1979 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001980 /* only subword offset */
1981 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001982 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001985 c->dst.bytes = c->op_bytes;
1986 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
1987 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001990 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 case 0:
1992 goto bt;
1993 case 1:
1994 goto bts;
1995 case 2:
1996 goto btr;
1997 case 3:
1998 goto btc;
1999 }
2000 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002001 case 0xbb:
2002 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002003 /* only subword offset */
2004 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002005 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002006 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002008 c->dst.bytes = c->op_bytes;
2009 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2010 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002012 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002013 c->dst.bytes = c->op_bytes;
2014 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2015 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002016 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002018 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002019 if (rc != 0)
2020 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002021 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 }
2024 goto writeback;
2025
2026cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002027 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002028 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029 return -1;
2030}