blob: 45a4f7c1bb0b45bad98b2f41683ec6d4dedd762c [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));
619 rc = ops->read_std(linear, fc->data, size, ctxt->vcpu);
620 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,
674 ctxt->vcpu);
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,
678 ctxt->vcpu);
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:
902 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200903 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 break;
905 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200906 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800908#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200910 def_op_bytes = 4;
911 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 break;
913#endif
914 default:
915 return -1;
916 }
917
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200918 c->op_bytes = def_op_bytes;
919 c->ad_bytes = def_ad_bytes;
920
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200922 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200923 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200925 /* switch between 2/4 bytes */
926 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 break;
928 case 0x67: /* address-size override */
929 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200930 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200931 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200933 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200934 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300937 case 0x2e: /* CS override */
938 case 0x36: /* SS override */
939 case 0x3e: /* DS override */
940 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 break;
942 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300944 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200946 case 0x40 ... 0x4f: /* REX */
947 if (mode != X86EMUL_MODE_PROT64)
948 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200949 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200950 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200952 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +0200954 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100955 c->rep_prefix = REPNE_PREFIX;
956 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100958 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 default:
961 goto done_prefixes;
962 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200963
964 /* Any legacy prefix after a REX prefix nullifies its effect. */
965
Avi Kivity33615aa2007-10-31 11:15:56 +0200966 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 }
968
969done_prefixes:
970
971 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +0200973 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200974 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975
976 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200977 c->d = opcode_table[c->b];
978 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200980 if (c->b == 0x0f) {
981 c->twobyte = 1;
982 c->b = insn_fetch(u8, 1, c->eip);
983 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 }
Avi Kivitye09d0822008-01-18 12:38:59 +0200985 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300987 if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
988 kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");;
989 return -1;
990 }
991
Avi Kivitye09d0822008-01-18 12:38:59 +0200992 if (c->d & Group) {
993 group = c->d & GroupMask;
994 c->modrm = insn_fetch(u8, 1, c->eip);
995 --c->eip;
996
997 group = (group << 3) + ((c->modrm >> 3) & 7);
998 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
999 c->d = group2_table[group];
1000 else
1001 c->d = group_table[group];
1002 }
1003
1004 /* Unrecognised? */
1005 if (c->d == 0) {
1006 DPRINTF("Cannot emulate %02x\n", c->b);
1007 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 }
1009
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001010 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1011 c->op_bytes = 8;
1012
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001014 if (c->d & ModRM)
1015 rc = decode_modrm(ctxt, ops);
1016 else if (c->d & MemAbs)
1017 rc = decode_abs(ctxt, ops);
1018 if (rc)
1019 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001021 if (!c->has_seg_override)
1022 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001023
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001024 if (!(!c->twobyte && c->b == 0x8d))
1025 c->modrm_ea += seg_override_base(ctxt, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001026
1027 if (c->ad_bytes != 8)
1028 c->modrm_ea = (u32)c->modrm_ea;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 /*
1030 * Decode and fetch the source operand: register, memory
1031 * or immediate.
1032 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001033 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 case SrcNone:
1035 break;
1036 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001037 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 break;
1039 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001040 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 goto srcmem_common;
1042 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001043 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 goto srcmem_common;
1045 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001046 c->src.bytes = (c->d & ByteOp) ? 1 :
1047 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001048 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001049 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001050 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001051 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001052 /*
1053 * For instructions with a ModR/M byte, switch to register
1054 * access if Mod = 3.
1055 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001056 if ((c->d & ModRM) && c->modrm_mod == 3) {
1057 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001058 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001059 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001060 break;
1061 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001062 c->src.type = OP_MEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 break;
1064 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001065 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001066 c->src.type = OP_IMM;
1067 c->src.ptr = (unsigned long *)c->eip;
1068 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1069 if (c->src.bytes == 8)
1070 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001072 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001074 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 break;
1076 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001077 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 break;
1079 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001080 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 break;
1082 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001083 if ((c->d & SrcMask) == SrcImmU) {
1084 switch (c->src.bytes) {
1085 case 1:
1086 c->src.val &= 0xff;
1087 break;
1088 case 2:
1089 c->src.val &= 0xffff;
1090 break;
1091 case 4:
1092 c->src.val &= 0xffffffff;
1093 break;
1094 }
1095 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 break;
1097 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001098 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001099 c->src.type = OP_IMM;
1100 c->src.ptr = (unsigned long *)c->eip;
1101 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001102 if ((c->d & SrcMask) == SrcImmByte)
1103 c->src.val = insn_fetch(s8, 1, c->eip);
1104 else
1105 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001107 case SrcOne:
1108 c->src.bytes = 1;
1109 c->src.val = 1;
1110 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 }
1112
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001113 /*
1114 * Decode and fetch the second source operand: register, memory
1115 * or immediate.
1116 */
1117 switch (c->d & Src2Mask) {
1118 case Src2None:
1119 break;
1120 case Src2CL:
1121 c->src2.bytes = 1;
1122 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1123 break;
1124 case Src2ImmByte:
1125 c->src2.type = OP_IMM;
1126 c->src2.ptr = (unsigned long *)c->eip;
1127 c->src2.bytes = 1;
1128 c->src2.val = insn_fetch(u8, 1, c->eip);
1129 break;
Gleb Natapova5f868b2009-04-12 13:36:14 +03001130 case Src2Imm16:
1131 c->src2.type = OP_IMM;
1132 c->src2.ptr = (unsigned long *)c->eip;
1133 c->src2.bytes = 2;
1134 c->src2.val = insn_fetch(u16, 2, c->eip);
1135 break;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001136 case Src2One:
1137 c->src2.bytes = 1;
1138 c->src2.val = 1;
1139 break;
1140 }
1141
Avi Kivity038e51d2007-01-22 20:40:40 -08001142 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001143 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001144 case ImplicitOps:
1145 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001146 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001147 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001148 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001149 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001150 break;
1151 case DstMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001152 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001153 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001154 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001155 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001156 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001157 break;
1158 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001159 c->dst.type = OP_MEM;
1160 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001161 case DstAcc:
1162 c->dst.type = OP_REG;
1163 c->dst.bytes = c->op_bytes;
1164 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
1165 switch (c->op_bytes) {
1166 case 1:
1167 c->dst.val = *(u8 *)c->dst.ptr;
1168 break;
1169 case 2:
1170 c->dst.val = *(u16 *)c->dst.ptr;
1171 break;
1172 case 4:
1173 c->dst.val = *(u32 *)c->dst.ptr;
1174 break;
1175 }
1176 c->dst.orig_val = c->dst.val;
1177 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001178 }
1179
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001180 if (c->rip_relative)
1181 c->modrm_ea += c->eip;
1182
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001183done:
1184 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1185}
1186
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001187static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
1188{
1189 struct decode_cache *c = &ctxt->decode;
1190
1191 c->dst.type = OP_MEM;
1192 c->dst.bytes = c->op_bytes;
1193 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001194 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001195 c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001196 c->regs[VCPU_REGS_RSP]);
1197}
1198
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001199static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001200 struct x86_emulate_ops *ops,
1201 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001202{
1203 struct decode_cache *c = &ctxt->decode;
1204 int rc;
1205
Avi Kivity781d0ed2008-11-27 18:00:28 +02001206 rc = ops->read_emulated(register_address(c, ss_base(ctxt),
1207 c->regs[VCPU_REGS_RSP]),
Avi Kivity350f69d2009-01-05 11:12:40 +02001208 dest, len, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001209 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001210 return rc;
1211
Avi Kivity350f69d2009-01-05 11:12:40 +02001212 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001213 return rc;
1214}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001215
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001216static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
1217{
1218 struct decode_cache *c = &ctxt->decode;
1219 struct kvm_segment segment;
1220
1221 kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg);
1222
1223 c->src.val = segment.selector;
1224 emulate_push(ctxt);
1225}
1226
1227static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1228 struct x86_emulate_ops *ops, int seg)
1229{
1230 struct decode_cache *c = &ctxt->decode;
1231 unsigned long selector;
1232 int rc;
1233
1234 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
1235 if (rc != 0)
1236 return rc;
1237
1238 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg);
1239 return rc;
1240}
1241
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001242static void emulate_pusha(struct x86_emulate_ctxt *ctxt)
1243{
1244 struct decode_cache *c = &ctxt->decode;
1245 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1246 int reg = VCPU_REGS_RAX;
1247
1248 while (reg <= VCPU_REGS_RDI) {
1249 (reg == VCPU_REGS_RSP) ?
1250 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1251
1252 emulate_push(ctxt);
1253 ++reg;
1254 }
1255}
1256
1257static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1258 struct x86_emulate_ops *ops)
1259{
1260 struct decode_cache *c = &ctxt->decode;
1261 int rc = 0;
1262 int reg = VCPU_REGS_RDI;
1263
1264 while (reg >= VCPU_REGS_RAX) {
1265 if (reg == VCPU_REGS_RSP) {
1266 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1267 c->op_bytes);
1268 --reg;
1269 }
1270
1271 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
1272 if (rc != 0)
1273 break;
1274 --reg;
1275 }
1276 return rc;
1277}
1278
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001279static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1280 struct x86_emulate_ops *ops)
1281{
1282 struct decode_cache *c = &ctxt->decode;
1283 int rc;
1284
Avi Kivity350f69d2009-01-05 11:12:40 +02001285 rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001286 if (rc != 0)
1287 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001288 return 0;
1289}
1290
Laurent Vivier05f086f2007-09-24 11:10:55 +02001291static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001292{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001293 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001294 switch (c->modrm_reg) {
1295 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001296 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001297 break;
1298 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001299 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300 break;
1301 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001302 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001303 break;
1304 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001305 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001306 break;
1307 case 4: /* sal/shl */
1308 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001309 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001310 break;
1311 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001312 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001313 break;
1314 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001315 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001316 break;
1317 }
1318}
1319
1320static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001321 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001322{
1323 struct decode_cache *c = &ctxt->decode;
1324 int rc = 0;
1325
1326 switch (c->modrm_reg) {
1327 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001328 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001329 break;
1330 case 2: /* not */
1331 c->dst.val = ~c->dst.val;
1332 break;
1333 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001334 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001335 break;
1336 default:
1337 DPRINTF("Cannot emulate %02x\n", c->b);
1338 rc = X86EMUL_UNHANDLEABLE;
1339 break;
1340 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001341 return rc;
1342}
1343
1344static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001345 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346{
1347 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001348
1349 switch (c->modrm_reg) {
1350 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001351 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001352 break;
1353 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001354 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001355 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001356 case 2: /* call near abs */ {
1357 long int old_eip;
1358 old_eip = c->eip;
1359 c->eip = c->src.val;
1360 c->src.val = old_eip;
1361 emulate_push(ctxt);
1362 break;
1363 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001364 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001365 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001366 break;
1367 case 6: /* push */
Avi Kivityfd607542008-01-18 13:12:26 +02001368 emulate_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001369 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370 }
1371 return 0;
1372}
1373
1374static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
1375 struct x86_emulate_ops *ops,
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001376 unsigned long memop)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377{
1378 struct decode_cache *c = &ctxt->decode;
1379 u64 old, new;
1380 int rc;
1381
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001382 rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001383 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384 return rc;
1385
1386 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1387 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1388
1389 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1390 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001391 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392
1393 } else {
1394 new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1395 (u32) c->regs[VCPU_REGS_RBX];
1396
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001397 rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001398 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001399 return rc;
Laurent Vivier05f086f2007-09-24 11:10:55 +02001400 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401 }
1402 return 0;
1403}
1404
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001405static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1406 struct x86_emulate_ops *ops)
1407{
1408 struct decode_cache *c = &ctxt->decode;
1409 int rc;
1410 unsigned long cs;
1411
1412 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
1413 if (rc)
1414 return rc;
1415 if (c->op_bytes == 4)
1416 c->eip = (u32)c->eip;
1417 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1418 if (rc)
1419 return rc;
1420 rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS);
1421 return rc;
1422}
1423
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001424static inline int writeback(struct x86_emulate_ctxt *ctxt,
1425 struct x86_emulate_ops *ops)
1426{
1427 int rc;
1428 struct decode_cache *c = &ctxt->decode;
1429
1430 switch (c->dst.type) {
1431 case OP_REG:
1432 /* The 4-byte case *is* correct:
1433 * in 64-bit mode we zero-extend.
1434 */
1435 switch (c->dst.bytes) {
1436 case 1:
1437 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1438 break;
1439 case 2:
1440 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1441 break;
1442 case 4:
1443 *c->dst.ptr = (u32)c->dst.val;
1444 break; /* 64b: zero-ext */
1445 case 8:
1446 *c->dst.ptr = c->dst.val;
1447 break;
1448 }
1449 break;
1450 case OP_MEM:
1451 if (c->lock_prefix)
1452 rc = ops->cmpxchg_emulated(
1453 (unsigned long)c->dst.ptr,
1454 &c->dst.orig_val,
1455 &c->dst.val,
1456 c->dst.bytes,
1457 ctxt->vcpu);
1458 else
1459 rc = ops->write_emulated(
1460 (unsigned long)c->dst.ptr,
1461 &c->dst.val,
1462 c->dst.bytes,
1463 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001464 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001465 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001466 break;
1467 case OP_NONE:
1468 /* no writeback */
1469 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001470 default:
1471 break;
1472 }
1473 return 0;
1474}
1475
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301476static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001477{
1478 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1479 /*
1480 * an sti; sti; sequence only disable interrupts for the first
1481 * instruction. So, if the last instruction, be it emulated or
1482 * not, left the system with the INT_STI flag enabled, it
1483 * means that the last instruction is an sti. We should not
1484 * leave the flag on in this case. The same goes for mov ss
1485 */
1486 if (!(int_shadow & mask))
1487 ctxt->interruptibility = mask;
1488}
1489
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001490static inline void
1491setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
1492 struct kvm_segment *cs, struct kvm_segment *ss)
1493{
1494 memset(cs, 0, sizeof(struct kvm_segment));
1495 kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS);
1496 memset(ss, 0, sizeof(struct kvm_segment));
1497
1498 cs->l = 0; /* will be adjusted later */
1499 cs->base = 0; /* flat segment */
1500 cs->g = 1; /* 4kb granularity */
1501 cs->limit = 0xffffffff; /* 4GB limit */
1502 cs->type = 0x0b; /* Read, Execute, Accessed */
1503 cs->s = 1;
1504 cs->dpl = 0; /* will be adjusted later */
1505 cs->present = 1;
1506 cs->db = 1;
1507
1508 ss->unusable = 0;
1509 ss->base = 0; /* flat segment */
1510 ss->limit = 0xffffffff; /* 4GB limit */
1511 ss->g = 1; /* 4kb granularity */
1512 ss->s = 1;
1513 ss->type = 0x03; /* Read/Write, Accessed */
1514 ss->db = 1; /* 32bit stack segment */
1515 ss->dpl = 0;
1516 ss->present = 1;
1517}
1518
1519static int
1520emulate_syscall(struct x86_emulate_ctxt *ctxt)
1521{
1522 struct decode_cache *c = &ctxt->decode;
1523 struct kvm_segment cs, ss;
1524 u64 msr_data;
1525
1526 /* syscall is not available in real mode */
1527 if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL
Avi Kivity3eeb3282010-01-21 15:31:48 +02001528 || !is_protmode(ctxt->vcpu))
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001529 return -1;
1530
1531 setup_syscalls_segments(ctxt, &cs, &ss);
1532
1533 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1534 msr_data >>= 32;
1535 cs.selector = (u16)(msr_data & 0xfffc);
1536 ss.selector = (u16)(msr_data + 8);
1537
1538 if (is_long_mode(ctxt->vcpu)) {
1539 cs.db = 0;
1540 cs.l = 1;
1541 }
1542 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1543 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1544
1545 c->regs[VCPU_REGS_RCX] = c->eip;
1546 if (is_long_mode(ctxt->vcpu)) {
1547#ifdef CONFIG_X86_64
1548 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1549
1550 kvm_x86_ops->get_msr(ctxt->vcpu,
1551 ctxt->mode == X86EMUL_MODE_PROT64 ?
1552 MSR_LSTAR : MSR_CSTAR, &msr_data);
1553 c->eip = msr_data;
1554
1555 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
1556 ctxt->eflags &= ~(msr_data | EFLG_RF);
1557#endif
1558 } else {
1559 /* legacy mode */
1560 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
1561 c->eip = (u32)msr_data;
1562
1563 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1564 }
1565
1566 return 0;
1567}
1568
Andre Przywara8c604352009-06-18 12:56:01 +02001569static int
1570emulate_sysenter(struct x86_emulate_ctxt *ctxt)
1571{
1572 struct decode_cache *c = &ctxt->decode;
1573 struct kvm_segment cs, ss;
1574 u64 msr_data;
1575
1576 /* inject #UD if LOCK prefix is used */
1577 if (c->lock_prefix)
1578 return -1;
1579
1580 /* inject #GP if in real mode or paging is disabled */
Avi Kivity3eeb3282010-01-21 15:31:48 +02001581 if (ctxt->mode == X86EMUL_MODE_REAL || !is_protmode(ctxt->vcpu)) {
Andre Przywara8c604352009-06-18 12:56:01 +02001582 kvm_inject_gp(ctxt->vcpu, 0);
1583 return -1;
1584 }
1585
1586 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1587 * Therefore, we inject an #UD.
1588 */
1589 if (ctxt->mode == X86EMUL_MODE_PROT64)
1590 return -1;
1591
1592 setup_syscalls_segments(ctxt, &cs, &ss);
1593
1594 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1595 switch (ctxt->mode) {
1596 case X86EMUL_MODE_PROT32:
1597 if ((msr_data & 0xfffc) == 0x0) {
1598 kvm_inject_gp(ctxt->vcpu, 0);
1599 return -1;
1600 }
1601 break;
1602 case X86EMUL_MODE_PROT64:
1603 if (msr_data == 0x0) {
1604 kvm_inject_gp(ctxt->vcpu, 0);
1605 return -1;
1606 }
1607 break;
1608 }
1609
1610 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1611 cs.selector = (u16)msr_data;
1612 cs.selector &= ~SELECTOR_RPL_MASK;
1613 ss.selector = cs.selector + 8;
1614 ss.selector &= ~SELECTOR_RPL_MASK;
1615 if (ctxt->mode == X86EMUL_MODE_PROT64
1616 || is_long_mode(ctxt->vcpu)) {
1617 cs.db = 0;
1618 cs.l = 1;
1619 }
1620
1621 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1622 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1623
1624 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
1625 c->eip = msr_data;
1626
1627 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
1628 c->regs[VCPU_REGS_RSP] = msr_data;
1629
1630 return 0;
1631}
1632
Andre Przywara4668f052009-06-18 12:56:02 +02001633static int
1634emulate_sysexit(struct x86_emulate_ctxt *ctxt)
1635{
1636 struct decode_cache *c = &ctxt->decode;
1637 struct kvm_segment cs, ss;
1638 u64 msr_data;
1639 int usermode;
1640
1641 /* inject #UD if LOCK prefix is used */
1642 if (c->lock_prefix)
1643 return -1;
1644
1645 /* inject #GP if in real mode or paging is disabled */
Avi Kivity3eeb3282010-01-21 15:31:48 +02001646 if (ctxt->mode == X86EMUL_MODE_REAL || !is_protmode(ctxt->vcpu)) {
Andre Przywara4668f052009-06-18 12:56:02 +02001647 kvm_inject_gp(ctxt->vcpu, 0);
1648 return -1;
1649 }
1650
1651 /* sysexit must be called from CPL 0 */
1652 if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) {
1653 kvm_inject_gp(ctxt->vcpu, 0);
1654 return -1;
1655 }
1656
1657 setup_syscalls_segments(ctxt, &cs, &ss);
1658
1659 if ((c->rex_prefix & 0x8) != 0x0)
1660 usermode = X86EMUL_MODE_PROT64;
1661 else
1662 usermode = X86EMUL_MODE_PROT32;
1663
1664 cs.dpl = 3;
1665 ss.dpl = 3;
1666 kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
1667 switch (usermode) {
1668 case X86EMUL_MODE_PROT32:
1669 cs.selector = (u16)(msr_data + 16);
1670 if ((msr_data & 0xfffc) == 0x0) {
1671 kvm_inject_gp(ctxt->vcpu, 0);
1672 return -1;
1673 }
1674 ss.selector = (u16)(msr_data + 24);
1675 break;
1676 case X86EMUL_MODE_PROT64:
1677 cs.selector = (u16)(msr_data + 32);
1678 if (msr_data == 0x0) {
1679 kvm_inject_gp(ctxt->vcpu, 0);
1680 return -1;
1681 }
1682 ss.selector = cs.selector + 8;
1683 cs.db = 0;
1684 cs.l = 1;
1685 break;
1686 }
1687 cs.selector |= SELECTOR_RPL_MASK;
1688 ss.selector |= SELECTOR_RPL_MASK;
1689
1690 kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS);
1691 kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS);
1692
1693 c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX];
1694 c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX];
1695
1696 return 0;
1697}
1698
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001699int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001700x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001701{
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001702 unsigned long memop = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001703 u64 msr_data;
Laurent Vivier34273182007-09-18 11:27:37 +02001704 unsigned long saved_eip = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001705 struct decode_cache *c = &ctxt->decode;
Mohammed Gamala6a30342008-09-06 17:22:29 +03001706 unsigned int port;
1707 int io_dir_in;
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001708 int rc = 0;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001709
Glauber Costa310b5d32009-05-12 16:21:06 -04001710 ctxt->interruptibility = 0;
1711
Laurent Vivier34273182007-09-18 11:27:37 +02001712 /* Shadow copy of register state. Committed on successful emulation.
1713 * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
1714 * modify them.
1715 */
1716
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001717 memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
Laurent Vivier34273182007-09-18 11:27:37 +02001718 saved_eip = c->eip;
1719
Avi Kivityc7e75a32007-10-28 16:34:25 +02001720 if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001721 memop = c->modrm_ea;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001722
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001723 if (c->rep_prefix && (c->d & String)) {
1724 /* All REP prefixes have the same first termination condition */
1725 if (c->regs[VCPU_REGS_RCX] == 0) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001726 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001727 goto done;
1728 }
1729 /* The second termination condition only applies for REPE
1730 * and REPNE. Test if the repeat string operation prefix is
1731 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
1732 * corresponding termination condition according to:
1733 * - if REPE/REPZ and ZF = 0 then done
1734 * - if REPNE/REPNZ and ZF = 1 then done
1735 */
1736 if ((c->b == 0xa6) || (c->b == 0xa7) ||
1737 (c->b == 0xae) || (c->b == 0xaf)) {
1738 if ((c->rep_prefix == REPE_PREFIX) &&
1739 ((ctxt->eflags & EFLG_ZF) == 0)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001740 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001741 goto done;
1742 }
1743 if ((c->rep_prefix == REPNE_PREFIX) &&
1744 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001745 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001746 goto done;
1747 }
1748 }
1749 c->regs[VCPU_REGS_RCX]--;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001750 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02001751 }
1752
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001753 if (c->src.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001754 c->src.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001755 c->src.val = 0;
Mike Dayd77c26f2007-10-08 09:02:08 -04001756 rc = ops->read_emulated((unsigned long)c->src.ptr,
1757 &c->src.val,
1758 c->src.bytes,
1759 ctxt->vcpu);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001760 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001761 goto done;
1762 c->src.orig_val = c->src.val;
1763 }
1764
1765 if ((c->d & DstMask) == ImplicitOps)
1766 goto special_insn;
1767
1768
1769 if (c->dst.type == OP_MEM) {
Sheng Yange8d8d7f2007-11-16 16:29:15 +08001770 c->dst.ptr = (unsigned long *)memop;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001771 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1772 c->dst.val = 0;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001773 if (c->d & BitOp) {
1774 unsigned long mask = ~(c->dst.bytes * 8 - 1);
Avi Kivitydf513e22007-03-28 20:04:16 +02001775
Laurent Viviere4e03de2007-09-18 11:52:50 +02001776 c->dst.ptr = (void *)c->dst.ptr +
1777 (c->src.val & mask) / 8;
Avi Kivity038e51d2007-01-22 20:40:40 -08001778 }
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001779 if (!(c->d & Mov)) {
1780 /* optimisation - avoid slow emulated read */
1781 rc = ops->read_emulated((unsigned long)c->dst.ptr,
1782 &c->dst.val,
1783 c->dst.bytes,
1784 ctxt->vcpu);
1785 if (rc != X86EMUL_CONTINUE)
1786 goto done;
1787 }
Avi Kivity038e51d2007-01-22 20:40:40 -08001788 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001789 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08001790
Avi Kivity018a98d2007-11-27 19:30:56 +02001791special_insn:
1792
Laurent Viviere4e03de2007-09-18 11:52:50 +02001793 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 goto twobyte_insn;
1795
Laurent Viviere4e03de2007-09-18 11:52:50 +02001796 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 case 0x00 ... 0x05:
1798 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001799 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001801 case 0x06: /* push es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001802 emulate_push_sreg(ctxt, VCPU_SREG_ES);
1803 break;
1804 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001805 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
1806 if (rc != 0)
1807 goto done;
1808 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 case 0x08 ... 0x0d:
1810 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001811 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001813 case 0x0e: /* push cs */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001814 emulate_push_sreg(ctxt, VCPU_SREG_CS);
1815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 case 0x10 ... 0x15:
1817 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001818 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001820 case 0x16: /* push ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821 emulate_push_sreg(ctxt, VCPU_SREG_SS);
1822 break;
1823 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001824 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
1825 if (rc != 0)
1826 goto done;
1827 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828 case 0x18 ... 0x1d:
1829 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001830 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001832 case 0x1e: /* push ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001833 emulate_push_sreg(ctxt, VCPU_SREG_DS);
1834 break;
1835 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001836 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
1837 if (rc != 0)
1838 goto done;
1839 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02001840 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001842 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 break;
1844 case 0x28 ... 0x2d:
1845 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001846 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 break;
1848 case 0x30 ... 0x35:
1849 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001850 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 break;
1852 case 0x38 ... 0x3d:
1853 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001854 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02001856 case 0x40 ... 0x47: /* inc r16/r32 */
1857 emulate_1op("inc", c->dst, ctxt->eflags);
1858 break;
1859 case 0x48 ... 0x4f: /* dec r16/r32 */
1860 emulate_1op("dec", c->dst, ctxt->eflags);
1861 break;
1862 case 0x50 ... 0x57: /* push reg */
Guillaume Thouvenin2786b012008-09-22 16:08:06 +02001863 emulate_push(ctxt);
Avi Kivity33615aa2007-10-31 11:15:56 +02001864 break;
1865 case 0x58 ... 0x5f: /* pop reg */
1866 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02001867 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity8a09b682008-11-27 18:06:33 +02001868 if (rc != 0)
Avi Kivity33615aa2007-10-31 11:15:56 +02001869 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02001870 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871 case 0x60: /* pusha */
1872 emulate_pusha(ctxt);
1873 break;
1874 case 0x61: /* popa */
1875 rc = emulate_popa(ctxt, ops);
1876 if (rc != 0)
1877 goto done;
1878 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001880 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001882 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03001884 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02001885 case 0x6a: /* push imm8 */
Avi Kivity018a98d2007-11-27 19:30:56 +02001886 emulate_push(ctxt);
1887 break;
1888 case 0x6c: /* insb */
1889 case 0x6d: /* insw/insd */
Avi Kivity851ba692009-08-24 11:10:17 +03001890 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001891 1,
1892 (c->d & ByteOp) ? 1 : c->op_bytes,
1893 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001894 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001895 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001896 register_address(c, es_base(ctxt),
Avi Kivity018a98d2007-11-27 19:30:56 +02001897 c->regs[VCPU_REGS_RDI]),
1898 c->rep_prefix,
1899 c->regs[VCPU_REGS_RDX]) == 0) {
1900 c->eip = saved_eip;
1901 return -1;
1902 }
1903 return 0;
1904 case 0x6e: /* outsb */
1905 case 0x6f: /* outsw/outsd */
Avi Kivity851ba692009-08-24 11:10:17 +03001906 if (kvm_emulate_pio_string(ctxt->vcpu,
Avi Kivity018a98d2007-11-27 19:30:56 +02001907 0,
1908 (c->d & ByteOp) ? 1 : c->op_bytes,
1909 c->rep_prefix ?
Harvey Harrisone4706772008-02-19 07:40:38 -08001910 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
Avi Kivity018a98d2007-11-27 19:30:56 +02001911 (ctxt->eflags & EFLG_DF),
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001912 register_address(c,
1913 seg_override_base(ctxt, c),
Avi Kivity018a98d2007-11-27 19:30:56 +02001914 c->regs[VCPU_REGS_RSI]),
1915 c->rep_prefix,
1916 c->regs[VCPU_REGS_RDX]) == 0) {
1917 c->eip = saved_eip;
1918 return -1;
1919 }
1920 return 0;
Gleb Natapovb2833e32009-04-12 13:36:30 +03001921 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02001922 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03001923 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02001924 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001926 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 case 0:
1928 goto add;
1929 case 1:
1930 goto or;
1931 case 2:
1932 goto adc;
1933 case 3:
1934 goto sbb;
1935 case 4:
1936 goto and;
1937 case 5:
1938 goto sub;
1939 case 6:
1940 goto xor;
1941 case 7:
1942 goto cmp;
1943 }
1944 break;
1945 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02001946 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 break;
1948 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03001949 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001951 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001953 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 break;
1955 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001956 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 break;
1958 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001959 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 break; /* 64b reg: zero-extend */
1961 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001962 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 break;
1964 }
1965 /*
1966 * Write back the memory destination with implicit LOCK
1967 * prefix.
1968 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001969 c->dst.val = c->src.val;
1970 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03001973 goto mov;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02001974 case 0x8c: { /* mov r/m, sreg */
1975 struct kvm_segment segreg;
1976
1977 if (c->modrm_reg <= 5)
1978 kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg);
1979 else {
1980 printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n",
1981 c->modrm);
1982 goto cannot_emulate;
1983 }
1984 c->dst.val = segreg.selector;
1985 break;
1986 }
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001987 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03001988 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03001989 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001990 case 0x8e: { /* mov seg, r/m16 */
1991 uint16_t sel;
1992 int type_bits;
1993 int err;
1994
1995 sel = c->src.val;
Glauber Costa310b5d32009-05-12 16:21:06 -04001996 if (c->modrm_reg == VCPU_SREG_SS)
1997 toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS);
1998
Guillaume Thouvenin42571982008-05-27 14:49:15 +02001999 if (c->modrm_reg <= 5) {
2000 type_bits = (c->modrm_reg == 1) ? 9 : 1;
2001 err = kvm_load_segment_descriptor(ctxt->vcpu, sel,
2002 type_bits, c->modrm_reg);
2003 } else {
2004 printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n",
2005 c->modrm);
2006 goto cannot_emulate;
2007 }
2008
2009 if (err < 0)
2010 goto cannot_emulate;
2011
2012 c->dst.type = OP_NONE; /* Disable writeback. */
2013 break;
2014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016 rc = emulate_grp1a(ctxt, ops);
2017 if (rc != 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002020 case 0x90: /* nop / xchg r8,rax */
2021 if (!(c->rex_prefix & 1)) { /* nop */
2022 c->dst.type = OP_NONE;
2023 break;
2024 }
2025 case 0x91 ... 0x97: /* xchg reg,rax */
2026 c->src.type = c->dst.type = OP_REG;
2027 c->src.bytes = c->dst.bytes = c->op_bytes;
2028 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2029 c->src.val = *(c->src.ptr);
2030 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002031 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002032 c->src.val = (unsigned long) ctxt->eflags;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002033 emulate_push(ctxt);
2034 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002035 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002036 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002037 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002038 c->dst.bytes = c->op_bytes;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002039 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002040 case 0xa0 ... 0xa1: /* mov */
2041 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2042 c->dst.val = c->src.val;
2043 break;
2044 case 0xa2 ... 0xa3: /* mov */
2045 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2046 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 case 0xa4 ... 0xa5: /* movs */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002048 c->dst.type = OP_MEM;
2049 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002050 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002051 es_base(ctxt),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002052 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002053 rc = ops->read_emulated(register_address(c,
2054 seg_override_base(ctxt, c),
2055 c->regs[VCPU_REGS_RSI]),
Laurent Viviere4e03de2007-09-18 11:52:50 +02002056 &c->dst.val,
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002057 c->dst.bytes, ctxt->vcpu);
2058 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002060 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002061 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002062 : c->dst.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002063 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002064 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002065 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 break;
2067 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002068 c->src.type = OP_NONE; /* Disable writeback. */
2069 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002070 c->src.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002071 seg_override_base(ctxt, c),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002072 c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002073 rc = ops->read_emulated((unsigned long)c->src.ptr,
2074 &c->src.val,
2075 c->src.bytes,
2076 ctxt->vcpu);
2077 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002078 goto done;
2079
2080 c->dst.type = OP_NONE; /* Disable writeback. */
2081 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002082 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002083 es_base(ctxt),
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002084 c->regs[VCPU_REGS_RDI]);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002085 rc = ops->read_emulated((unsigned long)c->dst.ptr,
2086 &c->dst.val,
2087 c->dst.bytes,
2088 ctxt->vcpu);
2089 if (rc != X86EMUL_CONTINUE)
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002090 goto done;
2091
2092 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
2093
2094 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2095
Harvey Harrison7a9572752008-02-19 07:40:41 -08002096 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002097 (ctxt->eflags & EFLG_DF) ? -c->src.bytes
2098 : c->src.bytes);
Harvey Harrison7a9572752008-02-19 07:40:41 -08002099 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002100 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
2101 : c->dst.bytes);
2102
2103 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002105 c->dst.type = OP_MEM;
2106 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Harvey Harrisone4706772008-02-19 07:40:38 -08002107 c->dst.ptr = (unsigned long *)register_address(c,
Avi Kivity7a5b56d2008-06-22 16:22:51 +03002108 es_base(ctxt),
Sheng Yanga7e6c882007-11-15 14:52:28 +08002109 c->regs[VCPU_REGS_RDI]);
Laurent Viviere4e03de2007-09-18 11:52:50 +02002110 c->dst.val = c->regs[VCPU_REGS_RAX];
Harvey Harrison7a9572752008-02-19 07:40:41 -08002111 register_address_increment(c, &c->regs[VCPU_REGS_RDI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002112 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002113 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break;
2115 case 0xac ... 0xad: /* lods */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002116 c->dst.type = OP_REG;
2117 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2118 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002119 rc = ops->read_emulated(register_address(c,
2120 seg_override_base(ctxt, c),
2121 c->regs[VCPU_REGS_RSI]),
2122 &c->dst.val,
2123 c->dst.bytes,
2124 ctxt->vcpu);
2125 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 goto done;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002127 register_address_increment(c, &c->regs[VCPU_REGS_RSI],
Laurent Vivier05f086f2007-09-24 11:10:55 +02002128 (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
Laurent Viviere4e03de2007-09-18 11:52:50 +02002129 : c->dst.bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130 break;
2131 case 0xae ... 0xaf: /* scas */
2132 DPRINTF("Urk! I don't handle SCAS.\n");
2133 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002134 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002135 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002136 case 0xc0 ... 0xc1:
2137 emulate_grp2(ctxt);
2138 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002139 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002140 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002141 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002142 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002143 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002144 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2145 mov:
2146 c->dst.val = c->src.val;
2147 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002148 case 0xcb: /* ret far */
2149 rc = emulate_ret_far(ctxt, ops);
2150 if (rc)
2151 goto done;
2152 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002153 case 0xd0 ... 0xd1: /* Grp2 */
2154 c->src.val = 1;
2155 emulate_grp2(ctxt);
2156 break;
2157 case 0xd2 ... 0xd3: /* Grp2 */
2158 c->src.val = c->regs[VCPU_REGS_RCX];
2159 emulate_grp2(ctxt);
2160 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002161 case 0xe4: /* inb */
2162 case 0xe5: /* in */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002163 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002164 io_dir_in = 1;
2165 goto do_io;
2166 case 0xe6: /* outb */
2167 case 0xe7: /* out */
Gleb Natapov84ce66a2009-04-12 13:36:46 +03002168 port = c->src.val;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002169 io_dir_in = 0;
2170 goto do_io;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002171 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002172 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002173 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002174 jmp_rel(c, rel);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002175 emulate_push(ctxt);
2176 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002177 }
2178 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002179 goto jmp;
Gleb Natapov782b8772009-04-12 13:36:25 +03002180 case 0xea: /* jmp far */
2181 if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9,
2182 VCPU_SREG_CS) < 0) {
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002183 DPRINTF("jmp far: Failed to load CS descriptor\n");
2184 goto cannot_emulate;
2185 }
2186
Gleb Natapov782b8772009-04-12 13:36:25 +03002187 c->eip = c->src.val;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002188 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002189 case 0xeb:
2190 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002191 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002192 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002193 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002194 case 0xec: /* in al,dx */
2195 case 0xed: /* in (e/r)ax,dx */
2196 port = c->regs[VCPU_REGS_RDX];
2197 io_dir_in = 1;
2198 goto do_io;
2199 case 0xee: /* out al,dx */
2200 case 0xef: /* out (e/r)ax,dx */
2201 port = c->regs[VCPU_REGS_RDX];
2202 io_dir_in = 0;
Avi Kivity851ba692009-08-24 11:10:17 +03002203 do_io: if (kvm_emulate_pio(ctxt->vcpu, io_dir_in,
Mohammed Gamala6a30342008-09-06 17:22:29 +03002204 (c->d & ByteOp) ? 1 : c->op_bytes,
2205 port) != 0) {
2206 c->eip = saved_eip;
2207 goto cannot_emulate;
2208 }
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002209 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002210 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002211 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002212 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002213 case 0xf5: /* cmc */
2214 /* complement carry flag from eflags reg */
2215 ctxt->eflags ^= EFLG_CF;
2216 c->dst.type = OP_NONE; /* Disable writeback. */
2217 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002218 case 0xf6 ... 0xf7: /* Grp3 */
2219 rc = emulate_grp3(ctxt, ops);
2220 if (rc != 0)
2221 goto done;
2222 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002223 case 0xf8: /* clc */
2224 ctxt->eflags &= ~EFLG_CF;
2225 c->dst.type = OP_NONE; /* Disable writeback. */
2226 break;
2227 case 0xfa: /* cli */
2228 ctxt->eflags &= ~X86_EFLAGS_IF;
2229 c->dst.type = OP_NONE; /* Disable writeback. */
2230 break;
2231 case 0xfb: /* sti */
Glauber Costa310b5d32009-05-12 16:21:06 -04002232 toggle_interruptibility(ctxt, X86_SHADOW_INT_STI);
Avi Kivity111de5d2007-11-27 19:14:21 +02002233 ctxt->eflags |= X86_EFLAGS_IF;
2234 c->dst.type = OP_NONE; /* Disable writeback. */
2235 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002236 case 0xfc: /* cld */
2237 ctxt->eflags &= ~EFLG_DF;
2238 c->dst.type = OP_NONE; /* Disable writeback. */
2239 break;
2240 case 0xfd: /* std */
2241 ctxt->eflags |= EFLG_DF;
2242 c->dst.type = OP_NONE; /* Disable writeback. */
2243 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002244 case 0xfe ... 0xff: /* Grp4/Grp5 */
2245 rc = emulate_grp45(ctxt, ops);
2246 if (rc != 0)
2247 goto done;
2248 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002250
2251writeback:
2252 rc = writeback(ctxt, ops);
2253 if (rc != 0)
2254 goto done;
2255
2256 /* Commit shadow register state. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002257 memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002258 kvm_rip_write(ctxt->vcpu, c->eip);
Avi Kivity018a98d2007-11-27 19:30:56 +02002259
2260done:
2261 if (rc == X86EMUL_UNHANDLEABLE) {
2262 c->eip = saved_eip;
2263 return -1;
2264 }
2265 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266
2267twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002268 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002270 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 u16 size;
2272 unsigned long address;
2273
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002274 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002275 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002276 goto cannot_emulate;
2277
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002278 rc = kvm_fix_hypercall(ctxt->vcpu);
2279 if (rc)
2280 goto done;
2281
Avi Kivity33e38852008-05-21 15:34:25 +03002282 /* Let the processor re-execute the fixed hypercall */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002283 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity16286d02008-04-14 14:40:50 +03002284 /* Disable writeback. */
2285 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002286 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002288 rc = read_descriptor(ctxt, ops, c->src.ptr,
2289 &size, &address, c->op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 if (rc)
2291 goto done;
2292 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03002293 /* Disable writeback. */
2294 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002296 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02002297 if (c->modrm_mod == 3) {
2298 switch (c->modrm_rm) {
2299 case 1:
2300 rc = kvm_fix_hypercall(ctxt->vcpu);
2301 if (rc)
2302 goto done;
2303 break;
2304 default:
2305 goto cannot_emulate;
2306 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002307 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02002308 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002309 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02002310 c->op_bytes);
Anthony Liguoriaca7f962007-09-17 14:57:49 -05002311 if (rc)
2312 goto done;
2313 realmode_lidt(ctxt->vcpu, size, address);
2314 }
Avi Kivity16286d02008-04-14 14:40:50 +03002315 /* Disable writeback. */
2316 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 break;
2318 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002319 c->dst.bytes = 2;
2320 c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 break;
2322 case 6: /* lmsw */
Avi Kivity16286d02008-04-14 14:40:50 +03002323 realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
2324 &ctxt->eflags);
Avi Kivitydc7457e2008-04-30 16:13:36 +03002325 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 break;
2327 case 7: /* invlpg*/
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002328 emulate_invlpg(ctxt->vcpu, memop);
Avi Kivity16286d02008-04-14 14:40:50 +03002329 /* Disable writeback. */
2330 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 break;
2332 default:
2333 goto cannot_emulate;
2334 }
2335 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002336 case 0x05: /* syscall */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002337 if (emulate_syscall(ctxt) == -1)
2338 goto cannot_emulate;
2339 else
2340 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002341 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002342 case 0x06:
2343 emulate_clts(ctxt->vcpu);
2344 c->dst.type = OP_NONE;
2345 break;
2346 case 0x08: /* invd */
2347 case 0x09: /* wbinvd */
2348 case 0x0d: /* GrpP (prefetch) */
2349 case 0x18: /* Grp16 (prefetch/nop) */
2350 c->dst.type = OP_NONE;
2351 break;
2352 case 0x20: /* mov cr, reg */
2353 if (c->modrm_mod != 3)
2354 goto cannot_emulate;
2355 c->regs[c->modrm_rm] =
2356 realmode_get_cr(ctxt->vcpu, c->modrm_reg);
2357 c->dst.type = OP_NONE; /* no writeback */
2358 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 case 0x21: /* mov from dr to reg */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002360 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 goto cannot_emulate;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002362 rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002363 if (rc)
2364 goto cannot_emulate;
2365 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002367 case 0x22: /* mov reg, cr */
2368 if (c->modrm_mod != 3)
2369 goto cannot_emulate;
2370 realmode_set_cr(ctxt->vcpu,
2371 c->modrm_reg, c->modrm_val, &ctxt->eflags);
2372 c->dst.type = OP_NONE;
2373 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374 case 0x23: /* mov from reg to dr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002375 if (c->modrm_mod != 3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002377 rc = emulator_set_dr(ctxt, c->modrm_reg,
2378 c->regs[c->modrm_rm]);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002379 if (rc)
2380 goto cannot_emulate;
2381 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002383 case 0x30:
2384 /* wrmsr */
2385 msr_data = (u32)c->regs[VCPU_REGS_RAX]
2386 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
2387 rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
2388 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002389 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002390 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002391 }
2392 rc = X86EMUL_CONTINUE;
2393 c->dst.type = OP_NONE;
2394 break;
2395 case 0x32:
2396 /* rdmsr */
2397 rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
2398 if (rc) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002399 kvm_inject_gp(ctxt->vcpu, 0);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002400 c->eip = kvm_rip_read(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02002401 } else {
2402 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
2403 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
2404 }
2405 rc = X86EMUL_CONTINUE;
2406 c->dst.type = OP_NONE;
2407 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02002408 case 0x34: /* sysenter */
Andre Przywara8c604352009-06-18 12:56:01 +02002409 if (emulate_sysenter(ctxt) == -1)
2410 goto cannot_emulate;
2411 else
2412 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002413 break;
2414 case 0x35: /* sysexit */
Andre Przywara4668f052009-06-18 12:56:02 +02002415 if (emulate_sysexit(ctxt) == -1)
2416 goto cannot_emulate;
2417 else
2418 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02002419 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002421 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02002422 if (!test_cc(c->b, ctxt->eflags))
2423 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002425 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02002426 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002427 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002428 c->dst.type = OP_NONE;
2429 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002430 case 0xa0: /* push fs */
2431 emulate_push_sreg(ctxt, VCPU_SREG_FS);
2432 break;
2433 case 0xa1: /* pop fs */
2434 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
2435 if (rc != 0)
2436 goto done;
2437 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002438 case 0xa3:
2439 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08002440 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002441 /* only subword offset */
2442 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002443 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002444 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002445 case 0xa4: /* shld imm8, r, r/m */
2446 case 0xa5: /* shld cl, r, r/m */
2447 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
2448 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002449 case 0xa8: /* push gs */
2450 emulate_push_sreg(ctxt, VCPU_SREG_GS);
2451 break;
2452 case 0xa9: /* pop gs */
2453 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
2454 if (rc != 0)
2455 goto done;
2456 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002457 case 0xab:
2458 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002459 /* only subword offset */
2460 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002461 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002462 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01002463 case 0xac: /* shrd imm8, r, r/m */
2464 case 0xad: /* shrd cl, r, r/m */
2465 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
2466 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03002467 case 0xae: /* clflush */
2468 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 case 0xb0 ... 0xb1: /* cmpxchg */
2470 /*
2471 * Save real source value, then compare EAX against
2472 * destination.
2473 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002474 c->src.orig_val = c->src.val;
2475 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02002476 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2477 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002479 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 } else {
2481 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002482 c->dst.type = OP_REG;
2483 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 }
2485 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 case 0xb3:
2487 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002488 /* only subword offset */
2489 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002490 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002493 c->dst.bytes = c->op_bytes;
2494 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
2495 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002498 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 case 0:
2500 goto bt;
2501 case 1:
2502 goto bts;
2503 case 2:
2504 goto btr;
2505 case 3:
2506 goto btc;
2507 }
2508 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03002509 case 0xbb:
2510 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002511 /* only subword offset */
2512 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002513 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03002514 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002516 c->dst.bytes = c->op_bytes;
2517 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
2518 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 break;
Sheng Yanga012e652007-10-15 14:24:20 +08002520 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002521 c->dst.bytes = c->op_bytes;
2522 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
2523 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08002524 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 case 0xc7: /* Grp9 (cmpxchg8b) */
Sheng Yange8d8d7f2007-11-16 16:29:15 +08002526 rc = emulate_grp9(ctxt, ops, memop);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002527 if (rc != 0)
2528 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02002529 c->dst.type = OP_NONE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002530 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 }
2532 goto writeback;
2533
2534cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002535 DPRINTF("Cannot emulate %02x\n", c->b);
Laurent Vivier34273182007-09-18 11:27:37 +02002536 c->eip = saved_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 return -1;
2538}