blob: 0644d3df621a6899094682dded391e37e32f66e9 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
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 Kivity56e82312009-08-12 15:04:37 +030033#include <asm/kvm_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 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
79#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010080/* Source 2 operand type */
81#define Src2None (0<<29)
82#define Src2CL (1<<29)
83#define Src2ImmByte (2<<29)
84#define Src2One (3<<29)
Gleb Natapova5f868b2009-04-12 13:36:14 +030085#define Src2Imm16 (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010086#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087
Avi Kivity43bb19c2008-01-18 12:46:50 +020088enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020089 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020090 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Avi Kivity43bb19c2008-01-18 12:46:50 +020091};
92
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010093static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080094 /* 0x00 - 0x07 */
95 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
96 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +030097 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +030098 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 /* 0x08 - 0x0F */
100 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
101 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300102 0, 0, ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 /* 0x10 - 0x17 */
104 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
105 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300106 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300107 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108 /* 0x18 - 0x1F */
109 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
110 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300111 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300112 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113 /* 0x20 - 0x27 */
114 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
115 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200116 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 /* 0x28 - 0x2F */
118 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
119 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
120 0, 0, 0, 0,
121 /* 0x30 - 0x37 */
122 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
123 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
124 0, 0, 0, 0,
125 /* 0x38 - 0x3F */
126 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
127 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200128 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
129 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700130 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200131 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700132 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200133 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300134 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200135 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
136 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300137 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200138 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
139 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700140 /* 0x60 - 0x67 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700142 0, 0, 0, 0,
143 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300144 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300145 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
146 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300147 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300148 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
149 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300150 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300151 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
152 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200154 Group | Group1_80, Group | Group1_81,
155 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
157 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
158 /* 0x88 - 0x8F */
159 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
160 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200161 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200162 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300163 /* 0x90 - 0x97 */
164 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
165 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300166 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300167 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200169 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
170 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200171 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
172 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200174 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
175 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
176 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300177 /* 0xB0 - 0xB7 */
178 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
179 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
180 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
181 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
182 /* 0xB8 - 0xBF */
183 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
184 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
185 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
186 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300188 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200189 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300190 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300192 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300193 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194 /* 0xD0 - 0xD7 */
195 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
196 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
197 0, 0, 0, 0,
198 /* 0xD8 - 0xDF */
199 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300200 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300201 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300202 ByteOp | SrcImmUByte, SrcImmUByte,
203 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300204 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300205 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300206 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300207 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
208 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 /* 0xF0 - 0xF7 */
210 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200211 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700213 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300214 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215};
216
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100217static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 /* 0x00 - 0x0F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200219 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200220 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0x10 - 0x1F */
222 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
223 /* 0x20 - 0x2F */
224 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
225 0, 0, 0, 0, 0, 0, 0, 0,
226 /* 0x30 - 0x3F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200227 ImplicitOps, 0, ImplicitOps, 0,
228 ImplicitOps, ImplicitOps, 0, 0,
229 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 /* 0x40 - 0x47 */
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 /* 0x48 - 0x4F */
236 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
237 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
238 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
239 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
240 /* 0x50 - 0x5F */
241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 /* 0x60 - 0x6F */
243 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244 /* 0x70 - 0x7F */
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300247 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
248 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249 /* 0x90 - 0x9F */
250 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300252 ImplicitOps | Stack, ImplicitOps | Stack,
253 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100254 DstMem | SrcReg | Src2ImmByte | ModRM,
255 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300257 ImplicitOps | Stack, ImplicitOps | Stack,
258 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100259 DstMem | SrcReg | Src2ImmByte | ModRM,
260 DstMem | SrcReg | Src2CL | ModRM,
261 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 /* 0xB0 - 0xB7 */
263 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800264 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
266 DstReg | SrcMem16 | ModRM | Mov,
267 /* 0xB8 - 0xBF */
Avi Kivity038e51d2007-01-22 20:40:40 -0800268 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
270 DstReg | SrcMem16 | ModRM | Mov,
271 /* 0xC0 - 0xCF */
Sheng Yanga012e652007-10-15 14:24:20 +0800272 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM,
273 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 /* 0xD0 - 0xDF */
275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276 /* 0xE0 - 0xEF */
277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278 /* 0xF0 - 0xFF */
279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
280};
281
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100282static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200283 [Group1_80*8] =
284 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
285 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
286 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
287 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
288 [Group1_81*8] =
289 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
290 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
291 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
292 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
293 [Group1_82*8] =
294 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
295 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
296 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
297 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
298 [Group1_83*8] =
299 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
300 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
301 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
302 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200303 [Group1A*8] =
304 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200305 [Group3_Byte*8] =
306 ByteOp | SrcImm | DstMem | ModRM, 0,
307 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
308 0, 0, 0, 0,
309 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400310 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300311 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200312 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200313 [Group4*8] =
314 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
315 0, 0, 0, 0, 0, 0,
316 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300317 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
318 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300319 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200320 [Group7*8] =
321 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300322 SrcNone | ModRM | DstMem | Mov, 0,
323 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Avi Kivitye09d0822008-01-18 12:38:59 +0200324};
325
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100326static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200327 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000328 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300329 SrcNone | ModRM | DstMem | Mov, 0,
330 SrcMem16 | ModRM | Mov, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200331};
332
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200334#define EFLG_VM (1<<17)
335#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336#define EFLG_OF (1<<11)
337#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200338#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339#define EFLG_SF (1<<7)
340#define EFLG_ZF (1<<6)
341#define EFLG_AF (1<<4)
342#define EFLG_PF (1<<2)
343#define EFLG_CF (1<<0)
344
345/*
346 * Instruction emulation:
347 * Most instructions are emulated directly via a fragment of inline assembly
348 * code. This allows us to save/restore EFLAGS and thus very easily pick up
349 * any modified flags.
350 */
351
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800352#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353#define _LO32 "k" /* force 32-bit operand */
354#define _STK "%%rsp" /* stack pointer */
355#elif defined(__i386__)
356#define _LO32 "" /* force 32-bit operand */
357#define _STK "%%esp" /* stack pointer */
358#endif
359
360/*
361 * These EFLAGS bits are restored from saved value during emulation, and
362 * any changes are written back to the saved value after emulation.
363 */
364#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
365
366/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200367#define _PRE_EFLAGS(_sav, _msk, _tmp) \
368 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
369 "movl %"_sav",%"_LO32 _tmp"; " \
370 "push %"_tmp"; " \
371 "push %"_tmp"; " \
372 "movl %"_msk",%"_LO32 _tmp"; " \
373 "andl %"_LO32 _tmp",("_STK"); " \
374 "pushf; " \
375 "notl %"_LO32 _tmp"; " \
376 "andl %"_LO32 _tmp",("_STK"); " \
377 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
378 "pop %"_tmp"; " \
379 "orl %"_LO32 _tmp",("_STK"); " \
380 "popf; " \
381 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382
383/* After executing instruction: write-back necessary bits in EFLAGS. */
384#define _POST_EFLAGS(_sav, _msk, _tmp) \
385 /* _sav |= EFLAGS & _msk; */ \
386 "pushf; " \
387 "pop %"_tmp"; " \
388 "andl %"_msk",%"_LO32 _tmp"; " \
389 "orl %"_LO32 _tmp",%"_sav"; "
390
Avi Kivitydda96d82008-11-26 15:14:10 +0200391#ifdef CONFIG_X86_64
392#define ON64(x) x
393#else
394#define ON64(x)
395#endif
396
Avi Kivity6b7ad612008-11-26 15:30:45 +0200397#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
398 do { \
399 __asm__ __volatile__ ( \
400 _PRE_EFLAGS("0", "4", "2") \
401 _op _suffix " %"_x"3,%1; " \
402 _POST_EFLAGS("0", "4", "2") \
403 : "=m" (_eflags), "=m" ((_dst).val), \
404 "=&r" (_tmp) \
405 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200406 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200407
408
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409/* Raw emulation: instruction has two explicit operands. */
410#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200411 do { \
412 unsigned long _tmp; \
413 \
414 switch ((_dst).bytes) { \
415 case 2: \
416 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
417 break; \
418 case 4: \
419 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
420 break; \
421 case 8: \
422 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
423 break; \
424 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 } while (0)
426
427#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
428 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200429 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400430 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200432 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 break; \
434 default: \
435 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
436 _wx, _wy, _lx, _ly, _qx, _qy); \
437 break; \
438 } \
439 } while (0)
440
441/* Source operand is byte-sized and may be restricted to just %cl. */
442#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
443 __emulate_2op(_op, _src, _dst, _eflags, \
444 "b", "c", "b", "c", "b", "c", "b", "c")
445
446/* Source operand is byte, word, long or quad sized. */
447#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
448 __emulate_2op(_op, _src, _dst, _eflags, \
449 "b", "q", "w", "r", _LO32, "r", "", "r")
450
451/* Source operand is word, long or quad sized. */
452#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
453 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
454 "w", "r", _LO32, "r", "", "r")
455
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100456/* Instruction has three operands and one operand is stored in ECX register */
457#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
458 do { \
459 unsigned long _tmp; \
460 _type _clv = (_cl).val; \
461 _type _srcv = (_src).val; \
462 _type _dstv = (_dst).val; \
463 \
464 __asm__ __volatile__ ( \
465 _PRE_EFLAGS("0", "5", "2") \
466 _op _suffix " %4,%1 \n" \
467 _POST_EFLAGS("0", "5", "2") \
468 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
469 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
470 ); \
471 \
472 (_cl).val = (unsigned long) _clv; \
473 (_src).val = (unsigned long) _srcv; \
474 (_dst).val = (unsigned long) _dstv; \
475 } while (0)
476
477#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
478 do { \
479 switch ((_dst).bytes) { \
480 case 2: \
481 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
482 "w", unsigned short); \
483 break; \
484 case 4: \
485 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
486 "l", unsigned int); \
487 break; \
488 case 8: \
489 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
490 "q", unsigned long)); \
491 break; \
492 } \
493 } while (0)
494
Avi Kivitydda96d82008-11-26 15:14:10 +0200495#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 do { \
497 unsigned long _tmp; \
498 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200499 __asm__ __volatile__ ( \
500 _PRE_EFLAGS("0", "3", "2") \
501 _op _suffix " %1; " \
502 _POST_EFLAGS("0", "3", "2") \
503 : "=m" (_eflags), "+m" ((_dst).val), \
504 "=&r" (_tmp) \
505 : "i" (EFLAGS_MASK)); \
506 } while (0)
507
508/* Instruction has only one explicit operand (no source operand). */
509#define emulate_1op(_op, _dst, _eflags) \
510 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400511 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200512 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
513 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
514 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
515 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 } \
517 } while (0)
518
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519/* Fetch next part of the instruction being emulated. */
520#define insn_fetch(_type, _size, _eip) \
521({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200522 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400523 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 goto done; \
525 (_eip) += (_size); \
526 (_type)_x; \
527})
528
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800529static inline unsigned long ad_mask(struct decode_cache *c)
530{
531 return (1UL << (c->ad_bytes << 3)) - 1;
532}
533
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800535static inline unsigned long
536address_mask(struct decode_cache *c, unsigned long reg)
537{
538 if (c->ad_bytes == sizeof(unsigned long))
539 return reg;
540 else
541 return reg & ad_mask(c);
542}
543
544static inline unsigned long
545register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
546{
547 return base + address_mask(c, reg);
548}
549
Harvey Harrison7a9572752008-02-19 07:40:41 -0800550static inline void
551register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
552{
553 if (c->ad_bytes == sizeof(unsigned long))
554 *reg += inc;
555 else
556 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
557}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Harvey Harrison7a9572752008-02-19 07:40:41 -0800559static inline void jmp_rel(struct decode_cache *c, int rel)
560{
561 register_address_increment(c, &c->eip, rel);
562}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300563
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300564static void set_seg_override(struct decode_cache *c, int seg)
565{
566 c->has_seg_override = true;
567 c->seg_override = seg;
568}
569
570static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
571{
572 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
573 return 0;
574
575 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
576}
577
578static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
579 struct decode_cache *c)
580{
581 if (!c->has_seg_override)
582 return 0;
583
584 return seg_base(ctxt, c->seg_override);
585}
586
587static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
588{
589 return seg_base(ctxt, VCPU_SREG_ES);
590}
591
592static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
593{
594 return seg_base(ctxt, VCPU_SREG_SS);
595}
596
Avi Kivity62266862007-11-20 13:15:52 +0200597static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
598 struct x86_emulate_ops *ops,
599 unsigned long linear, u8 *dest)
600{
601 struct fetch_cache *fc = &ctxt->decode.fetch;
602 int rc;
603 int size;
604
605 if (linear < fc->start || linear >= fc->end) {
606 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
607 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
608 if (rc)
609 return rc;
610 fc->start = linear;
611 fc->end = linear + size;
612 }
613 *dest = fc->data[linear - fc->start];
614 return 0;
615}
616
617static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
618 struct x86_emulate_ops *ops,
619 unsigned long eip, void *dest, unsigned size)
620{
621 int rc = 0;
622
623 eip += ctxt->cs_base;
624 while (size--) {
625 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
626 if (rc)
627 return rc;
628 }
629 return 0;
630}
631
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000632/*
633 * Given the 'reg' portion of a ModRM byte, and a register block, return a
634 * pointer into the block that addresses the relevant register.
635 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
636 */
637static void *decode_register(u8 modrm_reg, unsigned long *regs,
638 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639{
640 void *p;
641
642 p = &regs[modrm_reg];
643 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
644 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
645 return p;
646}
647
648static int read_descriptor(struct x86_emulate_ctxt *ctxt,
649 struct x86_emulate_ops *ops,
650 void *ptr,
651 u16 *size, unsigned long *address, int op_bytes)
652{
653 int rc;
654
655 if (op_bytes == 2)
656 op_bytes = 3;
657 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300658 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
659 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 if (rc)
661 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300662 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
663 ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 return rc;
665}
666
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300667static int test_cc(unsigned int condition, unsigned int flags)
668{
669 int rc = 0;
670
671 switch ((condition & 15) >> 1) {
672 case 0: /* o */
673 rc |= (flags & EFLG_OF);
674 break;
675 case 1: /* b/c/nae */
676 rc |= (flags & EFLG_CF);
677 break;
678 case 2: /* z/e */
679 rc |= (flags & EFLG_ZF);
680 break;
681 case 3: /* be/na */
682 rc |= (flags & (EFLG_CF|EFLG_ZF));
683 break;
684 case 4: /* s */
685 rc |= (flags & EFLG_SF);
686 break;
687 case 5: /* p/pe */
688 rc |= (flags & EFLG_PF);
689 break;
690 case 7: /* le/ng */
691 rc |= (flags & EFLG_ZF);
692 /* fall through */
693 case 6: /* l/nge */
694 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
695 break;
696 }
697
698 /* Odd condition identifiers (lsb == 1) have inverted sense. */
699 return (!!rc ^ (condition & 1));
700}
701
Avi Kivity3c118e22007-10-31 10:27:04 +0200702static void decode_register_operand(struct operand *op,
703 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200704 int inhibit_bytereg)
705{
Avi Kivity33615aa2007-10-31 11:15:56 +0200706 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200707 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200708
709 if (!(c->d & ModRM))
710 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200711 op->type = OP_REG;
712 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200713 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200714 op->val = *(u8 *)op->ptr;
715 op->bytes = 1;
716 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200717 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200718 op->bytes = c->op_bytes;
719 switch (op->bytes) {
720 case 2:
721 op->val = *(u16 *)op->ptr;
722 break;
723 case 4:
724 op->val = *(u32 *)op->ptr;
725 break;
726 case 8:
727 op->val = *(u64 *) op->ptr;
728 break;
729 }
730 }
731 op->orig_val = op->val;
732}
733
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200734static int decode_modrm(struct x86_emulate_ctxt *ctxt,
735 struct x86_emulate_ops *ops)
736{
737 struct decode_cache *c = &ctxt->decode;
738 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700739 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200740 int rc = 0;
741
742 if (c->rex_prefix) {
743 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
744 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
745 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
746 }
747
748 c->modrm = insn_fetch(u8, 1, c->eip);
749 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
750 c->modrm_reg |= (c->modrm & 0x38) >> 3;
751 c->modrm_rm |= (c->modrm & 0x07);
752 c->modrm_ea = 0;
753 c->use_modrm_ea = 1;
754
755 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300756 c->modrm_ptr = decode_register(c->modrm_rm,
757 c->regs, c->d & ByteOp);
758 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200759 return rc;
760 }
761
762 if (c->ad_bytes == 2) {
763 unsigned bx = c->regs[VCPU_REGS_RBX];
764 unsigned bp = c->regs[VCPU_REGS_RBP];
765 unsigned si = c->regs[VCPU_REGS_RSI];
766 unsigned di = c->regs[VCPU_REGS_RDI];
767
768 /* 16-bit ModR/M decode. */
769 switch (c->modrm_mod) {
770 case 0:
771 if (c->modrm_rm == 6)
772 c->modrm_ea += insn_fetch(u16, 2, c->eip);
773 break;
774 case 1:
775 c->modrm_ea += insn_fetch(s8, 1, c->eip);
776 break;
777 case 2:
778 c->modrm_ea += insn_fetch(u16, 2, c->eip);
779 break;
780 }
781 switch (c->modrm_rm) {
782 case 0:
783 c->modrm_ea += bx + si;
784 break;
785 case 1:
786 c->modrm_ea += bx + di;
787 break;
788 case 2:
789 c->modrm_ea += bp + si;
790 break;
791 case 3:
792 c->modrm_ea += bp + di;
793 break;
794 case 4:
795 c->modrm_ea += si;
796 break;
797 case 5:
798 c->modrm_ea += di;
799 break;
800 case 6:
801 if (c->modrm_mod != 0)
802 c->modrm_ea += bp;
803 break;
804 case 7:
805 c->modrm_ea += bx;
806 break;
807 }
808 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
809 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300810 if (!c->has_seg_override)
811 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200812 c->modrm_ea = (u16)c->modrm_ea;
813 } else {
814 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700815 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200816 sib = insn_fetch(u8, 1, c->eip);
817 index_reg |= (sib >> 3) & 7;
818 base_reg |= sib & 7;
819 scale = sib >> 6;
820
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700821 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
822 c->modrm_ea += insn_fetch(s32, 4, c->eip);
823 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200824 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700825 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200826 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700827 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
828 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700829 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700830 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200831 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200832 switch (c->modrm_mod) {
833 case 0:
834 if (c->modrm_rm == 5)
835 c->modrm_ea += insn_fetch(s32, 4, c->eip);
836 break;
837 case 1:
838 c->modrm_ea += insn_fetch(s8, 1, c->eip);
839 break;
840 case 2:
841 c->modrm_ea += insn_fetch(s32, 4, c->eip);
842 break;
843 }
844 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200845done:
846 return rc;
847}
848
849static int decode_abs(struct x86_emulate_ctxt *ctxt,
850 struct x86_emulate_ops *ops)
851{
852 struct decode_cache *c = &ctxt->decode;
853 int rc = 0;
854
855 switch (c->ad_bytes) {
856 case 2:
857 c->modrm_ea = insn_fetch(u16, 2, c->eip);
858 break;
859 case 4:
860 c->modrm_ea = insn_fetch(u32, 4, c->eip);
861 break;
862 case 8:
863 c->modrm_ea = insn_fetch(u64, 8, c->eip);
864 break;
865 }
866done:
867 return rc;
868}
869
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200871x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200873 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200876 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
878 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879
Laurent Viviere4e03de2007-09-18 11:52:50 +0200880 memset(c, 0, sizeof(struct decode_cache));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300881 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300882 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800883 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
885 switch (mode) {
886 case X86EMUL_MODE_REAL:
887 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200888 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 break;
890 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200891 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800893#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200895 def_op_bytes = 4;
896 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 break;
898#endif
899 default:
900 return -1;
901 }
902
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200903 c->op_bytes = def_op_bytes;
904 c->ad_bytes = def_ad_bytes;
905
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200907 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200908 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200910 /* switch between 2/4 bytes */
911 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 break;
913 case 0x67: /* address-size override */
914 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200915 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200916 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200918 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200919 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300922 case 0x2e: /* CS override */
923 case 0x36: /* SS override */
924 case 0x3e: /* DS override */
925 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 break;
927 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300929 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200931 case 0x40 ... 0x4f: /* REX */
932 if (mode != X86EMUL_MODE_PROT64)
933 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200934 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200935 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200937 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200939 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100940 c->rep_prefix = REPNE_PREFIX;
941 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100943 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 default:
946 goto done_prefixes;
947 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200948
949 /* Any legacy prefix after a REX prefix nullifies its effect. */
950
Avi Kivity33615aa2007-10-31 11:15:56 +0200951 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 }
953
954done_prefixes:
955
956 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200958 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200959 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960
961 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200962 c->d = opcode_table[c->b];
963 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200965 if (c->b == 0x0f) {
966 c->twobyte = 1;
967 c->b = insn_fetch(u8, 1, c->eip);
968 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200970 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300972 if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
973 kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");;
974 return -1;
975 }
976
Avi Kivitye09d0822008-01-18 12:38:59 +0200977 if (c->d & Group) {
978 group = c->d & GroupMask;
979 c->modrm = insn_fetch(u8, 1, c->eip);
980 --c->eip;
981
982 group = (group << 3) + ((c->modrm >> 3) & 7);
983 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
984 c->d = group2_table[group];
985 else
986 c->d = group_table[group];
987 }
988
989 /* Unrecognised? */
990 if (c->d == 0) {
991 DPRINTF("Cannot emulate %02x\n", c->b);
992 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 }
994
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200995 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
996 c->op_bytes = 8;
997
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200999 if (c->d & ModRM)
1000 rc = decode_modrm(ctxt, ops);
1001 else if (c->d & MemAbs)
1002 rc = decode_abs(ctxt, ops);
1003 if (rc)
1004 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001006 if (!c->has_seg_override)
1007 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001008
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001009 if (!(!c->twobyte && c->b == 0x8d))
1010 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001011
1012 if (c->ad_bytes != 8)
1013 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 /*
1015 * Decode and fetch the source operand: register, memory
1016 * or immediate.
1017 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001018 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 case SrcNone:
1020 break;
1021 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001022 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 break;
1024 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001025 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 goto srcmem_common;
1027 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001028 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 goto srcmem_common;
1030 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001031 c->src.bytes = (c->d & ByteOp) ? 1 :
1032 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001033 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001034 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001035 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001036 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001037 /*
1038 * For instructions with a ModR/M byte, switch to register
1039 * access if Mod = 3.
1040 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 if ((c->d & ModRM) && c->modrm_mod == 3) {
1042 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001043 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001044 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001045 break;
1046 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001047 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 break;
1049 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001050 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001051 c->src.type = OP_IMM;
1052 c->src.ptr = (unsigned long *)c->eip;
1053 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1054 if (c->src.bytes == 8)
1055 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001057 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001059 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 break;
1061 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001062 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 break;
1064 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001065 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 break;
1067 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001068 if ((c->d & SrcMask) == SrcImmU) {
1069 switch (c->src.bytes) {
1070 case 1:
1071 c->src.val &= 0xff;
1072 break;
1073 case 2:
1074 c->src.val &= 0xffff;
1075 break;
1076 case 4:
1077 c->src.val &= 0xffffffff;
1078 break;
1079 }
1080 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 break;
1082 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001083 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001084 c->src.type = OP_IMM;
1085 c->src.ptr = (unsigned long *)c->eip;
1086 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001087 if ((c->d & SrcMask) == SrcImmByte)
1088 c->src.val = insn_fetch(s8, 1, c->eip);
1089 else
1090 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001092 case SrcOne:
1093 c->src.bytes = 1;
1094 c->src.val = 1;
1095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 }
1097
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001098 /*
1099 * Decode and fetch the second source operand: register, memory
1100 * or immediate.
1101 */
1102 switch (c->d & Src2Mask) {
1103 case Src2None:
1104 break;
1105 case Src2CL:
1106 c->src2.bytes = 1;
1107 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1108 break;
1109 case Src2ImmByte:
1110 c->src2.type = OP_IMM;
1111 c->src2.ptr = (unsigned long *)c->eip;
1112 c->src2.bytes = 1;
1113 c->src2.val = insn_fetch(u8, 1, c->eip);
1114 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001115 case Src2Imm16:
1116 c->src2.type = OP_IMM;
1117 c->src2.ptr = (unsigned long *)c->eip;
1118 c->src2.bytes = 2;
1119 c->src2.val = insn_fetch(u16, 2, c->eip);
1120 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001121 case Src2One:
1122 c->src2.bytes = 1;
1123 c->src2.val = 1;
1124 break;
1125 }
1126
Avi Kivity038e51d2007-01-22 20:40:40 -08001127 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001128 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001129 case ImplicitOps:
1130 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001131 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001132 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001133 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001134 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001135 break;
1136 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001137 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001138 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001139 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001140 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001141 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001142 break;
1143 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001144 c->dst.type = OP_MEM;
1145 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001146 case DstAcc:
1147 c->dst.type = OP_REG;
1148 c->dst.bytes = c->op_bytes;
1149 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1150 switch (c->op_bytes) {
1151 case 1:
1152 c->dst.val = *(u8 *)c->dst.ptr;
1153 break;
1154 case 2:
1155 c->dst.val = *(u16 *)c->dst.ptr;
1156 break;
1157 case 4:
1158 c->dst.val = *(u32 *)c->dst.ptr;
1159 break;
1160 }
1161 c->dst.orig_val = c->dst.val;
1162 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001163 }
1164
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001165 if (c->rip_relative)
1166 c->modrm_ea += c->eip;
1167
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001168done:
1169 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1170}
1171
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001172static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1173{
1174 struct decode_cache *c = &ctxt->decode;
1175
1176 c->dst.type = OP_MEM;
1177 c->dst.bytes = c->op_bytes;
1178 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001179 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001180 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001181 c->regs[VCPU_REGS_RSP]);
1182}
1183
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001184static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001185 struct x86_emulate_ops *ops,
1186 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187{
1188 struct decode_cache *c = &ctxt->decode;
1189 int rc;
1190
Avi Kivity781d0ed2008-11-27 18:00:28 +02001191 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1192 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001193 dest, len, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001194 if (rc != 0)
1195 return rc;
1196
Avi Kivity350f69d2009-01-05 11:12:40 +02001197 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001198 return rc;
1199}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001200
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001201static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1202{
1203 struct decode_cache *c = &ctxt->decode;
1204 struct kvm_segment segment;
1205
1206 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1207
1208 c->src.val = segment.selector;
1209 emulate_push(ctxt);
1210}
1211
1212static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1213 struct x86_emulate_ops *ops, int seg)
1214{
1215 struct decode_cache *c = &ctxt->decode;
1216 unsigned long selector;
1217 int rc;
1218
1219 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1220 if (rc != 0)
1221 return rc;
1222
1223 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg);
1224 return rc;
1225}
1226
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001227static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1228 struct x86_emulate_ops *ops)
1229{
1230 struct decode_cache *c = &ctxt->decode;
1231 int rc;
1232
Avi Kivity350f69d2009-01-05 11:12:40 +02001233 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001234 if (rc != 0)
1235 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001236 return 0;
1237}
1238
Laurent Vivier05f086f2007-09-24 11:10:55 +02001239static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001240{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001241 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001242 switch (c->modrm_reg) {
1243 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001244 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001245 break;
1246 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001247 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248 break;
1249 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001250 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251 break;
1252 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001253 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001254 break;
1255 case 4: /* sal/shl */
1256 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001257 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258 break;
1259 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001260 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261 break;
1262 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001263 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001264 break;
1265 }
1266}
1267
1268static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001269 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270{
1271 struct decode_cache *c = &ctxt->decode;
1272 int rc = 0;
1273
1274 switch (c->modrm_reg) {
1275 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001276 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001277 break;
1278 case 2: /* not */
1279 c->dst.val = ~c->dst.val;
1280 break;
1281 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001282 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001283 break;
1284 default:
1285 DPRINTF("Cannot emulate %02x\n", c->b);
1286 rc = X86EMUL_UNHANDLEABLE;
1287 break;
1288 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001289 return rc;
1290}
1291
1292static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001293 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001294{
1295 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001296
1297 switch (c->modrm_reg) {
1298 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001299 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300 break;
1301 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001302 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001303 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001304 case 2: /* call near abs */ {
1305 long int old_eip;
1306 old_eip = c->eip;
1307 c->eip = c->src.val;
1308 c->src.val = old_eip;
1309 emulate_push(ctxt);
1310 break;
1311 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001312 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001313 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001314 break;
1315 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001316 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001317 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001318 }
1319 return 0;
1320}
1321
1322static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1323 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001324 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001325{
1326 struct decode_cache *c = &ctxt->decode;
1327 u64 old, new;
1328 int rc;
1329
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001330 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001331 if (rc != 0)
1332 return rc;
1333
1334 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1335 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1336
1337 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1338 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001339 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001340
1341 } else {
1342 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1343 (u32) c->regs[VCPU_REGS_RBX];
1344
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001345 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 if (rc != 0)
1347 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001348 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001349 }
1350 return 0;
1351}
1352
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001353static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1354 struct x86_emulate_ops *ops)
1355{
1356 struct decode_cache *c = &ctxt->decode;
1357 int rc;
1358 unsigned long cs;
1359
1360 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1361 if (rc)
1362 return rc;
1363 if (c->op_bytes == 4)
1364 c->eip = (u32)c->eip;
1365 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1366 if (rc)
1367 return rc;
1368 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1369 return rc;
1370}
1371
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001372static inline int writeback(struct x86_emulate_ctxt *ctxt,
1373 struct x86_emulate_ops *ops)
1374{
1375 int rc;
1376 struct decode_cache *c = &ctxt->decode;
1377
1378 switch (c->dst.type) {
1379 case OP_REG:
1380 /* The 4-byte case *is* correct:
1381 * in 64-bit mode we zero-extend.
1382 */
1383 switch (c->dst.bytes) {
1384 case 1:
1385 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1386 break;
1387 case 2:
1388 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1389 break;
1390 case 4:
1391 *c->dst.ptr = (u32)c->dst.val;
1392 break; /* 64b: zero-ext */
1393 case 8:
1394 *c->dst.ptr = c->dst.val;
1395 break;
1396 }
1397 break;
1398 case OP_MEM:
1399 if (c->lock_prefix)
1400 rc = ops->cmpxchg_emulated(
1401 (unsigned long)c->dst.ptr,
1402 &c->dst.orig_val,
1403 &c->dst.val,
1404 c->dst.bytes,
1405 ctxt->vcpu);
1406 else
1407 rc = ops->write_emulated(
1408 (unsigned long)c->dst.ptr,
1409 &c->dst.val,
1410 c->dst.bytes,
1411 ctxt->vcpu);
1412 if (rc != 0)
1413 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001414 break;
1415 case OP_NONE:
1416 /* no writeback */
1417 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001418 default:
1419 break;
1420 }
1421 return 0;
1422}
1423
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301424static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001425{
1426 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1427 /*
1428 * an sti; sti; sequence only disable interrupts for the first
1429 * instruction. So, if the last instruction, be it emulated or
1430 * not, left the system with the INT_STI flag enabled, it
1431 * means that the last instruction is an sti. We should not
1432 * leave the flag on in this case. The same goes for mov ss
1433 */
1434 if (!(int_shadow & mask))
1435 ctxt->interruptibility = mask;
1436}
1437
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001438static inline void
1439setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1440 struct kvm_segment *cs, struct kvm_segment *ss)
1441{
1442 memset(cs, 0, sizeof(struct kvm_segment));
1443 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1444 memset(ss, 0, sizeof(struct kvm_segment));
1445
1446 cs->l = 0; /* will be adjusted later */
1447 cs->base = 0; /* flat segment */
1448 cs->g = 1; /* 4kb granularity */
1449 cs->limit = 0xffffffff; /* 4GB limit */
1450 cs->type = 0x0b; /* Read, Execute, Accessed */
1451 cs->s = 1;
1452 cs->dpl = 0; /* will be adjusted later */
1453 cs->present = 1;
1454 cs->db = 1;
1455
1456 ss->unusable = 0;
1457 ss->base = 0; /* flat segment */
1458 ss->limit = 0xffffffff; /* 4GB limit */
1459 ss->g = 1; /* 4kb granularity */
1460 ss->s = 1;
1461 ss->type = 0x03; /* Read/Write, Accessed */
1462 ss->db = 1; /* 32bit stack segment */
1463 ss->dpl = 0;
1464 ss->present = 1;
1465}
1466
1467static int
1468emulate_syscall(struct x86_emulate_ctxt *ctxt)
1469{
1470 struct decode_cache *c = &ctxt->decode;
1471 struct kvm_segment cs, ss;
1472 u64 msr_data;
1473
1474 /* syscall is not available in real mode */
1475 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
1476 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE))
1477 return -1;
1478
1479 setup_syscalls_segments(ctxt, &cs, &ss);
1480
1481 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1482 msr_data >>= 32;
1483 cs.selector = (u16)(msr_data & 0xfffc);
1484 ss.selector = (u16)(msr_data + 8);
1485
1486 if (is_long_mode(ctxt->vcpu)) {
1487 cs.db = 0;
1488 cs.l = 1;
1489 }
1490 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1491 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1492
1493 c->regs[VCPU_REGS_RCX] = c->eip;
1494 if (is_long_mode(ctxt->vcpu)) {
1495#ifdef CONFIG_X86_64
1496 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1497
1498 kvm_x86_ops->get_msr(ctxt->vcpu,
1499 ctxt->mode == X86EMUL_MODE_PROT64 ?
1500 MSR_LSTAR : MSR_CSTAR, &msr_data);
1501 c->eip = msr_data;
1502
1503 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1504 ctxt->eflags &= ~(msr_data | EFLG_RF);
1505#endif
1506 } else {
1507 /* legacy mode */
1508 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1509 c->eip = (u32)msr_data;
1510
1511 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1512 }
1513
1514 return 0;
1515}
1516
Andre Przywara8c604352009-06-18 12:56:01 +02001517static int
1518emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1519{
1520 struct decode_cache *c = &ctxt->decode;
1521 struct kvm_segment cs, ss;
1522 u64 msr_data;
1523
1524 /* inject #UD if LOCK prefix is used */
1525 if (c->lock_prefix)
1526 return -1;
1527
1528 /* inject #GP if in real mode or paging is disabled */
1529 if (ctxt->mode == X86EMUL_MODE_REAL ||
1530 !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1531 kvm_inject_gp(ctxt->vcpu, 0);
1532 return -1;
1533 }
1534
1535 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1536 * Therefore, we inject an #UD.
1537 */
1538 if (ctxt->mode == X86EMUL_MODE_PROT64)
1539 return -1;
1540
1541 setup_syscalls_segments(ctxt, &cs, &ss);
1542
1543 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1544 switch (ctxt->mode) {
1545 case X86EMUL_MODE_PROT32:
1546 if ((msr_data & 0xfffc) == 0x0) {
1547 kvm_inject_gp(ctxt->vcpu, 0);
1548 return -1;
1549 }
1550 break;
1551 case X86EMUL_MODE_PROT64:
1552 if (msr_data == 0x0) {
1553 kvm_inject_gp(ctxt->vcpu, 0);
1554 return -1;
1555 }
1556 break;
1557 }
1558
1559 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1560 cs.selector = (u16)msr_data;
1561 cs.selector &= ~SELECTOR_RPL_MASK;
1562 ss.selector = cs.selector + 8;
1563 ss.selector &= ~SELECTOR_RPL_MASK;
1564 if (ctxt->mode == X86EMUL_MODE_PROT64
1565 || is_long_mode(ctxt->vcpu)) {
1566 cs.db = 0;
1567 cs.l = 1;
1568 }
1569
1570 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1571 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1572
1573 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1574 c->eip = msr_data;
1575
1576 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1577 c->regs[VCPU_REGS_RSP] = msr_data;
1578
1579 return 0;
1580}
1581
Andre Przywara4668f052009-06-18 12:56:02 +02001582static int
1583emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1584{
1585 struct decode_cache *c = &ctxt->decode;
1586 struct kvm_segment cs, ss;
1587 u64 msr_data;
1588 int usermode;
1589
1590 /* inject #UD if LOCK prefix is used */
1591 if (c->lock_prefix)
1592 return -1;
1593
1594 /* inject #GP if in real mode or paging is disabled */
1595 if (ctxt->mode == X86EMUL_MODE_REAL
1596 || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) {
1597 kvm_inject_gp(ctxt->vcpu, 0);
1598 return -1;
1599 }
1600
1601 /* sysexit must be called from CPL 0 */
1602 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1603 kvm_inject_gp(ctxt->vcpu, 0);
1604 return -1;
1605 }
1606
1607 setup_syscalls_segments(ctxt, &cs, &ss);
1608
1609 if ((c->rex_prefix & 0x8) != 0x0)
1610 usermode = X86EMUL_MODE_PROT64;
1611 else
1612 usermode = X86EMUL_MODE_PROT32;
1613
1614 cs.dpl = 3;
1615 ss.dpl = 3;
1616 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1617 switch (usermode) {
1618 case X86EMUL_MODE_PROT32:
1619 cs.selector = (u16)(msr_data + 16);
1620 if ((msr_data & 0xfffc) == 0x0) {
1621 kvm_inject_gp(ctxt->vcpu, 0);
1622 return -1;
1623 }
1624 ss.selector = (u16)(msr_data + 24);
1625 break;
1626 case X86EMUL_MODE_PROT64:
1627 cs.selector = (u16)(msr_data + 32);
1628 if (msr_data == 0x0) {
1629 kvm_inject_gp(ctxt->vcpu, 0);
1630 return -1;
1631 }
1632 ss.selector = cs.selector + 8;
1633 cs.db = 0;
1634 cs.l = 1;
1635 break;
1636 }
1637 cs.selector |= SELECTOR_RPL_MASK;
1638 ss.selector |= SELECTOR_RPL_MASK;
1639
1640 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1641 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1642
1643 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1644 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1645
1646 return 0;
1647}
1648
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001649int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001650x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001651{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001652 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001653 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001654 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001655 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001656 unsigned int port;
1657 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001658 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001659
Glauber Costa310b5d32009-05-12 16:21:06 -04001660 ctxt->interruptibility = 0;
1661
Laurent Vivier34273182007-09-18 11:27:37 +02001662 /* Shadow copy of register state. Committed on successful emulation.
1663 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1664 * modify them.
1665 */
1666
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001667 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001668 saved_eip = c->eip;
1669
Avi Kivityc7e75a32007-10-28 16:34:25 +02001670 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001671 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001672
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001673 if (c->rep_prefix && (c->d & String)) {
1674 /* All REP prefixes have the same first termination condition */
1675 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001676 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001677 goto done;
1678 }
1679 /* The second termination condition only applies for REPE
1680 * and REPNE. Test if the repeat string operation prefix is
1681 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1682 * corresponding termination condition according to:
1683 * - if REPE/REPZ and ZF = 0 then done
1684 * - if REPNE/REPNZ and ZF = 1 then done
1685 */
1686 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1687 (c->b == 0xae) || (c->b == 0xaf)) {
1688 if ((c->rep_prefix == REPE_PREFIX) &&
1689 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001690 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001691 goto done;
1692 }
1693 if ((c->rep_prefix == REPNE_PREFIX) &&
1694 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001695 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001696 goto done;
1697 }
1698 }
1699 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001700 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001701 }
1702
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001703 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001704 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001705 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001706 rc = ops->read_emulated((unsigned long)c->src.ptr,
1707 &c->src.val,
1708 c->src.bytes,
1709 ctxt->vcpu);
1710 if (rc != 0)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001711 goto done;
1712 c->src.orig_val = c->src.val;
1713 }
1714
1715 if ((c->d & DstMask) == ImplicitOps)
1716 goto special_insn;
1717
1718
1719 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001720 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001721 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1722 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001723 if (c->d & BitOp) {
1724 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001725
Laurent Viviere4e03de2007-09-18 11:52:50 +02001726 c->dst.ptr = (void *)c->dst.ptr +
1727 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001728 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001729 if (!(c->d & Mov) &&
1730 /* optimisation - avoid slow emulated read */
1731 ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
1732 &c->dst.val,
1733 c->dst.bytes, ctxt->vcpu)) != 0))
Avi Kivity038e51d2007-01-22 20:40:40 -08001734 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08001735 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001736 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001737
Avi Kivity018a98d2007-11-27 19:30:56 +02001738special_insn:
1739
Laurent Viviere4e03de2007-09-18 11:52:50 +02001740 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741 goto twobyte_insn;
1742
Laurent Viviere4e03de2007-09-18 11:52:50 +02001743 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 case 0x00 ... 0x05:
1745 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001746 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001748 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001749 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1750 break;
1751 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001752 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1753 if (rc != 0)
1754 goto done;
1755 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756 case 0x08 ... 0x0d:
1757 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001758 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001760 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001761 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1762 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 case 0x10 ... 0x15:
1764 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001765 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001767 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001768 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1769 break;
1770 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001771 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1772 if (rc != 0)
1773 goto done;
1774 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775 case 0x18 ... 0x1d:
1776 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001777 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001779 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001780 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1781 break;
1782 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001783 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1784 if (rc != 0)
1785 goto done;
1786 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001787 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001789 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 break;
1791 case 0x28 ... 0x2d:
1792 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001793 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 break;
1795 case 0x30 ... 0x35:
1796 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001797 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 break;
1799 case 0x38 ... 0x3d:
1800 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001801 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001803 case 0x40 ... 0x47: /* inc r16/r32 */
1804 emulate_1op("inc", c->dst, ctxt->eflags);
1805 break;
1806 case 0x48 ... 0x4f: /* dec r16/r32 */
1807 emulate_1op("dec", c->dst, ctxt->eflags);
1808 break;
1809 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001810 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001811 break;
1812 case 0x58 ... 0x5f: /* pop reg */
1813 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001814 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001815 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001816 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001817 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001819 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001821 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001823 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001824 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001825 emulate_push(ctxt);
1826 break;
1827 case 0x6c: /* insb */
1828 case 0x6d: /* insw/insd */
Avi Kivity851ba692009-08-24 11:10:17 +03001829 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001830 1,
1831 (c->d & ByteOp) ? 1 : c->op_bytes,
1832 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001833 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001834 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001835 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001836 c->regs[VCPU_REGS_RDI]),
1837 c->rep_prefix,
1838 c->regs[VCPU_REGS_RDX]) == 0) {
1839 c->eip = saved_eip;
1840 return -1;
1841 }
1842 return 0;
1843 case 0x6e: /* outsb */
1844 case 0x6f: /* outsw/outsd */
Avi Kivity851ba692009-08-24 11:10:17 +03001845 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001846 0,
1847 (c->d & ByteOp) ? 1 : c->op_bytes,
1848 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001849 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001850 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001851 register_address(c,
1852 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001853 c->regs[VCPU_REGS_RSI]),
1854 c->rep_prefix,
1855 c->regs[VCPU_REGS_RDX]) == 0) {
1856 c->eip = saved_eip;
1857 return -1;
1858 }
1859 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001860 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001861 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001862 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001863 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001865 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 case 0:
1867 goto add;
1868 case 1:
1869 goto or;
1870 case 2:
1871 goto adc;
1872 case 3:
1873 goto sbb;
1874 case 4:
1875 goto and;
1876 case 5:
1877 goto sub;
1878 case 6:
1879 goto xor;
1880 case 7:
1881 goto cmp;
1882 }
1883 break;
1884 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001885 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886 break;
1887 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001888 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001890 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001892 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 break;
1894 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001895 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896 break;
1897 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001898 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 break; /* 64b reg: zero-extend */
1900 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001901 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 break;
1903 }
1904 /*
1905 * Write back the memory destination with implicit LOCK
1906 * prefix.
1907 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001908 c->dst.val = c->src.val;
1909 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001912 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001913 case 0x8c: { /* mov r/m, sreg */
1914 struct kvm_segment segreg;
1915
1916 if (c->modrm_reg <= 5)
1917 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1918 else {
1919 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1920 c->modrm);
1921 goto cannot_emulate;
1922 }
1923 c->dst.val = segreg.selector;
1924 break;
1925 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001926 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001927 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001928 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001929 case 0x8e: { /* mov seg, r/m16 */
1930 uint16_t sel;
1931 int type_bits;
1932 int err;
1933
1934 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001935 if (c->modrm_reg == VCPU_SREG_SS)
1936 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1937
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001938 if (c->modrm_reg <= 5) {
1939 type_bits = (c->modrm_reg == 1) ? 9 : 1;
1940 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
1941 type_bits, c->modrm_reg);
1942 } else {
1943 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
1944 c->modrm);
1945 goto cannot_emulate;
1946 }
1947
1948 if (err < 0)
1949 goto cannot_emulate;
1950
1951 c->dst.type = OP_NONE; /* Disable writeback. */
1952 break;
1953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001955 rc = emulate_grp1a(ctxt, ops);
1956 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001959 case 0x90: /* nop / xchg r8,rax */
1960 if (!(c->rex_prefix & 1)) { /* nop */
1961 c->dst.type = OP_NONE;
1962 break;
1963 }
1964 case 0x91 ... 0x97: /* xchg reg,rax */
1965 c->src.type = c->dst.type = OP_REG;
1966 c->src.bytes = c->dst.bytes = c->op_bytes;
1967 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
1968 c->src.val = *(c->src.ptr);
1969 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07001970 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001971 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001972 emulate_push(ctxt);
1973 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001974 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02001975 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001976 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02001977 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03001978 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02001979 case 0xa0 ... 0xa1: /* mov */
1980 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
1981 c->dst.val = c->src.val;
1982 break;
1983 case 0xa2 ... 0xa3: /* mov */
1984 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
1985 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001987 c->dst.type = OP_MEM;
1988 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08001989 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001990 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001991 c->regs[VCPU_REGS_RDI]);
Harvey Harrisone4706772008-02-19 07:40:38 -08001992 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001993 seg_override_base(ctxt, c),
Laurent Viviere4e03de2007-09-18 11:52:50 +02001994 c->regs[VCPU_REGS_RSI]),
1995 &c->dst.val,
1996 c->dst.bytes, ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001998 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02001999 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002000 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002001 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002002 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002003 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 break;
2005 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002006 c->src.type = OP_NONE; /* Disable writeback. */
2007 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002008 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002009 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002010 c->regs[VCPU_REGS_RSI]);
2011 if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
2012 &c->src.val,
2013 c->src.bytes,
2014 ctxt->vcpu)) != 0)
2015 goto done;
2016
2017 c->dst.type = OP_NONE; /* Disable writeback. */
2018 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002019 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002020 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002021 c->regs[VCPU_REGS_RDI]);
2022 if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
2023 &c->dst.val,
2024 c->dst.bytes,
2025 ctxt->vcpu)) != 0)
2026 goto done;
2027
2028 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2029
2030 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2031
Harvey Harrison7a9572752008-02-19 07:40:41 -08002032 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002033 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2034 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002035 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002036 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2037 : c->dst.bytes);
2038
2039 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002041 c->dst.type = OP_MEM;
2042 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002043 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002044 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002045 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002046 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002047 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002048 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002049 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 break;
2051 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002052 c->dst.type = OP_REG;
2053 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2054 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Harvey Harrisone4706772008-02-19 07:40:38 -08002055 if ((rc = ops->read_emulated(register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002056 seg_override_base(ctxt, c),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002057 c->regs[VCPU_REGS_RSI]),
2058 &c->dst.val,
2059 c->dst.bytes,
2060 ctxt->vcpu)) != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002062 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002063 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002064 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 break;
2066 case 0xae ... 0xaf: /* scas */
2067 DPRINTF("Urk! I don't handle SCAS.\n");
2068 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002069 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002070 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002071 case 0xc0 ... 0xc1:
2072 emulate_grp2(ctxt);
2073 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002074 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002075 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002076 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002077 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002078 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002079 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2080 mov:
2081 c->dst.val = c->src.val;
2082 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002083 case 0xcb: /* ret far */
2084 rc = emulate_ret_far(ctxt, ops);
2085 if (rc)
2086 goto done;
2087 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002088 case 0xd0 ... 0xd1: /* Grp2 */
2089 c->src.val = 1;
2090 emulate_grp2(ctxt);
2091 break;
2092 case 0xd2 ... 0xd3: /* Grp2 */
2093 c->src.val = c->regs[VCPU_REGS_RCX];
2094 emulate_grp2(ctxt);
2095 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002096 case 0xe4: /* inb */
2097 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002098 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002099 io_dir_in = 1;
2100 goto do_io;
2101 case 0xe6: /* outb */
2102 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002103 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002104 io_dir_in = 0;
2105 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002106 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002107 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002108 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002109 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002110 emulate_push(ctxt);
2111 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002112 }
2113 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002114 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002115 case 0xea: /* jmp far */
2116 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2117 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002118 DPRINTF("jmp far: Failed to load CS descriptor\n");
2119 goto cannot_emulate;
2120 }
2121
Gleb Natapov782b8772009-04-12 13:36:25 +03002122 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002123 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002124 case 0xeb:
2125 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002126 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002127 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002128 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002129 case 0xec: /* in al,dx */
2130 case 0xed: /* in (e/r)ax,dx */
2131 port = c->regs[VCPU_REGS_RDX];
2132 io_dir_in = 1;
2133 goto do_io;
2134 case 0xee: /* out al,dx */
2135 case 0xef: /* out (e/r)ax,dx */
2136 port = c->regs[VCPU_REGS_RDX];
2137 io_dir_in = 0;
Avi Kivity851ba692009-08-24 11:10:17 +03002138 do_io: if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002139 (c->d & ByteOp) ? 1 : c->op_bytes,
2140 port) != 0) {
2141 c->eip = saved_eip;
2142 goto cannot_emulate;
2143 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002144 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002145 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002146 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002147 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002148 case 0xf5: /* cmc */
2149 /* complement carry flag from eflags reg */
2150 ctxt->eflags ^= EFLG_CF;
2151 c->dst.type = OP_NONE; /* Disable writeback. */
2152 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002153 case 0xf6 ... 0xf7: /* Grp3 */
2154 rc = emulate_grp3(ctxt, ops);
2155 if (rc != 0)
2156 goto done;
2157 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002158 case 0xf8: /* clc */
2159 ctxt->eflags &= ~EFLG_CF;
2160 c->dst.type = OP_NONE; /* Disable writeback. */
2161 break;
2162 case 0xfa: /* cli */
2163 ctxt->eflags &= ~X86_EFLAGS_IF;
2164 c->dst.type = OP_NONE; /* Disable writeback. */
2165 break;
2166 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04002167 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02002168 ctxt->eflags |= X86_EFLAGS_IF;
2169 c->dst.type = OP_NONE; /* Disable writeback. */
2170 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002171 case 0xfc: /* cld */
2172 ctxt->eflags &= ~EFLG_DF;
2173 c->dst.type = OP_NONE; /* Disable writeback. */
2174 break;
2175 case 0xfd: /* std */
2176 ctxt->eflags |= EFLG_DF;
2177 c->dst.type = OP_NONE; /* Disable writeback. */
2178 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002179 case 0xfe ... 0xff: /* Grp4/Grp5 */
2180 rc = emulate_grp45(ctxt, ops);
2181 if (rc != 0)
2182 goto done;
2183 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002185
2186writeback:
2187 rc = writeback(ctxt, ops);
2188 if (rc != 0)
2189 goto done;
2190
2191 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002192 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002193 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002194
2195done:
2196 if (rc == X86EMUL_UNHANDLEABLE) {
2197 c->eip = saved_eip;
2198 return -1;
2199 }
2200 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201
2202twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002203 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002205 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 u16 size;
2207 unsigned long address;
2208
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002209 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002210 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002211 goto cannot_emulate;
2212
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002213 rc = kvm_fix_hypercall(ctxt->vcpu);
2214 if (rc)
2215 goto done;
2216
Avi Kivity33e38852008-05-21 15:34:25 +03002217 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002218 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002219 /* Disable writeback. */
2220 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002221 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002223 rc = read_descriptor(ctxt, ops, c->src.ptr,
2224 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225 if (rc)
2226 goto done;
2227 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002228 /* Disable writeback. */
2229 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002231 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002232 if (c->modrm_mod == 3) {
2233 switch (c->modrm_rm) {
2234 case 1:
2235 rc = kvm_fix_hypercall(ctxt->vcpu);
2236 if (rc)
2237 goto done;
2238 break;
2239 default:
2240 goto cannot_emulate;
2241 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002242 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002243 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002244 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002245 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002246 if (rc)
2247 goto done;
2248 realmode_lidt(ctxt->vcpu, size, address);
2249 }
Avi Kivity16286d02008-04-14 14:40:50 +03002250 /* Disable writeback. */
2251 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 break;
2253 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002254 c->dst.bytes = 2;
2255 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 break;
2257 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002258 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2259 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002260 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 break;
2262 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002263 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002264 /* Disable writeback. */
2265 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 break;
2267 default:
2268 goto cannot_emulate;
2269 }
2270 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002271 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002272 if (emulate_syscall(ctxt) == -1)
2273 goto cannot_emulate;
2274 else
2275 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002276 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002277 case 0x06:
2278 emulate_clts(ctxt->vcpu);
2279 c->dst.type = OP_NONE;
2280 break;
2281 case 0x08: /* invd */
2282 case 0x09: /* wbinvd */
2283 case 0x0d: /* GrpP (prefetch) */
2284 case 0x18: /* Grp16 (prefetch/nop) */
2285 c->dst.type = OP_NONE;
2286 break;
2287 case 0x20: /* mov cr, reg */
2288 if (c->modrm_mod != 3)
2289 goto cannot_emulate;
2290 c->regs[c->modrm_rm] =
2291 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2292 c->dst.type = OP_NONE; /* no writeback */
2293 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002295 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002297 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002298 if (rc)
2299 goto cannot_emulate;
2300 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002302 case 0x22: /* mov reg, cr */
2303 if (c->modrm_mod != 3)
2304 goto cannot_emulate;
2305 realmode_set_cr(ctxt->vcpu,
2306 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2307 c->dst.type = OP_NONE;
2308 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002310 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002312 rc = emulator_set_dr(ctxt, c->modrm_reg,
2313 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002314 if (rc)
2315 goto cannot_emulate;
2316 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002318 case 0x30:
2319 /* wrmsr */
2320 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2321 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2322 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2323 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002324 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002325 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002326 }
2327 rc = X86EMUL_CONTINUE;
2328 c->dst.type = OP_NONE;
2329 break;
2330 case 0x32:
2331 /* rdmsr */
2332 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2333 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002334 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002335 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002336 } else {
2337 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2338 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2339 }
2340 rc = X86EMUL_CONTINUE;
2341 c->dst.type = OP_NONE;
2342 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002343 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002344 if (emulate_sysenter(ctxt) == -1)
2345 goto cannot_emulate;
2346 else
2347 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002348 break;
2349 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002350 if (emulate_sysexit(ctxt) == -1)
2351 goto cannot_emulate;
2352 else
2353 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002354 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002356 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002357 if (!test_cc(c->b, ctxt->eflags))
2358 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002360 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002361 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002362 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002363 c->dst.type = OP_NONE;
2364 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002365 case 0xa0: /* push fs */
2366 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2367 break;
2368 case 0xa1: /* pop fs */
2369 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2370 if (rc != 0)
2371 goto done;
2372 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002373 case 0xa3:
2374 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002375 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002376 /* only subword offset */
2377 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002378 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002379 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002380 case 0xa4: /* shld imm8, r, r/m */
2381 case 0xa5: /* shld cl, r, r/m */
2382 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2383 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002384 case 0xa8: /* push gs */
2385 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2386 break;
2387 case 0xa9: /* pop gs */
2388 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2389 if (rc != 0)
2390 goto done;
2391 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002392 case 0xab:
2393 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002394 /* only subword offset */
2395 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002396 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002397 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002398 case 0xac: /* shrd imm8, r, r/m */
2399 case 0xad: /* shrd cl, r, r/m */
2400 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2401 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002402 case 0xae: /* clflush */
2403 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 case 0xb0 ... 0xb1: /* cmpxchg */
2405 /*
2406 * Save real source value, then compare EAX against
2407 * destination.
2408 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002409 c->src.orig_val = c->src.val;
2410 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002411 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2412 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002414 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 } else {
2416 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002417 c->dst.type = OP_REG;
2418 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419 }
2420 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 case 0xb3:
2422 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002423 /* only subword offset */
2424 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002425 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002428 c->dst.bytes = c->op_bytes;
2429 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2430 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002433 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 case 0:
2435 goto bt;
2436 case 1:
2437 goto bts;
2438 case 2:
2439 goto btr;
2440 case 3:
2441 goto btc;
2442 }
2443 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002444 case 0xbb:
2445 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002446 /* only subword offset */
2447 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002448 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002449 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002451 c->dst.bytes = c->op_bytes;
2452 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2453 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002455 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002456 c->dst.bytes = c->op_bytes;
2457 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2458 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002459 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002461 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002462 if (rc != 0)
2463 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002464 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002465 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 }
2467 goto writeback;
2468
2469cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002470 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002471 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 return -1;
2473}