blob: 296e8519dc53410d2e136fb4e6e40060634e6b12 [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
Avi Kivity3eeb3282010-01-21 15:31:48 +020035#include "x86.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020036
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 Kivityc9eaf20f2009-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,
Gleb Natapov60a29d42010-02-10 14:21:30 +020091 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020092};
93
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +010094static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080095 /* 0x00 - 0x07 */
96 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
97 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +030098 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +030099 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 /* 0x08 - 0x0F */
101 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
102 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200103 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
104 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105 /* 0x10 - 0x17 */
106 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
107 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300108 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300109 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110 /* 0x18 - 0x1F */
111 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
112 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300113 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300114 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 /* 0x20 - 0x27 */
116 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
117 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200118 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 /* 0x28 - 0x2F */
120 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
121 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
122 0, 0, 0, 0,
123 /* 0x30 - 0x37 */
124 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
125 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
126 0, 0, 0, 0,
127 /* 0x38 - 0x3F */
128 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
129 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200130 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
131 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700132 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200133 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700134 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200135 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300136 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200137 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
138 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300139 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200140 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
141 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700142 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200143 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
144 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700145 0, 0, 0, 0,
146 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300147 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Laurent Viviere70669a2007-08-05 10:36:40 +0300148 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
149 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300150 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300151 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
152 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300153 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300154 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
155 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200157 Group | Group1_80, Group | Group1_81,
158 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
160 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
161 /* 0x88 - 0x8F */
162 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
163 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200164 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Guillaume Thouvenin42571982008-05-27 14:49:15 +0200165 DstReg | SrcMem | ModRM | Mov, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300166 /* 0x90 - 0x97 */
167 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
168 /* 0x98 - 0x9F */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300169 0, 0, SrcImm | Src2Imm16 | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300170 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200172 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
173 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200174 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
175 ByteOp | ImplicitOps | String, ImplicitOps | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 /* 0xA8 - 0xAF */
Avi Kivityb9fa9d62007-11-27 19:05:37 +0200177 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
178 ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String,
179 ByteOp | ImplicitOps | String, ImplicitOps | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300180 /* 0xB0 - 0xB7 */
181 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
182 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
183 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
184 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
185 /* 0xB8 - 0xBF */
186 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
187 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
188 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
189 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300191 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200192 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300193 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300195 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300196 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 /* 0xD0 - 0xD7 */
198 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
199 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
200 0, 0, 0, 0,
201 /* 0xD8 - 0xDF */
202 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300203 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300204 0, 0, 0, 0,
Gleb Natapov84ce66a2009-04-12 13:36:46 +0300205 ByteOp | SrcImmUByte, SrcImmUByte,
206 ByteOp | SrcImmUByte, SrcImmUByte,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300207 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300208 SrcImm | Stack, SrcImm | ImplicitOps,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300209 SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps,
Mohammed Gamala6a30342008-09-06 17:22:29 +0300210 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
211 SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212 /* 0xF0 - 0xF7 */
213 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200214 ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700216 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300217 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218};
219
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100220static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 /* 0x00 - 0x0F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200222 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0,
Avi Kivity651a3e22007-10-28 16:09:18 +0200223 ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 /* 0x10 - 0x1F */
225 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
226 /* 0x20 - 0x2F */
227 ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
228 0, 0, 0, 0, 0, 0, 0, 0,
229 /* 0x30 - 0x3F */
Andre Przywarae99f0502009-06-17 15:50:33 +0200230 ImplicitOps, 0, ImplicitOps, 0,
231 ImplicitOps, ImplicitOps, 0, 0,
232 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 /* 0x40 - 0x47 */
234 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
235 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
236 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
237 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
238 /* 0x48 - 0x4F */
239 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
240 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
241 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
242 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
243 /* 0x50 - 0x5F */
244 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
245 /* 0x60 - 0x6F */
246 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
247 /* 0x70 - 0x7F */
248 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300250 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
251 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252 /* 0x90 - 0x9F */
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300255 ImplicitOps | Stack, ImplicitOps | Stack,
256 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100257 DstMem | SrcReg | Src2ImmByte | ModRM,
258 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300260 ImplicitOps | Stack, ImplicitOps | Stack,
261 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100262 DstMem | SrcReg | Src2ImmByte | ModRM,
263 DstMem | SrcReg | Src2CL | ModRM,
264 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 /* 0xB0 - 0xB7 */
266 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
Avi Kivity038e51d2007-01-22 20:40:40 -0800267 DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
269 DstReg | SrcMem16 | ModRM | Mov,
270 /* 0xB8 - 0xBF */
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200271 0, 0, Group | Group8, DstMem | SrcReg | ModRM | BitOp,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
273 DstReg | SrcMem16 | ModRM | Mov,
274 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200275 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
276 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800277 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 /* 0xD0 - 0xDF */
279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280 /* 0xE0 - 0xEF */
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282 /* 0xF0 - 0xFF */
283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
284};
285
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100286static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200287 [Group1_80*8] =
288 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
289 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
290 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
291 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
292 [Group1_81*8] =
293 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
294 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
295 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
296 DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
297 [Group1_82*8] =
298 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
299 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
300 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
301 ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM,
302 [Group1_83*8] =
303 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
304 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
305 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
306 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200307 [Group1A*8] =
308 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200309 [Group3_Byte*8] =
310 ByteOp | SrcImm | DstMem | ModRM, 0,
311 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
312 0, 0, 0, 0,
313 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400314 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300315 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200316 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200317 [Group4*8] =
318 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
319 0, 0, 0, 0, 0, 0,
320 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300321 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
322 SrcMem | ModRM | Stack, 0,
Avi Kivityef46f182008-09-11 19:47:13 +0300323 SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200324 [Group7*8] =
325 0, 0, ModRM | SrcMem, ModRM | SrcMem,
Avi Kivity16286d02008-04-14 14:40:50 +0300326 SrcNone | ModRM | DstMem | Mov, 0,
327 SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200328 [Group8*8] =
329 0, 0, 0, 0,
330 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
331 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200332 [Group9*8] =
333 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200334};
335
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100336static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200337 [Group7*8] =
Amit Shahfbce5542008-12-04 11:11:40 +0000338 SrcNone | ModRM, 0, 0, SrcNone | ModRM,
Avi Kivity16286d02008-04-14 14:40:50 +0300339 SrcNone | ModRM | DstMem | Mov, 0,
340 SrcMem16 | ModRM | Mov, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200341 [Group9*8] =
342 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200343};
344
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345/* EFLAGS bit definitions. */
Andre Przywarab1d86142009-06-17 15:50:32 +0200346#define EFLG_VM (1<<17)
347#define EFLG_RF (1<<16)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348#define EFLG_OF (1<<11)
349#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200350#define EFLG_IF (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351#define EFLG_SF (1<<7)
352#define EFLG_ZF (1<<6)
353#define EFLG_AF (1<<4)
354#define EFLG_PF (1<<2)
355#define EFLG_CF (1<<0)
356
357/*
358 * Instruction emulation:
359 * Most instructions are emulated directly via a fragment of inline assembly
360 * code. This allows us to save/restore EFLAGS and thus very easily pick up
361 * any modified flags.
362 */
363
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800364#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365#define _LO32 "k" /* force 32-bit operand */
366#define _STK "%%rsp" /* stack pointer */
367#elif defined(__i386__)
368#define _LO32 "" /* force 32-bit operand */
369#define _STK "%%esp" /* stack pointer */
370#endif
371
372/*
373 * These EFLAGS bits are restored from saved value during emulation, and
374 * any changes are written back to the saved value after emulation.
375 */
376#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
377
378/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200379#define _PRE_EFLAGS(_sav, _msk, _tmp) \
380 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
381 "movl %"_sav",%"_LO32 _tmp"; " \
382 "push %"_tmp"; " \
383 "push %"_tmp"; " \
384 "movl %"_msk",%"_LO32 _tmp"; " \
385 "andl %"_LO32 _tmp",("_STK"); " \
386 "pushf; " \
387 "notl %"_LO32 _tmp"; " \
388 "andl %"_LO32 _tmp",("_STK"); " \
389 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
390 "pop %"_tmp"; " \
391 "orl %"_LO32 _tmp",("_STK"); " \
392 "popf; " \
393 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394
395/* After executing instruction: write-back necessary bits in EFLAGS. */
396#define _POST_EFLAGS(_sav, _msk, _tmp) \
397 /* _sav |= EFLAGS & _msk; */ \
398 "pushf; " \
399 "pop %"_tmp"; " \
400 "andl %"_msk",%"_LO32 _tmp"; " \
401 "orl %"_LO32 _tmp",%"_sav"; "
402
Avi Kivitydda96d82008-11-26 15:14:10 +0200403#ifdef CONFIG_X86_64
404#define ON64(x) x
405#else
406#define ON64(x)
407#endif
408
Avi Kivity6b7ad612008-11-26 15:30:45 +0200409#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
410 do { \
411 __asm__ __volatile__ ( \
412 _PRE_EFLAGS("0", "4", "2") \
413 _op _suffix " %"_x"3,%1; " \
414 _POST_EFLAGS("0", "4", "2") \
415 : "=m" (_eflags), "=m" ((_dst).val), \
416 "=&r" (_tmp) \
417 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200418 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200419
420
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421/* Raw emulation: instruction has two explicit operands. */
422#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200423 do { \
424 unsigned long _tmp; \
425 \
426 switch ((_dst).bytes) { \
427 case 2: \
428 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
429 break; \
430 case 4: \
431 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
432 break; \
433 case 8: \
434 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
435 break; \
436 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 } while (0)
438
439#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
440 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200441 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200444 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 break; \
446 default: \
447 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
448 _wx, _wy, _lx, _ly, _qx, _qy); \
449 break; \
450 } \
451 } while (0)
452
453/* Source operand is byte-sized and may be restricted to just %cl. */
454#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
455 __emulate_2op(_op, _src, _dst, _eflags, \
456 "b", "c", "b", "c", "b", "c", "b", "c")
457
458/* Source operand is byte, word, long or quad sized. */
459#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
460 __emulate_2op(_op, _src, _dst, _eflags, \
461 "b", "q", "w", "r", _LO32, "r", "", "r")
462
463/* Source operand is word, long or quad sized. */
464#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
465 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
466 "w", "r", _LO32, "r", "", "r")
467
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100468/* Instruction has three operands and one operand is stored in ECX register */
469#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
470 do { \
471 unsigned long _tmp; \
472 _type _clv = (_cl).val; \
473 _type _srcv = (_src).val; \
474 _type _dstv = (_dst).val; \
475 \
476 __asm__ __volatile__ ( \
477 _PRE_EFLAGS("0", "5", "2") \
478 _op _suffix " %4,%1 \n" \
479 _POST_EFLAGS("0", "5", "2") \
480 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
481 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
482 ); \
483 \
484 (_cl).val = (unsigned long) _clv; \
485 (_src).val = (unsigned long) _srcv; \
486 (_dst).val = (unsigned long) _dstv; \
487 } while (0)
488
489#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
490 do { \
491 switch ((_dst).bytes) { \
492 case 2: \
493 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
494 "w", unsigned short); \
495 break; \
496 case 4: \
497 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
498 "l", unsigned int); \
499 break; \
500 case 8: \
501 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
502 "q", unsigned long)); \
503 break; \
504 } \
505 } while (0)
506
Avi Kivitydda96d82008-11-26 15:14:10 +0200507#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 do { \
509 unsigned long _tmp; \
510 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200511 __asm__ __volatile__ ( \
512 _PRE_EFLAGS("0", "3", "2") \
513 _op _suffix " %1; " \
514 _POST_EFLAGS("0", "3", "2") \
515 : "=m" (_eflags), "+m" ((_dst).val), \
516 "=&r" (_tmp) \
517 : "i" (EFLAGS_MASK)); \
518 } while (0)
519
520/* Instruction has only one explicit operand (no source operand). */
521#define emulate_1op(_op, _dst, _eflags) \
522 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400523 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200524 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
525 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
526 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
527 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 } \
529 } while (0)
530
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531/* Fetch next part of the instruction being emulated. */
532#define insn_fetch(_type, _size, _eip) \
533({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200534 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Mike Dayd77c26f2007-10-08 09:02:08 -0400535 if (rc != 0) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 goto done; \
537 (_eip) += (_size); \
538 (_type)_x; \
539})
540
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800541static inline unsigned long ad_mask(struct decode_cache *c)
542{
543 return (1UL << (c->ad_bytes << 3)) - 1;
544}
545
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800547static inline unsigned long
548address_mask(struct decode_cache *c, unsigned long reg)
549{
550 if (c->ad_bytes == sizeof(unsigned long))
551 return reg;
552 else
553 return reg & ad_mask(c);
554}
555
556static inline unsigned long
557register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
558{
559 return base + address_mask(c, reg);
560}
561
Harvey Harrison7a9572752008-02-19 07:40:41 -0800562static inline void
563register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
564{
565 if (c->ad_bytes == sizeof(unsigned long))
566 *reg += inc;
567 else
568 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
569}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
Harvey Harrison7a9572752008-02-19 07:40:41 -0800571static inline void jmp_rel(struct decode_cache *c, int rel)
572{
573 register_address_increment(c, &c->eip, rel);
574}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300575
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300576static void set_seg_override(struct decode_cache *c, int seg)
577{
578 c->has_seg_override = true;
579 c->seg_override = seg;
580}
581
582static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
583{
584 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
585 return 0;
586
587 return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
588}
589
590static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
591 struct decode_cache *c)
592{
593 if (!c->has_seg_override)
594 return 0;
595
596 return seg_base(ctxt, c->seg_override);
597}
598
599static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
600{
601 return seg_base(ctxt, VCPU_SREG_ES);
602}
603
604static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
605{
606 return seg_base(ctxt, VCPU_SREG_SS);
607}
608
Avi Kivity62266862007-11-20 13:15:52 +0200609static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
610 struct x86_emulate_ops *ops,
611 unsigned long linear, u8 *dest)
612{
613 struct fetch_cache *fc = &ctxt->decode.fetch;
614 int rc;
615 int size;
616
617 if (linear < fc->start || linear >= fc->end) {
618 size = min(15UL, PAGE_SIZE - offset_in_page(linear));
Gleb Natapov1871c602010-02-10 14:21:32 +0200619 rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL);
Avi Kivity62266862007-11-20 13:15:52 +0200620 if (rc)
621 return rc;
622 fc->start = linear;
623 fc->end = linear + size;
624 }
625 *dest = fc->data[linear - fc->start];
626 return 0;
627}
628
629static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
630 struct x86_emulate_ops *ops,
631 unsigned long eip, void *dest, unsigned size)
632{
633 int rc = 0;
634
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200635 /* x86 instructions are limited to 15 bytes. */
636 if (eip + size - ctxt->decode.eip_orig > 15)
637 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200638 eip += ctxt->cs_base;
639 while (size--) {
640 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
641 if (rc)
642 return rc;
643 }
644 return 0;
645}
646
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000647/*
648 * Given the 'reg' portion of a ModRM byte, and a register block, return a
649 * pointer into the block that addresses the relevant register.
650 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
651 */
652static void *decode_register(u8 modrm_reg, unsigned long *regs,
653 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654{
655 void *p;
656
657 p = &regs[modrm_reg];
658 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
659 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
660 return p;
661}
662
663static int read_descriptor(struct x86_emulate_ctxt *ctxt,
664 struct x86_emulate_ops *ops,
665 void *ptr,
666 u16 *size, unsigned long *address, int op_bytes)
667{
668 int rc;
669
670 if (op_bytes == 2)
671 op_bytes = 3;
672 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300673 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200674 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 if (rc)
676 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300677 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200678 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679 return rc;
680}
681
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300682static int test_cc(unsigned int condition, unsigned int flags)
683{
684 int rc = 0;
685
686 switch ((condition & 15) >> 1) {
687 case 0: /* o */
688 rc |= (flags & EFLG_OF);
689 break;
690 case 1: /* b/c/nae */
691 rc |= (flags & EFLG_CF);
692 break;
693 case 2: /* z/e */
694 rc |= (flags & EFLG_ZF);
695 break;
696 case 3: /* be/na */
697 rc |= (flags & (EFLG_CF|EFLG_ZF));
698 break;
699 case 4: /* s */
700 rc |= (flags & EFLG_SF);
701 break;
702 case 5: /* p/pe */
703 rc |= (flags & EFLG_PF);
704 break;
705 case 7: /* le/ng */
706 rc |= (flags & EFLG_ZF);
707 /* fall through */
708 case 6: /* l/nge */
709 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
710 break;
711 }
712
713 /* Odd condition identifiers (lsb == 1) have inverted sense. */
714 return (!!rc ^ (condition & 1));
715}
716
Avi Kivity3c118e22007-10-31 10:27:04 +0200717static void decode_register_operand(struct operand *op,
718 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200719 int inhibit_bytereg)
720{
Avi Kivity33615aa2007-10-31 11:15:56 +0200721 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200722 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200723
724 if (!(c->d & ModRM))
725 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200726 op->type = OP_REG;
727 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200728 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200729 op->val = *(u8 *)op->ptr;
730 op->bytes = 1;
731 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200732 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200733 op->bytes = c->op_bytes;
734 switch (op->bytes) {
735 case 2:
736 op->val = *(u16 *)op->ptr;
737 break;
738 case 4:
739 op->val = *(u32 *)op->ptr;
740 break;
741 case 8:
742 op->val = *(u64 *) op->ptr;
743 break;
744 }
745 }
746 op->orig_val = op->val;
747}
748
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200749static int decode_modrm(struct x86_emulate_ctxt *ctxt,
750 struct x86_emulate_ops *ops)
751{
752 struct decode_cache *c = &ctxt->decode;
753 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700754 int index_reg = 0, base_reg = 0, scale;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200755 int rc = 0;
756
757 if (c->rex_prefix) {
758 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
759 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
760 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
761 }
762
763 c->modrm = insn_fetch(u8, 1, c->eip);
764 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
765 c->modrm_reg |= (c->modrm & 0x38) >> 3;
766 c->modrm_rm |= (c->modrm & 0x07);
767 c->modrm_ea = 0;
768 c->use_modrm_ea = 1;
769
770 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300771 c->modrm_ptr = decode_register(c->modrm_rm,
772 c->regs, c->d & ByteOp);
773 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200774 return rc;
775 }
776
777 if (c->ad_bytes == 2) {
778 unsigned bx = c->regs[VCPU_REGS_RBX];
779 unsigned bp = c->regs[VCPU_REGS_RBP];
780 unsigned si = c->regs[VCPU_REGS_RSI];
781 unsigned di = c->regs[VCPU_REGS_RDI];
782
783 /* 16-bit ModR/M decode. */
784 switch (c->modrm_mod) {
785 case 0:
786 if (c->modrm_rm == 6)
787 c->modrm_ea += insn_fetch(u16, 2, c->eip);
788 break;
789 case 1:
790 c->modrm_ea += insn_fetch(s8, 1, c->eip);
791 break;
792 case 2:
793 c->modrm_ea += insn_fetch(u16, 2, c->eip);
794 break;
795 }
796 switch (c->modrm_rm) {
797 case 0:
798 c->modrm_ea += bx + si;
799 break;
800 case 1:
801 c->modrm_ea += bx + di;
802 break;
803 case 2:
804 c->modrm_ea += bp + si;
805 break;
806 case 3:
807 c->modrm_ea += bp + di;
808 break;
809 case 4:
810 c->modrm_ea += si;
811 break;
812 case 5:
813 c->modrm_ea += di;
814 break;
815 case 6:
816 if (c->modrm_mod != 0)
817 c->modrm_ea += bp;
818 break;
819 case 7:
820 c->modrm_ea += bx;
821 break;
822 }
823 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
824 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300825 if (!c->has_seg_override)
826 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200827 c->modrm_ea = (u16)c->modrm_ea;
828 } else {
829 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700830 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200831 sib = insn_fetch(u8, 1, c->eip);
832 index_reg |= (sib >> 3) & 7;
833 base_reg |= sib & 7;
834 scale = sib >> 6;
835
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700836 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
837 c->modrm_ea += insn_fetch(s32, 4, c->eip);
838 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200839 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700840 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200841 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700842 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
843 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700844 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700845 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200846 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200847 switch (c->modrm_mod) {
848 case 0:
849 if (c->modrm_rm == 5)
850 c->modrm_ea += insn_fetch(s32, 4, c->eip);
851 break;
852 case 1:
853 c->modrm_ea += insn_fetch(s8, 1, c->eip);
854 break;
855 case 2:
856 c->modrm_ea += insn_fetch(s32, 4, c->eip);
857 break;
858 }
859 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200860done:
861 return rc;
862}
863
864static int decode_abs(struct x86_emulate_ctxt *ctxt,
865 struct x86_emulate_ops *ops)
866{
867 struct decode_cache *c = &ctxt->decode;
868 int rc = 0;
869
870 switch (c->ad_bytes) {
871 case 2:
872 c->modrm_ea = insn_fetch(u16, 2, c->eip);
873 break;
874 case 4:
875 c->modrm_ea = insn_fetch(u32, 4, c->eip);
876 break;
877 case 8:
878 c->modrm_ea = insn_fetch(u64, 8, c->eip);
879 break;
880 }
881done:
882 return rc;
883}
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200886x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200888 struct decode_cache *c = &ctxt->decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 int rc = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200891 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892
893 /* Shadow copy of register state. Committed on successful emulation. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
Laurent Viviere4e03de2007-09-18 11:52:50 +0200895 memset(c, 0, sizeof(struct decode_cache));
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200896 c->eip = c->eip_orig = kvm_rip_read(ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300897 ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800898 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899
900 switch (mode) {
901 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200902 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200904 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 break;
906 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200907 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800909#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200911 def_op_bytes = 4;
912 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
914#endif
915 default:
916 return -1;
917 }
918
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200919 c->op_bytes = def_op_bytes;
920 c->ad_bytes = def_ad_bytes;
921
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200923 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200924 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200926 /* switch between 2/4 bytes */
927 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 break;
929 case 0x67: /* address-size override */
930 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200931 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200932 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200934 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200935 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300938 case 0x2e: /* CS override */
939 case 0x36: /* SS override */
940 case 0x3e: /* DS override */
941 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 break;
943 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300945 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200947 case 0x40 ... 0x4f: /* REX */
948 if (mode != X86EMUL_MODE_PROT64)
949 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200950 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200951 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200953 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200955 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100956 c->rep_prefix = REPNE_PREFIX;
957 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100959 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 default:
962 goto done_prefixes;
963 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200964
965 /* Any legacy prefix after a REX prefix nullifies its effect. */
966
Avi Kivity33615aa2007-10-31 11:15:56 +0200967 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 }
969
970done_prefixes:
971
972 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200973 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200974 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200975 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
977 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200978 c->d = opcode_table[c->b];
979 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200981 if (c->b == 0x0f) {
982 c->twobyte = 1;
983 c->b = insn_fetch(u8, 1, c->eip);
984 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200986 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300988 if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
989 kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");;
990 return -1;
991 }
992
Avi Kivitye09d0822008-01-18 12:38:59 +0200993 if (c->d & Group) {
994 group = c->d & GroupMask;
995 c->modrm = insn_fetch(u8, 1, c->eip);
996 --c->eip;
997
998 group = (group << 3) + ((c->modrm >> 3) & 7);
999 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1000 c->d = group2_table[group];
1001 else
1002 c->d = group_table[group];
1003 }
1004
1005 /* Unrecognised? */
1006 if (c->d == 0) {
1007 DPRINTF("Cannot emulate %02x\n", c->b);
1008 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 }
1010
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001011 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1012 c->op_bytes = 8;
1013
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001015 if (c->d & ModRM)
1016 rc = decode_modrm(ctxt, ops);
1017 else if (c->d & MemAbs)
1018 rc = decode_abs(ctxt, ops);
1019 if (rc)
1020 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001022 if (!c->has_seg_override)
1023 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001024
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001025 if (!(!c->twobyte && c->b == 0x8d))
1026 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001027
1028 if (c->ad_bytes != 8)
1029 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 /*
1031 * Decode and fetch the source operand: register, memory
1032 * or immediate.
1033 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001034 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 case SrcNone:
1036 break;
1037 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001038 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 break;
1040 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001041 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 goto srcmem_common;
1043 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001044 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 goto srcmem_common;
1046 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001047 c->src.bytes = (c->d & ByteOp) ? 1 :
1048 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001049 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001050 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001051 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001052 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001053 /*
1054 * For instructions with a ModR/M byte, switch to register
1055 * access if Mod = 3.
1056 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001057 if ((c->d & ModRM) && c->modrm_mod == 3) {
1058 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001059 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001060 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001061 break;
1062 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001063 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 break;
1065 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001066 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001067 c->src.type = OP_IMM;
1068 c->src.ptr = (unsigned long *)c->eip;
1069 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1070 if (c->src.bytes == 8)
1071 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001073 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001075 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 break;
1077 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001078 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 break;
1080 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001081 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 break;
1083 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001084 if ((c->d & SrcMask) == SrcImmU) {
1085 switch (c->src.bytes) {
1086 case 1:
1087 c->src.val &= 0xff;
1088 break;
1089 case 2:
1090 c->src.val &= 0xffff;
1091 break;
1092 case 4:
1093 c->src.val &= 0xffffffff;
1094 break;
1095 }
1096 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 break;
1098 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001099 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001100 c->src.type = OP_IMM;
1101 c->src.ptr = (unsigned long *)c->eip;
1102 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001103 if ((c->d & SrcMask) == SrcImmByte)
1104 c->src.val = insn_fetch(s8, 1, c->eip);
1105 else
1106 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001108 case SrcOne:
1109 c->src.bytes = 1;
1110 c->src.val = 1;
1111 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 }
1113
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001114 /*
1115 * Decode and fetch the second source operand: register, memory
1116 * or immediate.
1117 */
1118 switch (c->d & Src2Mask) {
1119 case Src2None:
1120 break;
1121 case Src2CL:
1122 c->src2.bytes = 1;
1123 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1124 break;
1125 case Src2ImmByte:
1126 c->src2.type = OP_IMM;
1127 c->src2.ptr = (unsigned long *)c->eip;
1128 c->src2.bytes = 1;
1129 c->src2.val = insn_fetch(u8, 1, c->eip);
1130 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001131 case Src2Imm16:
1132 c->src2.type = OP_IMM;
1133 c->src2.ptr = (unsigned long *)c->eip;
1134 c->src2.bytes = 2;
1135 c->src2.val = insn_fetch(u16, 2, c->eip);
1136 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001137 case Src2One:
1138 c->src2.bytes = 1;
1139 c->src2.val = 1;
1140 break;
1141 }
1142
Avi Kivity038e51d2007-01-22 20:40:40 -08001143 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001144 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001145 case ImplicitOps:
1146 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001147 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001148 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001149 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001150 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001151 break;
1152 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001153 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001154 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001155 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001156 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001157 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001158 break;
1159 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001160 c->dst.type = OP_MEM;
1161 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001162 case DstAcc:
1163 c->dst.type = OP_REG;
1164 c->dst.bytes = c->op_bytes;
1165 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1166 switch (c->op_bytes) {
1167 case 1:
1168 c->dst.val = *(u8 *)c->dst.ptr;
1169 break;
1170 case 2:
1171 c->dst.val = *(u16 *)c->dst.ptr;
1172 break;
1173 case 4:
1174 c->dst.val = *(u32 *)c->dst.ptr;
1175 break;
1176 }
1177 c->dst.orig_val = c->dst.val;
1178 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001179 }
1180
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001181 if (c->rip_relative)
1182 c->modrm_ea += c->eip;
1183
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001184done:
1185 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1186}
1187
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001188static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1189{
1190 struct decode_cache *c = &ctxt->decode;
1191
1192 c->dst.type = OP_MEM;
1193 c->dst.bytes = c->op_bytes;
1194 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001195 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001196 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001197 c->regs[VCPU_REGS_RSP]);
1198}
1199
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001200static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001201 struct x86_emulate_ops *ops,
1202 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001203{
1204 struct decode_cache *c = &ctxt->decode;
1205 int rc;
1206
Avi Kivity781d0ed2008-11-27 18:00:28 +02001207 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1208 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001209 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001210 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001211 return rc;
1212
Avi Kivity350f69d2009-01-05 11:12:40 +02001213 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001214 return rc;
1215}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001216
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001217static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1218{
1219 struct decode_cache *c = &ctxt->decode;
1220 struct kvm_segment segment;
1221
1222 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1223
1224 c->src.val = segment.selector;
1225 emulate_push(ctxt);
1226}
1227
1228static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1229 struct x86_emulate_ops *ops, int seg)
1230{
1231 struct decode_cache *c = &ctxt->decode;
1232 unsigned long selector;
1233 int rc;
1234
1235 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1236 if (rc != 0)
1237 return rc;
1238
1239 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg);
1240 return rc;
1241}
1242
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001243static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1244{
1245 struct decode_cache *c = &ctxt->decode;
1246 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1247 int reg = VCPU_REGS_RAX;
1248
1249 while (reg <= VCPU_REGS_RDI) {
1250 (reg == VCPU_REGS_RSP) ?
1251 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1252
1253 emulate_push(ctxt);
1254 ++reg;
1255 }
1256}
1257
1258static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1259 struct x86_emulate_ops *ops)
1260{
1261 struct decode_cache *c = &ctxt->decode;
1262 int rc = 0;
1263 int reg = VCPU_REGS_RDI;
1264
1265 while (reg >= VCPU_REGS_RAX) {
1266 if (reg == VCPU_REGS_RSP) {
1267 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1268 c->op_bytes);
1269 --reg;
1270 }
1271
1272 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
1273 if (rc != 0)
1274 break;
1275 --reg;
1276 }
1277 return rc;
1278}
1279
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001280static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1281 struct x86_emulate_ops *ops)
1282{
1283 struct decode_cache *c = &ctxt->decode;
1284 int rc;
1285
Avi Kivity350f69d2009-01-05 11:12:40 +02001286 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001287 if (rc != 0)
1288 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001289 return 0;
1290}
1291
Laurent Vivier05f086f2007-09-24 11:10:55 +02001292static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001294 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001295 switch (c->modrm_reg) {
1296 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001297 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001298 break;
1299 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001300 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001301 break;
1302 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001303 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001304 break;
1305 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001306 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001307 break;
1308 case 4: /* sal/shl */
1309 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001310 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311 break;
1312 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001313 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001314 break;
1315 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001316 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001317 break;
1318 }
1319}
1320
1321static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001322 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001323{
1324 struct decode_cache *c = &ctxt->decode;
1325 int rc = 0;
1326
1327 switch (c->modrm_reg) {
1328 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001329 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001330 break;
1331 case 2: /* not */
1332 c->dst.val = ~c->dst.val;
1333 break;
1334 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001335 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001336 break;
1337 default:
1338 DPRINTF("Cannot emulate %02x\n", c->b);
1339 rc = X86EMUL_UNHANDLEABLE;
1340 break;
1341 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001342 return rc;
1343}
1344
1345static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001346 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001347{
1348 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001349
1350 switch (c->modrm_reg) {
1351 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001352 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001353 break;
1354 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001355 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001356 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001357 case 2: /* call near abs */ {
1358 long int old_eip;
1359 old_eip = c->eip;
1360 c->eip = c->src.val;
1361 c->src.val = old_eip;
1362 emulate_push(ctxt);
1363 break;
1364 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001366 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001367 break;
1368 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001369 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001371 }
1372 return 0;
1373}
1374
1375static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1376 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001377 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378{
1379 struct decode_cache *c = &ctxt->decode;
1380 u64 old, new;
1381 int rc;
1382
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001383 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001384 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001385 return rc;
1386
1387 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1388 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1389
1390 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1391 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001392 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393
1394 } else {
1395 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1396 (u32) c->regs[VCPU_REGS_RBX];
1397
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001398 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001399 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001400 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001401 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001402 }
1403 return 0;
1404}
1405
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001406static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1407 struct x86_emulate_ops *ops)
1408{
1409 struct decode_cache *c = &ctxt->decode;
1410 int rc;
1411 unsigned long cs;
1412
1413 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1414 if (rc)
1415 return rc;
1416 if (c->op_bytes == 4)
1417 c->eip = (u32)c->eip;
1418 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1419 if (rc)
1420 return rc;
1421 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1422 return rc;
1423}
1424
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001425static inline int writeback(struct x86_emulate_ctxt *ctxt,
1426 struct x86_emulate_ops *ops)
1427{
1428 int rc;
1429 struct decode_cache *c = &ctxt->decode;
1430
1431 switch (c->dst.type) {
1432 case OP_REG:
1433 /* The 4-byte case *is* correct:
1434 * in 64-bit mode we zero-extend.
1435 */
1436 switch (c->dst.bytes) {
1437 case 1:
1438 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1439 break;
1440 case 2:
1441 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1442 break;
1443 case 4:
1444 *c->dst.ptr = (u32)c->dst.val;
1445 break; /* 64b: zero-ext */
1446 case 8:
1447 *c->dst.ptr = c->dst.val;
1448 break;
1449 }
1450 break;
1451 case OP_MEM:
1452 if (c->lock_prefix)
1453 rc = ops->cmpxchg_emulated(
1454 (unsigned long)c->dst.ptr,
1455 &c->dst.orig_val,
1456 &c->dst.val,
1457 c->dst.bytes,
1458 ctxt->vcpu);
1459 else
1460 rc = ops->write_emulated(
1461 (unsigned long)c->dst.ptr,
1462 &c->dst.val,
1463 c->dst.bytes,
1464 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001465 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001466 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001467 break;
1468 case OP_NONE:
1469 /* no writeback */
1470 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001471 default:
1472 break;
1473 }
1474 return 0;
1475}
1476
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301477static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001478{
1479 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1480 /*
1481 * an sti; sti; sequence only disable interrupts for the first
1482 * instruction. So, if the last instruction, be it emulated or
1483 * not, left the system with the INT_STI flag enabled, it
1484 * means that the last instruction is an sti. We should not
1485 * leave the flag on in this case. The same goes for mov ss
1486 */
1487 if (!(int_shadow & mask))
1488 ctxt->interruptibility = mask;
1489}
1490
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001491static inline void
1492setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1493 struct kvm_segment *cs, struct kvm_segment *ss)
1494{
1495 memset(cs, 0, sizeof(struct kvm_segment));
1496 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1497 memset(ss, 0, sizeof(struct kvm_segment));
1498
1499 cs->l = 0; /* will be adjusted later */
1500 cs->base = 0; /* flat segment */
1501 cs->g = 1; /* 4kb granularity */
1502 cs->limit = 0xffffffff; /* 4GB limit */
1503 cs->type = 0x0b; /* Read, Execute, Accessed */
1504 cs->s = 1;
1505 cs->dpl = 0; /* will be adjusted later */
1506 cs->present = 1;
1507 cs->db = 1;
1508
1509 ss->unusable = 0;
1510 ss->base = 0; /* flat segment */
1511 ss->limit = 0xffffffff; /* 4GB limit */
1512 ss->g = 1; /* 4kb granularity */
1513 ss->s = 1;
1514 ss->type = 0x03; /* Read/Write, Accessed */
1515 ss->db = 1; /* 32bit stack segment */
1516 ss->dpl = 0;
1517 ss->present = 1;
1518}
1519
1520static int
1521emulate_syscall(struct x86_emulate_ctxt *ctxt)
1522{
1523 struct decode_cache *c = &ctxt->decode;
1524 struct kvm_segment cs, ss;
1525 u64 msr_data;
1526
1527 /* syscall is not available in real mode */
1528 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
Gleb Natapova0044752010-02-10 14:21:31 +02001529 || ctxt->mode == X86EMUL_MODE_VM86)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001530 return -1;
1531
1532 setup_syscalls_segments(ctxt, &cs, &ss);
1533
1534 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1535 msr_data >>= 32;
1536 cs.selector = (u16)(msr_data & 0xfffc);
1537 ss.selector = (u16)(msr_data + 8);
1538
1539 if (is_long_mode(ctxt->vcpu)) {
1540 cs.db = 0;
1541 cs.l = 1;
1542 }
1543 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1544 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1545
1546 c->regs[VCPU_REGS_RCX] = c->eip;
1547 if (is_long_mode(ctxt->vcpu)) {
1548#ifdef CONFIG_X86_64
1549 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1550
1551 kvm_x86_ops->get_msr(ctxt->vcpu,
1552 ctxt->mode == X86EMUL_MODE_PROT64 ?
1553 MSR_LSTAR : MSR_CSTAR, &msr_data);
1554 c->eip = msr_data;
1555
1556 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1557 ctxt->eflags &= ~(msr_data | EFLG_RF);
1558#endif
1559 } else {
1560 /* legacy mode */
1561 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1562 c->eip = (u32)msr_data;
1563
1564 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1565 }
1566
1567 return 0;
1568}
1569
Andre Przywara8c604352009-06-18 12:56:01 +02001570static int
1571emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1572{
1573 struct decode_cache *c = &ctxt->decode;
1574 struct kvm_segment cs, ss;
1575 u64 msr_data;
1576
1577 /* inject #UD if LOCK prefix is used */
1578 if (c->lock_prefix)
1579 return -1;
1580
Gleb Natapova0044752010-02-10 14:21:31 +02001581 /* inject #GP if in real mode */
1582 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001583 kvm_inject_gp(ctxt->vcpu, 0);
1584 return -1;
1585 }
1586
1587 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1588 * Therefore, we inject an #UD.
1589 */
1590 if (ctxt->mode == X86EMUL_MODE_PROT64)
1591 return -1;
1592
1593 setup_syscalls_segments(ctxt, &cs, &ss);
1594
1595 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1596 switch (ctxt->mode) {
1597 case X86EMUL_MODE_PROT32:
1598 if ((msr_data & 0xfffc) == 0x0) {
1599 kvm_inject_gp(ctxt->vcpu, 0);
1600 return -1;
1601 }
1602 break;
1603 case X86EMUL_MODE_PROT64:
1604 if (msr_data == 0x0) {
1605 kvm_inject_gp(ctxt->vcpu, 0);
1606 return -1;
1607 }
1608 break;
1609 }
1610
1611 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1612 cs.selector = (u16)msr_data;
1613 cs.selector &= ~SELECTOR_RPL_MASK;
1614 ss.selector = cs.selector + 8;
1615 ss.selector &= ~SELECTOR_RPL_MASK;
1616 if (ctxt->mode == X86EMUL_MODE_PROT64
1617 || is_long_mode(ctxt->vcpu)) {
1618 cs.db = 0;
1619 cs.l = 1;
1620 }
1621
1622 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1623 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1624
1625 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1626 c->eip = msr_data;
1627
1628 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1629 c->regs[VCPU_REGS_RSP] = msr_data;
1630
1631 return 0;
1632}
1633
Andre Przywara4668f052009-06-18 12:56:02 +02001634static int
1635emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1636{
1637 struct decode_cache *c = &ctxt->decode;
1638 struct kvm_segment cs, ss;
1639 u64 msr_data;
1640 int usermode;
1641
1642 /* inject #UD if LOCK prefix is used */
1643 if (c->lock_prefix)
1644 return -1;
1645
Gleb Natapova0044752010-02-10 14:21:31 +02001646 /* inject #GP if in real mode or Virtual 8086 mode */
1647 if (ctxt->mode == X86EMUL_MODE_REAL ||
1648 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02001649 kvm_inject_gp(ctxt->vcpu, 0);
1650 return -1;
1651 }
1652
1653 /* sysexit must be called from CPL 0 */
1654 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1655 kvm_inject_gp(ctxt->vcpu, 0);
1656 return -1;
1657 }
1658
1659 setup_syscalls_segments(ctxt, &cs, &ss);
1660
1661 if ((c->rex_prefix & 0x8) != 0x0)
1662 usermode = X86EMUL_MODE_PROT64;
1663 else
1664 usermode = X86EMUL_MODE_PROT32;
1665
1666 cs.dpl = 3;
1667 ss.dpl = 3;
1668 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1669 switch (usermode) {
1670 case X86EMUL_MODE_PROT32:
1671 cs.selector = (u16)(msr_data + 16);
1672 if ((msr_data & 0xfffc) == 0x0) {
1673 kvm_inject_gp(ctxt->vcpu, 0);
1674 return -1;
1675 }
1676 ss.selector = (u16)(msr_data + 24);
1677 break;
1678 case X86EMUL_MODE_PROT64:
1679 cs.selector = (u16)(msr_data + 32);
1680 if (msr_data == 0x0) {
1681 kvm_inject_gp(ctxt->vcpu, 0);
1682 return -1;
1683 }
1684 ss.selector = cs.selector + 8;
1685 cs.db = 0;
1686 cs.l = 1;
1687 break;
1688 }
1689 cs.selector |= SELECTOR_RPL_MASK;
1690 ss.selector |= SELECTOR_RPL_MASK;
1691
1692 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1693 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1694
1695 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1696 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1697
1698 return 0;
1699}
1700
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001701static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
1702{
1703 int iopl;
1704 if (ctxt->mode == X86EMUL_MODE_REAL)
1705 return false;
1706 if (ctxt->mode == X86EMUL_MODE_VM86)
1707 return true;
1708 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
1709 return kvm_x86_ops->get_cpl(ctxt->vcpu) > iopl;
1710}
1711
1712static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1713 struct x86_emulate_ops *ops,
1714 u16 port, u16 len)
1715{
1716 struct kvm_segment tr_seg;
1717 int r;
1718 u16 io_bitmap_ptr;
1719 u8 perm, bit_idx = port & 0x7;
1720 unsigned mask = (1 << len) - 1;
1721
1722 kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR);
1723 if (tr_seg.unusable)
1724 return false;
1725 if (tr_seg.limit < 103)
1726 return false;
1727 r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu,
1728 NULL);
1729 if (r != X86EMUL_CONTINUE)
1730 return false;
1731 if (io_bitmap_ptr + port/8 > tr_seg.limit)
1732 return false;
1733 r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1,
1734 ctxt->vcpu, NULL);
1735 if (r != X86EMUL_CONTINUE)
1736 return false;
1737 if ((perm >> bit_idx) & mask)
1738 return false;
1739 return true;
1740}
1741
1742static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1743 struct x86_emulate_ops *ops,
1744 u16 port, u16 len)
1745{
1746 if (emulator_bad_iopl(ctxt))
1747 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1748 return false;
1749 return true;
1750}
1751
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001752int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001753x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001754{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001755 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001756 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001757 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001758 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001759 unsigned int port;
1760 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001761 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001762
Glauber Costa310b5d32009-05-12 16:21:06 -04001763 ctxt->interruptibility = 0;
1764
Laurent Vivier34273182007-09-18 11:27:37 +02001765 /* Shadow copy of register state. Committed on successful emulation.
1766 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1767 * modify them.
1768 */
1769
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001771 saved_eip = c->eip;
1772
Avi Kivityc7e75a32007-10-28 16:34:25 +02001773 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001774 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001775
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001776 if (c->rep_prefix && (c->d & String)) {
1777 /* All REP prefixes have the same first termination condition */
1778 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001779 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001780 goto done;
1781 }
1782 /* The second termination condition only applies for REPE
1783 * and REPNE. Test if the repeat string operation prefix is
1784 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1785 * corresponding termination condition according to:
1786 * - if REPE/REPZ and ZF = 0 then done
1787 * - if REPNE/REPNZ and ZF = 1 then done
1788 */
1789 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1790 (c->b == 0xae) || (c->b == 0xaf)) {
1791 if ((c->rep_prefix == REPE_PREFIX) &&
1792 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001793 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001794 goto done;
1795 }
1796 if ((c->rep_prefix == REPNE_PREFIX) &&
1797 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001798 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001799 goto done;
1800 }
1801 }
1802 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001803 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001804 }
1805
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001806 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001807 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001808 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001809 rc = ops->read_emulated((unsigned long)c->src.ptr,
1810 &c->src.val,
1811 c->src.bytes,
1812 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001813 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001814 goto done;
1815 c->src.orig_val = c->src.val;
1816 }
1817
1818 if ((c->d & DstMask) == ImplicitOps)
1819 goto special_insn;
1820
1821
1822 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001823 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001824 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1825 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001826 if (c->d & BitOp) {
1827 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001828
Laurent Viviere4e03de2007-09-18 11:52:50 +02001829 c->dst.ptr = (void *)c->dst.ptr +
1830 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001831 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001832 if (!(c->d & Mov)) {
1833 /* optimisation - avoid slow emulated read */
1834 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1835 &c->dst.val,
1836 c->dst.bytes,
1837 ctxt->vcpu);
1838 if (rc != X86EMUL_CONTINUE)
1839 goto done;
1840 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001841 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001842 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001843
Avi Kivity018a98d2007-11-27 19:30:56 +02001844special_insn:
1845
Laurent Viviere4e03de2007-09-18 11:52:50 +02001846 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 goto twobyte_insn;
1848
Laurent Viviere4e03de2007-09-18 11:52:50 +02001849 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 case 0x00 ... 0x05:
1851 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001852 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001854 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001855 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1856 break;
1857 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001858 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1859 if (rc != 0)
1860 goto done;
1861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 case 0x08 ... 0x0d:
1863 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001864 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001866 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001867 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1868 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 case 0x10 ... 0x15:
1870 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001871 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001873 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001874 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1875 break;
1876 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001877 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1878 if (rc != 0)
1879 goto done;
1880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 case 0x18 ... 0x1d:
1882 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001883 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001885 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001886 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1887 break;
1888 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001889 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1890 if (rc != 0)
1891 goto done;
1892 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001893 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001895 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896 break;
1897 case 0x28 ... 0x2d:
1898 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001899 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 break;
1901 case 0x30 ... 0x35:
1902 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001903 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 break;
1905 case 0x38 ... 0x3d:
1906 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001907 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001909 case 0x40 ... 0x47: /* inc r16/r32 */
1910 emulate_1op("inc", c->dst, ctxt->eflags);
1911 break;
1912 case 0x48 ... 0x4f: /* dec r16/r32 */
1913 emulate_1op("dec", c->dst, ctxt->eflags);
1914 break;
1915 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001916 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001917 break;
1918 case 0x58 ... 0x5f: /* pop reg */
1919 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001920 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001921 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001922 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001923 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001924 case 0x60: /* pusha */
1925 emulate_pusha(ctxt);
1926 break;
1927 case 0x61: /* popa */
1928 rc = emulate_popa(ctxt, ops);
1929 if (rc != 0)
1930 goto done;
1931 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001933 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001935 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001937 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001938 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001939 emulate_push(ctxt);
1940 break;
1941 case 0x6c: /* insb */
1942 case 0x6d: /* insw/insd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001943 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
1944 (c->d & ByteOp) ? 1 : c->op_bytes)) {
1945 kvm_inject_gp(ctxt->vcpu, 0);
1946 goto done;
1947 }
1948 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001949 1,
1950 (c->d & ByteOp) ? 1 : c->op_bytes,
1951 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001952 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001953 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001954 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001955 c->regs[VCPU_REGS_RDI]),
1956 c->rep_prefix,
1957 c->regs[VCPU_REGS_RDX]) == 0) {
1958 c->eip = saved_eip;
1959 return -1;
1960 }
1961 return 0;
1962 case 0x6e: /* outsb */
1963 case 0x6f: /* outsw/outsd */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001964 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
1965 (c->d & ByteOp) ? 1 : c->op_bytes)) {
1966 kvm_inject_gp(ctxt->vcpu, 0);
1967 goto done;
1968 }
Avi Kivity851ba692009-08-24 11:10:17 +03001969 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001970 0,
1971 (c->d & ByteOp) ? 1 : c->op_bytes,
1972 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001973 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001974 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001975 register_address(c,
1976 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001977 c->regs[VCPU_REGS_RSI]),
1978 c->rep_prefix,
1979 c->regs[VCPU_REGS_RDX]) == 0) {
1980 c->eip = saved_eip;
1981 return -1;
1982 }
1983 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001984 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001985 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001986 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001989 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 case 0:
1991 goto add;
1992 case 1:
1993 goto or;
1994 case 2:
1995 goto adc;
1996 case 3:
1997 goto sbb;
1998 case 4:
1999 goto and;
2000 case 5:
2001 goto sub;
2002 case 6:
2003 goto xor;
2004 case 7:
2005 goto cmp;
2006 }
2007 break;
2008 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002009 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 break;
2011 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002012 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002014 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002016 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 break;
2018 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002019 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 break;
2021 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002022 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 break; /* 64b reg: zero-extend */
2024 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002025 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 break;
2027 }
2028 /*
2029 * Write back the memory destination with implicit LOCK
2030 * prefix.
2031 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002032 c->dst.val = c->src.val;
2033 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002036 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002037 case 0x8c: { /* mov r/m, sreg */
2038 struct kvm_segment segreg;
2039
2040 if (c->modrm_reg <= 5)
2041 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
2042 else {
2043 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
2044 c->modrm);
2045 goto cannot_emulate;
2046 }
2047 c->dst.val = segreg.selector;
2048 break;
2049 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002050 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002051 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002052 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002053 case 0x8e: { /* mov seg, r/m16 */
2054 uint16_t sel;
2055 int type_bits;
2056 int err;
2057
2058 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04002059 if (c->modrm_reg == VCPU_SREG_SS)
2060 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
2061
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002062 if (c->modrm_reg <= 5) {
2063 type_bits = (c->modrm_reg == 1) ? 9 : 1;
2064 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
2065 type_bits, c->modrm_reg);
2066 } else {
2067 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
2068 c->modrm);
2069 goto cannot_emulate;
2070 }
2071
2072 if (err < 0)
2073 goto cannot_emulate;
2074
2075 c->dst.type = OP_NONE; /* Disable writeback. */
2076 break;
2077 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002079 rc = emulate_grp1a(ctxt, ops);
2080 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002083 case 0x90: /* nop / xchg r8,rax */
2084 if (!(c->rex_prefix & 1)) { /* nop */
2085 c->dst.type = OP_NONE;
2086 break;
2087 }
2088 case 0x91 ... 0x97: /* xchg reg,rax */
2089 c->src.type = c->dst.type = OP_REG;
2090 c->src.bytes = c->dst.bytes = c->op_bytes;
2091 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2092 c->src.val = *(c->src.ptr);
2093 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002094 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002095 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002096 emulate_push(ctxt);
2097 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002098 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002099 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002100 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002101 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002102 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002103 case 0xa0 ... 0xa1: /* mov */
2104 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2105 c->dst.val = c->src.val;
2106 break;
2107 case 0xa2 ... 0xa3: /* mov */
2108 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2109 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002111 c->dst.type = OP_MEM;
2112 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002113 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002114 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002115 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002116 rc = ops->read_emulated(register_address(c,
2117 seg_override_base(ctxt, c),
2118 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002119 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002120 c->dst.bytes, ctxt->vcpu);
2121 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002123 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002124 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002125 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002126 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002127 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002128 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 break;
2130 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002131 c->src.type = OP_NONE; /* Disable writeback. */
2132 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002133 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002134 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002135 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002136 rc = ops->read_emulated((unsigned long)c->src.ptr,
2137 &c->src.val,
2138 c->src.bytes,
2139 ctxt->vcpu);
2140 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002141 goto done;
2142
2143 c->dst.type = OP_NONE; /* Disable writeback. */
2144 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002145 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002146 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002147 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002148 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2149 &c->dst.val,
2150 c->dst.bytes,
2151 ctxt->vcpu);
2152 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002153 goto done;
2154
2155 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2156
2157 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2158
Harvey Harrison7a9572752008-02-19 07:40:41 -08002159 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002160 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2161 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002162 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002163 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2164 : c->dst.bytes);
2165
2166 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002168 c->dst.type = OP_MEM;
2169 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002170 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002171 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002172 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002173 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002174 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002175 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002176 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 break;
2178 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002179 c->dst.type = OP_REG;
2180 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2181 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002182 rc = ops->read_emulated(register_address(c,
2183 seg_override_base(ctxt, c),
2184 c->regs[VCPU_REGS_RSI]),
2185 &c->dst.val,
2186 c->dst.bytes,
2187 ctxt->vcpu);
2188 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002190 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002191 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002192 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 break;
2194 case 0xae ... 0xaf: /* scas */
2195 DPRINTF("Urk! I don't handle SCAS.\n");
2196 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002197 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002198 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002199 case 0xc0 ... 0xc1:
2200 emulate_grp2(ctxt);
2201 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002202 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002203 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002204 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002205 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002206 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002207 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2208 mov:
2209 c->dst.val = c->src.val;
2210 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002211 case 0xcb: /* ret far */
2212 rc = emulate_ret_far(ctxt, ops);
2213 if (rc)
2214 goto done;
2215 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002216 case 0xd0 ... 0xd1: /* Grp2 */
2217 c->src.val = 1;
2218 emulate_grp2(ctxt);
2219 break;
2220 case 0xd2 ... 0xd3: /* Grp2 */
2221 c->src.val = c->regs[VCPU_REGS_RCX];
2222 emulate_grp2(ctxt);
2223 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002224 case 0xe4: /* inb */
2225 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002226 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002227 io_dir_in = 1;
2228 goto do_io;
2229 case 0xe6: /* outb */
2230 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002231 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002232 io_dir_in = 0;
2233 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002234 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002235 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002236 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002237 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002238 emulate_push(ctxt);
2239 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002240 }
2241 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002242 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002243 case 0xea: /* jmp far */
2244 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2245 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002246 DPRINTF("jmp far: Failed to load CS descriptor\n");
2247 goto cannot_emulate;
2248 }
2249
Gleb Natapov782b8772009-04-12 13:36:25 +03002250 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002251 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002252 case 0xeb:
2253 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002254 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002255 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002256 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002257 case 0xec: /* in al,dx */
2258 case 0xed: /* in (e/r)ax,dx */
2259 port = c->regs[VCPU_REGS_RDX];
2260 io_dir_in = 1;
2261 goto do_io;
2262 case 0xee: /* out al,dx */
2263 case 0xef: /* out (e/r)ax,dx */
2264 port = c->regs[VCPU_REGS_RDX];
2265 io_dir_in = 0;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002266 do_io:
2267 if (!emulator_io_permited(ctxt, ops, port,
2268 (c->d & ByteOp) ? 1 : c->op_bytes)) {
2269 kvm_inject_gp(ctxt->vcpu, 0);
2270 goto done;
2271 }
2272 if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002273 (c->d & ByteOp) ? 1 : c->op_bytes,
2274 port) != 0) {
2275 c->eip = saved_eip;
2276 goto cannot_emulate;
2277 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002278 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002279 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002280 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002281 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002282 case 0xf5: /* cmc */
2283 /* complement carry flag from eflags reg */
2284 ctxt->eflags ^= EFLG_CF;
2285 c->dst.type = OP_NONE; /* Disable writeback. */
2286 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002287 case 0xf6 ... 0xf7: /* Grp3 */
2288 rc = emulate_grp3(ctxt, ops);
2289 if (rc != 0)
2290 goto done;
2291 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002292 case 0xf8: /* clc */
2293 ctxt->eflags &= ~EFLG_CF;
2294 c->dst.type = OP_NONE; /* Disable writeback. */
2295 break;
2296 case 0xfa: /* cli */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002297 if (emulator_bad_iopl(ctxt))
2298 kvm_inject_gp(ctxt->vcpu, 0);
2299 else {
2300 ctxt->eflags &= ~X86_EFLAGS_IF;
2301 c->dst.type = OP_NONE; /* Disable writeback. */
2302 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002303 break;
2304 case 0xfb: /* sti */
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002305 if (emulator_bad_iopl(ctxt))
2306 kvm_inject_gp(ctxt->vcpu, 0);
2307 else {
2308 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
2309 ctxt->eflags |= X86_EFLAGS_IF;
2310 c->dst.type = OP_NONE; /* Disable writeback. */
2311 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002312 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002313 case 0xfc: /* cld */
2314 ctxt->eflags &= ~EFLG_DF;
2315 c->dst.type = OP_NONE; /* Disable writeback. */
2316 break;
2317 case 0xfd: /* std */
2318 ctxt->eflags |= EFLG_DF;
2319 c->dst.type = OP_NONE; /* Disable writeback. */
2320 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002321 case 0xfe ... 0xff: /* Grp4/Grp5 */
2322 rc = emulate_grp45(ctxt, ops);
2323 if (rc != 0)
2324 goto done;
2325 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002327
2328writeback:
2329 rc = writeback(ctxt, ops);
2330 if (rc != 0)
2331 goto done;
2332
2333 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002334 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002335 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002336
2337done:
2338 if (rc == X86EMUL_UNHANDLEABLE) {
2339 c->eip = saved_eip;
2340 return -1;
2341 }
2342 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343
2344twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002345 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002347 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348 u16 size;
2349 unsigned long address;
2350
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002351 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002352 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002353 goto cannot_emulate;
2354
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002355 rc = kvm_fix_hypercall(ctxt->vcpu);
2356 if (rc)
2357 goto done;
2358
Avi Kivity33e38852008-05-21 15:34:25 +03002359 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002360 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002361 /* Disable writeback. */
2362 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002363 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002365 rc = read_descriptor(ctxt, ops, c->src.ptr,
2366 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 if (rc)
2368 goto done;
2369 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002370 /* Disable writeback. */
2371 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002373 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002374 if (c->modrm_mod == 3) {
2375 switch (c->modrm_rm) {
2376 case 1:
2377 rc = kvm_fix_hypercall(ctxt->vcpu);
2378 if (rc)
2379 goto done;
2380 break;
2381 default:
2382 goto cannot_emulate;
2383 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002384 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002385 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002386 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002387 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002388 if (rc)
2389 goto done;
2390 realmode_lidt(ctxt->vcpu, size, address);
2391 }
Avi Kivity16286d02008-04-14 14:40:50 +03002392 /* Disable writeback. */
2393 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 break;
2395 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002396 c->dst.bytes = 2;
2397 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 break;
2399 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002400 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2401 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002402 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 break;
2404 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002405 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002406 /* Disable writeback. */
2407 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 break;
2409 default:
2410 goto cannot_emulate;
2411 }
2412 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002413 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002414 if (emulate_syscall(ctxt) == -1)
2415 goto cannot_emulate;
2416 else
2417 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002418 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002419 case 0x06:
2420 emulate_clts(ctxt->vcpu);
2421 c->dst.type = OP_NONE;
2422 break;
2423 case 0x08: /* invd */
2424 case 0x09: /* wbinvd */
2425 case 0x0d: /* GrpP (prefetch) */
2426 case 0x18: /* Grp16 (prefetch/nop) */
2427 c->dst.type = OP_NONE;
2428 break;
2429 case 0x20: /* mov cr, reg */
2430 if (c->modrm_mod != 3)
2431 goto cannot_emulate;
2432 c->regs[c->modrm_rm] =
2433 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2434 c->dst.type = OP_NONE; /* no writeback */
2435 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002437 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002439 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002440 if (rc)
2441 goto cannot_emulate;
2442 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002444 case 0x22: /* mov reg, cr */
2445 if (c->modrm_mod != 3)
2446 goto cannot_emulate;
2447 realmode_set_cr(ctxt->vcpu,
2448 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2449 c->dst.type = OP_NONE;
2450 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002452 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002454 rc = emulator_set_dr(ctxt, c->modrm_reg,
2455 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002456 if (rc)
2457 goto cannot_emulate;
2458 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002460 case 0x30:
2461 /* wrmsr */
2462 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2463 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2464 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2465 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002466 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002467 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002468 }
2469 rc = X86EMUL_CONTINUE;
2470 c->dst.type = OP_NONE;
2471 break;
2472 case 0x32:
2473 /* rdmsr */
2474 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2475 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002476 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002477 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002478 } else {
2479 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2480 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2481 }
2482 rc = X86EMUL_CONTINUE;
2483 c->dst.type = OP_NONE;
2484 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002485 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002486 if (emulate_sysenter(ctxt) == -1)
2487 goto cannot_emulate;
2488 else
2489 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002490 break;
2491 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002492 if (emulate_sysexit(ctxt) == -1)
2493 goto cannot_emulate;
2494 else
2495 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002496 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002498 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002499 if (!test_cc(c->b, ctxt->eflags))
2500 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002502 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002503 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002504 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002505 c->dst.type = OP_NONE;
2506 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002507 case 0xa0: /* push fs */
2508 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2509 break;
2510 case 0xa1: /* pop fs */
2511 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2512 if (rc != 0)
2513 goto done;
2514 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002515 case 0xa3:
2516 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002517 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002518 /* only subword offset */
2519 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002520 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002521 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002522 case 0xa4: /* shld imm8, r, r/m */
2523 case 0xa5: /* shld cl, r, r/m */
2524 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2525 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002526 case 0xa8: /* push gs */
2527 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2528 break;
2529 case 0xa9: /* pop gs */
2530 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2531 if (rc != 0)
2532 goto done;
2533 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002534 case 0xab:
2535 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002536 /* only subword offset */
2537 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002538 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002539 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002540 case 0xac: /* shrd imm8, r, r/m */
2541 case 0xad: /* shrd cl, r, r/m */
2542 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2543 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002544 case 0xae: /* clflush */
2545 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 case 0xb0 ... 0xb1: /* cmpxchg */
2547 /*
2548 * Save real source value, then compare EAX against
2549 * destination.
2550 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002551 c->src.orig_val = c->src.val;
2552 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002553 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2554 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002556 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 } else {
2558 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002559 c->dst.type = OP_REG;
2560 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 }
2562 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563 case 0xb3:
2564 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002565 /* only subword offset */
2566 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002567 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002570 c->dst.bytes = c->op_bytes;
2571 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2572 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002575 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 case 0:
2577 goto bt;
2578 case 1:
2579 goto bts;
2580 case 2:
2581 goto btr;
2582 case 3:
2583 goto btc;
2584 }
2585 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002586 case 0xbb:
2587 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002588 /* only subword offset */
2589 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002590 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002591 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002593 c->dst.bytes = c->op_bytes;
2594 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2595 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002597 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002598 c->dst.bytes = c->op_bytes;
2599 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2600 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002601 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002603 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002604 if (rc != 0)
2605 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002606 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002607 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 }
2609 goto writeback;
2610
2611cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002612 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002613 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 return -1;
2615}