blob: c6663d46f328b4deafbe6f535741e0bb474aaa7e [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
Andre Przywarae99f0502009-06-17 15:50:33 +020035#include "mmu.h" /* for is_long_mode() */
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037/*
38 * Opcode effective-address decode tables.
39 * Note that we only emulate instructions that have at least one memory
40 * operand (excluding implicit stack references). We assume that stack
41 * references and instruction fetches will never occur in special memory
42 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
43 * not be handled.
44 */
45
46/* Operand sizes: 8-bit operands or specified/overridden size. */
47#define ByteOp (1<<0) /* 8-bit operands. */
48/* Destination operand type. */
49#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
50#define DstReg (2<<1) /* Register operand. */
51#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define DstAcc (4<<1) /* Destination Accumulator */
53#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080054/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020055#define SrcNone (0<<4) /* No source operand. */
56#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
57#define SrcReg (1<<4) /* Register operand. */
58#define SrcMem (2<<4) /* Memory operand. */
59#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
60#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
61#define SrcImm (5<<4) /* Immediate operand. */
62#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010063#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030064#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030065#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapov341de7e2009-04-12 13:36:41 +030066#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030070#define Mov (1<<9)
71#define BitOp (1<<10)
72#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020073#define String (1<<12) /* String instruction (rep capable) */
74#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020075#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
76#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
77#define GroupMask 0xff /* Group number stored in bits 0:7 */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010078/* Source 2 operand type */
79#define Src2None (0<<29)
80#define Src2CL (1<<29)
81#define Src2ImmByte (2<<29)
82#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030083#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010084#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivity43bb19c2008-01-18 12:46:50 +020086enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020087 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020088 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020089};
90
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010091static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 /* 0x00 - 0x07 */
93 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
94 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin291fd392008-10-20 13:11:58 +020095 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080096 /* 0x08 - 0x0F */
97 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
98 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
99 0, 0, 0, 0,
100 /* 0x10 - 0x17 */
101 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
103 0, 0, 0, 0,
104 /* 0x18 - 0x1F */
105 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
106 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
107 0, 0, 0, 0,
108 /* 0x20 - 0x27 */
109 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200111 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 /* 0x28 - 0x2F */
113 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
114 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
115 0, 0, 0, 0,
116 /* 0x30 - 0x37 */
117 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
118 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
119 0, 0, 0, 0,
120 /* 0x38 - 0x3F */
121 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
122 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200123 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
124 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700125 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200126 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700127 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200128 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300129 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200130 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
131 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300132 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200133 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
134 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700135 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700137 0, 0, 0, 0,
138 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300139 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300140 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
141 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300142 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300143 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
144 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300145 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300146 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
147 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200149 Group | Group1_80, Group | Group1_81,
150 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
152 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
153 /* 0x88 - 0x8F */
154 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
155 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200156 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200157 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300158 /* 0x90 - 0x97 */
159 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
160 /* 0x98 - 0x9F */
Gleb Natapov06541692009-04-12 13:36:20 +0300161 0, 0, SrcImm | Src2Imm16, 0,
162 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200164 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
165 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200166 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
167 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200169 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
170 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
171 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300172 /* 0xB0 - 0xB7 */
173 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
174 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
175 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
176 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
177 /* 0xB8 - 0xBF */
178 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
179 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
180 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
181 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300183 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200184 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300185 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300187 0, 0, 0, ImplicitOps | Stack,
188 ImplicitOps, SrcImmByte, ImplicitOps, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189 /* 0xD0 - 0xD7 */
190 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
191 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
192 0, 0, 0, 0,
193 /* 0xD8 - 0xDF */
194 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300195 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300196 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300197 ByteOp | SrcImmUByte, SrcImmUByte,
198 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300199 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300200 SrcImm | Stack, SrcImm | ImplicitOps,
Avi Kivityee3d29e2009-05-18 16:15:20 +0300201 SrcImmU | Src2Imm16, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300202 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
203 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204 /* 0xF0 - 0xF7 */
205 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200206 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700208 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300209 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210};
211
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100212static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213 /* 0x00 - 0x0F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200214 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200215 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216 /* 0x10 - 0x1F */
217 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
218 /* 0x20 - 0x2F */
219 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0,
221 /* 0x30 - 0x3F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200222 ImplicitOps, 0, ImplicitOps, 0,
223 ImplicitOps, ImplicitOps, 0, 0,
224 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 /* 0x40 - 0x47 */
226 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
227 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
228 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
229 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
230 /* 0x48 - 0x4F */
231 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
232 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
233 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
234 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
235 /* 0x50 - 0x5F */
236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 /* 0x60 - 0x6F */
238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239 /* 0x70 - 0x7F */
240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300242 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
243 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0x90 - 0x9F */
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246 /* 0xA0 - 0xA7 */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100247 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
248 DstMem | SrcReg | Src2ImmByte | ModRM,
249 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 /* 0xA8 - 0xAF */
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100251 0, 0, 0, DstMem | SrcReg | ModRM | BitOp,
252 DstMem | SrcReg | Src2ImmByte | ModRM,
253 DstMem | SrcReg | Src2CL | ModRM,
254 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 /* 0xB0 - 0xB7 */
256 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800257 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
259 DstReg | SrcMem16 | ModRM | Mov,
260 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800261 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
263 DstReg | SrcMem16 | ModRM | Mov,
264 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800265 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
266 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 /* 0xD0 - 0xDF */
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
269 /* 0xE0 - 0xEF */
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
271 /* 0xF0 - 0xFF */
272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
273};
274
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100275static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200276 [Group1_80*8] =
277 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
278 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
279 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
280 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
281 [Group1_81*8] =
282 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
283 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
284 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
285 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
286 [Group1_82*8] =
287 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
288 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
289 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
290 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
291 [Group1_83*8] =
292 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
293 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
294 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
295 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200296 [Group1A*8] =
297 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200298 [Group3_Byte*8] =
299 ByteOp | SrcImm | DstMem | ModRM, 0,
300 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
301 0, 0, 0, 0,
302 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400303 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300304 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200305 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200306 [Group4*8] =
307 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
308 0, 0, 0, 0, 0, 0,
309 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300310 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
311 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300312 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200313 [Group7*8] =
314 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300315 SrcNone | ModRM | DstMem | Mov, 0,
316 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200317};
318
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100319static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200320 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000321 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300322 SrcNone | ModRM | DstMem | Mov, 0,
323 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200324};
325
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200327#define EFLG_VM (1<<17)
328#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329#define EFLG_OF (1<<11)
330#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200331#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332#define EFLG_SF (1<<7)
333#define EFLG_ZF (1<<6)
334#define EFLG_AF (1<<4)
335#define EFLG_PF (1<<2)
336#define EFLG_CF (1<<0)
337
338/*
339 * Instruction emulation:
340 * Most instructions are emulated directly via a fragment of inline assembly
341 * code. This allows us to save/restore EFLAGS and thus very easily pick up
342 * any modified flags.
343 */
344
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800345#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346#define _LO32 "k" /* force 32-bit operand */
347#define _STK "%%rsp" /* stack pointer */
348#elif defined(__i386__)
349#define _LO32 "" /* force 32-bit operand */
350#define _STK "%%esp" /* stack pointer */
351#endif
352
353/*
354 * These EFLAGS bits are restored from saved value during emulation, and
355 * any changes are written back to the saved value after emulation.
356 */
357#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
358
359/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200360#define _PRE_EFLAGS(_sav, _msk, _tmp) \
361 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
362 "movl %"_sav",%"_LO32 _tmp"; " \
363 "push %"_tmp"; " \
364 "push %"_tmp"; " \
365 "movl %"_msk",%"_LO32 _tmp"; " \
366 "andl %"_LO32 _tmp",("_STK"); " \
367 "pushf; " \
368 "notl %"_LO32 _tmp"; " \
369 "andl %"_LO32 _tmp",("_STK"); " \
370 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
371 "pop %"_tmp"; " \
372 "orl %"_LO32 _tmp",("_STK"); " \
373 "popf; " \
374 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
376/* After executing instruction: write-back necessary bits in EFLAGS. */
377#define _POST_EFLAGS(_sav, _msk, _tmp) \
378 /* _sav |= EFLAGS & _msk; */ \
379 "pushf; " \
380 "pop %"_tmp"; " \
381 "andl %"_msk",%"_LO32 _tmp"; " \
382 "orl %"_LO32 _tmp",%"_sav"; "
383
Avi Kivitydda96d82008-11-26 15:14:10 +0200384#ifdef CONFIG_X86_64
385#define ON64(x) x
386#else
387#define ON64(x)
388#endif
389
Avi Kivity6b7ad612008-11-26 15:30:45 +0200390#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
391 do { \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "4", "2") \
394 _op _suffix " %"_x"3,%1; " \
395 _POST_EFLAGS("0", "4", "2") \
396 : "=m" (_eflags), "=m" ((_dst).val), \
397 "=&r" (_tmp) \
398 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200399 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200400
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402/* Raw emulation: instruction has two explicit operands. */
403#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200404 do { \
405 unsigned long _tmp; \
406 \
407 switch ((_dst).bytes) { \
408 case 2: \
409 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
410 break; \
411 case 4: \
412 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
413 break; \
414 case 8: \
415 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
416 break; \
417 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 } while (0)
419
420#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
421 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200422 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400423 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200425 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 break; \
427 default: \
428 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
429 _wx, _wy, _lx, _ly, _qx, _qy); \
430 break; \
431 } \
432 } while (0)
433
434/* Source operand is byte-sized and may be restricted to just %cl. */
435#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
436 __emulate_2op(_op, _src, _dst, _eflags, \
437 "b", "c", "b", "c", "b", "c", "b", "c")
438
439/* Source operand is byte, word, long or quad sized. */
440#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
441 __emulate_2op(_op, _src, _dst, _eflags, \
442 "b", "q", "w", "r", _LO32, "r", "", "r")
443
444/* Source operand is word, long or quad sized. */
445#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
446 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
447 "w", "r", _LO32, "r", "", "r")
448
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100449/* Instruction has three operands and one operand is stored in ECX register */
450#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
451 do { \
452 unsigned long _tmp; \
453 _type _clv = (_cl).val; \
454 _type _srcv = (_src).val; \
455 _type _dstv = (_dst).val; \
456 \
457 __asm__ __volatile__ ( \
458 _PRE_EFLAGS("0", "5", "2") \
459 _op _suffix " %4,%1 \n" \
460 _POST_EFLAGS("0", "5", "2") \
461 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
462 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
463 ); \
464 \
465 (_cl).val = (unsigned long) _clv; \
466 (_src).val = (unsigned long) _srcv; \
467 (_dst).val = (unsigned long) _dstv; \
468 } while (0)
469
470#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
471 do { \
472 switch ((_dst).bytes) { \
473 case 2: \
474 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
475 "w", unsigned short); \
476 break; \
477 case 4: \
478 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
479 "l", unsigned int); \
480 break; \
481 case 8: \
482 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
483 "q", unsigned long)); \
484 break; \
485 } \
486 } while (0)
487
Avi Kivitydda96d82008-11-26 15:14:10 +0200488#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 do { \
490 unsigned long _tmp; \
491 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200492 __asm__ __volatile__ ( \
493 _PRE_EFLAGS("0", "3", "2") \
494 _op _suffix " %1; " \
495 _POST_EFLAGS("0", "3", "2") \
496 : "=m" (_eflags), "+m" ((_dst).val), \
497 "=&r" (_tmp) \
498 : "i" (EFLAGS_MASK)); \
499 } while (0)
500
501/* Instruction has only one explicit operand (no source operand). */
502#define emulate_1op(_op, _dst, _eflags) \
503 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400504 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200505 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
506 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
507 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
508 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 } \
510 } while (0)
511
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512/* Fetch next part of the instruction being emulated. */
513#define insn_fetch(_type, _size, _eip) \
514({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200515 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400516 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 goto done; \
518 (_eip) += (_size); \
519 (_type)_x; \
520})
521
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800522static inline unsigned long ad_mask(struct decode_cache *c)
523{
524 return (1UL << (c->ad_bytes << 3)) - 1;
525}
526
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800528static inline unsigned long
529address_mask(struct decode_cache *c, unsigned long reg)
530{
531 if (c->ad_bytes == sizeof(unsigned long))
532 return reg;
533 else
534 return reg & ad_mask(c);
535}
536
537static inline unsigned long
538register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
539{
540 return base + address_mask(c, reg);
541}
542
Harvey Harrison7a9572752008-02-19 07:40:41 -0800543static inline void
544register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
545{
546 if (c->ad_bytes == sizeof(unsigned long))
547 *reg += inc;
548 else
549 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
550}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551
Harvey Harrison7a9572752008-02-19 07:40:41 -0800552static inline void jmp_rel(struct decode_cache *c, int rel)
553{
554 register_address_increment(c, &c->eip, rel);
555}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300556
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300557static void set_seg_override(struct decode_cache *c, int seg)
558{
559 c->has_seg_override = true;
560 c->seg_override = seg;
561}
562
563static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
564{
565 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
566 return 0;
567
568 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
569}
570
571static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
572 struct decode_cache *c)
573{
574 if (!c->has_seg_override)
575 return 0;
576
577 return seg_base(ctxt, c->seg_override);
578}
579
580static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
581{
582 return seg_base(ctxt, VCPU_SREG_ES);
583}
584
585static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
586{
587 return seg_base(ctxt, VCPU_SREG_SS);
588}
589
Avi Kivity62266862007-11-20 13:15:52 +0200590static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
591 struct x86_emulate_ops *ops,
592 unsigned long linear, u8 *dest)
593{
594 struct fetch_cache *fc = &ctxt->decode.fetch;
595 int rc;
596 int size;
597
598 if (linear < fc->start || linear >= fc->end) {
599 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
600 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
601 if (rc)
602 return rc;
603 fc->start = linear;
604 fc->end = linear + size;
605 }
606 *dest = fc->data[linear - fc->start];
607 return 0;
608}
609
610static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
611 struct x86_emulate_ops *ops,
612 unsigned long eip, void *dest, unsigned size)
613{
614 int rc = 0;
615
616 eip += ctxt->cs_base;
617 while (size--) {
618 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
619 if (rc)
620 return rc;
621 }
622 return 0;
623}
624
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000625/*
626 * Given the 'reg' portion of a ModRM byte, and a register block, return a
627 * pointer into the block that addresses the relevant register.
628 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
629 */
630static void *decode_register(u8 modrm_reg, unsigned long *regs,
631 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632{
633 void *p;
634
635 p = &regs[modrm_reg];
636 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
637 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
638 return p;
639}
640
641static int read_descriptor(struct x86_emulate_ctxt *ctxt,
642 struct x86_emulate_ops *ops,
643 void *ptr,
644 u16 *size, unsigned long *address, int op_bytes)
645{
646 int rc;
647
648 if (op_bytes == 2)
649 op_bytes = 3;
650 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300651 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
652 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 if (rc)
654 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300655 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
656 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 return rc;
658}
659
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300660static int test_cc(unsigned int condition, unsigned int flags)
661{
662 int rc = 0;
663
664 switch ((condition & 15) >> 1) {
665 case 0: /* o */
666 rc |= (flags & EFLG_OF);
667 break;
668 case 1: /* b/c/nae */
669 rc |= (flags & EFLG_CF);
670 break;
671 case 2: /* z/e */
672 rc |= (flags & EFLG_ZF);
673 break;
674 case 3: /* be/na */
675 rc |= (flags & (EFLG_CF|EFLG_ZF));
676 break;
677 case 4: /* s */
678 rc |= (flags & EFLG_SF);
679 break;
680 case 5: /* p/pe */
681 rc |= (flags & EFLG_PF);
682 break;
683 case 7: /* le/ng */
684 rc |= (flags & EFLG_ZF);
685 /* fall through */
686 case 6: /* l/nge */
687 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
688 break;
689 }
690
691 /* Odd condition identifiers (lsb == 1) have inverted sense. */
692 return (!!rc ^ (condition & 1));
693}
694
Avi Kivity3c118e22007-10-31 10:27:04 +0200695static void decode_register_operand(struct operand *op,
696 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200697 int inhibit_bytereg)
698{
Avi Kivity33615aa2007-10-31 11:15:56 +0200699 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200700 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200701
702 if (!(c->d & ModRM))
703 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200704 op->type = OP_REG;
705 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200706 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200707 op->val = *(u8 *)op->ptr;
708 op->bytes = 1;
709 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200710 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200711 op->bytes = c->op_bytes;
712 switch (op->bytes) {
713 case 2:
714 op->val = *(u16 *)op->ptr;
715 break;
716 case 4:
717 op->val = *(u32 *)op->ptr;
718 break;
719 case 8:
720 op->val = *(u64 *) op->ptr;
721 break;
722 }
723 }
724 op->orig_val = op->val;
725}
726
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200727static int decode_modrm(struct x86_emulate_ctxt *ctxt,
728 struct x86_emulate_ops *ops)
729{
730 struct decode_cache *c = &ctxt->decode;
731 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700732 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733 int rc = 0;
734
735 if (c->rex_prefix) {
736 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
737 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
738 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
739 }
740
741 c->modrm = insn_fetch(u8, 1, c->eip);
742 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
743 c->modrm_reg |= (c->modrm & 0x38) >> 3;
744 c->modrm_rm |= (c->modrm & 0x07);
745 c->modrm_ea = 0;
746 c->use_modrm_ea = 1;
747
748 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300749 c->modrm_ptr = decode_register(c->modrm_rm,
750 c->regs, c->d & ByteOp);
751 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200752 return rc;
753 }
754
755 if (c->ad_bytes == 2) {
756 unsigned bx = c->regs[VCPU_REGS_RBX];
757 unsigned bp = c->regs[VCPU_REGS_RBP];
758 unsigned si = c->regs[VCPU_REGS_RSI];
759 unsigned di = c->regs[VCPU_REGS_RDI];
760
761 /* 16-bit ModR/M decode. */
762 switch (c->modrm_mod) {
763 case 0:
764 if (c->modrm_rm == 6)
765 c->modrm_ea += insn_fetch(u16, 2, c->eip);
766 break;
767 case 1:
768 c->modrm_ea += insn_fetch(s8, 1, c->eip);
769 break;
770 case 2:
771 c->modrm_ea += insn_fetch(u16, 2, c->eip);
772 break;
773 }
774 switch (c->modrm_rm) {
775 case 0:
776 c->modrm_ea += bx + si;
777 break;
778 case 1:
779 c->modrm_ea += bx + di;
780 break;
781 case 2:
782 c->modrm_ea += bp + si;
783 break;
784 case 3:
785 c->modrm_ea += bp + di;
786 break;
787 case 4:
788 c->modrm_ea += si;
789 break;
790 case 5:
791 c->modrm_ea += di;
792 break;
793 case 6:
794 if (c->modrm_mod != 0)
795 c->modrm_ea += bp;
796 break;
797 case 7:
798 c->modrm_ea += bx;
799 break;
800 }
801 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
802 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300803 if (!c->has_seg_override)
804 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200805 c->modrm_ea = (u16)c->modrm_ea;
806 } else {
807 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700808 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 sib = insn_fetch(u8, 1, c->eip);
810 index_reg |= (sib >> 3) & 7;
811 base_reg |= sib & 7;
812 scale = sib >> 6;
813
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700814 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
815 c->modrm_ea += insn_fetch(s32, 4, c->eip);
816 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700818 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200819 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700820 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
821 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700822 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700823 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200824 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200825 switch (c->modrm_mod) {
826 case 0:
827 if (c->modrm_rm == 5)
828 c->modrm_ea += insn_fetch(s32, 4, c->eip);
829 break;
830 case 1:
831 c->modrm_ea += insn_fetch(s8, 1, c->eip);
832 break;
833 case 2:
834 c->modrm_ea += insn_fetch(s32, 4, c->eip);
835 break;
836 }
837 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200838done:
839 return rc;
840}
841
842static int decode_abs(struct x86_emulate_ctxt *ctxt,
843 struct x86_emulate_ops *ops)
844{
845 struct decode_cache *c = &ctxt->decode;
846 int rc = 0;
847
848 switch (c->ad_bytes) {
849 case 2:
850 c->modrm_ea = insn_fetch(u16, 2, c->eip);
851 break;
852 case 4:
853 c->modrm_ea = insn_fetch(u32, 4, c->eip);
854 break;
855 case 8:
856 c->modrm_ea = insn_fetch(u64, 8, c->eip);
857 break;
858 }
859done:
860 return rc;
861}
862
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200864x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200866 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200869 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
871 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872
Laurent Viviere4e03de2007-09-18 11:52:50 +0200873 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300874 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300875 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800876 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
878 switch (mode) {
879 case X86EMUL_MODE_REAL:
880 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200881 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 break;
883 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200884 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800886#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200888 def_op_bytes = 4;
889 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 break;
891#endif
892 default:
893 return -1;
894 }
895
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200896 c->op_bytes = def_op_bytes;
897 c->ad_bytes = def_ad_bytes;
898
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200900 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200901 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200903 /* switch between 2/4 bytes */
904 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
906 case 0x67: /* address-size override */
907 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200908 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200909 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200911 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200912 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300915 case 0x2e: /* CS override */
916 case 0x36: /* SS override */
917 case 0x3e: /* DS override */
918 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 break;
920 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300922 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200924 case 0x40 ... 0x4f: /* REX */
925 if (mode != X86EMUL_MODE_PROT64)
926 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200927 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200928 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200932 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100933 c->rep_prefix = REPNE_PREFIX;
934 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100936 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 default:
939 goto done_prefixes;
940 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200941
942 /* Any legacy prefix after a REX prefix nullifies its effect. */
943
Avi Kivity33615aa2007-10-31 11:15:56 +0200944 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 }
946
947done_prefixes:
948
949 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200950 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200951 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200952 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953
954 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200955 c->d = opcode_table[c->b];
956 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200958 if (c->b == 0x0f) {
959 c->twobyte = 1;
960 c->b = insn_fetch(u8, 1, c->eip);
961 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200963 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
Avi Kivitye09d0822008-01-18 12:38:59 +0200965 if (c->d & Group) {
966 group = c->d & GroupMask;
967 c->modrm = insn_fetch(u8, 1, c->eip);
968 --c->eip;
969
970 group = (group << 3) + ((c->modrm >> 3) & 7);
971 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
972 c->d = group2_table[group];
973 else
974 c->d = group_table[group];
975 }
976
977 /* Unrecognised? */
978 if (c->d == 0) {
979 DPRINTF("Cannot emulate %02x\n", c->b);
980 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 }
982
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200983 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
984 c->op_bytes = 8;
985
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200987 if (c->d & ModRM)
988 rc = decode_modrm(ctxt, ops);
989 else if (c->d & MemAbs)
990 rc = decode_abs(ctxt, ops);
991 if (rc)
992 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300994 if (!c->has_seg_override)
995 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200996
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300997 if (!(!c->twobyte && c->b == 0x8d))
998 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +0200999
1000 if (c->ad_bytes != 8)
1001 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 /*
1003 * Decode and fetch the source operand: register, memory
1004 * or immediate.
1005 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001006 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 case SrcNone:
1008 break;
1009 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001010 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011 break;
1012 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 goto srcmem_common;
1015 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001016 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 goto srcmem_common;
1018 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001019 c->src.bytes = (c->d & ByteOp) ? 1 :
1020 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001021 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001022 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001023 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001024 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001025 /*
1026 * For instructions with a ModR/M byte, switch to register
1027 * access if Mod = 3.
1028 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 if ((c->d & ModRM) && c->modrm_mod == 3) {
1030 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001031 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001032 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001033 break;
1034 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001035 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 break;
1037 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001038 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001039 c->src.type = OP_IMM;
1040 c->src.ptr = (unsigned long *)c->eip;
1041 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1042 if (c->src.bytes == 8)
1043 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001045 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001047 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 break;
1049 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001050 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 break;
1052 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001053 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 break;
1055 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001056 if ((c->d & SrcMask) == SrcImmU) {
1057 switch (c->src.bytes) {
1058 case 1:
1059 c->src.val &= 0xff;
1060 break;
1061 case 2:
1062 c->src.val &= 0xffff;
1063 break;
1064 case 4:
1065 c->src.val &= 0xffffffff;
1066 break;
1067 }
1068 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 break;
1070 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001071 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001072 c->src.type = OP_IMM;
1073 c->src.ptr = (unsigned long *)c->eip;
1074 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001075 if ((c->d & SrcMask) == SrcImmByte)
1076 c->src.val = insn_fetch(s8, 1, c->eip);
1077 else
1078 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001080 case SrcOne:
1081 c->src.bytes = 1;
1082 c->src.val = 1;
1083 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 }
1085
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001086 /*
1087 * Decode and fetch the second source operand: register, memory
1088 * or immediate.
1089 */
1090 switch (c->d & Src2Mask) {
1091 case Src2None:
1092 break;
1093 case Src2CL:
1094 c->src2.bytes = 1;
1095 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1096 break;
1097 case Src2ImmByte:
1098 c->src2.type = OP_IMM;
1099 c->src2.ptr = (unsigned long *)c->eip;
1100 c->src2.bytes = 1;
1101 c->src2.val = insn_fetch(u8, 1, c->eip);
1102 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001103 case Src2Imm16:
1104 c->src2.type = OP_IMM;
1105 c->src2.ptr = (unsigned long *)c->eip;
1106 c->src2.bytes = 2;
1107 c->src2.val = insn_fetch(u16, 2, c->eip);
1108 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001109 case Src2One:
1110 c->src2.bytes = 1;
1111 c->src2.val = 1;
1112 break;
1113 }
1114
Avi Kivity038e51d2007-01-22 20:40:40 -08001115 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001117 case ImplicitOps:
1118 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001119 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001120 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001121 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001122 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001123 break;
1124 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001125 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001126 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001127 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001128 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001129 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001130 break;
1131 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001132 c->dst.type = OP_MEM;
1133 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001134 case DstAcc:
1135 c->dst.type = OP_REG;
1136 c->dst.bytes = c->op_bytes;
1137 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1138 switch (c->op_bytes) {
1139 case 1:
1140 c->dst.val = *(u8 *)c->dst.ptr;
1141 break;
1142 case 2:
1143 c->dst.val = *(u16 *)c->dst.ptr;
1144 break;
1145 case 4:
1146 c->dst.val = *(u32 *)c->dst.ptr;
1147 break;
1148 }
1149 c->dst.orig_val = c->dst.val;
1150 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001151 }
1152
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001153 if (c->rip_relative)
1154 c->modrm_ea += c->eip;
1155
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001156done:
1157 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1158}
1159
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001160static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1161{
1162 struct decode_cache *c = &ctxt->decode;
1163
1164 c->dst.type = OP_MEM;
1165 c->dst.bytes = c->op_bytes;
1166 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001167 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001168 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001169 c->regs[VCPU_REGS_RSP]);
1170}
1171
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001172static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001173 struct x86_emulate_ops *ops,
1174 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001175{
1176 struct decode_cache *c = &ctxt->decode;
1177 int rc;
1178
Avi Kivity781d0ed2008-11-27 18:00:28 +02001179 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1180 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001181 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001182 if (rc != 0)
1183 return rc;
1184
Avi Kivity350f69d2009-01-05 11:12:40 +02001185 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001186 return rc;
1187}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001188
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001189static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1190 struct x86_emulate_ops *ops)
1191{
1192 struct decode_cache *c = &ctxt->decode;
1193 int rc;
1194
Avi Kivity350f69d2009-01-05 11:12:40 +02001195 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001196 if (rc != 0)
1197 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001198 return 0;
1199}
1200
Laurent Vivier05f086f2007-09-24 11:10:55 +02001201static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001202{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001203 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001204 switch (c->modrm_reg) {
1205 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001206 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001207 break;
1208 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001209 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001210 break;
1211 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001212 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213 break;
1214 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001215 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001216 break;
1217 case 4: /* sal/shl */
1218 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001219 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001220 break;
1221 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001222 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001223 break;
1224 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001225 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001226 break;
1227 }
1228}
1229
1230static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001231 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001232{
1233 struct decode_cache *c = &ctxt->decode;
1234 int rc = 0;
1235
1236 switch (c->modrm_reg) {
1237 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001238 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239 break;
1240 case 2: /* not */
1241 c->dst.val = ~c->dst.val;
1242 break;
1243 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001244 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001245 break;
1246 default:
1247 DPRINTF("Cannot emulate %02x\n", c->b);
1248 rc = X86EMUL_UNHANDLEABLE;
1249 break;
1250 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251 return rc;
1252}
1253
1254static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001255 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001256{
1257 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258
1259 switch (c->modrm_reg) {
1260 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001261 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001262 break;
1263 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001264 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001265 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001266 case 2: /* call near abs */ {
1267 long int old_eip;
1268 old_eip = c->eip;
1269 c->eip = c->src.val;
1270 c->src.val = old_eip;
1271 emulate_push(ctxt);
1272 break;
1273 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001274 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001275 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001276 break;
1277 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001278 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001279 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001280 }
1281 return 0;
1282}
1283
1284static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1285 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001286 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001287{
1288 struct decode_cache *c = &ctxt->decode;
1289 u64 old, new;
1290 int rc;
1291
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001292 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293 if (rc != 0)
1294 return rc;
1295
1296 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1297 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1298
1299 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1300 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001301 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001302
1303 } else {
1304 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1305 (u32) c->regs[VCPU_REGS_RBX];
1306
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001307 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001308 if (rc != 0)
1309 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001310 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311 }
1312 return 0;
1313}
1314
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001315static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1316 struct x86_emulate_ops *ops)
1317{
1318 struct decode_cache *c = &ctxt->decode;
1319 int rc;
1320 unsigned long cs;
1321
1322 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1323 if (rc)
1324 return rc;
1325 if (c->op_bytes == 4)
1326 c->eip = (u32)c->eip;
1327 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1328 if (rc)
1329 return rc;
1330 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1331 return rc;
1332}
1333
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001334static inline int writeback(struct x86_emulate_ctxt *ctxt,
1335 struct x86_emulate_ops *ops)
1336{
1337 int rc;
1338 struct decode_cache *c = &ctxt->decode;
1339
1340 switch (c->dst.type) {
1341 case OP_REG:
1342 /* The 4-byte case *is* correct:
1343 * in 64-bit mode we zero-extend.
1344 */
1345 switch (c->dst.bytes) {
1346 case 1:
1347 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1348 break;
1349 case 2:
1350 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1351 break;
1352 case 4:
1353 *c->dst.ptr = (u32)c->dst.val;
1354 break; /* 64b: zero-ext */
1355 case 8:
1356 *c->dst.ptr = c->dst.val;
1357 break;
1358 }
1359 break;
1360 case OP_MEM:
1361 if (c->lock_prefix)
1362 rc = ops->cmpxchg_emulated(
1363 (unsigned long)c->dst.ptr,
1364 &c->dst.orig_val,
1365 &c->dst.val,
1366 c->dst.bytes,
1367 ctxt->vcpu);
1368 else
1369 rc = ops->write_emulated(
1370 (unsigned long)c->dst.ptr,
1371 &c->dst.val,
1372 c->dst.bytes,
1373 ctxt->vcpu);
1374 if (rc != 0)
1375 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001376 break;
1377 case OP_NONE:
1378 /* no writeback */
1379 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001380 default:
1381 break;
1382 }
1383 return 0;
1384}
1385
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301386static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001387{
1388 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1389 /*
1390 * an sti; sti; sequence only disable interrupts for the first
1391 * instruction. So, if the last instruction, be it emulated or
1392 * not, left the system with the INT_STI flag enabled, it
1393 * means that the last instruction is an sti. We should not
1394 * leave the flag on in this case. The same goes for mov ss
1395 */
1396 if (!(int_shadow & mask))
1397 ctxt->interruptibility = mask;
1398}
1399
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001400static inline void
1401setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1402 struct kvm_segment *cs, struct kvm_segment *ss)
1403{
1404 memset(cs, 0, sizeof(struct kvm_segment));
1405 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1406 memset(ss, 0, sizeof(struct kvm_segment));
1407
1408 cs->l = 0; /* will be adjusted later */
1409 cs->base = 0; /* flat segment */
1410 cs->g = 1; /* 4kb granularity */
1411 cs->limit = 0xffffffff; /* 4GB limit */
1412 cs->type = 0x0b; /* Read, Execute, Accessed */
1413 cs->s = 1;
1414 cs->dpl = 0; /* will be adjusted later */
1415 cs->present = 1;
1416 cs->db = 1;
1417
1418 ss->unusable = 0;
1419 ss->base = 0; /* flat segment */
1420 ss->limit = 0xffffffff; /* 4GB limit */
1421 ss->g = 1; /* 4kb granularity */
1422 ss->s = 1;
1423 ss->type = 0x03; /* Read/Write, Accessed */
1424 ss->db = 1; /* 32bit stack segment */
1425 ss->dpl = 0;
1426 ss->present = 1;
1427}
1428
1429static int
1430emulate_syscall(struct x86_emulate_ctxt *ctxt)
1431{
1432 struct decode_cache *c = &ctxt->decode;
1433 struct kvm_segment cs, ss;
1434 u64 msr_data;
1435
1436 /* syscall is not available in real mode */
1437 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
1438 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE))
1439 return -1;
1440
1441 setup_syscalls_segments(ctxt, &cs, &ss);
1442
1443 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1444 msr_data >>= 32;
1445 cs.selector = (u16)(msr_data & 0xfffc);
1446 ss.selector = (u16)(msr_data + 8);
1447
1448 if (is_long_mode(ctxt->vcpu)) {
1449 cs.db = 0;
1450 cs.l = 1;
1451 }
1452 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1453 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1454
1455 c->regs[VCPU_REGS_RCX] = c->eip;
1456 if (is_long_mode(ctxt->vcpu)) {
1457#ifdef CONFIG_X86_64
1458 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1459
1460 kvm_x86_ops->get_msr(ctxt->vcpu,
1461 ctxt->mode == X86EMUL_MODE_PROT64 ?
1462 MSR_LSTAR : MSR_CSTAR, &msr_data);
1463 c->eip = msr_data;
1464
1465 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1466 ctxt->eflags &= ~(msr_data | EFLG_RF);
1467#endif
1468 } else {
1469 /* legacy mode */
1470 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1471 c->eip = (u32)msr_data;
1472
1473 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1474 }
1475
1476 return 0;
1477}
1478
Andre Przywara8c604352009-06-18 12:56:01 +02001479static int
1480emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1481{
1482 struct decode_cache *c = &ctxt->decode;
1483 struct kvm_segment cs, ss;
1484 u64 msr_data;
1485
1486 /* inject #UD if LOCK prefix is used */
1487 if (c->lock_prefix)
1488 return -1;
1489
1490 /* inject #GP if in real mode or paging is disabled */
1491 if (ctxt->mode == X86EMUL_MODE_REAL ||
1492 !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1493 kvm_inject_gp(ctxt->vcpu, 0);
1494 return -1;
1495 }
1496
1497 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1498 * Therefore, we inject an #UD.
1499 */
1500 if (ctxt->mode == X86EMUL_MODE_PROT64)
1501 return -1;
1502
1503 setup_syscalls_segments(ctxt, &cs, &ss);
1504
1505 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1506 switch (ctxt->mode) {
1507 case X86EMUL_MODE_PROT32:
1508 if ((msr_data & 0xfffc) == 0x0) {
1509 kvm_inject_gp(ctxt->vcpu, 0);
1510 return -1;
1511 }
1512 break;
1513 case X86EMUL_MODE_PROT64:
1514 if (msr_data == 0x0) {
1515 kvm_inject_gp(ctxt->vcpu, 0);
1516 return -1;
1517 }
1518 break;
1519 }
1520
1521 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1522 cs.selector = (u16)msr_data;
1523 cs.selector &= ~SELECTOR_RPL_MASK;
1524 ss.selector = cs.selector + 8;
1525 ss.selector &= ~SELECTOR_RPL_MASK;
1526 if (ctxt->mode == X86EMUL_MODE_PROT64
1527 || is_long_mode(ctxt->vcpu)) {
1528 cs.db = 0;
1529 cs.l = 1;
1530 }
1531
1532 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1533 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1534
1535 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1536 c->eip = msr_data;
1537
1538 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1539 c->regs[VCPU_REGS_RSP] = msr_data;
1540
1541 return 0;
1542}
1543
Andre Przywara4668f052009-06-18 12:56:02 +02001544static int
1545emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1546{
1547 struct decode_cache *c = &ctxt->decode;
1548 struct kvm_segment cs, ss;
1549 u64 msr_data;
1550 int usermode;
1551
1552 /* inject #UD if LOCK prefix is used */
1553 if (c->lock_prefix)
1554 return -1;
1555
1556 /* inject #GP if in real mode or paging is disabled */
1557 if (ctxt->mode == X86EMUL_MODE_REAL
1558 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1559 kvm_inject_gp(ctxt->vcpu, 0);
1560 return -1;
1561 }
1562
1563 /* sysexit must be called from CPL 0 */
1564 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1565 kvm_inject_gp(ctxt->vcpu, 0);
1566 return -1;
1567 }
1568
1569 setup_syscalls_segments(ctxt, &cs, &ss);
1570
1571 if ((c->rex_prefix & 0x8) != 0x0)
1572 usermode = X86EMUL_MODE_PROT64;
1573 else
1574 usermode = X86EMUL_MODE_PROT32;
1575
1576 cs.dpl = 3;
1577 ss.dpl = 3;
1578 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1579 switch (usermode) {
1580 case X86EMUL_MODE_PROT32:
1581 cs.selector = (u16)(msr_data + 16);
1582 if ((msr_data & 0xfffc) == 0x0) {
1583 kvm_inject_gp(ctxt->vcpu, 0);
1584 return -1;
1585 }
1586 ss.selector = (u16)(msr_data + 24);
1587 break;
1588 case X86EMUL_MODE_PROT64:
1589 cs.selector = (u16)(msr_data + 32);
1590 if (msr_data == 0x0) {
1591 kvm_inject_gp(ctxt->vcpu, 0);
1592 return -1;
1593 }
1594 ss.selector = cs.selector + 8;
1595 cs.db = 0;
1596 cs.l = 1;
1597 break;
1598 }
1599 cs.selector |= SELECTOR_RPL_MASK;
1600 ss.selector |= SELECTOR_RPL_MASK;
1601
1602 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1603 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1604
1605 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1606 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1607
1608 return 0;
1609}
1610
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001611int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001612x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001613{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001614 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001615 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001616 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001617 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001618 unsigned int port;
1619 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001620 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001621
Glauber Costa310b5d32009-05-12 16:21:06 -04001622 ctxt->interruptibility = 0;
1623
Laurent Vivier34273182007-09-18 11:27:37 +02001624 /* Shadow copy of register state. Committed on successful emulation.
1625 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1626 * modify them.
1627 */
1628
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001629 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001630 saved_eip = c->eip;
1631
Avi Kivityc7e75a32007-10-28 16:34:25 +02001632 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001633 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001634
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001635 if (c->rep_prefix && (c->d & String)) {
1636 /* All REP prefixes have the same first termination condition */
1637 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001638 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001639 goto done;
1640 }
1641 /* The second termination condition only applies for REPE
1642 * and REPNE. Test if the repeat string operation prefix is
1643 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1644 * corresponding termination condition according to:
1645 * - if REPE/REPZ and ZF = 0 then done
1646 * - if REPNE/REPNZ and ZF = 1 then done
1647 */
1648 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1649 (c->b == 0xae) || (c->b == 0xaf)) {
1650 if ((c->rep_prefix == REPE_PREFIX) &&
1651 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001652 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001653 goto done;
1654 }
1655 if ((c->rep_prefix == REPNE_PREFIX) &&
1656 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001657 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001658 goto done;
1659 }
1660 }
1661 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001662 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001663 }
1664
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001665 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001666 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001667 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001668 rc = ops->read_emulated((unsigned long)c->src.ptr,
1669 &c->src.val,
1670 c->src.bytes,
1671 ctxt->vcpu);
1672 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001673 goto done;
1674 c->src.orig_val = c->src.val;
1675 }
1676
1677 if ((c->d & DstMask) == ImplicitOps)
1678 goto special_insn;
1679
1680
1681 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001682 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001683 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1684 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001685 if (c->d & BitOp) {
1686 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001687
Laurent Viviere4e03de2007-09-18 11:52:50 +02001688 c->dst.ptr = (void *)c->dst.ptr +
1689 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001690 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001691 if (!(c->d & Mov) &&
1692 /* optimisation - avoid slow emulated read */
1693 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1694 &c->dst.val,
1695 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001696 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001697 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001698 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001699
Avi Kivity018a98d2007-11-27 19:30:56 +02001700special_insn:
1701
Laurent Viviere4e03de2007-09-18 11:52:50 +02001702 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 goto twobyte_insn;
1704
Laurent Viviere4e03de2007-09-18 11:52:50 +02001705 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 case 0x00 ... 0x05:
1707 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001708 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 break;
1710 case 0x08 ... 0x0d:
1711 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001712 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 break;
1714 case 0x10 ... 0x15:
1715 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001716 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717 break;
1718 case 0x18 ... 0x1d:
1719 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001720 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001722 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001724 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 break;
1726 case 0x28 ... 0x2d:
1727 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001728 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 break;
1730 case 0x30 ... 0x35:
1731 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001732 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 break;
1734 case 0x38 ... 0x3d:
1735 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001736 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001738 case 0x40 ... 0x47: /* inc r16/r32 */
1739 emulate_1op("inc", c->dst, ctxt->eflags);
1740 break;
1741 case 0x48 ... 0x4f: /* dec r16/r32 */
1742 emulate_1op("dec", c->dst, ctxt->eflags);
1743 break;
1744 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001745 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001746 break;
1747 case 0x58 ... 0x5f: /* pop reg */
1748 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001749 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001750 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001751 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001752 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001754 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001756 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001758 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001759 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001760 emulate_push(ctxt);
1761 break;
1762 case 0x6c: /* insb */
1763 case 0x6d: /* insw/insd */
1764 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1765 1,
1766 (c->d & ByteOp) ? 1 : c->op_bytes,
1767 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001768 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001769 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001770 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001771 c->regs[VCPU_REGS_RDI]),
1772 c->rep_prefix,
1773 c->regs[VCPU_REGS_RDX]) == 0) {
1774 c->eip = saved_eip;
1775 return -1;
1776 }
1777 return 0;
1778 case 0x6e: /* outsb */
1779 case 0x6f: /* outsw/outsd */
1780 if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
1781 0,
1782 (c->d & ByteOp) ? 1 : c->op_bytes,
1783 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001784 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001785 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001786 register_address(c,
1787 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001788 c->regs[VCPU_REGS_RSI]),
1789 c->rep_prefix,
1790 c->regs[VCPU_REGS_RDX]) == 0) {
1791 c->eip = saved_eip;
1792 return -1;
1793 }
1794 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001795 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001796 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001797 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001798 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001800 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 case 0:
1802 goto add;
1803 case 1:
1804 goto or;
1805 case 2:
1806 goto adc;
1807 case 3:
1808 goto sbb;
1809 case 4:
1810 goto and;
1811 case 5:
1812 goto sub;
1813 case 6:
1814 goto xor;
1815 case 7:
1816 goto cmp;
1817 }
1818 break;
1819 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001820 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821 break;
1822 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001823 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001825 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001827 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828 break;
1829 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001830 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 break;
1832 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001833 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 break; /* 64b reg: zero-extend */
1835 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001836 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 break;
1838 }
1839 /*
1840 * Write back the memory destination with implicit LOCK
1841 * prefix.
1842 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001843 c->dst.val = c->src.val;
1844 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001847 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001848 case 0x8c: { /* mov r/m, sreg */
1849 struct kvm_segment segreg;
1850
1851 if (c->modrm_reg <= 5)
1852 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1853 else {
1854 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1855 c->modrm);
1856 goto cannot_emulate;
1857 }
1858 c->dst.val = segreg.selector;
1859 break;
1860 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001861 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001862 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001863 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001864 case 0x8e: { /* mov seg, r/m16 */
1865 uint16_t sel;
1866 int type_bits;
1867 int err;
1868
1869 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001870 if (c->modrm_reg == VCPU_SREG_SS)
1871 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1872
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001873 if (c->modrm_reg <= 5) {
1874 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1875 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1876 type_bits, c->modrm_reg);
1877 } else {
1878 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1879 c->modrm);
1880 goto cannot_emulate;
1881 }
1882
1883 if (err < 0)
1884 goto cannot_emulate;
1885
1886 c->dst.type = OP_NONE; /* Disable writeback. */
1887 break;
1888 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001890 rc = emulate_grp1a(ctxt, ops);
1891 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001894 case 0x90: /* nop / xchg r8,rax */
1895 if (!(c->rex_prefix & 1)) { /* nop */
1896 c->dst.type = OP_NONE;
1897 break;
1898 }
1899 case 0x91 ... 0x97: /* xchg reg,rax */
1900 c->src.type = c->dst.type = OP_REG;
1901 c->src.bytes = c->dst.bytes = c->op_bytes;
1902 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1903 c->src.val = *(c->src.ptr);
1904 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001905 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001906 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907 emulate_push(ctxt);
1908 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001909 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001910 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001911 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001912 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001913 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001914 case 0xa0 ... 0xa1: /* mov */
1915 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1916 c->dst.val = c->src.val;
1917 break;
1918 case 0xa2 ... 0xa3: /* mov */
1919 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1920 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001922 c->dst.type = OP_MEM;
1923 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001924 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001925 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001927 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001928 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001929 c->regs[VCPU_REGS_RSI]),
1930 &c->dst.val,
1931 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001933 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001934 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001935 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001936 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001937 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001938 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 break;
1940 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001941 c->src.type = OP_NONE; /* Disable writeback. */
1942 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001943 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001944 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001945 c->regs[VCPU_REGS_RSI]);
1946 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
1947 &c->src.val,
1948 c->src.bytes,
1949 ctxt->vcpu)) != 0)
1950 goto done;
1951
1952 c->dst.type = OP_NONE; /* Disable writeback. */
1953 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001954 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001955 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001956 c->regs[VCPU_REGS_RDI]);
1957 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1958 &c->dst.val,
1959 c->dst.bytes,
1960 ctxt->vcpu)) != 0)
1961 goto done;
1962
1963 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
1964
1965 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
1966
Harvey Harrison7a9572752008-02-19 07:40:41 -08001967 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001968 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
1969 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08001970 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01001971 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
1972 : c->dst.bytes);
1973
1974 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001976 c->dst.type = OP_MEM;
1977 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001978 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001979 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001980 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02001981 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08001982 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001983 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001984 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 break;
1986 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001987 c->dst.type = OP_REG;
1988 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1989 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08001990 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001991 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08001992 c->regs[VCPU_REGS_RSI]),
1993 &c->dst.val,
1994 c->dst.bytes,
1995 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001997 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001998 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02001999 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 break;
2001 case 0xae ... 0xaf: /* scas */
2002 DPRINTF("Urk! I don't handle SCAS.\n");
2003 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002004 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002005 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002006 case 0xc0 ... 0xc1:
2007 emulate_grp2(ctxt);
2008 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002009 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002010 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002011 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002012 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002013 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002014 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2015 mov:
2016 c->dst.val = c->src.val;
2017 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002018 case 0xcb: /* ret far */
2019 rc = emulate_ret_far(ctxt, ops);
2020 if (rc)
2021 goto done;
2022 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002023 case 0xd0 ... 0xd1: /* Grp2 */
2024 c->src.val = 1;
2025 emulate_grp2(ctxt);
2026 break;
2027 case 0xd2 ... 0xd3: /* Grp2 */
2028 c->src.val = c->regs[VCPU_REGS_RCX];
2029 emulate_grp2(ctxt);
2030 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002031 case 0xe4: /* inb */
2032 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002033 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002034 io_dir_in = 1;
2035 goto do_io;
2036 case 0xe6: /* outb */
2037 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002038 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002039 io_dir_in = 0;
2040 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002041 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002042 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002043 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002044 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002045 emulate_push(ctxt);
2046 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002047 }
2048 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002049 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002050 case 0xea: /* jmp far */
2051 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2052 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002053 DPRINTF("jmp far: Failed to load CS descriptor\n");
2054 goto cannot_emulate;
2055 }
2056
Gleb Natapov782b8772009-04-12 13:36:25 +03002057 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002058 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002059 case 0xeb:
2060 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002061 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002062 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002063 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002064 case 0xec: /* in al,dx */
2065 case 0xed: /* in (e/r)ax,dx */
2066 port = c->regs[VCPU_REGS_RDX];
2067 io_dir_in = 1;
2068 goto do_io;
2069 case 0xee: /* out al,dx */
2070 case 0xef: /* out (e/r)ax,dx */
2071 port = c->regs[VCPU_REGS_RDX];
2072 io_dir_in = 0;
2073 do_io: if (kvm_emulate_pio(ctxt->vcpu, NULL, io_dir_in,
2074 (c->d & ByteOp) ? 1 : c->op_bytes,
2075 port) != 0) {
2076 c->eip = saved_eip;
2077 goto cannot_emulate;
2078 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002079 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002080 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002081 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002082 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002083 case 0xf5: /* cmc */
2084 /* complement carry flag from eflags reg */
2085 ctxt->eflags ^= EFLG_CF;
2086 c->dst.type = OP_NONE; /* Disable writeback. */
2087 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002088 case 0xf6 ... 0xf7: /* Grp3 */
2089 rc = emulate_grp3(ctxt, ops);
2090 if (rc != 0)
2091 goto done;
2092 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002093 case 0xf8: /* clc */
2094 ctxt->eflags &= ~EFLG_CF;
2095 c->dst.type = OP_NONE; /* Disable writeback. */
2096 break;
2097 case 0xfa: /* cli */
2098 ctxt->eflags &= ~X86_EFLAGS_IF;
2099 c->dst.type = OP_NONE; /* Disable writeback. */
2100 break;
2101 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04002102 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02002103 ctxt->eflags |= X86_EFLAGS_IF;
2104 c->dst.type = OP_NONE; /* Disable writeback. */
2105 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002106 case 0xfc: /* cld */
2107 ctxt->eflags &= ~EFLG_DF;
2108 c->dst.type = OP_NONE; /* Disable writeback. */
2109 break;
2110 case 0xfd: /* std */
2111 ctxt->eflags |= EFLG_DF;
2112 c->dst.type = OP_NONE; /* Disable writeback. */
2113 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002114 case 0xfe ... 0xff: /* Grp4/Grp5 */
2115 rc = emulate_grp45(ctxt, ops);
2116 if (rc != 0)
2117 goto done;
2118 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002120
2121writeback:
2122 rc = writeback(ctxt, ops);
2123 if (rc != 0)
2124 goto done;
2125
2126 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002127 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002128 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002129
2130done:
2131 if (rc == X86EMUL_UNHANDLEABLE) {
2132 c->eip = saved_eip;
2133 return -1;
2134 }
2135 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136
2137twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002138 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002140 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 u16 size;
2142 unsigned long address;
2143
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002144 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002145 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002146 goto cannot_emulate;
2147
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002148 rc = kvm_fix_hypercall(ctxt->vcpu);
2149 if (rc)
2150 goto done;
2151
Avi Kivity33e38852008-05-21 15:34:25 +03002152 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002153 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002154 /* Disable writeback. */
2155 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002158 rc = read_descriptor(ctxt, ops, c->src.ptr,
2159 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 if (rc)
2161 goto done;
2162 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002163 /* Disable writeback. */
2164 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002166 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002167 if (c->modrm_mod == 3) {
2168 switch (c->modrm_rm) {
2169 case 1:
2170 rc = kvm_fix_hypercall(ctxt->vcpu);
2171 if (rc)
2172 goto done;
2173 break;
2174 default:
2175 goto cannot_emulate;
2176 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002177 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002178 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002179 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002180 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002181 if (rc)
2182 goto done;
2183 realmode_lidt(ctxt->vcpu, size, address);
2184 }
Avi Kivity16286d02008-04-14 14:40:50 +03002185 /* Disable writeback. */
2186 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 break;
2188 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002189 c->dst.bytes = 2;
2190 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 break;
2192 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002193 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2194 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002195 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196 break;
2197 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002198 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002199 /* Disable writeback. */
2200 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201 break;
2202 default:
2203 goto cannot_emulate;
2204 }
2205 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002206 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207 if (emulate_syscall(ctxt) == -1)
2208 goto cannot_emulate;
2209 else
2210 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002211 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002212 case 0x06:
2213 emulate_clts(ctxt->vcpu);
2214 c->dst.type = OP_NONE;
2215 break;
2216 case 0x08: /* invd */
2217 case 0x09: /* wbinvd */
2218 case 0x0d: /* GrpP (prefetch) */
2219 case 0x18: /* Grp16 (prefetch/nop) */
2220 c->dst.type = OP_NONE;
2221 break;
2222 case 0x20: /* mov cr, reg */
2223 if (c->modrm_mod != 3)
2224 goto cannot_emulate;
2225 c->regs[c->modrm_rm] =
2226 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2227 c->dst.type = OP_NONE; /* no writeback */
2228 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002230 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002232 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002233 if (rc)
2234 goto cannot_emulate;
2235 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002237 case 0x22: /* mov reg, cr */
2238 if (c->modrm_mod != 3)
2239 goto cannot_emulate;
2240 realmode_set_cr(ctxt->vcpu,
2241 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2242 c->dst.type = OP_NONE;
2243 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002245 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002247 rc = emulator_set_dr(ctxt, c->modrm_reg,
2248 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002249 if (rc)
2250 goto cannot_emulate;
2251 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002253 case 0x30:
2254 /* wrmsr */
2255 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2256 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2257 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2258 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002259 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002260 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002261 }
2262 rc = X86EMUL_CONTINUE;
2263 c->dst.type = OP_NONE;
2264 break;
2265 case 0x32:
2266 /* rdmsr */
2267 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2268 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002269 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002270 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002271 } else {
2272 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2273 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2274 }
2275 rc = X86EMUL_CONTINUE;
2276 c->dst.type = OP_NONE;
2277 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002278 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002279 if (emulate_sysenter(ctxt) == -1)
2280 goto cannot_emulate;
2281 else
2282 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002283 break;
2284 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002285 if (emulate_sysexit(ctxt) == -1)
2286 goto cannot_emulate;
2287 else
2288 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002289 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002291 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002292 if (!test_cc(c->b, ctxt->eflags))
2293 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002295 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002296 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002297 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002298 c->dst.type = OP_NONE;
2299 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002300 case 0xa3:
2301 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002302 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002303 /* only subword offset */
2304 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002305 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002306 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002307 case 0xa4: /* shld imm8, r, r/m */
2308 case 0xa5: /* shld cl, r, r/m */
2309 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2310 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002311 case 0xab:
2312 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002313 /* only subword offset */
2314 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002315 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002316 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002317 case 0xac: /* shrd imm8, r, r/m */
2318 case 0xad: /* shrd cl, r, r/m */
2319 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2320 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002321 case 0xae: /* clflush */
2322 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 case 0xb0 ... 0xb1: /* cmpxchg */
2324 /*
2325 * Save real source value, then compare EAX against
2326 * destination.
2327 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002328 c->src.orig_val = c->src.val;
2329 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002330 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2331 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002333 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 } else {
2335 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002336 c->dst.type = OP_REG;
2337 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 }
2339 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 case 0xb3:
2341 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002342 /* only subword offset */
2343 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002344 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002347 c->dst.bytes = c->op_bytes;
2348 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2349 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002352 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 case 0:
2354 goto bt;
2355 case 1:
2356 goto bts;
2357 case 2:
2358 goto btr;
2359 case 3:
2360 goto btc;
2361 }
2362 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002363 case 0xbb:
2364 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002365 /* only subword offset */
2366 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002367 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002368 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002370 c->dst.bytes = c->op_bytes;
2371 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2372 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002374 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002375 c->dst.bytes = c->op_bytes;
2376 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2377 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002378 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002380 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002381 if (rc != 0)
2382 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002383 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 }
2386 goto writeback;
2387
2388cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002389 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002390 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 return -1;
2392}