blob: b43ac98ef79002828ac0cc3446ffc20f6aa7ee74 [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"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020036#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038/*
39 * Opcode effective-address decode tables.
40 * Note that we only emulate instructions that have at least one memory
41 * operand (excluding implicit stack references). We assume that stack
42 * references and instruction fetches will never occur in special memory
43 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
44 * not be handled.
45 */
46
47/* Operand sizes: 8-bit operands or specified/overridden size. */
48#define ByteOp (1<<0) /* 8-bit operands. */
49/* Destination operand type. */
50#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
51#define DstReg (2<<1) /* Register operand. */
52#define DstMem (3<<1) /* Memory operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define DstAcc (4<<1) /* Destination Accumulator */
Gleb Natapova682e352010-03-18 15:20:21 +020054#define DstDI (5<<1) /* Destination is in ES:(E)DI */
Gleb Natapov6550e1f2010-03-21 13:08:21 +020055#define DstMem64 (6<<1) /* 64bit memory operand */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020056#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080057/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020058#define SrcNone (0<<4) /* No source operand. */
59#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
60#define SrcReg (1<<4) /* Register operand. */
61#define SrcMem (2<<4) /* Memory operand. */
62#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
63#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
64#define SrcImm (5<<4) /* Immediate operand. */
65#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010066#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030068#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020069#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030070#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
71#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Gleb Natapov341de7e2009-04-12 13:36:41 +030072#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080073/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define Mov (1<<9)
77#define BitOp (1<<10)
78#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020079#define String (1<<12) /* String instruction (rep capable) */
80#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020081#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
82#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
83#define GroupMask 0xff /* Group number stored in bits 0:7 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084/* Misc flags */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020085#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020086#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030087#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010088/* Source 2 operand type */
89#define Src2None (0<<29)
90#define Src2CL (1<<29)
91#define Src2ImmByte (2<<29)
92#define Src2One (3<<29)
93#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094
Avi Kivity43bb19c2008-01-18 12:46:50 +020095enum {
Avi Kivity1d6ad202008-01-23 22:26:09 +020096 Group1_80, Group1_81, Group1_82, Group1_83,
Avi Kivityd95058a2008-01-18 13:36:50 +020097 Group1A, Group3_Byte, Group3, Group4, Group5, Group7,
Gleb Natapov60a29d42010-02-10 14:21:30 +020098 Group8, Group9,
Avi Kivity43bb19c2008-01-18 12:46:50 +020099};
100
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100101static u32 opcode_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102 /* 0x00 - 0x07 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200103 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300105 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300106 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 /* 0x08 - 0x0F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200108 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal94677e62009-08-28 16:41:44 +0200110 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
111 ImplicitOps | Stack | No64, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112 /* 0x10 - 0x17 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200113 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300115 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300116 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 /* 0x18 - 0x1F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200118 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300120 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 /* 0x20 - 0x27 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200123 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +0200125 DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126 /* 0x28 - 0x2F */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
129 0, 0, 0, 0,
130 /* 0x30 - 0x37 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200131 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
133 0, 0, 0, 0,
134 /* 0x38 - 0x3F */
135 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
136 ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
Guillaume Thouvenin8a9fee62008-09-12 13:51:15 +0200137 ByteOp | DstAcc | SrcImm, DstAcc | SrcImm,
138 0, 0,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700139 /* 0x40 - 0x47 */
Avi Kivity33615aa2007-10-31 11:15:56 +0200140 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kambled77a2502007-10-12 17:40:33 -0700141 /* 0x48 - 0x4F */
Avi Kivity33615aa2007-10-31 11:15:56 +0200142 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300143 /* 0x50 - 0x57 */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200144 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
145 SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack,
Nitin A Kamble7f0aaee2007-06-19 11:16:04 +0300146 /* 0x58 - 0x5F */
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200147 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
148 DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700149 /* 0x60 - 0x67 */
Mohammed Gamalabcf14b2009-09-01 15:28:11 +0200150 ImplicitOps | Stack | No64, ImplicitOps | Stack | No64,
151 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
Nitin A Kamble7d316912007-08-28 17:58:52 -0700152 0, 0, 0, 0,
153 /* 0x68 - 0x6F */
Avi Kivity91ed7a02008-05-29 14:38:38 +0300154 SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0,
Gleb Natapov79729952010-03-18 15:20:24 +0200155 DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */
156 SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300157 /* 0x70 - 0x77 */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300158 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
159 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Nitin A Kamble55bebde2007-09-15 10:25:41 +0300160 /* 0x78 - 0x7F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300161 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
162 SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 /* 0x80 - 0x87 */
Avi Kivity1d6ad202008-01-23 22:26:09 +0200164 Group | Group1_80, Group | Group1_81,
165 Group | Group1_82, Group | Group1_83,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200167 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 /* 0x88 - 0x8F */
169 ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
170 ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +0200171 DstMem | SrcReg | ModRM | Mov, ModRM | DstReg,
Gleb Natapov054fe9f2010-04-28 19:15:23 +0300172 ImplicitOps | SrcMem | ModRM, Group | Group1A,
Mohammed Gamalb13354f2008-06-15 19:37:38 +0300173 /* 0x90 - 0x97 */
174 DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg,
175 /* 0x98 - 0x9F */
Gleb Natapov414e6272010-04-28 19:15:26 +0300176 0, 0, SrcImmFAddr | No64, 0,
Gleb Natapov06541692009-04-12 13:36:20 +0300177 ImplicitOps | Stack, ImplicitOps | Stack, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178 /* 0xA0 - 0xA7 */
Avi Kivityc7e75a32007-10-28 16:34:25 +0200179 ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs,
180 ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs,
Gleb Natapova682e352010-03-18 15:20:21 +0200181 ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String,
182 ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 /* 0xA8 - 0xAF */
Gleb Natapova682e352010-03-18 15:20:21 +0200184 0, 0, ByteOp | DstDI | Mov | String, DstDI | Mov | String,
185 ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String,
186 ByteOp | DstDI | String, DstDI | String,
Mohammed Gamala5e2e822008-08-27 05:02:56 +0300187 /* 0xB0 - 0xB7 */
188 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
189 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
190 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
191 ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov,
192 /* 0xB8 - 0xBF */
193 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
194 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
195 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
196 DstReg | SrcImm | Mov, DstReg | SrcImm | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 /* 0xC0 - 0xC7 */
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300198 ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
Avi Kivity6e3d5df2007-12-06 18:14:14 +0200199 0, ImplicitOps | Stack, 0, 0,
Nitin A Kambled9413cd2007-06-19 11:21:15 +0300200 ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 /* 0xC8 - 0xCF */
Gleb Natapove637b822009-04-12 13:36:52 +0300202 0, 0, 0, ImplicitOps | Stack,
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300203 ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204 /* 0xD0 - 0xD7 */
205 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
206 ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
207 0, 0, 0, 0,
208 /* 0xD8 - 0xDF */
209 0, 0, 0, 0, 0, 0, 0, 0,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300210 /* 0xE0 - 0xE7 */
Mohammed Gamala6a30342008-09-06 17:22:29 +0300211 0, 0, 0, 0,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200212 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
213 ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc,
Nitin A Kamble098c9372007-08-19 11:00:36 +0300214 /* 0xE8 - 0xEF */
Gleb Natapovd53c4772009-04-12 13:36:36 +0300215 SrcImm | Stack, SrcImm | ImplicitOps,
Gleb Natapov414e6272010-04-28 19:15:26 +0300216 SrcImmFAddr | No64, SrcImmByte | ImplicitOps,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200217 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
218 SrcNone | ByteOp | DstAcc, SrcNone | DstAcc,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 /* 0xF0 - 0xF7 */
220 0, 0, 0, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200221 ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 /* 0xF8 - 0xFF */
Nitin A Kambleb284be52007-10-16 18:23:27 -0700223 ImplicitOps, 0, ImplicitOps, ImplicitOps,
Mohammed Gamalfb4616f2008-09-01 04:52:24 +0300224 ImplicitOps, ImplicitOps, Group | Group4, Group | Group5,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225};
226
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100227static u32 twobyte_table[256] = {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 /* 0x00 - 0x0F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200229 0, Group | GroupDual | Group7, 0, 0,
230 0, ImplicitOps, ImplicitOps | Priv, 0,
231 ImplicitOps | Priv, ImplicitOps | Priv, 0, 0,
232 0, ImplicitOps | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 /* 0x10 - 0x1F */
234 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
235 /* 0x20 - 0x2F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200236 ModRM | ImplicitOps | Priv, ModRM | Priv,
237 ModRM | ImplicitOps | Priv, ModRM | Priv,
238 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 0, 0, 0, 0, 0, 0, 0, 0,
240 /* 0x30 - 0x3F */
Gleb Natapove92805a2010-02-10 14:21:35 +0200241 ImplicitOps | Priv, 0, ImplicitOps | Priv, 0,
242 ImplicitOps, ImplicitOps | Priv, 0, 0,
Andre Przywarae99f0502009-06-17 15:50:33 +0200243 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 /* 0x40 - 0x47 */
245 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
246 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
247 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
248 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
249 /* 0x48 - 0x4F */
250 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
251 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
252 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
253 DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
254 /* 0x50 - 0x5F */
255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256 /* 0x60 - 0x6F */
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 /* 0x70 - 0x7F */
259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260 /* 0x80 - 0x8F */
Gleb Natapovb2833e32009-04-12 13:36:30 +0300261 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
262 SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 /* 0x90 - 0x9F */
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265 /* 0xA0 - 0xA7 */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300266 ImplicitOps | Stack, ImplicitOps | Stack,
267 0, DstMem | SrcReg | ModRM | BitOp,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100268 DstMem | SrcReg | Src2ImmByte | ModRM,
269 DstMem | SrcReg | Src2CL | ModRM, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270 /* 0xA8 - 0xAF */
Mohammed Gamal0934ac92009-08-23 14:24:24 +0300271 ImplicitOps | Stack, ImplicitOps | Stack,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200272 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +0100273 DstMem | SrcReg | Src2ImmByte | ModRM,
274 DstMem | SrcReg | Src2CL | ModRM,
275 ModRM, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 /* 0xB0 - 0xB7 */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200277 ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock,
278 0, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
280 DstReg | SrcMem16 | ModRM | Mov,
281 /* 0xB8 - 0xBF */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200282 0, 0,
283 Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
285 DstReg | SrcMem16 | ModRM | Mov,
286 /* 0xC0 - 0xCF */
Gleb Natapov60a29d42010-02-10 14:21:30 +0200287 0, 0, 0, DstMem | SrcReg | ModRM | Mov,
288 0, 0, 0, Group | GroupDual | Group9,
Sheng Yanga012e652007-10-15 14:24:20 +0800289 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290 /* 0xD0 - 0xDF */
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292 /* 0xE0 - 0xEF */
293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
294 /* 0xF0 - 0xFF */
295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
296};
297
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100298static u32 group_table[] = {
Avi Kivity1d6ad202008-01-23 22:26:09 +0200299 [Group1_80*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200300 ByteOp | DstMem | SrcImm | ModRM | Lock,
301 ByteOp | DstMem | SrcImm | ModRM | Lock,
302 ByteOp | DstMem | SrcImm | ModRM | Lock,
303 ByteOp | DstMem | SrcImm | ModRM | Lock,
304 ByteOp | DstMem | SrcImm | ModRM | Lock,
305 ByteOp | DstMem | SrcImm | ModRM | Lock,
306 ByteOp | DstMem | SrcImm | ModRM | Lock,
307 ByteOp | DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200308 [Group1_81*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200309 DstMem | SrcImm | ModRM | Lock,
310 DstMem | SrcImm | ModRM | Lock,
311 DstMem | SrcImm | ModRM | Lock,
312 DstMem | SrcImm | ModRM | Lock,
313 DstMem | SrcImm | ModRM | Lock,
314 DstMem | SrcImm | ModRM | Lock,
315 DstMem | SrcImm | ModRM | Lock,
316 DstMem | SrcImm | ModRM,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200317 [Group1_82*8] =
Gleb Natapove424e192010-02-11 12:41:10 +0200318 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
319 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
320 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
321 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
322 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
323 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
324 ByteOp | DstMem | SrcImm | ModRM | No64 | Lock,
325 ByteOp | DstMem | SrcImm | ModRM | No64,
Avi Kivity1d6ad202008-01-23 22:26:09 +0200326 [Group1_83*8] =
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200327 DstMem | SrcImmByte | ModRM | Lock,
328 DstMem | SrcImmByte | ModRM | Lock,
329 DstMem | SrcImmByte | ModRM | Lock,
330 DstMem | SrcImmByte | ModRM | Lock,
331 DstMem | SrcImmByte | ModRM | Lock,
332 DstMem | SrcImmByte | ModRM | Lock,
333 DstMem | SrcImmByte | ModRM | Lock,
334 DstMem | SrcImmByte | ModRM,
Avi Kivity43bb19c2008-01-18 12:46:50 +0200335 [Group1A*8] =
336 DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0,
Avi Kivity7d858a12008-01-18 12:58:04 +0200337 [Group3_Byte*8] =
338 ByteOp | SrcImm | DstMem | ModRM, 0,
339 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
340 0, 0, 0, 0,
341 [Group3*8] =
roel kluin41afa022008-08-18 21:25:01 -0400342 DstMem | SrcImm | ModRM, 0,
Avi Kivity6eb06cb2008-08-21 17:41:39 +0300343 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
Avi Kivity7d858a12008-01-18 12:58:04 +0200344 0, 0, 0, 0,
Avi Kivityfd607542008-01-18 13:12:26 +0200345 [Group4*8] =
346 ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM,
347 0, 0, 0, 0, 0, 0,
348 [Group5*8] =
Mohammed Gamald19292e2008-09-08 21:47:19 +0300349 DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
350 SrcMem | ModRM | Stack, 0,
Gleb Natapov414e6272010-04-28 19:15:26 +0300351 SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps,
Gleb Natapovea79849d2010-02-25 16:36:43 +0200352 SrcMem | ModRM | Stack, 0,
Avi Kivityd95058a2008-01-18 13:36:50 +0200353 [Group7*8] =
Gleb Natapove92805a2010-02-10 14:21:35 +0200354 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300355 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapove92805a2010-02-10 14:21:35 +0200356 SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv,
Gleb Natapov2db2c2e2010-02-10 14:21:29 +0200357 [Group8*8] =
358 0, 0, 0, 0,
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200359 DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock,
360 DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200361 [Group9*8] =
Gleb Natapov6550e1f2010-03-21 13:08:21 +0200362 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200363};
364
Guillaume Thouvenin45ed60b2008-12-04 14:25:38 +0100365static u32 group2_table[] = {
Avi Kivityd95058a2008-01-18 13:36:50 +0200366 [Group7*8] =
Gleb Natapov835e6b82010-03-03 17:53:05 +0200367 SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv,
Avi Kivity16286d02008-04-14 14:40:50 +0300368 SrcNone | ModRM | DstMem | Mov, 0,
Gleb Natapov835e6b82010-03-03 17:53:05 +0200369 SrcMem16 | ModRM | Mov | Priv, 0,
Gleb Natapov60a29d42010-02-10 14:21:30 +0200370 [Group9*8] =
371 0, 0, 0, 0, 0, 0, 0, 0,
Avi Kivitye09d0822008-01-18 12:38:59 +0200372};
373
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200375#define EFLG_ID (1<<21)
376#define EFLG_VIP (1<<20)
377#define EFLG_VIF (1<<19)
378#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200379#define EFLG_VM (1<<17)
380#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200381#define EFLG_IOPL (3<<12)
382#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383#define EFLG_OF (1<<11)
384#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200385#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200386#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387#define EFLG_SF (1<<7)
388#define EFLG_ZF (1<<6)
389#define EFLG_AF (1<<4)
390#define EFLG_PF (1<<2)
391#define EFLG_CF (1<<0)
392
393/*
394 * Instruction emulation:
395 * Most instructions are emulated directly via a fragment of inline assembly
396 * code. This allows us to save/restore EFLAGS and thus very easily pick up
397 * any modified flags.
398 */
399
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800400#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401#define _LO32 "k" /* force 32-bit operand */
402#define _STK "%%rsp" /* stack pointer */
403#elif defined(__i386__)
404#define _LO32 "" /* force 32-bit operand */
405#define _STK "%%esp" /* stack pointer */
406#endif
407
408/*
409 * These EFLAGS bits are restored from saved value during emulation, and
410 * any changes are written back to the saved value after emulation.
411 */
412#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
413
414/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200415#define _PRE_EFLAGS(_sav, _msk, _tmp) \
416 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
417 "movl %"_sav",%"_LO32 _tmp"; " \
418 "push %"_tmp"; " \
419 "push %"_tmp"; " \
420 "movl %"_msk",%"_LO32 _tmp"; " \
421 "andl %"_LO32 _tmp",("_STK"); " \
422 "pushf; " \
423 "notl %"_LO32 _tmp"; " \
424 "andl %"_LO32 _tmp",("_STK"); " \
425 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
426 "pop %"_tmp"; " \
427 "orl %"_LO32 _tmp",("_STK"); " \
428 "popf; " \
429 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430
431/* After executing instruction: write-back necessary bits in EFLAGS. */
432#define _POST_EFLAGS(_sav, _msk, _tmp) \
433 /* _sav |= EFLAGS & _msk; */ \
434 "pushf; " \
435 "pop %"_tmp"; " \
436 "andl %"_msk",%"_LO32 _tmp"; " \
437 "orl %"_LO32 _tmp",%"_sav"; "
438
Avi Kivitydda96d82008-11-26 15:14:10 +0200439#ifdef CONFIG_X86_64
440#define ON64(x) x
441#else
442#define ON64(x)
443#endif
444
Avi Kivity6b7ad612008-11-26 15:30:45 +0200445#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
446 do { \
447 __asm__ __volatile__ ( \
448 _PRE_EFLAGS("0", "4", "2") \
449 _op _suffix " %"_x"3,%1; " \
450 _POST_EFLAGS("0", "4", "2") \
451 : "=m" (_eflags), "=m" ((_dst).val), \
452 "=&r" (_tmp) \
453 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200454 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200455
456
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457/* Raw emulation: instruction has two explicit operands. */
458#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200459 do { \
460 unsigned long _tmp; \
461 \
462 switch ((_dst).bytes) { \
463 case 2: \
464 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
465 break; \
466 case 4: \
467 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
468 break; \
469 case 8: \
470 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
471 break; \
472 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 } while (0)
474
475#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
476 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200477 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400478 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200480 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 break; \
482 default: \
483 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
484 _wx, _wy, _lx, _ly, _qx, _qy); \
485 break; \
486 } \
487 } while (0)
488
489/* Source operand is byte-sized and may be restricted to just %cl. */
490#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
491 __emulate_2op(_op, _src, _dst, _eflags, \
492 "b", "c", "b", "c", "b", "c", "b", "c")
493
494/* Source operand is byte, word, long or quad sized. */
495#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
496 __emulate_2op(_op, _src, _dst, _eflags, \
497 "b", "q", "w", "r", _LO32, "r", "", "r")
498
499/* Source operand is word, long or quad sized. */
500#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
501 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
502 "w", "r", _LO32, "r", "", "r")
503
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100504/* Instruction has three operands and one operand is stored in ECX register */
505#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
506 do { \
507 unsigned long _tmp; \
508 _type _clv = (_cl).val; \
509 _type _srcv = (_src).val; \
510 _type _dstv = (_dst).val; \
511 \
512 __asm__ __volatile__ ( \
513 _PRE_EFLAGS("0", "5", "2") \
514 _op _suffix " %4,%1 \n" \
515 _POST_EFLAGS("0", "5", "2") \
516 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
517 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
518 ); \
519 \
520 (_cl).val = (unsigned long) _clv; \
521 (_src).val = (unsigned long) _srcv; \
522 (_dst).val = (unsigned long) _dstv; \
523 } while (0)
524
525#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
526 do { \
527 switch ((_dst).bytes) { \
528 case 2: \
529 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
530 "w", unsigned short); \
531 break; \
532 case 4: \
533 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
534 "l", unsigned int); \
535 break; \
536 case 8: \
537 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
538 "q", unsigned long)); \
539 break; \
540 } \
541 } while (0)
542
Avi Kivitydda96d82008-11-26 15:14:10 +0200543#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 do { \
545 unsigned long _tmp; \
546 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200547 __asm__ __volatile__ ( \
548 _PRE_EFLAGS("0", "3", "2") \
549 _op _suffix " %1; " \
550 _POST_EFLAGS("0", "3", "2") \
551 : "=m" (_eflags), "+m" ((_dst).val), \
552 "=&r" (_tmp) \
553 : "i" (EFLAGS_MASK)); \
554 } while (0)
555
556/* Instruction has only one explicit operand (no source operand). */
557#define emulate_1op(_op, _dst, _eflags) \
558 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400559 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200560 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
561 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
562 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
563 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 } \
565 } while (0)
566
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567/* Fetch next part of the instruction being emulated. */
568#define insn_fetch(_type, _size, _eip) \
569({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200570 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200571 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 goto done; \
573 (_eip) += (_size); \
574 (_type)_x; \
575})
576
Gleb Natapov414e6272010-04-28 19:15:26 +0300577#define insn_fetch_arr(_arr, _size, _eip) \
578({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
579 if (rc != X86EMUL_CONTINUE) \
580 goto done; \
581 (_eip) += (_size); \
582})
583
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800584static inline unsigned long ad_mask(struct decode_cache *c)
585{
586 return (1UL << (c->ad_bytes << 3)) - 1;
587}
588
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800590static inline unsigned long
591address_mask(struct decode_cache *c, unsigned long reg)
592{
593 if (c->ad_bytes == sizeof(unsigned long))
594 return reg;
595 else
596 return reg & ad_mask(c);
597}
598
599static inline unsigned long
600register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
601{
602 return base + address_mask(c, reg);
603}
604
Harvey Harrison7a9572752008-02-19 07:40:41 -0800605static inline void
606register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
607{
608 if (c->ad_bytes == sizeof(unsigned long))
609 *reg += inc;
610 else
611 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
612}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613
Harvey Harrison7a9572752008-02-19 07:40:41 -0800614static inline void jmp_rel(struct decode_cache *c, int rel)
615{
616 register_address_increment(c, &c->eip, rel);
617}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300618
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300619static void set_seg_override(struct decode_cache *c, int seg)
620{
621 c->has_seg_override = true;
622 c->seg_override = seg;
623}
624
Gleb Natapov79168fd2010-04-28 19:15:30 +0300625static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
626 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300627{
628 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
629 return 0;
630
Gleb Natapov79168fd2010-04-28 19:15:30 +0300631 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300632}
633
634static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300635 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300636 struct decode_cache *c)
637{
638 if (!c->has_seg_override)
639 return 0;
640
Gleb Natapov79168fd2010-04-28 19:15:30 +0300641 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300642}
643
Gleb Natapov79168fd2010-04-28 19:15:30 +0300644static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
645 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300646{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300647 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300648}
649
Gleb Natapov79168fd2010-04-28 19:15:30 +0300650static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
651 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300652{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300653 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300654}
655
Gleb Natapov54b84862010-04-28 19:15:44 +0300656static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
657 u32 error, bool valid)
658{
659 ctxt->exception = vec;
660 ctxt->error_code = error;
661 ctxt->error_code_valid = valid;
662 ctxt->restart = false;
663}
664
665static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
666{
667 emulate_exception(ctxt, GP_VECTOR, err, true);
668}
669
670static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
671 int err)
672{
673 ctxt->cr2 = addr;
674 emulate_exception(ctxt, PF_VECTOR, err, true);
675}
676
677static void emulate_ud(struct x86_emulate_ctxt *ctxt)
678{
679 emulate_exception(ctxt, UD_VECTOR, 0, false);
680}
681
682static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
683{
684 emulate_exception(ctxt, TS_VECTOR, err, true);
685}
686
Avi Kivity62266862007-11-20 13:15:52 +0200687static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
688 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300689 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200690{
691 struct fetch_cache *fc = &ctxt->decode.fetch;
692 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300693 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200694
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300695 if (eip == fc->end) {
696 cur_size = fc->end - fc->start;
697 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
698 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
699 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900700 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200701 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300702 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200703 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300704 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900705 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200706}
707
708static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
709 struct x86_emulate_ops *ops,
710 unsigned long eip, void *dest, unsigned size)
711{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900712 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200713
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200714 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200715 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200716 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200717 while (size--) {
718 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900719 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200720 return rc;
721 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900722 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200723}
724
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000725/*
726 * Given the 'reg' portion of a ModRM byte, and a register block, return a
727 * pointer into the block that addresses the relevant register.
728 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
729 */
730static void *decode_register(u8 modrm_reg, unsigned long *regs,
731 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732{
733 void *p;
734
735 p = &regs[modrm_reg];
736 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
737 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
738 return p;
739}
740
741static int read_descriptor(struct x86_emulate_ctxt *ctxt,
742 struct x86_emulate_ops *ops,
743 void *ptr,
744 u16 *size, unsigned long *address, int op_bytes)
745{
746 int rc;
747
748 if (op_bytes == 2)
749 op_bytes = 3;
750 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300751 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200752 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900753 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300755 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200756 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 return rc;
758}
759
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300760static int test_cc(unsigned int condition, unsigned int flags)
761{
762 int rc = 0;
763
764 switch ((condition & 15) >> 1) {
765 case 0: /* o */
766 rc |= (flags & EFLG_OF);
767 break;
768 case 1: /* b/c/nae */
769 rc |= (flags & EFLG_CF);
770 break;
771 case 2: /* z/e */
772 rc |= (flags & EFLG_ZF);
773 break;
774 case 3: /* be/na */
775 rc |= (flags & (EFLG_CF|EFLG_ZF));
776 break;
777 case 4: /* s */
778 rc |= (flags & EFLG_SF);
779 break;
780 case 5: /* p/pe */
781 rc |= (flags & EFLG_PF);
782 break;
783 case 7: /* le/ng */
784 rc |= (flags & EFLG_ZF);
785 /* fall through */
786 case 6: /* l/nge */
787 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
788 break;
789 }
790
791 /* Odd condition identifiers (lsb == 1) have inverted sense. */
792 return (!!rc ^ (condition & 1));
793}
794
Avi Kivity3c118e22007-10-31 10:27:04 +0200795static void decode_register_operand(struct operand *op,
796 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200797 int inhibit_bytereg)
798{
Avi Kivity33615aa2007-10-31 11:15:56 +0200799 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200800 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200801
802 if (!(c->d & ModRM))
803 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200804 op->type = OP_REG;
805 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200806 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200807 op->val = *(u8 *)op->ptr;
808 op->bytes = 1;
809 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200810 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200811 op->bytes = c->op_bytes;
812 switch (op->bytes) {
813 case 2:
814 op->val = *(u16 *)op->ptr;
815 break;
816 case 4:
817 op->val = *(u32 *)op->ptr;
818 break;
819 case 8:
820 op->val = *(u64 *) op->ptr;
821 break;
822 }
823 }
824 op->orig_val = op->val;
825}
826
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200827static int decode_modrm(struct x86_emulate_ctxt *ctxt,
828 struct x86_emulate_ops *ops)
829{
830 struct decode_cache *c = &ctxt->decode;
831 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700832 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900833 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200834
835 if (c->rex_prefix) {
836 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
837 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
838 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
839 }
840
841 c->modrm = insn_fetch(u8, 1, c->eip);
842 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
843 c->modrm_reg |= (c->modrm & 0x38) >> 3;
844 c->modrm_rm |= (c->modrm & 0x07);
845 c->modrm_ea = 0;
846 c->use_modrm_ea = 1;
847
848 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300849 c->modrm_ptr = decode_register(c->modrm_rm,
850 c->regs, c->d & ByteOp);
851 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200852 return rc;
853 }
854
855 if (c->ad_bytes == 2) {
856 unsigned bx = c->regs[VCPU_REGS_RBX];
857 unsigned bp = c->regs[VCPU_REGS_RBP];
858 unsigned si = c->regs[VCPU_REGS_RSI];
859 unsigned di = c->regs[VCPU_REGS_RDI];
860
861 /* 16-bit ModR/M decode. */
862 switch (c->modrm_mod) {
863 case 0:
864 if (c->modrm_rm == 6)
865 c->modrm_ea += insn_fetch(u16, 2, c->eip);
866 break;
867 case 1:
868 c->modrm_ea += insn_fetch(s8, 1, c->eip);
869 break;
870 case 2:
871 c->modrm_ea += insn_fetch(u16, 2, c->eip);
872 break;
873 }
874 switch (c->modrm_rm) {
875 case 0:
876 c->modrm_ea += bx + si;
877 break;
878 case 1:
879 c->modrm_ea += bx + di;
880 break;
881 case 2:
882 c->modrm_ea += bp + si;
883 break;
884 case 3:
885 c->modrm_ea += bp + di;
886 break;
887 case 4:
888 c->modrm_ea += si;
889 break;
890 case 5:
891 c->modrm_ea += di;
892 break;
893 case 6:
894 if (c->modrm_mod != 0)
895 c->modrm_ea += bp;
896 break;
897 case 7:
898 c->modrm_ea += bx;
899 break;
900 }
901 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
902 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300903 if (!c->has_seg_override)
904 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200905 c->modrm_ea = (u16)c->modrm_ea;
906 } else {
907 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700908 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200909 sib = insn_fetch(u8, 1, c->eip);
910 index_reg |= (sib >> 3) & 7;
911 base_reg |= sib & 7;
912 scale = sib >> 6;
913
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700914 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
915 c->modrm_ea += insn_fetch(s32, 4, c->eip);
916 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200917 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700918 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200919 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700920 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
921 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700922 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700923 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200925 switch (c->modrm_mod) {
926 case 0:
927 if (c->modrm_rm == 5)
928 c->modrm_ea += insn_fetch(s32, 4, c->eip);
929 break;
930 case 1:
931 c->modrm_ea += insn_fetch(s8, 1, c->eip);
932 break;
933 case 2:
934 c->modrm_ea += insn_fetch(s32, 4, c->eip);
935 break;
936 }
937 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938done:
939 return rc;
940}
941
942static int decode_abs(struct x86_emulate_ctxt *ctxt,
943 struct x86_emulate_ops *ops)
944{
945 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900946 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200947
948 switch (c->ad_bytes) {
949 case 2:
950 c->modrm_ea = insn_fetch(u16, 2, c->eip);
951 break;
952 case 4:
953 c->modrm_ea = insn_fetch(u32, 4, c->eip);
954 break;
955 case 8:
956 c->modrm_ea = insn_fetch(u64, 8, c->eip);
957 break;
958 }
959done:
960 return rc;
961}
962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200964x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200966 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900967 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200969 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Gleb Natapov5cd21912010-03-18 15:20:26 +0200972 /* we cannot decode insn before we complete previous rep insn */
973 WARN_ON(ctxt->restart);
974
Gleb Natapov063db062010-03-18 15:20:06 +0200975 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300976 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300977 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
979 switch (mode) {
980 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200981 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200983 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 break;
985 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200986 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800988#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200990 def_op_bytes = 4;
991 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 break;
993#endif
994 default:
995 return -1;
996 }
997
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200998 c->op_bytes = def_op_bytes;
999 c->ad_bytes = def_ad_bytes;
1000
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001002 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +02001003 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001005 /* switch between 2/4 bytes */
1006 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 break;
1008 case 0x67: /* address-size override */
1009 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001010 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001011 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 else
Laurent Viviere4e03de2007-09-18 11:52:50 +02001013 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +02001014 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001017 case 0x2e: /* CS override */
1018 case 0x36: /* SS override */
1019 case 0x3e: /* DS override */
1020 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 break;
1022 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001024 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001026 case 0x40 ... 0x4f: /* REX */
1027 if (mode != X86EMUL_MODE_PROT64)
1028 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +02001029 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001030 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001032 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001034 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001035 c->rep_prefix = REPNE_PREFIX;
1036 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001038 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 default:
1041 goto done_prefixes;
1042 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001043
1044 /* Any legacy prefix after a REX prefix nullifies its effect. */
1045
Avi Kivity33615aa2007-10-31 11:15:56 +02001046 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 }
1048
1049done_prefixes:
1050
1051 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001052 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001053 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001054 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055
1056 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001057 c->d = opcode_table[c->b];
1058 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001060 if (c->b == 0x0f) {
1061 c->twobyte = 1;
1062 c->b = insn_fetch(u8, 1, c->eip);
1063 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001065 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Avi Kivitye09d0822008-01-18 12:38:59 +02001067 if (c->d & Group) {
1068 group = c->d & GroupMask;
1069 c->modrm = insn_fetch(u8, 1, c->eip);
1070 --c->eip;
1071
1072 group = (group << 3) + ((c->modrm >> 3) & 7);
1073 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1074 c->d = group2_table[group];
1075 else
1076 c->d = group_table[group];
1077 }
1078
1079 /* Unrecognised? */
1080 if (c->d == 0) {
1081 DPRINTF("Cannot emulate %02x\n", c->b);
1082 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 }
1084
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001085 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1086 c->op_bytes = 8;
1087
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001089 if (c->d & ModRM)
1090 rc = decode_modrm(ctxt, ops);
1091 else if (c->d & MemAbs)
1092 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001093 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001094 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001096 if (!c->has_seg_override)
1097 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001098
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001099 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001100 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001101
1102 if (c->ad_bytes != 8)
1103 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001104
1105 if (c->rip_relative)
1106 c->modrm_ea += c->eip;
1107
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 /*
1109 * Decode and fetch the source operand: register, memory
1110 * or immediate.
1111 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001112 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 case SrcNone:
1114 break;
1115 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001116 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 break;
1118 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001119 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 goto srcmem_common;
1121 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001122 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 goto srcmem_common;
1124 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001125 c->src.bytes = (c->d & ByteOp) ? 1 :
1126 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001127 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001128 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001129 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001130 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001131 /*
1132 * For instructions with a ModR/M byte, switch to register
1133 * access if Mod = 3.
1134 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001135 if ((c->d & ModRM) && c->modrm_mod == 3) {
1136 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001137 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001138 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001139 break;
1140 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001141 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001142 c->src.ptr = (unsigned long *)c->modrm_ea;
1143 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 break;
1145 case SrcImm:
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001146 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001147 c->src.type = OP_IMM;
1148 c->src.ptr = (unsigned long *)c->eip;
1149 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1150 if (c->src.bytes == 8)
1151 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001153 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001155 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 break;
1157 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001158 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 break;
1160 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001161 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162 break;
1163 }
Avi Kivityc9eaf20f2009-05-18 16:13:45 +03001164 if ((c->d & SrcMask) == SrcImmU) {
1165 switch (c->src.bytes) {
1166 case 1:
1167 c->src.val &= 0xff;
1168 break;
1169 case 2:
1170 c->src.val &= 0xffff;
1171 break;
1172 case 4:
1173 c->src.val &= 0xffffffff;
1174 break;
1175 }
1176 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 break;
1178 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001179 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001180 c->src.type = OP_IMM;
1181 c->src.ptr = (unsigned long *)c->eip;
1182 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001183 if ((c->d & SrcMask) == SrcImmByte)
1184 c->src.val = insn_fetch(s8, 1, c->eip);
1185 else
1186 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001188 case SrcOne:
1189 c->src.bytes = 1;
1190 c->src.val = 1;
1191 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001192 case SrcSI:
1193 c->src.type = OP_MEM;
1194 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1195 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001196 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001197 c->regs[VCPU_REGS_RSI]);
1198 c->src.val = 0;
1199 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001200 case SrcImmFAddr:
1201 c->src.type = OP_IMM;
1202 c->src.ptr = (unsigned long *)c->eip;
1203 c->src.bytes = c->op_bytes + 2;
1204 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1205 break;
1206 case SrcMemFAddr:
1207 c->src.type = OP_MEM;
1208 c->src.ptr = (unsigned long *)c->modrm_ea;
1209 c->src.bytes = c->op_bytes + 2;
1210 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 }
1212
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001213 /*
1214 * Decode and fetch the second source operand: register, memory
1215 * or immediate.
1216 */
1217 switch (c->d & Src2Mask) {
1218 case Src2None:
1219 break;
1220 case Src2CL:
1221 c->src2.bytes = 1;
1222 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1223 break;
1224 case Src2ImmByte:
1225 c->src2.type = OP_IMM;
1226 c->src2.ptr = (unsigned long *)c->eip;
1227 c->src2.bytes = 1;
1228 c->src2.val = insn_fetch(u8, 1, c->eip);
1229 break;
1230 case Src2One:
1231 c->src2.bytes = 1;
1232 c->src2.val = 1;
1233 break;
1234 }
1235
Avi Kivity038e51d2007-01-22 20:40:40 -08001236 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001237 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001238 case ImplicitOps:
1239 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001240 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001241 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001242 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001243 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001244 break;
1245 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001246 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001247 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001248 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001249 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001250 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001251 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001252 break;
1253 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001254 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001255 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001256 if ((c->d & DstMask) == DstMem64)
1257 c->dst.bytes = 8;
1258 else
1259 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001260 c->dst.val = 0;
1261 if (c->d & BitOp) {
1262 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1263
1264 c->dst.ptr = (void *)c->dst.ptr +
1265 (c->src.val & mask) / 8;
1266 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001267 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001268 case DstAcc:
1269 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001270 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001271 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001272 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001273 case 1:
1274 c->dst.val = *(u8 *)c->dst.ptr;
1275 break;
1276 case 2:
1277 c->dst.val = *(u16 *)c->dst.ptr;
1278 break;
1279 case 4:
1280 c->dst.val = *(u32 *)c->dst.ptr;
1281 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001282 case 8:
1283 c->dst.val = *(u64 *)c->dst.ptr;
1284 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001285 }
1286 c->dst.orig_val = c->dst.val;
1287 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001288 case DstDI:
1289 c->dst.type = OP_MEM;
1290 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1291 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001292 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001293 c->regs[VCPU_REGS_RDI]);
1294 c->dst.val = 0;
1295 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001296 }
1297
1298done:
1299 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1300}
1301
Gleb Natapov9de41572010-04-28 19:15:22 +03001302static int read_emulated(struct x86_emulate_ctxt *ctxt,
1303 struct x86_emulate_ops *ops,
1304 unsigned long addr, void *dest, unsigned size)
1305{
1306 int rc;
1307 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001308 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001309
1310 while (size) {
1311 int n = min(size, 8u);
1312 size -= n;
1313 if (mc->pos < mc->end)
1314 goto read_cached;
1315
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001316 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1317 ctxt->vcpu);
1318 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001319 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
1322 mc->end += n;
1323
1324 read_cached:
1325 memcpy(dest, mc->data + mc->pos, n);
1326 mc->pos += n;
1327 dest += n;
1328 addr += n;
1329 }
1330 return X86EMUL_CONTINUE;
1331}
1332
Gleb Natapov7b262e92010-03-18 15:20:27 +02001333static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1334 struct x86_emulate_ops *ops,
1335 unsigned int size, unsigned short port,
1336 void *dest)
1337{
1338 struct read_cache *rc = &ctxt->decode.io_read;
1339
1340 if (rc->pos == rc->end) { /* refill pio read ahead */
1341 struct decode_cache *c = &ctxt->decode;
1342 unsigned int in_page, n;
1343 unsigned int count = c->rep_prefix ?
1344 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1345 in_page = (ctxt->eflags & EFLG_DF) ?
1346 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1347 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1348 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1349 count);
1350 if (n == 0)
1351 n = 1;
1352 rc->pos = rc->end = 0;
1353 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1354 return 0;
1355 rc->end = n * size;
1356 }
1357
1358 memcpy(dest, rc->data + rc->pos, size);
1359 rc->pos += size;
1360 return 1;
1361}
1362
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001363static u32 desc_limit_scaled(struct desc_struct *desc)
1364{
1365 u32 limit = get_desc_limit(desc);
1366
1367 return desc->g ? (limit << 12) | 0xfff : limit;
1368}
1369
1370static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1371 struct x86_emulate_ops *ops,
1372 u16 selector, struct desc_ptr *dt)
1373{
1374 if (selector & 1 << 2) {
1375 struct desc_struct desc;
1376 memset (dt, 0, sizeof *dt);
1377 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1378 return;
1379
1380 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1381 dt->address = get_desc_base(&desc);
1382 } else
1383 ops->get_gdt(dt, ctxt->vcpu);
1384}
1385
1386/* allowed just for 8 bytes segments */
1387static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1388 struct x86_emulate_ops *ops,
1389 u16 selector, struct desc_struct *desc)
1390{
1391 struct desc_ptr dt;
1392 u16 index = selector >> 3;
1393 int ret;
1394 u32 err;
1395 ulong addr;
1396
1397 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1398
1399 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001400 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 return X86EMUL_PROPAGATE_FAULT;
1402 }
1403 addr = dt.address + index * 8;
1404 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1405 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001406 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407
1408 return ret;
1409}
1410
1411/* allowed just for 8 bytes segments */
1412static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1413 struct x86_emulate_ops *ops,
1414 u16 selector, struct desc_struct *desc)
1415{
1416 struct desc_ptr dt;
1417 u16 index = selector >> 3;
1418 u32 err;
1419 ulong addr;
1420 int ret;
1421
1422 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1423
1424 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001425 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426 return X86EMUL_PROPAGATE_FAULT;
1427 }
1428
1429 addr = dt.address + index * 8;
1430 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1431 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001432 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433
1434 return ret;
1435}
1436
1437static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1438 struct x86_emulate_ops *ops,
1439 u16 selector, int seg)
1440{
1441 struct desc_struct seg_desc;
1442 u8 dpl, rpl, cpl;
1443 unsigned err_vec = GP_VECTOR;
1444 u32 err_code = 0;
1445 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1446 int ret;
1447
1448 memset(&seg_desc, 0, sizeof seg_desc);
1449
1450 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1451 || ctxt->mode == X86EMUL_MODE_REAL) {
1452 /* set real mode segment descriptor */
1453 set_desc_base(&seg_desc, selector << 4);
1454 set_desc_limit(&seg_desc, 0xffff);
1455 seg_desc.type = 3;
1456 seg_desc.p = 1;
1457 seg_desc.s = 1;
1458 goto load;
1459 }
1460
1461 /* NULL selector is not valid for TR, CS and SS */
1462 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1463 && null_selector)
1464 goto exception;
1465
1466 /* TR should be in GDT only */
1467 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1468 goto exception;
1469
1470 if (null_selector) /* for NULL selector skip all following checks */
1471 goto load;
1472
1473 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1474 if (ret != X86EMUL_CONTINUE)
1475 return ret;
1476
1477 err_code = selector & 0xfffc;
1478 err_vec = GP_VECTOR;
1479
1480 /* can't load system descriptor into segment selecor */
1481 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1482 goto exception;
1483
1484 if (!seg_desc.p) {
1485 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1486 goto exception;
1487 }
1488
1489 rpl = selector & 3;
1490 dpl = seg_desc.dpl;
1491 cpl = ops->cpl(ctxt->vcpu);
1492
1493 switch (seg) {
1494 case VCPU_SREG_SS:
1495 /*
1496 * segment is not a writable data segment or segment
1497 * selector's RPL != CPL or segment selector's RPL != CPL
1498 */
1499 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1500 goto exception;
1501 break;
1502 case VCPU_SREG_CS:
1503 if (!(seg_desc.type & 8))
1504 goto exception;
1505
1506 if (seg_desc.type & 4) {
1507 /* conforming */
1508 if (dpl > cpl)
1509 goto exception;
1510 } else {
1511 /* nonconforming */
1512 if (rpl > cpl || dpl != cpl)
1513 goto exception;
1514 }
1515 /* CS(RPL) <- CPL */
1516 selector = (selector & 0xfffc) | cpl;
1517 break;
1518 case VCPU_SREG_TR:
1519 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1520 goto exception;
1521 break;
1522 case VCPU_SREG_LDTR:
1523 if (seg_desc.s || seg_desc.type != 2)
1524 goto exception;
1525 break;
1526 default: /* DS, ES, FS, or GS */
1527 /*
1528 * segment is not a data or readable code segment or
1529 * ((segment is a data or nonconforming code segment)
1530 * and (both RPL and CPL > DPL))
1531 */
1532 if ((seg_desc.type & 0xa) == 0x8 ||
1533 (((seg_desc.type & 0xc) != 0xc) &&
1534 (rpl > dpl && cpl > dpl)))
1535 goto exception;
1536 break;
1537 }
1538
1539 if (seg_desc.s) {
1540 /* mark segment as accessed */
1541 seg_desc.type |= 1;
1542 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1543 if (ret != X86EMUL_CONTINUE)
1544 return ret;
1545 }
1546load:
1547 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1548 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1549 return X86EMUL_CONTINUE;
1550exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001551 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552 return X86EMUL_PROPAGATE_FAULT;
1553}
1554
Gleb Natapov79168fd2010-04-28 19:15:30 +03001555static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1556 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001557{
1558 struct decode_cache *c = &ctxt->decode;
1559
1560 c->dst.type = OP_MEM;
1561 c->dst.bytes = c->op_bytes;
1562 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001563 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001564 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001565 c->regs[VCPU_REGS_RSP]);
1566}
1567
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001568static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001569 struct x86_emulate_ops *ops,
1570 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001571{
1572 struct decode_cache *c = &ctxt->decode;
1573 int rc;
1574
Gleb Natapov79168fd2010-04-28 19:15:30 +03001575 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001576 c->regs[VCPU_REGS_RSP]),
1577 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001578 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001579 return rc;
1580
Avi Kivity350f69d2009-01-05 11:12:40 +02001581 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001582 return rc;
1583}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001584
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001585static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1586 struct x86_emulate_ops *ops,
1587 void *dest, int len)
1588{
1589 int rc;
1590 unsigned long val, change_mask;
1591 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001592 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001593
1594 rc = emulate_pop(ctxt, ops, &val, len);
1595 if (rc != X86EMUL_CONTINUE)
1596 return rc;
1597
1598 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1599 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1600
1601 switch(ctxt->mode) {
1602 case X86EMUL_MODE_PROT64:
1603 case X86EMUL_MODE_PROT32:
1604 case X86EMUL_MODE_PROT16:
1605 if (cpl == 0)
1606 change_mask |= EFLG_IOPL;
1607 if (cpl <= iopl)
1608 change_mask |= EFLG_IF;
1609 break;
1610 case X86EMUL_MODE_VM86:
1611 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001612 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001613 return X86EMUL_PROPAGATE_FAULT;
1614 }
1615 change_mask |= EFLG_IF;
1616 break;
1617 default: /* real mode */
1618 change_mask |= (EFLG_IOPL | EFLG_IF);
1619 break;
1620 }
1621
1622 *(unsigned long *)dest =
1623 (ctxt->eflags & ~change_mask) | (val & change_mask);
1624
1625 return rc;
1626}
1627
Gleb Natapov79168fd2010-04-28 19:15:30 +03001628static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1629 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001630{
1631 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001632
Gleb Natapov79168fd2010-04-28 19:15:30 +03001633 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001634
Gleb Natapov79168fd2010-04-28 19:15:30 +03001635 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001636}
1637
1638static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1639 struct x86_emulate_ops *ops, int seg)
1640{
1641 struct decode_cache *c = &ctxt->decode;
1642 unsigned long selector;
1643 int rc;
1644
1645 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001646 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001647 return rc;
1648
Gleb Natapov2e873022010-03-18 15:20:18 +02001649 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001650 return rc;
1651}
1652
Gleb Natapov79168fd2010-04-28 19:15:30 +03001653static void emulate_pusha(struct x86_emulate_ctxt *ctxt,
1654 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001655{
1656 struct decode_cache *c = &ctxt->decode;
1657 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1658 int reg = VCPU_REGS_RAX;
1659
1660 while (reg <= VCPU_REGS_RDI) {
1661 (reg == VCPU_REGS_RSP) ?
1662 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1663
Gleb Natapov79168fd2010-04-28 19:15:30 +03001664 emulate_push(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001665 ++reg;
1666 }
1667}
1668
1669static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1670 struct x86_emulate_ops *ops)
1671{
1672 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001673 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001674 int reg = VCPU_REGS_RDI;
1675
1676 while (reg >= VCPU_REGS_RAX) {
1677 if (reg == VCPU_REGS_RSP) {
1678 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1679 c->op_bytes);
1680 --reg;
1681 }
1682
1683 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001684 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001685 break;
1686 --reg;
1687 }
1688 return rc;
1689}
1690
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001691static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1692 struct x86_emulate_ops *ops)
1693{
1694 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001695
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001696 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697}
1698
Laurent Vivier05f086f2007-09-24 11:10:55 +02001699static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001701 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702 switch (c->modrm_reg) {
1703 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001704 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705 break;
1706 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001707 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001708 break;
1709 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001710 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711 break;
1712 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001713 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 break;
1715 case 4: /* sal/shl */
1716 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001717 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001718 break;
1719 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001720 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721 break;
1722 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001723 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724 break;
1725 }
1726}
1727
1728static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001729 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001730{
1731 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001732
1733 switch (c->modrm_reg) {
1734 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001735 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736 break;
1737 case 2: /* not */
1738 c->dst.val = ~c->dst.val;
1739 break;
1740 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001741 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001742 break;
1743 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001744 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001746 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747}
1748
1749static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001750 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751{
1752 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001753
1754 switch (c->modrm_reg) {
1755 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001756 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757 break;
1758 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001759 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001761 case 2: /* call near abs */ {
1762 long int old_eip;
1763 old_eip = c->eip;
1764 c->eip = c->src.val;
1765 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001766 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001767 break;
1768 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001770 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771 break;
1772 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001773 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001776 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777}
1778
1779static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001780 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781{
1782 struct decode_cache *c = &ctxt->decode;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001783 u64 old = c->dst.orig_val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784
1785 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1786 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1787
1788 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1789 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001790 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 } else {
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001792 c->dst.val = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793 (u32) c->regs[VCPU_REGS_RBX];
1794
Laurent Vivier05f086f2007-09-24 11:10:55 +02001795 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001797 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001798}
1799
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001800static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1801 struct x86_emulate_ops *ops)
1802{
1803 struct decode_cache *c = &ctxt->decode;
1804 int rc;
1805 unsigned long cs;
1806
1807 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001808 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001809 return rc;
1810 if (c->op_bytes == 4)
1811 c->eip = (u32)c->eip;
1812 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001813 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001814 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001815 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001816 return rc;
1817}
1818
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001819static inline int writeback(struct x86_emulate_ctxt *ctxt,
1820 struct x86_emulate_ops *ops)
1821{
1822 int rc;
1823 struct decode_cache *c = &ctxt->decode;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001824 u32 err;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001825
1826 switch (c->dst.type) {
1827 case OP_REG:
1828 /* The 4-byte case *is* correct:
1829 * in 64-bit mode we zero-extend.
1830 */
1831 switch (c->dst.bytes) {
1832 case 1:
1833 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1834 break;
1835 case 2:
1836 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1837 break;
1838 case 4:
1839 *c->dst.ptr = (u32)c->dst.val;
1840 break; /* 64b: zero-ext */
1841 case 8:
1842 *c->dst.ptr = c->dst.val;
1843 break;
1844 }
1845 break;
1846 case OP_MEM:
1847 if (c->lock_prefix)
1848 rc = ops->cmpxchg_emulated(
1849 (unsigned long)c->dst.ptr,
1850 &c->dst.orig_val,
1851 &c->dst.val,
1852 c->dst.bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001853 &err,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001854 ctxt->vcpu);
1855 else
1856 rc = ops->write_emulated(
1857 (unsigned long)c->dst.ptr,
1858 &c->dst.val,
1859 c->dst.bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001860 &err,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861 ctxt->vcpu);
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001862 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +03001863 emulate_pf(ctxt,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001864 (unsigned long)c->dst.ptr, err);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001865 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001867 break;
1868 case OP_NONE:
1869 /* no writeback */
1870 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871 default:
1872 break;
1873 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001874 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001875}
1876
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877static inline void
1878setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001879 struct x86_emulate_ops *ops, struct desc_struct *cs,
1880 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 memset(cs, 0, sizeof(struct desc_struct));
1883 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1884 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885
1886 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001887 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001889 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001890 cs->type = 0x0b; /* Read, Execute, Accessed */
1891 cs->s = 1;
1892 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001893 cs->p = 1;
1894 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895
Gleb Natapov79168fd2010-04-28 19:15:30 +03001896 set_desc_base(ss, 0); /* flat segment */
1897 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001898 ss->g = 1; /* 4kb granularity */
1899 ss->s = 1;
1900 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001901 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001902 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001903 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904}
1905
1906static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001907emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001908{
1909 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001910 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001912 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001913
1914 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001915 if (ctxt->mode == X86EMUL_MODE_REAL ||
1916 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001917 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001918 return X86EMUL_PROPAGATE_FAULT;
1919 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001920
Gleb Natapov79168fd2010-04-28 19:15:30 +03001921 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001922
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001923 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001924 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001925 cs_sel = (u16)(msr_data & 0xfffc);
1926 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001927
1928 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001929 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001930 cs.l = 1;
1931 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001932 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1933 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1934 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1935 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001936
1937 c->regs[VCPU_REGS_RCX] = c->eip;
1938 if (is_long_mode(ctxt->vcpu)) {
1939#ifdef CONFIG_X86_64
1940 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1941
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001942 ops->get_msr(ctxt->vcpu,
1943 ctxt->mode == X86EMUL_MODE_PROT64 ?
1944 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001945 c->eip = msr_data;
1946
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001947 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001948 ctxt->eflags &= ~(msr_data | EFLG_RF);
1949#endif
1950 } else {
1951 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001952 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001953 c->eip = (u32)msr_data;
1954
1955 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1956 }
1957
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001958 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001959}
1960
Andre Przywara8c604352009-06-18 12:56:01 +02001961static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001962emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001963{
1964 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001965 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001966 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001967 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001968
Gleb Natapova0044752010-02-10 14:21:31 +02001969 /* inject #GP if in real mode */
1970 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001971 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001972 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001973 }
1974
1975 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1976 * Therefore, we inject an #UD.
1977 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001978 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001979 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001980 return X86EMUL_PROPAGATE_FAULT;
1981 }
Andre Przywara8c604352009-06-18 12:56:01 +02001982
Gleb Natapov79168fd2010-04-28 19:15:30 +03001983 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001984
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001985 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001986 switch (ctxt->mode) {
1987 case X86EMUL_MODE_PROT32:
1988 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001989 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001990 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001991 }
1992 break;
1993 case X86EMUL_MODE_PROT64:
1994 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001995 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001996 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001997 }
1998 break;
1999 }
2000
2001 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002002 cs_sel = (u16)msr_data;
2003 cs_sel &= ~SELECTOR_RPL_MASK;
2004 ss_sel = cs_sel + 8;
2005 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02002006 if (ctxt->mode == X86EMUL_MODE_PROT64
2007 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002009 cs.l = 1;
2010 }
2011
Gleb Natapov79168fd2010-04-28 19:15:30 +03002012 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2013 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2014 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2015 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02002016
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002017 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002018 c->eip = msr_data;
2019
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002020 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002021 c->regs[VCPU_REGS_RSP] = msr_data;
2022
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002023 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002024}
2025
Andre Przywara4668f052009-06-18 12:56:02 +02002026static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002027emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002028{
2029 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002030 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002031 u64 msr_data;
2032 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002033 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002034
Gleb Natapova0044752010-02-10 14:21:31 +02002035 /* inject #GP if in real mode or Virtual 8086 mode */
2036 if (ctxt->mode == X86EMUL_MODE_REAL ||
2037 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002038 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002039 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002040 }
2041
Gleb Natapov79168fd2010-04-28 19:15:30 +03002042 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002043
2044 if ((c->rex_prefix & 0x8) != 0x0)
2045 usermode = X86EMUL_MODE_PROT64;
2046 else
2047 usermode = X86EMUL_MODE_PROT32;
2048
2049 cs.dpl = 3;
2050 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002051 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002052 switch (usermode) {
2053 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002055 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002056 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002057 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002058 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002060 break;
2061 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002062 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002063 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002064 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002065 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002066 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002067 ss_sel = cs_sel + 8;
2068 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002069 cs.l = 1;
2070 break;
2071 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002072 cs_sel |= SELECTOR_RPL_MASK;
2073 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002074
Gleb Natapov79168fd2010-04-28 19:15:30 +03002075 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2076 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2077 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2078 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002079
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002080 c->eip = c->regs[VCPU_REGS_RDX];
2081 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002082
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002083 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002084}
2085
Gleb Natapov9c537242010-03-18 15:20:05 +02002086static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2087 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002088{
2089 int iopl;
2090 if (ctxt->mode == X86EMUL_MODE_REAL)
2091 return false;
2092 if (ctxt->mode == X86EMUL_MODE_VM86)
2093 return true;
2094 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002095 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002096}
2097
2098static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2099 struct x86_emulate_ops *ops,
2100 u16 port, u16 len)
2101{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002103 int r;
2104 u16 io_bitmap_ptr;
2105 u8 perm, bit_idx = port & 0x7;
2106 unsigned mask = (1 << len) - 1;
2107
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2109 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002110 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002111 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002112 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2114 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002115 if (r != X86EMUL_CONTINUE)
2116 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002118 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002119 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2120 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002121 if (r != X86EMUL_CONTINUE)
2122 return false;
2123 if ((perm >> bit_idx) & mask)
2124 return false;
2125 return true;
2126}
2127
2128static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2129 struct x86_emulate_ops *ops,
2130 u16 port, u16 len)
2131{
Gleb Natapov9c537242010-03-18 15:20:05 +02002132 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002133 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2134 return false;
2135 return true;
2136}
2137
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002138static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2139 struct x86_emulate_ops *ops,
2140 struct tss_segment_16 *tss)
2141{
2142 struct decode_cache *c = &ctxt->decode;
2143
2144 tss->ip = c->eip;
2145 tss->flag = ctxt->eflags;
2146 tss->ax = c->regs[VCPU_REGS_RAX];
2147 tss->cx = c->regs[VCPU_REGS_RCX];
2148 tss->dx = c->regs[VCPU_REGS_RDX];
2149 tss->bx = c->regs[VCPU_REGS_RBX];
2150 tss->sp = c->regs[VCPU_REGS_RSP];
2151 tss->bp = c->regs[VCPU_REGS_RBP];
2152 tss->si = c->regs[VCPU_REGS_RSI];
2153 tss->di = c->regs[VCPU_REGS_RDI];
2154
2155 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2156 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2157 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2158 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2159 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2160}
2161
2162static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2163 struct x86_emulate_ops *ops,
2164 struct tss_segment_16 *tss)
2165{
2166 struct decode_cache *c = &ctxt->decode;
2167 int ret;
2168
2169 c->eip = tss->ip;
2170 ctxt->eflags = tss->flag | 2;
2171 c->regs[VCPU_REGS_RAX] = tss->ax;
2172 c->regs[VCPU_REGS_RCX] = tss->cx;
2173 c->regs[VCPU_REGS_RDX] = tss->dx;
2174 c->regs[VCPU_REGS_RBX] = tss->bx;
2175 c->regs[VCPU_REGS_RSP] = tss->sp;
2176 c->regs[VCPU_REGS_RBP] = tss->bp;
2177 c->regs[VCPU_REGS_RSI] = tss->si;
2178 c->regs[VCPU_REGS_RDI] = tss->di;
2179
2180 /*
2181 * SDM says that segment selectors are loaded before segment
2182 * descriptors
2183 */
2184 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2185 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2186 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2187 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2188 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2189
2190 /*
2191 * Now load segment descriptors. If fault happenes at this stage
2192 * it is handled in a context of new task
2193 */
2194 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2195 if (ret != X86EMUL_CONTINUE)
2196 return ret;
2197 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2198 if (ret != X86EMUL_CONTINUE)
2199 return ret;
2200 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2201 if (ret != X86EMUL_CONTINUE)
2202 return ret;
2203 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2204 if (ret != X86EMUL_CONTINUE)
2205 return ret;
2206 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2207 if (ret != X86EMUL_CONTINUE)
2208 return ret;
2209
2210 return X86EMUL_CONTINUE;
2211}
2212
2213static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2214 struct x86_emulate_ops *ops,
2215 u16 tss_selector, u16 old_tss_sel,
2216 ulong old_tss_base, struct desc_struct *new_desc)
2217{
2218 struct tss_segment_16 tss_seg;
2219 int ret;
2220 u32 err, new_tss_base = get_desc_base(new_desc);
2221
2222 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2223 &err);
2224 if (ret == X86EMUL_PROPAGATE_FAULT) {
2225 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002226 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002227 return ret;
2228 }
2229
2230 save_state_to_tss16(ctxt, ops, &tss_seg);
2231
2232 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2233 &err);
2234 if (ret == X86EMUL_PROPAGATE_FAULT) {
2235 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002236 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002237 return ret;
2238 }
2239
2240 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2241 &err);
2242 if (ret == X86EMUL_PROPAGATE_FAULT) {
2243 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002244 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002245 return ret;
2246 }
2247
2248 if (old_tss_sel != 0xffff) {
2249 tss_seg.prev_task_link = old_tss_sel;
2250
2251 ret = ops->write_std(new_tss_base,
2252 &tss_seg.prev_task_link,
2253 sizeof tss_seg.prev_task_link,
2254 ctxt->vcpu, &err);
2255 if (ret == X86EMUL_PROPAGATE_FAULT) {
2256 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002257 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002258 return ret;
2259 }
2260 }
2261
2262 return load_state_from_tss16(ctxt, ops, &tss_seg);
2263}
2264
2265static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2266 struct x86_emulate_ops *ops,
2267 struct tss_segment_32 *tss)
2268{
2269 struct decode_cache *c = &ctxt->decode;
2270
2271 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2272 tss->eip = c->eip;
2273 tss->eflags = ctxt->eflags;
2274 tss->eax = c->regs[VCPU_REGS_RAX];
2275 tss->ecx = c->regs[VCPU_REGS_RCX];
2276 tss->edx = c->regs[VCPU_REGS_RDX];
2277 tss->ebx = c->regs[VCPU_REGS_RBX];
2278 tss->esp = c->regs[VCPU_REGS_RSP];
2279 tss->ebp = c->regs[VCPU_REGS_RBP];
2280 tss->esi = c->regs[VCPU_REGS_RSI];
2281 tss->edi = c->regs[VCPU_REGS_RDI];
2282
2283 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2284 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2285 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2286 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2287 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2288 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2289 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2290}
2291
2292static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2293 struct x86_emulate_ops *ops,
2294 struct tss_segment_32 *tss)
2295{
2296 struct decode_cache *c = &ctxt->decode;
2297 int ret;
2298
Gleb Natapov0f122442010-04-28 19:15:31 +03002299 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002300 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002301 return X86EMUL_PROPAGATE_FAULT;
2302 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 c->eip = tss->eip;
2304 ctxt->eflags = tss->eflags | 2;
2305 c->regs[VCPU_REGS_RAX] = tss->eax;
2306 c->regs[VCPU_REGS_RCX] = tss->ecx;
2307 c->regs[VCPU_REGS_RDX] = tss->edx;
2308 c->regs[VCPU_REGS_RBX] = tss->ebx;
2309 c->regs[VCPU_REGS_RSP] = tss->esp;
2310 c->regs[VCPU_REGS_RBP] = tss->ebp;
2311 c->regs[VCPU_REGS_RSI] = tss->esi;
2312 c->regs[VCPU_REGS_RDI] = tss->edi;
2313
2314 /*
2315 * SDM says that segment selectors are loaded before segment
2316 * descriptors
2317 */
2318 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2319 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2320 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2321 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2322 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2323 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2324 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2325
2326 /*
2327 * Now load segment descriptors. If fault happenes at this stage
2328 * it is handled in a context of new task
2329 */
2330 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2331 if (ret != X86EMUL_CONTINUE)
2332 return ret;
2333 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2334 if (ret != X86EMUL_CONTINUE)
2335 return ret;
2336 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2337 if (ret != X86EMUL_CONTINUE)
2338 return ret;
2339 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2340 if (ret != X86EMUL_CONTINUE)
2341 return ret;
2342 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2343 if (ret != X86EMUL_CONTINUE)
2344 return ret;
2345 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2346 if (ret != X86EMUL_CONTINUE)
2347 return ret;
2348 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2349 if (ret != X86EMUL_CONTINUE)
2350 return ret;
2351
2352 return X86EMUL_CONTINUE;
2353}
2354
2355static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2356 struct x86_emulate_ops *ops,
2357 u16 tss_selector, u16 old_tss_sel,
2358 ulong old_tss_base, struct desc_struct *new_desc)
2359{
2360 struct tss_segment_32 tss_seg;
2361 int ret;
2362 u32 err, new_tss_base = get_desc_base(new_desc);
2363
2364 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2365 &err);
2366 if (ret == X86EMUL_PROPAGATE_FAULT) {
2367 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002368 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369 return ret;
2370 }
2371
2372 save_state_to_tss32(ctxt, ops, &tss_seg);
2373
2374 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2375 &err);
2376 if (ret == X86EMUL_PROPAGATE_FAULT) {
2377 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002378 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379 return ret;
2380 }
2381
2382 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2383 &err);
2384 if (ret == X86EMUL_PROPAGATE_FAULT) {
2385 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002386 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 return ret;
2388 }
2389
2390 if (old_tss_sel != 0xffff) {
2391 tss_seg.prev_task_link = old_tss_sel;
2392
2393 ret = ops->write_std(new_tss_base,
2394 &tss_seg.prev_task_link,
2395 sizeof tss_seg.prev_task_link,
2396 ctxt->vcpu, &err);
2397 if (ret == X86EMUL_PROPAGATE_FAULT) {
2398 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002399 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002400 return ret;
2401 }
2402 }
2403
2404 return load_state_from_tss32(ctxt, ops, &tss_seg);
2405}
2406
2407static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002408 struct x86_emulate_ops *ops,
2409 u16 tss_selector, int reason,
2410 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411{
2412 struct desc_struct curr_tss_desc, next_tss_desc;
2413 int ret;
2414 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2415 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002416 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002417 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418
2419 /* FIXME: old_tss_base == ~0 ? */
2420
2421 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2422 if (ret != X86EMUL_CONTINUE)
2423 return ret;
2424 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2425 if (ret != X86EMUL_CONTINUE)
2426 return ret;
2427
2428 /* FIXME: check that next_tss_desc is tss */
2429
2430 if (reason != TASK_SWITCH_IRET) {
2431 if ((tss_selector & 3) > next_tss_desc.dpl ||
2432 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002433 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 return X86EMUL_PROPAGATE_FAULT;
2435 }
2436 }
2437
Gleb Natapovceffb452010-03-18 15:20:19 +02002438 desc_limit = desc_limit_scaled(&next_tss_desc);
2439 if (!next_tss_desc.p ||
2440 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2441 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002442 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443 return X86EMUL_PROPAGATE_FAULT;
2444 }
2445
2446 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2447 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2448 write_segment_descriptor(ctxt, ops, old_tss_sel,
2449 &curr_tss_desc);
2450 }
2451
2452 if (reason == TASK_SWITCH_IRET)
2453 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2454
2455 /* set back link to prev task only if NT bit is set in eflags
2456 note that old_tss_sel is not used afetr this point */
2457 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2458 old_tss_sel = 0xffff;
2459
2460 if (next_tss_desc.type & 8)
2461 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2462 old_tss_base, &next_tss_desc);
2463 else
2464 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2465 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002466 if (ret != X86EMUL_CONTINUE)
2467 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002468
2469 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2470 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2471
2472 if (reason != TASK_SWITCH_IRET) {
2473 next_tss_desc.type |= (1 << 1); /* set busy flag */
2474 write_segment_descriptor(ctxt, ops, tss_selector,
2475 &next_tss_desc);
2476 }
2477
2478 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2479 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2480 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2481
Jan Kiszkae269fb22010-04-14 15:51:09 +02002482 if (has_error_code) {
2483 struct decode_cache *c = &ctxt->decode;
2484
2485 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2486 c->lock_prefix = 0;
2487 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002488 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002489 }
2490
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 return ret;
2492}
2493
2494int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2495 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002496 u16 tss_selector, int reason,
2497 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498{
2499 struct decode_cache *c = &ctxt->decode;
2500 int rc;
2501
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002503 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504
Jan Kiszkae269fb22010-04-14 15:51:09 +02002505 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2506 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507
2508 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002509 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002510 if (rc == X86EMUL_CONTINUE)
2511 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 }
2513
Gleb Natapov19d04432010-04-15 12:29:50 +03002514 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515}
2516
Gleb Natapova682e352010-03-18 15:20:21 +02002517static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002518 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002519{
2520 struct decode_cache *c = &ctxt->decode;
2521 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2522
Gleb Natapovd9271122010-03-18 15:20:22 +02002523 register_address_increment(c, &c->regs[reg], df * op->bytes);
2524 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002525}
2526
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002527int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002528x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002529{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002530 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002531 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002532 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002533 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002534
Gleb Natapov9de41572010-04-28 19:15:22 +03002535 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002536
Gleb Natapov11616242010-02-11 14:43:14 +02002537 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002538 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002539 goto done;
2540 }
2541
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002542 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002543 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002544 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002545 goto done;
2546 }
2547
Gleb Natapove92805a2010-02-10 14:21:35 +02002548 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002549 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002550 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002551 goto done;
2552 }
2553
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002554 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002555 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002556 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002557 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002558 string_done:
2559 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002560 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002561 goto done;
2562 }
2563 /* The second termination condition only applies for REPE
2564 * and REPNE. Test if the repeat string operation prefix is
2565 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2566 * corresponding termination condition according to:
2567 * - if REPE/REPZ and ZF = 0 then done
2568 * - if REPNE/REPNZ and ZF = 1 then done
2569 */
2570 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002571 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002572 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002573 ((ctxt->eflags & EFLG_ZF) == 0))
2574 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002575 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002576 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2577 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002578 }
Gleb Natapov063db062010-03-18 15:20:06 +02002579 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002580 }
2581
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002582 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002583 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002584 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002585 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002586 goto done;
2587 c->src.orig_val = c->src.val;
2588 }
2589
Gleb Natapove35b7b92010-02-25 16:36:42 +02002590 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002591 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2592 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002593 if (rc != X86EMUL_CONTINUE)
2594 goto done;
2595 }
2596
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002597 if ((c->d & DstMask) == ImplicitOps)
2598 goto special_insn;
2599
2600
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002601 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2602 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002603 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2604 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002605 if (rc != X86EMUL_CONTINUE)
2606 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002607 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002608 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002609
Avi Kivity018a98d2007-11-27 19:30:56 +02002610special_insn:
2611
Laurent Viviere4e03de2007-09-18 11:52:50 +02002612 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 goto twobyte_insn;
2614
Laurent Viviere4e03de2007-09-18 11:52:50 +02002615 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 case 0x00 ... 0x05:
2617 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002618 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002620 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002621 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002622 break;
2623 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002624 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002625 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002626 goto done;
2627 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 case 0x08 ... 0x0d:
2629 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002630 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002632 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002633 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002634 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 case 0x10 ... 0x15:
2636 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002637 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002639 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002640 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002641 break;
2642 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002643 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002644 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002645 goto done;
2646 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 case 0x18 ... 0x1d:
2648 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002649 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002651 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002652 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002653 break;
2654 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002655 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002656 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002657 goto done;
2658 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002659 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002661 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
2663 case 0x28 ... 0x2d:
2664 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002665 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666 break;
2667 case 0x30 ... 0x35:
2668 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002669 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 break;
2671 case 0x38 ... 0x3d:
2672 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002673 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002675 case 0x40 ... 0x47: /* inc r16/r32 */
2676 emulate_1op("inc", c->dst, ctxt->eflags);
2677 break;
2678 case 0x48 ... 0x4f: /* dec r16/r32 */
2679 emulate_1op("dec", c->dst, ctxt->eflags);
2680 break;
2681 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002682 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002683 break;
2684 case 0x58 ... 0x5f: /* pop reg */
2685 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002686 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002687 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002688 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002689 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002690 case 0x60: /* pusha */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002691 emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002692 break;
2693 case 0x61: /* popa */
2694 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002695 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002696 goto done;
2697 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002699 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002701 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002703 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002704 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002705 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002706 break;
2707 case 0x6c: /* insb */
2708 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002709 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002710 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002711 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002712 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002713 goto done;
2714 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002715 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2716 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002717 goto done; /* IO is needed, skip writeback */
2718 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002719 case 0x6e: /* outsb */
2720 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002721 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002722 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002723 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002724 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002725 goto done;
2726 }
Gleb Natapov79729952010-03-18 15:20:24 +02002727 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2728 &c->src.val, 1, ctxt->vcpu);
2729
2730 c->dst.type = OP_NONE; /* nothing to writeback */
2731 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002732 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002733 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002734 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002735 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002737 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 case 0:
2739 goto add;
2740 case 1:
2741 goto or;
2742 case 2:
2743 goto adc;
2744 case 3:
2745 goto sbb;
2746 case 4:
2747 goto and;
2748 case 5:
2749 goto sub;
2750 case 6:
2751 goto xor;
2752 case 7:
2753 goto cmp;
2754 }
2755 break;
2756 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002757 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
2759 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002760 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002762 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002764 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 break;
2766 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002767 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 break;
2769 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002770 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 break; /* 64b reg: zero-extend */
2772 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002773 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 break;
2775 }
2776 /*
2777 * Write back the memory destination with implicit LOCK
2778 * prefix.
2779 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002780 c->dst.val = c->src.val;
2781 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002784 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002785 case 0x8c: /* mov r/m, sreg */
2786 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002787 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002788 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002789 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002790 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002791 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002792 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002793 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002794 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002795 case 0x8e: { /* mov seg, r/m16 */
2796 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002797
2798 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002799
Gleb Natapovc6975182010-02-18 12:15:01 +02002800 if (c->modrm_reg == VCPU_SREG_CS ||
2801 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002802 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002803 goto done;
2804 }
2805
Glauber Costa310b5d32009-05-12 16:21:06 -04002806 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002807 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002808
Gleb Natapov2e873022010-03-18 15:20:18 +02002809 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002810
2811 c->dst.type = OP_NONE; /* Disable writeback. */
2812 break;
2813 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002815 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002816 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002819 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002820 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2821 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002822 break;
2823 }
2824 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002825 c->src.type = OP_REG;
2826 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002827 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2828 c->src.val = *(c->src.ptr);
2829 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002830 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002831 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002832 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002833 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002834 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002835 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002836 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002837 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002838 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2839 if (rc != X86EMUL_CONTINUE)
2840 goto done;
2841 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002842 case 0xa0 ... 0xa1: /* mov */
2843 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2844 c->dst.val = c->src.val;
2845 break;
2846 case 0xa2 ... 0xa3: /* mov */
2847 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2848 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002850 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002852 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002853 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002854 goto cmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002856 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 break;
2858 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002859 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 case 0xae ... 0xaf: /* scas */
2861 DPRINTF("Urk! I don't handle SCAS.\n");
2862 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002863 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002864 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002865 case 0xc0 ... 0xc1:
2866 emulate_grp2(ctxt);
2867 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002868 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002869 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002870 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002871 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002872 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002873 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2874 mov:
2875 c->dst.val = c->src.val;
2876 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002877 case 0xcb: /* ret far */
2878 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002879 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002880 goto done;
2881 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002882 case 0xd0 ... 0xd1: /* Grp2 */
2883 c->src.val = 1;
2884 emulate_grp2(ctxt);
2885 break;
2886 case 0xd2 ... 0xd3: /* Grp2 */
2887 c->src.val = c->regs[VCPU_REGS_RCX];
2888 emulate_grp2(ctxt);
2889 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002890 case 0xe4: /* inb */
2891 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002892 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002893 case 0xe6: /* outb */
2894 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002895 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002896 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002897 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002898 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002899 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002900 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002901 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002902 }
2903 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002904 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002905 case 0xea: { /* jmp far */
2906 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002907 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002908 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2909
2910 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002911 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002912
Gleb Natapov414e6272010-04-28 19:15:26 +03002913 c->eip = 0;
2914 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002915 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002916 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002917 case 0xeb:
2918 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002919 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002920 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002921 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002922 case 0xec: /* in al,dx */
2923 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002924 c->src.val = c->regs[VCPU_REGS_RDX];
2925 do_io_in:
2926 c->dst.bytes = min(c->dst.bytes, 4u);
2927 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002928 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002929 goto done;
2930 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002931 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2932 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002933 goto done; /* IO is needed */
2934 break;
2935 case 0xee: /* out al,dx */
2936 case 0xef: /* out (e/r)ax,dx */
2937 c->src.val = c->regs[VCPU_REGS_RDX];
2938 do_io_out:
2939 c->dst.bytes = min(c->dst.bytes, 4u);
2940 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002941 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002942 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002943 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002944 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2945 ctxt->vcpu);
2946 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002947 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002948 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002949 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002950 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002951 case 0xf5: /* cmc */
2952 /* complement carry flag from eflags reg */
2953 ctxt->eflags ^= EFLG_CF;
2954 c->dst.type = OP_NONE; /* Disable writeback. */
2955 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002956 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002957 if (!emulate_grp3(ctxt, ops))
2958 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002959 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002960 case 0xf8: /* clc */
2961 ctxt->eflags &= ~EFLG_CF;
2962 c->dst.type = OP_NONE; /* Disable writeback. */
2963 break;
2964 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002965 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapov54b84862010-04-28 19:15:44 +03002966 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002967 else {
2968 ctxt->eflags &= ~X86_EFLAGS_IF;
2969 c->dst.type = OP_NONE; /* Disable writeback. */
2970 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002971 break;
2972 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002973 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapov54b84862010-04-28 19:15:44 +03002974 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002975 else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03002976 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002977 ctxt->eflags |= X86_EFLAGS_IF;
2978 c->dst.type = OP_NONE; /* Disable writeback. */
2979 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002980 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002981 case 0xfc: /* cld */
2982 ctxt->eflags &= ~EFLG_DF;
2983 c->dst.type = OP_NONE; /* Disable writeback. */
2984 break;
2985 case 0xfd: /* std */
2986 ctxt->eflags |= EFLG_DF;
2987 c->dst.type = OP_NONE; /* Disable writeback. */
2988 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002989 case 0xfe: /* Grp4 */
2990 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002991 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002992 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002993 goto done;
2994 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002995 case 0xff: /* Grp5 */
2996 if (c->modrm_reg == 5)
2997 goto jump_far;
2998 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003000
3001writeback:
3002 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003003 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003004 goto done;
3005
Gleb Natapov5cd21912010-03-18 15:20:26 +02003006 /*
3007 * restore dst type in case the decoding will be reused
3008 * (happens for string instruction )
3009 */
3010 c->dst.type = saved_dst_type;
3011
Gleb Natapova682e352010-03-18 15:20:21 +02003012 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003013 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3014 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003015
3016 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003017 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3018 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003019
Gleb Natapov5cd21912010-03-18 15:20:26 +02003020 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003021 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003022 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003023 /*
3024 * Re-enter guest when pio read ahead buffer is empty or,
3025 * if it is not used, after each 1024 iteration.
3026 */
3027 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3028 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003029 ctxt->restart = false;
3030 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003031 /*
3032 * reset read cache here in case string instruction is restared
3033 * without decoding
3034 */
3035 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003036 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003037
3038done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003039 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040
3041twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003042 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003044 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045 u16 size;
3046 unsigned long address;
3047
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003048 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003049 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003050 goto cannot_emulate;
3051
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003052 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003053 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003054 goto done;
3055
Avi Kivity33e38852008-05-21 15:34:25 +03003056 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003057 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003058 /* Disable writeback. */
3059 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003062 rc = read_descriptor(ctxt, ops, c->src.ptr,
3063 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003064 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 goto done;
3066 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003067 /* Disable writeback. */
3068 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003070 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003071 if (c->modrm_mod == 3) {
3072 switch (c->modrm_rm) {
3073 case 1:
3074 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003075 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003076 goto done;
3077 break;
3078 default:
3079 goto cannot_emulate;
3080 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003081 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003082 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003083 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003084 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003085 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003086 goto done;
3087 realmode_lidt(ctxt->vcpu, size, address);
3088 }
Avi Kivity16286d02008-04-14 14:40:50 +03003089 /* Disable writeback. */
3090 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 break;
3092 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003093 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003094 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 break;
3096 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003097 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3098 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003099 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003101 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003102 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003103 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003105 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003106 /* Disable writeback. */
3107 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 break;
3109 default:
3110 goto cannot_emulate;
3111 }
3112 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003113 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003114 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003115 if (rc != X86EMUL_CONTINUE)
3116 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003117 else
3118 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003119 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003120 case 0x06:
3121 emulate_clts(ctxt->vcpu);
3122 c->dst.type = OP_NONE;
3123 break;
3124 case 0x08: /* invd */
3125 case 0x09: /* wbinvd */
3126 case 0x0d: /* GrpP (prefetch) */
3127 case 0x18: /* Grp16 (prefetch/nop) */
3128 c->dst.type = OP_NONE;
3129 break;
3130 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003131 switch (c->modrm_reg) {
3132 case 1:
3133 case 5 ... 7:
3134 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003135 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003136 goto done;
3137 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003138 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003139 c->dst.type = OP_NONE; /* no writeback */
3140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003142 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3143 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003144 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003145 goto done;
3146 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003147 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003148 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003150 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003151 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003152 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003153 goto done;
3154 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003155 c->dst.type = OP_NONE;
3156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003158 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3159 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003160 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003161 goto done;
3162 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003163
Gleb Natapov338dbc92010-04-28 19:15:32 +03003164 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3165 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3166 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3167 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003168 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003169 goto done;
3170 }
3171
Laurent Viviera01af5e2007-09-24 11:10:56 +02003172 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003174 case 0x30:
3175 /* wrmsr */
3176 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3177 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003178 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003179 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003180 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003181 }
3182 rc = X86EMUL_CONTINUE;
3183 c->dst.type = OP_NONE;
3184 break;
3185 case 0x32:
3186 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003187 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003188 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003189 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003190 } else {
3191 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3192 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3193 }
3194 rc = X86EMUL_CONTINUE;
3195 c->dst.type = OP_NONE;
3196 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003197 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003198 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003199 if (rc != X86EMUL_CONTINUE)
3200 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003201 else
3202 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003203 break;
3204 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003205 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003206 if (rc != X86EMUL_CONTINUE)
3207 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003208 else
3209 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003210 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003212 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003213 if (!test_cc(c->b, ctxt->eflags))
3214 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003216 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003217 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003218 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003219 c->dst.type = OP_NONE;
3220 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003221 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003222 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003223 break;
3224 case 0xa1: /* pop fs */
3225 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003226 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003227 goto done;
3228 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003229 case 0xa3:
3230 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003231 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003232 /* only subword offset */
3233 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003234 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003235 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003236 case 0xa4: /* shld imm8, r, r/m */
3237 case 0xa5: /* shld cl, r, r/m */
3238 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3239 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003240 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003241 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003242 break;
3243 case 0xa9: /* pop gs */
3244 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003245 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003246 goto done;
3247 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003248 case 0xab:
3249 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003250 /* only subword offset */
3251 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003252 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003253 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003254 case 0xac: /* shrd imm8, r, r/m */
3255 case 0xad: /* shrd cl, r, r/m */
3256 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3257 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003258 case 0xae: /* clflush */
3259 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 case 0xb0 ... 0xb1: /* cmpxchg */
3261 /*
3262 * Save real source value, then compare EAX against
3263 * destination.
3264 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003265 c->src.orig_val = c->src.val;
3266 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003267 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3268 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003270 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 } else {
3272 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003273 c->dst.type = OP_REG;
3274 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 }
3276 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 case 0xb3:
3278 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003279 /* only subword offset */
3280 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003281 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003284 c->dst.bytes = c->op_bytes;
3285 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3286 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003289 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 case 0:
3291 goto bt;
3292 case 1:
3293 goto bts;
3294 case 2:
3295 goto btr;
3296 case 3:
3297 goto btc;
3298 }
3299 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003300 case 0xbb:
3301 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003302 /* only subword offset */
3303 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003304 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003305 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003307 c->dst.bytes = c->op_bytes;
3308 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3309 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003311 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003312 c->dst.bytes = c->op_bytes;
3313 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3314 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003315 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003317 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003318 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003319 goto done;
3320 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 }
3322 goto writeback;
3323
3324cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003325 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326 return -1;
3327}