blob: 97a42e8c00d0db83278a295825a6d626a0073861 [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 Kivityc9eaf202009-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 Natapovea798492010-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
Avi Kivity62266862007-11-20 13:15:52 +0200656static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
657 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300658 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200659{
660 struct fetch_cache *fc = &ctxt->decode.fetch;
661 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300662 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200663
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300664 if (eip == fc->end) {
665 cur_size = fc->end - fc->start;
666 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
667 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
668 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900669 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200670 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300671 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200672 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300673 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900674 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200675}
676
677static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
678 struct x86_emulate_ops *ops,
679 unsigned long eip, void *dest, unsigned size)
680{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900681 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200682
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200683 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200684 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200685 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200686 while (size--) {
687 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900688 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200689 return rc;
690 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900691 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200692}
693
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000694/*
695 * Given the 'reg' portion of a ModRM byte, and a register block, return a
696 * pointer into the block that addresses the relevant register.
697 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
698 */
699static void *decode_register(u8 modrm_reg, unsigned long *regs,
700 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701{
702 void *p;
703
704 p = &regs[modrm_reg];
705 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
706 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
707 return p;
708}
709
710static int read_descriptor(struct x86_emulate_ctxt *ctxt,
711 struct x86_emulate_ops *ops,
712 void *ptr,
713 u16 *size, unsigned long *address, int op_bytes)
714{
715 int rc;
716
717 if (op_bytes == 2)
718 op_bytes = 3;
719 *address = 0;
Laurent Viviercebff022007-07-30 13:35:24 +0300720 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200721 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900722 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300724 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200725 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 return rc;
727}
728
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300729static int test_cc(unsigned int condition, unsigned int flags)
730{
731 int rc = 0;
732
733 switch ((condition & 15) >> 1) {
734 case 0: /* o */
735 rc |= (flags & EFLG_OF);
736 break;
737 case 1: /* b/c/nae */
738 rc |= (flags & EFLG_CF);
739 break;
740 case 2: /* z/e */
741 rc |= (flags & EFLG_ZF);
742 break;
743 case 3: /* be/na */
744 rc |= (flags & (EFLG_CF|EFLG_ZF));
745 break;
746 case 4: /* s */
747 rc |= (flags & EFLG_SF);
748 break;
749 case 5: /* p/pe */
750 rc |= (flags & EFLG_PF);
751 break;
752 case 7: /* le/ng */
753 rc |= (flags & EFLG_ZF);
754 /* fall through */
755 case 6: /* l/nge */
756 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
757 break;
758 }
759
760 /* Odd condition identifiers (lsb == 1) have inverted sense. */
761 return (!!rc ^ (condition & 1));
762}
763
Avi Kivity3c118e22007-10-31 10:27:04 +0200764static void decode_register_operand(struct operand *op,
765 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200766 int inhibit_bytereg)
767{
Avi Kivity33615aa2007-10-31 11:15:56 +0200768 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200769 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200770
771 if (!(c->d & ModRM))
772 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200773 op->type = OP_REG;
774 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200775 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200776 op->val = *(u8 *)op->ptr;
777 op->bytes = 1;
778 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200779 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200780 op->bytes = c->op_bytes;
781 switch (op->bytes) {
782 case 2:
783 op->val = *(u16 *)op->ptr;
784 break;
785 case 4:
786 op->val = *(u32 *)op->ptr;
787 break;
788 case 8:
789 op->val = *(u64 *) op->ptr;
790 break;
791 }
792 }
793 op->orig_val = op->val;
794}
795
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200796static int decode_modrm(struct x86_emulate_ctxt *ctxt,
797 struct x86_emulate_ops *ops)
798{
799 struct decode_cache *c = &ctxt->decode;
800 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700801 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900802 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200803
804 if (c->rex_prefix) {
805 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
806 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
807 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
808 }
809
810 c->modrm = insn_fetch(u8, 1, c->eip);
811 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
812 c->modrm_reg |= (c->modrm & 0x38) >> 3;
813 c->modrm_rm |= (c->modrm & 0x07);
814 c->modrm_ea = 0;
815 c->use_modrm_ea = 1;
816
817 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300818 c->modrm_ptr = decode_register(c->modrm_rm,
819 c->regs, c->d & ByteOp);
820 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200821 return rc;
822 }
823
824 if (c->ad_bytes == 2) {
825 unsigned bx = c->regs[VCPU_REGS_RBX];
826 unsigned bp = c->regs[VCPU_REGS_RBP];
827 unsigned si = c->regs[VCPU_REGS_RSI];
828 unsigned di = c->regs[VCPU_REGS_RDI];
829
830 /* 16-bit ModR/M decode. */
831 switch (c->modrm_mod) {
832 case 0:
833 if (c->modrm_rm == 6)
834 c->modrm_ea += insn_fetch(u16, 2, c->eip);
835 break;
836 case 1:
837 c->modrm_ea += insn_fetch(s8, 1, c->eip);
838 break;
839 case 2:
840 c->modrm_ea += insn_fetch(u16, 2, c->eip);
841 break;
842 }
843 switch (c->modrm_rm) {
844 case 0:
845 c->modrm_ea += bx + si;
846 break;
847 case 1:
848 c->modrm_ea += bx + di;
849 break;
850 case 2:
851 c->modrm_ea += bp + si;
852 break;
853 case 3:
854 c->modrm_ea += bp + di;
855 break;
856 case 4:
857 c->modrm_ea += si;
858 break;
859 case 5:
860 c->modrm_ea += di;
861 break;
862 case 6:
863 if (c->modrm_mod != 0)
864 c->modrm_ea += bp;
865 break;
866 case 7:
867 c->modrm_ea += bx;
868 break;
869 }
870 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
871 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300872 if (!c->has_seg_override)
873 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 c->modrm_ea = (u16)c->modrm_ea;
875 } else {
876 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700877 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200878 sib = insn_fetch(u8, 1, c->eip);
879 index_reg |= (sib >> 3) & 7;
880 base_reg |= sib & 7;
881 scale = sib >> 6;
882
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700883 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
884 c->modrm_ea += insn_fetch(s32, 4, c->eip);
885 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700887 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700889 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
890 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700891 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700892 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894 switch (c->modrm_mod) {
895 case 0:
896 if (c->modrm_rm == 5)
897 c->modrm_ea += insn_fetch(s32, 4, c->eip);
898 break;
899 case 1:
900 c->modrm_ea += insn_fetch(s8, 1, c->eip);
901 break;
902 case 2:
903 c->modrm_ea += insn_fetch(s32, 4, c->eip);
904 break;
905 }
906 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200907done:
908 return rc;
909}
910
911static int decode_abs(struct x86_emulate_ctxt *ctxt,
912 struct x86_emulate_ops *ops)
913{
914 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900915 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200916
917 switch (c->ad_bytes) {
918 case 2:
919 c->modrm_ea = insn_fetch(u16, 2, c->eip);
920 break;
921 case 4:
922 c->modrm_ea = insn_fetch(u32, 4, c->eip);
923 break;
924 case 8:
925 c->modrm_ea = insn_fetch(u64, 8, c->eip);
926 break;
927 }
928done:
929 return rc;
930}
931
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932int
Laurent Vivier8b4caf62007-09-18 11:27:19 +0200933x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934{
Laurent Viviere4e03de2007-09-18 11:52:50 +0200935 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900936 int rc = X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 int mode = ctxt->mode;
Avi Kivitye09d0822008-01-18 12:38:59 +0200938 int def_op_bytes, def_ad_bytes, group;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Gleb Natapov5cd21912010-03-18 15:20:26 +0200941 /* we cannot decode insn before we complete previous rep insn */
942 WARN_ON(ctxt->restart);
943
Gleb Natapov063db062010-03-18 15:20:06 +0200944 c->eip = ctxt->eip;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300945 c->fetch.start = c->fetch.end = c->eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +0300946 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947
948 switch (mode) {
949 case X86EMUL_MODE_REAL:
Gleb Natapova0044752010-02-10 14:21:31 +0200950 case X86EMUL_MODE_VM86:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 case X86EMUL_MODE_PROT16:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200952 def_op_bytes = def_ad_bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 break;
954 case X86EMUL_MODE_PROT32:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200955 def_op_bytes = def_ad_bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 break;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800957#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 case X86EMUL_MODE_PROT64:
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200959 def_op_bytes = 4;
960 def_ad_bytes = 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 break;
962#endif
963 default:
964 return -1;
965 }
966
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200967 c->op_bytes = def_op_bytes;
968 c->ad_bytes = def_ad_bytes;
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 /* Legacy prefixes. */
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200971 for (;;) {
Laurent Viviere4e03de2007-09-18 11:52:50 +0200972 switch (c->b = insn_fetch(u8, 1, c->eip)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 case 0x66: /* operand-size override */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200974 /* switch between 2/4 bytes */
975 c->op_bytes = def_op_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 break;
977 case 0x67: /* address-size override */
978 if (mode == X86EMUL_MODE_PROT64)
Laurent Viviere4e03de2007-09-18 11:52:50 +0200979 /* switch between 4/8 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200980 c->ad_bytes = def_ad_bytes ^ 12;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 else
Laurent Viviere4e03de2007-09-18 11:52:50 +0200982 /* switch between 2/4 bytes */
Avi Kivityf21b8bf2007-11-22 14:16:12 +0200983 c->ad_bytes = def_ad_bytes ^ 6;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 case 0x26: /* ES override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300986 case 0x2e: /* CS override */
987 case 0x36: /* SS override */
988 case 0x3e: /* DS override */
989 set_seg_override(c, (c->b >> 3) & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 break;
991 case 0x64: /* FS override */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 case 0x65: /* GS override */
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300993 set_seg_override(c, c->b & 7);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 break;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200995 case 0x40 ... 0x4f: /* REX */
996 if (mode != X86EMUL_MODE_PROT64)
997 goto done_prefixes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200998 c->rex_prefix = c->b;
Laurent Vivierb4c6abf2007-09-25 13:36:40 +0200999 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 case 0xf0: /* LOCK */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001001 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 break;
Laurent Vivierae6200b2007-09-20 11:17:24 +02001003 case 0xf2: /* REPNE/REPNZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001004 c->rep_prefix = REPNE_PREFIX;
1005 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 case 0xf3: /* REP/REPE/REPZ */
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +01001007 c->rep_prefix = REPE_PREFIX;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 default:
1010 goto done_prefixes;
1011 }
Laurent Vivierb4c6abf2007-09-25 13:36:40 +02001012
1013 /* Any legacy prefix after a REX prefix nullifies its effect. */
1014
Avi Kivity33615aa2007-10-31 11:15:56 +02001015 c->rex_prefix = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 }
1017
1018done_prefixes:
1019
1020 /* REX prefix. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021 if (c->rex_prefix)
Avi Kivity33615aa2007-10-31 11:15:56 +02001022 if (c->rex_prefix & 8)
Laurent Viviere4e03de2007-09-18 11:52:50 +02001023 c->op_bytes = 8; /* REX.W */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
1025 /* Opcode byte(s). */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001026 c->d = opcode_table[c->b];
1027 if (c->d == 0) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 /* Two-byte opcode? */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001029 if (c->b == 0x0f) {
1030 c->twobyte = 1;
1031 c->b = insn_fetch(u8, 1, c->eip);
1032 c->d = twobyte_table[c->b];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 }
Avi Kivitye09d0822008-01-18 12:38:59 +02001034 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
Avi Kivitye09d0822008-01-18 12:38:59 +02001036 if (c->d & Group) {
1037 group = c->d & GroupMask;
1038 c->modrm = insn_fetch(u8, 1, c->eip);
1039 --c->eip;
1040
1041 group = (group << 3) + ((c->modrm >> 3) & 7);
1042 if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
1043 c->d = group2_table[group];
1044 else
1045 c->d = group_table[group];
1046 }
1047
1048 /* Unrecognised? */
1049 if (c->d == 0) {
1050 DPRINTF("Cannot emulate %02x\n", c->b);
1051 return -1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 }
1053
Avi Kivity6e3d5df2007-12-06 18:14:14 +02001054 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
1055 c->op_bytes = 8;
1056
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 /* ModRM and SIB bytes. */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001058 if (c->d & ModRM)
1059 rc = decode_modrm(ctxt, ops);
1060 else if (c->d & MemAbs)
1061 rc = decode_abs(ctxt, ops);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001062 if (rc != X86EMUL_CONTINUE)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001065 if (!c->has_seg_override)
1066 set_seg_override(c, VCPU_SREG_DS);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001067
Avi Kivity7a5b56d2008-06-22 16:22:51 +03001068 if (!(!c->twobyte && c->b == 0x8d))
Gleb Natapov79168fd2010-04-28 19:15:30 +03001069 c->modrm_ea += seg_override_base(ctxt, ops, c);
Avi Kivityc7e75a32007-10-28 16:34:25 +02001070
1071 if (c->ad_bytes != 8)
1072 c->modrm_ea = (u32)c->modrm_ea;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001073
1074 if (c->rip_relative)
1075 c->modrm_ea += c->eip;
1076
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 /*
1078 * Decode and fetch the source operand: register, memory
1079 * or immediate.
1080 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001081 switch (c->d & SrcMask) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 case SrcNone:
1083 break;
1084 case SrcReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001085 decode_register_operand(&c->src, c, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 break;
1087 case SrcMem16:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001088 c->src.bytes = 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 goto srcmem_common;
1090 case SrcMem32:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001091 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 goto srcmem_common;
1093 case SrcMem:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001094 c->src.bytes = (c->d & ByteOp) ? 1 :
1095 c->op_bytes;
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001096 /* Don't fetch the address for invlpg: it could be unmapped. */
Mike Dayd77c26f2007-10-08 09:02:08 -04001097 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
Rusty Russellb85b9ee92007-09-09 14:12:54 +03001098 break;
Mike Dayd77c26f2007-10-08 09:02:08 -04001099 srcmem_common:
Aurelien Jarno4e624172007-10-17 19:30:41 +02001100 /*
1101 * For instructions with a ModR/M byte, switch to register
1102 * access if Mod = 3.
1103 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001104 if ((c->d & ModRM) && c->modrm_mod == 3) {
1105 c->src.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001106 c->src.val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001107 c->src.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001108 break;
1109 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02001110 c->src.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001111 c->src.ptr = (unsigned long *)c->modrm_ea;
1112 c->src.val = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 break;
1114 case SrcImm:
Avi Kivityc9eaf202009-05-18 16:13:45 +03001115 case SrcImmU:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001116 c->src.type = OP_IMM;
1117 c->src.ptr = (unsigned long *)c->eip;
1118 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1119 if (c->src.bytes == 8)
1120 c->src.bytes = 4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 /* NB. Immediates are sign-extended as necessary. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001122 switch (c->src.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001124 c->src.val = insn_fetch(s8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 break;
1126 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001127 c->src.val = insn_fetch(s16, 2, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 break;
1129 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001130 c->src.val = insn_fetch(s32, 4, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 break;
1132 }
Avi Kivityc9eaf202009-05-18 16:13:45 +03001133 if ((c->d & SrcMask) == SrcImmU) {
1134 switch (c->src.bytes) {
1135 case 1:
1136 c->src.val &= 0xff;
1137 break;
1138 case 2:
1139 c->src.val &= 0xffff;
1140 break;
1141 case 4:
1142 c->src.val &= 0xffffffff;
1143 break;
1144 }
1145 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 break;
1147 case SrcImmByte:
Gleb Natapov341de7e2009-04-12 13:36:41 +03001148 case SrcImmUByte:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001149 c->src.type = OP_IMM;
1150 c->src.ptr = (unsigned long *)c->eip;
1151 c->src.bytes = 1;
Gleb Natapov341de7e2009-04-12 13:36:41 +03001152 if ((c->d & SrcMask) == SrcImmByte)
1153 c->src.val = insn_fetch(s8, 1, c->eip);
1154 else
1155 c->src.val = insn_fetch(u8, 1, c->eip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 break;
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +01001157 case SrcOne:
1158 c->src.bytes = 1;
1159 c->src.val = 1;
1160 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001161 case SrcSI:
1162 c->src.type = OP_MEM;
1163 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1164 c->src.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001165 register_address(c, seg_override_base(ctxt, ops, c),
Gleb Natapova682e352010-03-18 15:20:21 +02001166 c->regs[VCPU_REGS_RSI]);
1167 c->src.val = 0;
1168 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03001169 case SrcImmFAddr:
1170 c->src.type = OP_IMM;
1171 c->src.ptr = (unsigned long *)c->eip;
1172 c->src.bytes = c->op_bytes + 2;
1173 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
1174 break;
1175 case SrcMemFAddr:
1176 c->src.type = OP_MEM;
1177 c->src.ptr = (unsigned long *)c->modrm_ea;
1178 c->src.bytes = c->op_bytes + 2;
1179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 }
1181
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +01001182 /*
1183 * Decode and fetch the second source operand: register, memory
1184 * or immediate.
1185 */
1186 switch (c->d & Src2Mask) {
1187 case Src2None:
1188 break;
1189 case Src2CL:
1190 c->src2.bytes = 1;
1191 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
1192 break;
1193 case Src2ImmByte:
1194 c->src2.type = OP_IMM;
1195 c->src2.ptr = (unsigned long *)c->eip;
1196 c->src2.bytes = 1;
1197 c->src2.val = insn_fetch(u8, 1, c->eip);
1198 break;
1199 case Src2One:
1200 c->src2.bytes = 1;
1201 c->src2.val = 1;
1202 break;
1203 }
1204
Avi Kivity038e51d2007-01-22 20:40:40 -08001205 /* Decode and fetch the destination operand: register or memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02001206 switch (c->d & DstMask) {
Avi Kivity038e51d2007-01-22 20:40:40 -08001207 case ImplicitOps:
1208 /* Special instructions do their own operand decoding. */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001209 return 0;
Avi Kivity038e51d2007-01-22 20:40:40 -08001210 case DstReg:
Avi Kivity9f1ef3f2007-10-31 11:21:06 +02001211 decode_register_operand(&c->dst, c,
Avi Kivity3c118e22007-10-31 10:27:04 +02001212 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
Avi Kivity038e51d2007-01-22 20:40:40 -08001213 break;
1214 case DstMem:
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001215 case DstMem64:
Laurent Viviere4e03de2007-09-18 11:52:50 +02001216 if ((c->d & ModRM) && c->modrm_mod == 3) {
Guillaume Thouvenin89c69632008-05-27 10:22:20 +02001217 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Laurent Viviere4e03de2007-09-18 11:52:50 +02001218 c->dst.type = OP_REG;
Avi Kivity66b85502008-04-14 23:27:07 +03001219 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity107d6d22008-05-05 14:58:26 +03001220 c->dst.ptr = c->modrm_ptr;
Aurelien Jarno4e624172007-10-17 19:30:41 +02001221 break;
1222 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001223 c->dst.type = OP_MEM;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001224 c->dst.ptr = (unsigned long *)c->modrm_ea;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001225 if ((c->d & DstMask) == DstMem64)
1226 c->dst.bytes = 8;
1227 else
1228 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001229 c->dst.val = 0;
1230 if (c->d & BitOp) {
1231 unsigned long mask = ~(c->dst.bytes * 8 - 1);
1232
1233 c->dst.ptr = (void *)c->dst.ptr +
1234 (c->src.val & mask) / 8;
1235 }
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001236 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001237 case DstAcc:
1238 c->dst.type = OP_REG;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001239 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001240 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001241 switch (c->dst.bytes) {
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001242 case 1:
1243 c->dst.val = *(u8 *)c->dst.ptr;
1244 break;
1245 case 2:
1246 c->dst.val = *(u16 *)c->dst.ptr;
1247 break;
1248 case 4:
1249 c->dst.val = *(u32 *)c->dst.ptr;
1250 break;
Gleb Natapovd6d367d2010-03-15 16:38:28 +02001251 case 8:
1252 c->dst.val = *(u64 *)c->dst.ptr;
1253 break;
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +02001254 }
1255 c->dst.orig_val = c->dst.val;
1256 break;
Gleb Natapova682e352010-03-18 15:20:21 +02001257 case DstDI:
1258 c->dst.type = OP_MEM;
1259 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
1260 c->dst.ptr = (unsigned long *)
Gleb Natapov79168fd2010-04-28 19:15:30 +03001261 register_address(c, es_base(ctxt, ops),
Gleb Natapova682e352010-03-18 15:20:21 +02001262 c->regs[VCPU_REGS_RDI]);
1263 c->dst.val = 0;
1264 break;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02001265 }
1266
1267done:
1268 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
1269}
1270
Gleb Natapov9de41572010-04-28 19:15:22 +03001271static int read_emulated(struct x86_emulate_ctxt *ctxt,
1272 struct x86_emulate_ops *ops,
1273 unsigned long addr, void *dest, unsigned size)
1274{
1275 int rc;
1276 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001277 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +03001278
1279 while (size) {
1280 int n = min(size, 8u);
1281 size -= n;
1282 if (mc->pos < mc->end)
1283 goto read_cached;
1284
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001285 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
1286 ctxt->vcpu);
1287 if (rc == X86EMUL_PROPAGATE_FAULT)
1288 kvm_inject_page_fault(ctxt->vcpu, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +03001289 if (rc != X86EMUL_CONTINUE)
1290 return rc;
1291 mc->end += n;
1292
1293 read_cached:
1294 memcpy(dest, mc->data + mc->pos, n);
1295 mc->pos += n;
1296 dest += n;
1297 addr += n;
1298 }
1299 return X86EMUL_CONTINUE;
1300}
1301
Gleb Natapov7b262e92010-03-18 15:20:27 +02001302static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1303 struct x86_emulate_ops *ops,
1304 unsigned int size, unsigned short port,
1305 void *dest)
1306{
1307 struct read_cache *rc = &ctxt->decode.io_read;
1308
1309 if (rc->pos == rc->end) { /* refill pio read ahead */
1310 struct decode_cache *c = &ctxt->decode;
1311 unsigned int in_page, n;
1312 unsigned int count = c->rep_prefix ?
1313 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1314 in_page = (ctxt->eflags & EFLG_DF) ?
1315 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1316 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1317 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1318 count);
1319 if (n == 0)
1320 n = 1;
1321 rc->pos = rc->end = 0;
1322 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1323 return 0;
1324 rc->end = n * size;
1325 }
1326
1327 memcpy(dest, rc->data + rc->pos, size);
1328 rc->pos += size;
1329 return 1;
1330}
1331
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001332static u32 desc_limit_scaled(struct desc_struct *desc)
1333{
1334 u32 limit = get_desc_limit(desc);
1335
1336 return desc->g ? (limit << 12) | 0xfff : limit;
1337}
1338
1339static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1340 struct x86_emulate_ops *ops,
1341 u16 selector, struct desc_ptr *dt)
1342{
1343 if (selector & 1 << 2) {
1344 struct desc_struct desc;
1345 memset (dt, 0, sizeof *dt);
1346 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
1347 return;
1348
1349 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1350 dt->address = get_desc_base(&desc);
1351 } else
1352 ops->get_gdt(dt, ctxt->vcpu);
1353}
1354
1355/* allowed just for 8 bytes segments */
1356static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1357 struct x86_emulate_ops *ops,
1358 u16 selector, struct desc_struct *desc)
1359{
1360 struct desc_ptr dt;
1361 u16 index = selector >> 3;
1362 int ret;
1363 u32 err;
1364 ulong addr;
1365
1366 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1367
1368 if (dt.size < index * 8 + 7) {
1369 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1370 return X86EMUL_PROPAGATE_FAULT;
1371 }
1372 addr = dt.address + index * 8;
1373 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1374 if (ret == X86EMUL_PROPAGATE_FAULT)
1375 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1376
1377 return ret;
1378}
1379
1380/* allowed just for 8 bytes segments */
1381static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1382 struct x86_emulate_ops *ops,
1383 u16 selector, struct desc_struct *desc)
1384{
1385 struct desc_ptr dt;
1386 u16 index = selector >> 3;
1387 u32 err;
1388 ulong addr;
1389 int ret;
1390
1391 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1392
1393 if (dt.size < index * 8 + 7) {
1394 kvm_inject_gp(ctxt->vcpu, selector & 0xfffc);
1395 return X86EMUL_PROPAGATE_FAULT;
1396 }
1397
1398 addr = dt.address + index * 8;
1399 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
1400 if (ret == X86EMUL_PROPAGATE_FAULT)
1401 kvm_inject_page_fault(ctxt->vcpu, addr, err);
1402
1403 return ret;
1404}
1405
1406static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1407 struct x86_emulate_ops *ops,
1408 u16 selector, int seg)
1409{
1410 struct desc_struct seg_desc;
1411 u8 dpl, rpl, cpl;
1412 unsigned err_vec = GP_VECTOR;
1413 u32 err_code = 0;
1414 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1415 int ret;
1416
1417 memset(&seg_desc, 0, sizeof seg_desc);
1418
1419 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1420 || ctxt->mode == X86EMUL_MODE_REAL) {
1421 /* set real mode segment descriptor */
1422 set_desc_base(&seg_desc, selector << 4);
1423 set_desc_limit(&seg_desc, 0xffff);
1424 seg_desc.type = 3;
1425 seg_desc.p = 1;
1426 seg_desc.s = 1;
1427 goto load;
1428 }
1429
1430 /* NULL selector is not valid for TR, CS and SS */
1431 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1432 && null_selector)
1433 goto exception;
1434
1435 /* TR should be in GDT only */
1436 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1437 goto exception;
1438
1439 if (null_selector) /* for NULL selector skip all following checks */
1440 goto load;
1441
1442 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1443 if (ret != X86EMUL_CONTINUE)
1444 return ret;
1445
1446 err_code = selector & 0xfffc;
1447 err_vec = GP_VECTOR;
1448
1449 /* can't load system descriptor into segment selecor */
1450 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1451 goto exception;
1452
1453 if (!seg_desc.p) {
1454 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1455 goto exception;
1456 }
1457
1458 rpl = selector & 3;
1459 dpl = seg_desc.dpl;
1460 cpl = ops->cpl(ctxt->vcpu);
1461
1462 switch (seg) {
1463 case VCPU_SREG_SS:
1464 /*
1465 * segment is not a writable data segment or segment
1466 * selector's RPL != CPL or segment selector's RPL != CPL
1467 */
1468 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1469 goto exception;
1470 break;
1471 case VCPU_SREG_CS:
1472 if (!(seg_desc.type & 8))
1473 goto exception;
1474
1475 if (seg_desc.type & 4) {
1476 /* conforming */
1477 if (dpl > cpl)
1478 goto exception;
1479 } else {
1480 /* nonconforming */
1481 if (rpl > cpl || dpl != cpl)
1482 goto exception;
1483 }
1484 /* CS(RPL) <- CPL */
1485 selector = (selector & 0xfffc) | cpl;
1486 break;
1487 case VCPU_SREG_TR:
1488 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1489 goto exception;
1490 break;
1491 case VCPU_SREG_LDTR:
1492 if (seg_desc.s || seg_desc.type != 2)
1493 goto exception;
1494 break;
1495 default: /* DS, ES, FS, or GS */
1496 /*
1497 * segment is not a data or readable code segment or
1498 * ((segment is a data or nonconforming code segment)
1499 * and (both RPL and CPL > DPL))
1500 */
1501 if ((seg_desc.type & 0xa) == 0x8 ||
1502 (((seg_desc.type & 0xc) != 0xc) &&
1503 (rpl > dpl && cpl > dpl)))
1504 goto exception;
1505 break;
1506 }
1507
1508 if (seg_desc.s) {
1509 /* mark segment as accessed */
1510 seg_desc.type |= 1;
1511 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1512 if (ret != X86EMUL_CONTINUE)
1513 return ret;
1514 }
1515load:
1516 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1517 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1518 return X86EMUL_CONTINUE;
1519exception:
1520 kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code);
1521 return X86EMUL_PROPAGATE_FAULT;
1522}
1523
Gleb Natapov79168fd2010-04-28 19:15:30 +03001524static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1525 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001526{
1527 struct decode_cache *c = &ctxt->decode;
1528
1529 c->dst.type = OP_MEM;
1530 c->dst.bytes = c->op_bytes;
1531 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001532 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001533 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001534 c->regs[VCPU_REGS_RSP]);
1535}
1536
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001537static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001538 struct x86_emulate_ops *ops,
1539 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001540{
1541 struct decode_cache *c = &ctxt->decode;
1542 int rc;
1543
Gleb Natapov79168fd2010-04-28 19:15:30 +03001544 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001545 c->regs[VCPU_REGS_RSP]),
1546 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001547 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001548 return rc;
1549
Avi Kivity350f69d2009-01-05 11:12:40 +02001550 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001551 return rc;
1552}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001553
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001554static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1555 struct x86_emulate_ops *ops,
1556 void *dest, int len)
1557{
1558 int rc;
1559 unsigned long val, change_mask;
1560 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001561 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001562
1563 rc = emulate_pop(ctxt, ops, &val, len);
1564 if (rc != X86EMUL_CONTINUE)
1565 return rc;
1566
1567 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1568 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1569
1570 switch(ctxt->mode) {
1571 case X86EMUL_MODE_PROT64:
1572 case X86EMUL_MODE_PROT32:
1573 case X86EMUL_MODE_PROT16:
1574 if (cpl == 0)
1575 change_mask |= EFLG_IOPL;
1576 if (cpl <= iopl)
1577 change_mask |= EFLG_IF;
1578 break;
1579 case X86EMUL_MODE_VM86:
1580 if (iopl < 3) {
1581 kvm_inject_gp(ctxt->vcpu, 0);
1582 return X86EMUL_PROPAGATE_FAULT;
1583 }
1584 change_mask |= EFLG_IF;
1585 break;
1586 default: /* real mode */
1587 change_mask |= (EFLG_IOPL | EFLG_IF);
1588 break;
1589 }
1590
1591 *(unsigned long *)dest =
1592 (ctxt->eflags & ~change_mask) | (val & change_mask);
1593
1594 return rc;
1595}
1596
Gleb Natapov79168fd2010-04-28 19:15:30 +03001597static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1598 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001599{
1600 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001601
Gleb Natapov79168fd2010-04-28 19:15:30 +03001602 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001603
Gleb Natapov79168fd2010-04-28 19:15:30 +03001604 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001605}
1606
1607static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1608 struct x86_emulate_ops *ops, int seg)
1609{
1610 struct decode_cache *c = &ctxt->decode;
1611 unsigned long selector;
1612 int rc;
1613
1614 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001615 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001616 return rc;
1617
Gleb Natapov2e873022010-03-18 15:20:18 +02001618 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001619 return rc;
1620}
1621
Gleb Natapov79168fd2010-04-28 19:15:30 +03001622static void emulate_pusha(struct x86_emulate_ctxt *ctxt,
1623 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001624{
1625 struct decode_cache *c = &ctxt->decode;
1626 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
1627 int reg = VCPU_REGS_RAX;
1628
1629 while (reg <= VCPU_REGS_RDI) {
1630 (reg == VCPU_REGS_RSP) ?
1631 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1632
Gleb Natapov79168fd2010-04-28 19:15:30 +03001633 emulate_push(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001634 ++reg;
1635 }
1636}
1637
1638static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1639 struct x86_emulate_ops *ops)
1640{
1641 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001642 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001643 int reg = VCPU_REGS_RDI;
1644
1645 while (reg >= VCPU_REGS_RAX) {
1646 if (reg == VCPU_REGS_RSP) {
1647 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1648 c->op_bytes);
1649 --reg;
1650 }
1651
1652 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001653 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001654 break;
1655 --reg;
1656 }
1657 return rc;
1658}
1659
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001660static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1661 struct x86_emulate_ops *ops)
1662{
1663 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001664
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001665 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001666}
1667
Laurent Vivier05f086f2007-09-24 11:10:55 +02001668static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001669{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001670 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671 switch (c->modrm_reg) {
1672 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001673 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001674 break;
1675 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001676 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001677 break;
1678 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001679 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680 break;
1681 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001682 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 case 4: /* sal/shl */
1685 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001686 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687 break;
1688 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001689 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690 break;
1691 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001692 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693 break;
1694 }
1695}
1696
1697static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001698 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699{
1700 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701
1702 switch (c->modrm_reg) {
1703 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001704 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705 break;
1706 case 2: /* not */
1707 c->dst.val = ~c->dst.val;
1708 break;
1709 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001710 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711 break;
1712 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001713 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001715 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716}
1717
1718static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001719 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001720{
1721 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722
1723 switch (c->modrm_reg) {
1724 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001725 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726 break;
1727 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001728 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001729 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001730 case 2: /* call near abs */ {
1731 long int old_eip;
1732 old_eip = c->eip;
1733 c->eip = c->src.val;
1734 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001735 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001736 break;
1737 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001738 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001739 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001740 break;
1741 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001742 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001745 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746}
1747
1748static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001749 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750{
1751 struct decode_cache *c = &ctxt->decode;
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001752 u64 old = c->dst.orig_val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001753
1754 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1755 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
1756
1757 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1758 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001759 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760 } else {
Gleb Natapov6550e1f2010-03-21 13:08:21 +02001761 c->dst.val = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001762 (u32) c->regs[VCPU_REGS_RBX];
1763
Laurent Vivier05f086f2007-09-24 11:10:55 +02001764 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001766 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767}
1768
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001769static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1770 struct x86_emulate_ops *ops)
1771{
1772 struct decode_cache *c = &ctxt->decode;
1773 int rc;
1774 unsigned long cs;
1775
1776 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001777 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001778 return rc;
1779 if (c->op_bytes == 4)
1780 c->eip = (u32)c->eip;
1781 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001782 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001783 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001784 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001785 return rc;
1786}
1787
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788static inline int writeback(struct x86_emulate_ctxt *ctxt,
1789 struct x86_emulate_ops *ops)
1790{
1791 int rc;
1792 struct decode_cache *c = &ctxt->decode;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001793 u32 err;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794
1795 switch (c->dst.type) {
1796 case OP_REG:
1797 /* The 4-byte case *is* correct:
1798 * in 64-bit mode we zero-extend.
1799 */
1800 switch (c->dst.bytes) {
1801 case 1:
1802 *(u8 *)c->dst.ptr = (u8)c->dst.val;
1803 break;
1804 case 2:
1805 *(u16 *)c->dst.ptr = (u16)c->dst.val;
1806 break;
1807 case 4:
1808 *c->dst.ptr = (u32)c->dst.val;
1809 break; /* 64b: zero-ext */
1810 case 8:
1811 *c->dst.ptr = c->dst.val;
1812 break;
1813 }
1814 break;
1815 case OP_MEM:
1816 if (c->lock_prefix)
1817 rc = ops->cmpxchg_emulated(
1818 (unsigned long)c->dst.ptr,
1819 &c->dst.orig_val,
1820 &c->dst.val,
1821 c->dst.bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001822 &err,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823 ctxt->vcpu);
1824 else
1825 rc = ops->write_emulated(
1826 (unsigned long)c->dst.ptr,
1827 &c->dst.val,
1828 c->dst.bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001829 &err,
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830 ctxt->vcpu);
Gleb Natapov8fe681e2010-04-28 19:15:37 +03001831 if (rc == X86EMUL_PROPAGATE_FAULT)
1832 kvm_inject_page_fault(ctxt->vcpu,
1833 (unsigned long)c->dst.ptr, err);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001834 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001835 return rc;
Laurent Viviera01af5e2007-09-24 11:10:56 +02001836 break;
1837 case OP_NONE:
1838 /* no writeback */
1839 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001840 default:
1841 break;
1842 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001843 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844}
1845
Jaswinder Singh Rajputa3f9d392009-06-18 16:53:25 +05301846static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)
Glauber Costa310b5d32009-05-12 16:21:06 -04001847{
1848 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask);
1849 /*
1850 * an sti; sti; sequence only disable interrupts for the first
1851 * instruction. So, if the last instruction, be it emulated or
1852 * not, left the system with the INT_STI flag enabled, it
1853 * means that the last instruction is an sti. We should not
1854 * leave the flag on in this case. The same goes for mov ss
1855 */
1856 if (!(int_shadow & mask))
1857 ctxt->interruptibility = mask;
1858}
1859
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860static inline void
1861setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 struct x86_emulate_ops *ops, struct desc_struct *cs,
1863 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001864{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001865 memset(cs, 0, sizeof(struct desc_struct));
1866 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1867 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868
1869 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001871 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001872 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001873 cs->type = 0x0b; /* Read, Execute, Accessed */
1874 cs->s = 1;
1875 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001876 cs->p = 1;
1877 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001878
Gleb Natapov79168fd2010-04-28 19:15:30 +03001879 set_desc_base(ss, 0); /* flat segment */
1880 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881 ss->g = 1; /* 4kb granularity */
1882 ss->s = 1;
1883 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001887}
1888
1889static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001890emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891{
1892 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001893 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001894 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001895 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001896
1897 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001898 if (ctxt->mode == X86EMUL_MODE_REAL ||
1899 ctxt->mode == X86EMUL_MODE_VM86) {
1900 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1901 return X86EMUL_PROPAGATE_FAULT;
1902 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001903
Gleb Natapov79168fd2010-04-28 19:15:30 +03001904 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001905
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001906 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001908 cs_sel = (u16)(msr_data & 0xfffc);
1909 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001910
1911 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001912 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001913 cs.l = 1;
1914 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001915 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1916 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1917 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1918 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001919
1920 c->regs[VCPU_REGS_RCX] = c->eip;
1921 if (is_long_mode(ctxt->vcpu)) {
1922#ifdef CONFIG_X86_64
1923 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1924
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001925 ops->get_msr(ctxt->vcpu,
1926 ctxt->mode == X86EMUL_MODE_PROT64 ?
1927 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001928 c->eip = msr_data;
1929
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001930 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001931 ctxt->eflags &= ~(msr_data | EFLG_RF);
1932#endif
1933 } else {
1934 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001935 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001936 c->eip = (u32)msr_data;
1937
1938 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1939 }
1940
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001941 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001942}
1943
Andre Przywara8c604352009-06-18 12:56:01 +02001944static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001945emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001946{
1947 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001948 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001949 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001950 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001951
Gleb Natapova0044752010-02-10 14:21:31 +02001952 /* inject #GP if in real mode */
1953 if (ctxt->mode == X86EMUL_MODE_REAL) {
Andre Przywara8c604352009-06-18 12:56:01 +02001954 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001955 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001956 }
1957
1958 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1959 * Therefore, we inject an #UD.
1960 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001961 if (ctxt->mode == X86EMUL_MODE_PROT64) {
1962 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
1963 return X86EMUL_PROPAGATE_FAULT;
1964 }
Andre Przywara8c604352009-06-18 12:56:01 +02001965
Gleb Natapov79168fd2010-04-28 19:15:30 +03001966 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001967
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001968 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001969 switch (ctxt->mode) {
1970 case X86EMUL_MODE_PROT32:
1971 if ((msr_data & 0xfffc) == 0x0) {
1972 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001973 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001974 }
1975 break;
1976 case X86EMUL_MODE_PROT64:
1977 if (msr_data == 0x0) {
1978 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001979 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001980 }
1981 break;
1982 }
1983
1984 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 cs_sel = (u16)msr_data;
1986 cs_sel &= ~SELECTOR_RPL_MASK;
1987 ss_sel = cs_sel + 8;
1988 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001989 if (ctxt->mode == X86EMUL_MODE_PROT64
1990 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001991 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001992 cs.l = 1;
1993 }
1994
Gleb Natapov79168fd2010-04-28 19:15:30 +03001995 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1996 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1997 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1998 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001999
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002000 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002001 c->eip = msr_data;
2002
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002003 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002004 c->regs[VCPU_REGS_RSP] = msr_data;
2005
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002006 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002007}
2008
Andre Przywara4668f052009-06-18 12:56:02 +02002009static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002010emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02002011{
2012 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002013 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002014 u64 msr_data;
2015 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002016 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002017
Gleb Natapova0044752010-02-10 14:21:31 +02002018 /* inject #GP if in real mode or Virtual 8086 mode */
2019 if (ctxt->mode == X86EMUL_MODE_REAL ||
2020 ctxt->mode == X86EMUL_MODE_VM86) {
Andre Przywara4668f052009-06-18 12:56:02 +02002021 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02002022 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002023 }
2024
Gleb Natapov79168fd2010-04-28 19:15:30 +03002025 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002026
2027 if ((c->rex_prefix & 0x8) != 0x0)
2028 usermode = X86EMUL_MODE_PROT64;
2029 else
2030 usermode = X86EMUL_MODE_PROT32;
2031
2032 cs.dpl = 3;
2033 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002034 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002035 switch (usermode) {
2036 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002037 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02002038 if ((msr_data & 0xfffc) == 0x0) {
2039 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002040 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002041 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002042 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002043 break;
2044 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02002046 if (msr_data == 0x0) {
2047 kvm_inject_gp(ctxt->vcpu, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002048 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02002049 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002050 ss_sel = cs_sel + 8;
2051 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002052 cs.l = 1;
2053 break;
2054 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002055 cs_sel |= SELECTOR_RPL_MASK;
2056 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002057
Gleb Natapov79168fd2010-04-28 19:15:30 +03002058 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
2059 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
2060 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
2061 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02002062
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002063 c->eip = c->regs[VCPU_REGS_RDX];
2064 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002065
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002066 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002067}
2068
Gleb Natapov9c537242010-03-18 15:20:05 +02002069static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2070 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002071{
2072 int iopl;
2073 if (ctxt->mode == X86EMUL_MODE_REAL)
2074 return false;
2075 if (ctxt->mode == X86EMUL_MODE_VM86)
2076 return true;
2077 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002078 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002079}
2080
2081static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2082 struct x86_emulate_ops *ops,
2083 u16 port, u16 len)
2084{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002085 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002086 int r;
2087 u16 io_bitmap_ptr;
2088 u8 perm, bit_idx = port & 0x7;
2089 unsigned mask = (1 << len) - 1;
2090
Gleb Natapov79168fd2010-04-28 19:15:30 +03002091 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
2092 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002093 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002094 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002095 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002096 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
2097 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002098 if (r != X86EMUL_CONTINUE)
2099 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002100 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002101 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
2103 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002104 if (r != X86EMUL_CONTINUE)
2105 return false;
2106 if ((perm >> bit_idx) & mask)
2107 return false;
2108 return true;
2109}
2110
2111static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2112 struct x86_emulate_ops *ops,
2113 u16 port, u16 len)
2114{
Gleb Natapov9c537242010-03-18 15:20:05 +02002115 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002116 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2117 return false;
2118 return true;
2119}
2120
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002121static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2122 struct x86_emulate_ops *ops,
2123 struct tss_segment_16 *tss)
2124{
2125 struct decode_cache *c = &ctxt->decode;
2126
2127 tss->ip = c->eip;
2128 tss->flag = ctxt->eflags;
2129 tss->ax = c->regs[VCPU_REGS_RAX];
2130 tss->cx = c->regs[VCPU_REGS_RCX];
2131 tss->dx = c->regs[VCPU_REGS_RDX];
2132 tss->bx = c->regs[VCPU_REGS_RBX];
2133 tss->sp = c->regs[VCPU_REGS_RSP];
2134 tss->bp = c->regs[VCPU_REGS_RBP];
2135 tss->si = c->regs[VCPU_REGS_RSI];
2136 tss->di = c->regs[VCPU_REGS_RDI];
2137
2138 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2139 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2140 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2141 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2142 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2143}
2144
2145static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2146 struct x86_emulate_ops *ops,
2147 struct tss_segment_16 *tss)
2148{
2149 struct decode_cache *c = &ctxt->decode;
2150 int ret;
2151
2152 c->eip = tss->ip;
2153 ctxt->eflags = tss->flag | 2;
2154 c->regs[VCPU_REGS_RAX] = tss->ax;
2155 c->regs[VCPU_REGS_RCX] = tss->cx;
2156 c->regs[VCPU_REGS_RDX] = tss->dx;
2157 c->regs[VCPU_REGS_RBX] = tss->bx;
2158 c->regs[VCPU_REGS_RSP] = tss->sp;
2159 c->regs[VCPU_REGS_RBP] = tss->bp;
2160 c->regs[VCPU_REGS_RSI] = tss->si;
2161 c->regs[VCPU_REGS_RDI] = tss->di;
2162
2163 /*
2164 * SDM says that segment selectors are loaded before segment
2165 * descriptors
2166 */
2167 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2168 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2169 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2170 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2171 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2172
2173 /*
2174 * Now load segment descriptors. If fault happenes at this stage
2175 * it is handled in a context of new task
2176 */
2177 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2178 if (ret != X86EMUL_CONTINUE)
2179 return ret;
2180 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2181 if (ret != X86EMUL_CONTINUE)
2182 return ret;
2183 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2184 if (ret != X86EMUL_CONTINUE)
2185 return ret;
2186 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2187 if (ret != X86EMUL_CONTINUE)
2188 return ret;
2189 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2190 if (ret != X86EMUL_CONTINUE)
2191 return ret;
2192
2193 return X86EMUL_CONTINUE;
2194}
2195
2196static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2197 struct x86_emulate_ops *ops,
2198 u16 tss_selector, u16 old_tss_sel,
2199 ulong old_tss_base, struct desc_struct *new_desc)
2200{
2201 struct tss_segment_16 tss_seg;
2202 int ret;
2203 u32 err, new_tss_base = get_desc_base(new_desc);
2204
2205 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2206 &err);
2207 if (ret == X86EMUL_PROPAGATE_FAULT) {
2208 /* FIXME: need to provide precise fault address */
2209 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2210 return ret;
2211 }
2212
2213 save_state_to_tss16(ctxt, ops, &tss_seg);
2214
2215 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2216 &err);
2217 if (ret == X86EMUL_PROPAGATE_FAULT) {
2218 /* FIXME: need to provide precise fault address */
2219 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2220 return ret;
2221 }
2222
2223 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2224 &err);
2225 if (ret == X86EMUL_PROPAGATE_FAULT) {
2226 /* FIXME: need to provide precise fault address */
2227 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2228 return ret;
2229 }
2230
2231 if (old_tss_sel != 0xffff) {
2232 tss_seg.prev_task_link = old_tss_sel;
2233
2234 ret = ops->write_std(new_tss_base,
2235 &tss_seg.prev_task_link,
2236 sizeof tss_seg.prev_task_link,
2237 ctxt->vcpu, &err);
2238 if (ret == X86EMUL_PROPAGATE_FAULT) {
2239 /* FIXME: need to provide precise fault address */
2240 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2241 return ret;
2242 }
2243 }
2244
2245 return load_state_from_tss16(ctxt, ops, &tss_seg);
2246}
2247
2248static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2249 struct x86_emulate_ops *ops,
2250 struct tss_segment_32 *tss)
2251{
2252 struct decode_cache *c = &ctxt->decode;
2253
2254 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2255 tss->eip = c->eip;
2256 tss->eflags = ctxt->eflags;
2257 tss->eax = c->regs[VCPU_REGS_RAX];
2258 tss->ecx = c->regs[VCPU_REGS_RCX];
2259 tss->edx = c->regs[VCPU_REGS_RDX];
2260 tss->ebx = c->regs[VCPU_REGS_RBX];
2261 tss->esp = c->regs[VCPU_REGS_RSP];
2262 tss->ebp = c->regs[VCPU_REGS_RBP];
2263 tss->esi = c->regs[VCPU_REGS_RSI];
2264 tss->edi = c->regs[VCPU_REGS_RDI];
2265
2266 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2267 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2268 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2269 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2270 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2271 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2272 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2273}
2274
2275static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2276 struct x86_emulate_ops *ops,
2277 struct tss_segment_32 *tss)
2278{
2279 struct decode_cache *c = &ctxt->decode;
2280 int ret;
2281
Gleb Natapov0f122442010-04-28 19:15:31 +03002282 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
2283 kvm_inject_gp(ctxt->vcpu, 0);
2284 return X86EMUL_PROPAGATE_FAULT;
2285 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 c->eip = tss->eip;
2287 ctxt->eflags = tss->eflags | 2;
2288 c->regs[VCPU_REGS_RAX] = tss->eax;
2289 c->regs[VCPU_REGS_RCX] = tss->ecx;
2290 c->regs[VCPU_REGS_RDX] = tss->edx;
2291 c->regs[VCPU_REGS_RBX] = tss->ebx;
2292 c->regs[VCPU_REGS_RSP] = tss->esp;
2293 c->regs[VCPU_REGS_RBP] = tss->ebp;
2294 c->regs[VCPU_REGS_RSI] = tss->esi;
2295 c->regs[VCPU_REGS_RDI] = tss->edi;
2296
2297 /*
2298 * SDM says that segment selectors are loaded before segment
2299 * descriptors
2300 */
2301 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2302 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2303 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2304 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2305 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2306 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2307 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2308
2309 /*
2310 * Now load segment descriptors. If fault happenes at this stage
2311 * it is handled in a context of new task
2312 */
2313 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2314 if (ret != X86EMUL_CONTINUE)
2315 return ret;
2316 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2317 if (ret != X86EMUL_CONTINUE)
2318 return ret;
2319 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2320 if (ret != X86EMUL_CONTINUE)
2321 return ret;
2322 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2323 if (ret != X86EMUL_CONTINUE)
2324 return ret;
2325 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2326 if (ret != X86EMUL_CONTINUE)
2327 return ret;
2328 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2329 if (ret != X86EMUL_CONTINUE)
2330 return ret;
2331 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2332 if (ret != X86EMUL_CONTINUE)
2333 return ret;
2334
2335 return X86EMUL_CONTINUE;
2336}
2337
2338static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2339 struct x86_emulate_ops *ops,
2340 u16 tss_selector, u16 old_tss_sel,
2341 ulong old_tss_base, struct desc_struct *new_desc)
2342{
2343 struct tss_segment_32 tss_seg;
2344 int ret;
2345 u32 err, new_tss_base = get_desc_base(new_desc);
2346
2347 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2348 &err);
2349 if (ret == X86EMUL_PROPAGATE_FAULT) {
2350 /* FIXME: need to provide precise fault address */
2351 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2352 return ret;
2353 }
2354
2355 save_state_to_tss32(ctxt, ops, &tss_seg);
2356
2357 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2358 &err);
2359 if (ret == X86EMUL_PROPAGATE_FAULT) {
2360 /* FIXME: need to provide precise fault address */
2361 kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err);
2362 return ret;
2363 }
2364
2365 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2366 &err);
2367 if (ret == X86EMUL_PROPAGATE_FAULT) {
2368 /* FIXME: need to provide precise fault address */
2369 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2370 return ret;
2371 }
2372
2373 if (old_tss_sel != 0xffff) {
2374 tss_seg.prev_task_link = old_tss_sel;
2375
2376 ret = ops->write_std(new_tss_base,
2377 &tss_seg.prev_task_link,
2378 sizeof tss_seg.prev_task_link,
2379 ctxt->vcpu, &err);
2380 if (ret == X86EMUL_PROPAGATE_FAULT) {
2381 /* FIXME: need to provide precise fault address */
2382 kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err);
2383 return ret;
2384 }
2385 }
2386
2387 return load_state_from_tss32(ctxt, ops, &tss_seg);
2388}
2389
2390static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002391 struct x86_emulate_ops *ops,
2392 u16 tss_selector, int reason,
2393 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394{
2395 struct desc_struct curr_tss_desc, next_tss_desc;
2396 int ret;
2397 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2398 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002399 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002400 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401
2402 /* FIXME: old_tss_base == ~0 ? */
2403
2404 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2405 if (ret != X86EMUL_CONTINUE)
2406 return ret;
2407 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2408 if (ret != X86EMUL_CONTINUE)
2409 return ret;
2410
2411 /* FIXME: check that next_tss_desc is tss */
2412
2413 if (reason != TASK_SWITCH_IRET) {
2414 if ((tss_selector & 3) > next_tss_desc.dpl ||
2415 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
2416 kvm_inject_gp(ctxt->vcpu, 0);
2417 return X86EMUL_PROPAGATE_FAULT;
2418 }
2419 }
2420
Gleb Natapovceffb452010-03-18 15:20:19 +02002421 desc_limit = desc_limit_scaled(&next_tss_desc);
2422 if (!next_tss_desc.p ||
2423 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2424 desc_limit < 0x2b)) {
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425 kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR,
2426 tss_selector & 0xfffc);
2427 return X86EMUL_PROPAGATE_FAULT;
2428 }
2429
2430 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2431 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2432 write_segment_descriptor(ctxt, ops, old_tss_sel,
2433 &curr_tss_desc);
2434 }
2435
2436 if (reason == TASK_SWITCH_IRET)
2437 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2438
2439 /* set back link to prev task only if NT bit is set in eflags
2440 note that old_tss_sel is not used afetr this point */
2441 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2442 old_tss_sel = 0xffff;
2443
2444 if (next_tss_desc.type & 8)
2445 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2446 old_tss_base, &next_tss_desc);
2447 else
2448 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2449 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002450 if (ret != X86EMUL_CONTINUE)
2451 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002452
2453 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2454 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2455
2456 if (reason != TASK_SWITCH_IRET) {
2457 next_tss_desc.type |= (1 << 1); /* set busy flag */
2458 write_segment_descriptor(ctxt, ops, tss_selector,
2459 &next_tss_desc);
2460 }
2461
2462 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2463 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2464 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2465
Jan Kiszkae269fb22010-04-14 15:51:09 +02002466 if (has_error_code) {
2467 struct decode_cache *c = &ctxt->decode;
2468
2469 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2470 c->lock_prefix = 0;
2471 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002472 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002473 }
2474
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475 return ret;
2476}
2477
2478int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
2479 struct x86_emulate_ops *ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002480 u16 tss_selector, int reason,
2481 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002482{
2483 struct decode_cache *c = &ctxt->decode;
2484 int rc;
2485
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002487 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488
Jan Kiszkae269fb22010-04-14 15:51:09 +02002489 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2490 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491
2492 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002493 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002494 if (rc == X86EMUL_CONTINUE)
2495 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 }
2497
Gleb Natapov19d04432010-04-15 12:29:50 +03002498 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499}
2500
Gleb Natapova682e352010-03-18 15:20:21 +02002501static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002502 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002503{
2504 struct decode_cache *c = &ctxt->decode;
2505 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2506
Gleb Natapovd9271122010-03-18 15:20:22 +02002507 register_address_increment(c, &c->regs[reg], df * op->bytes);
2508 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002509}
2510
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002511int
Laurent Vivier1be3aa42007-09-18 11:27:27 +02002512x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002513{
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002514 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002515 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002516 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002517 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002518
Glauber Costa310b5d32009-05-12 16:21:06 -04002519 ctxt->interruptibility = 0;
Gleb Natapov9de41572010-04-28 19:15:22 +03002520 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002521
Gleb Natapov11616242010-02-11 14:43:14 +02002522 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
2523 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2524 goto done;
2525 }
2526
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002527 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002528 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002529 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2530 goto done;
2531 }
2532
Gleb Natapove92805a2010-02-10 14:21:35 +02002533 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002534 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapove92805a2010-02-10 14:21:35 +02002535 kvm_inject_gp(ctxt->vcpu, 0);
2536 goto done;
2537 }
2538
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002539 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002540 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002541 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002542 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002543 string_done:
2544 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002545 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002546 goto done;
2547 }
2548 /* The second termination condition only applies for REPE
2549 * and REPNE. Test if the repeat string operation prefix is
2550 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2551 * corresponding termination condition according to:
2552 * - if REPE/REPZ and ZF = 0 then done
2553 * - if REPNE/REPNZ and ZF = 1 then done
2554 */
2555 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002556 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002557 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002558 ((ctxt->eflags & EFLG_ZF) == 0))
2559 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002560 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002561 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2562 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002563 }
Gleb Natapov063db062010-03-18 15:20:06 +02002564 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002565 }
2566
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002567 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002568 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002569 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002570 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002571 goto done;
2572 c->src.orig_val = c->src.val;
2573 }
2574
Gleb Natapove35b7b92010-02-25 16:36:42 +02002575 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002576 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2577 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002578 if (rc != X86EMUL_CONTINUE)
2579 goto done;
2580 }
2581
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002582 if ((c->d & DstMask) == ImplicitOps)
2583 goto special_insn;
2584
2585
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002586 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2587 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002588 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2589 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002590 if (rc != X86EMUL_CONTINUE)
2591 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002592 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002593 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002594
Avi Kivity018a98d2007-11-27 19:30:56 +02002595special_insn:
2596
Laurent Viviere4e03de2007-09-18 11:52:50 +02002597 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 goto twobyte_insn;
2599
Laurent Viviere4e03de2007-09-18 11:52:50 +02002600 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 case 0x00 ... 0x05:
2602 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002603 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002605 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002606 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002607 break;
2608 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002609 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002610 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002611 goto done;
2612 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 case 0x08 ... 0x0d:
2614 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002615 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002617 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002618 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002619 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 case 0x10 ... 0x15:
2621 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002622 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002624 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002625 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002626 break;
2627 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002628 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002629 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002630 goto done;
2631 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 case 0x18 ... 0x1d:
2633 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002634 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002636 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002637 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002638 break;
2639 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002640 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002641 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002642 goto done;
2643 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002644 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002646 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 break;
2648 case 0x28 ... 0x2d:
2649 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002650 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 break;
2652 case 0x30 ... 0x35:
2653 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002654 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 break;
2656 case 0x38 ... 0x3d:
2657 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002658 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002660 case 0x40 ... 0x47: /* inc r16/r32 */
2661 emulate_1op("inc", c->dst, ctxt->eflags);
2662 break;
2663 case 0x48 ... 0x4f: /* dec r16/r32 */
2664 emulate_1op("dec", c->dst, ctxt->eflags);
2665 break;
2666 case 0x50 ... 0x57: /* push reg */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002667 emulate_push(ctxt, ops);
Avi Kivity33615aa2007-10-31 11:15:56 +02002668 break;
2669 case 0x58 ... 0x5f: /* pop reg */
2670 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002671 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002672 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002673 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002674 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002675 case 0x60: /* pusha */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002676 emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002677 break;
2678 case 0x61: /* popa */
2679 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002680 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002681 goto done;
2682 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002684 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002686 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 break;
Avi Kivity91ed7a02008-05-29 14:38:38 +03002688 case 0x68: /* push imm */
Avi Kivity018a98d2007-11-27 19:30:56 +02002689 case 0x6a: /* push imm8 */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002690 emulate_push(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02002691 break;
2692 case 0x6c: /* insb */
2693 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002694 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002695 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002696 c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002697 kvm_inject_gp(ctxt->vcpu, 0);
2698 goto done;
2699 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002700 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2701 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002702 goto done; /* IO is needed, skip writeback */
2703 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002704 case 0x6e: /* outsb */
2705 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002706 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002707 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002708 c->src.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002709 kvm_inject_gp(ctxt->vcpu, 0);
2710 goto done;
2711 }
Gleb Natapov79729952010-03-18 15:20:24 +02002712 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2713 &c->src.val, 1, ctxt->vcpu);
2714
2715 c->dst.type = OP_NONE; /* nothing to writeback */
2716 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002717 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002718 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002719 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002720 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002722 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 case 0:
2724 goto add;
2725 case 1:
2726 goto or;
2727 case 2:
2728 goto adc;
2729 case 3:
2730 goto sbb;
2731 case 4:
2732 goto and;
2733 case 5:
2734 goto sub;
2735 case 6:
2736 goto xor;
2737 case 7:
2738 goto cmp;
2739 }
2740 break;
2741 case 0x84 ... 0x85:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002742 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 break;
2744 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002745 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002747 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002749 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 break;
2751 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002752 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 break;
2754 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002755 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 break; /* 64b reg: zero-extend */
2757 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002758 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 break;
2760 }
2761 /*
2762 * Write back the memory destination with implicit LOCK
2763 * prefix.
2764 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002765 c->dst.val = c->src.val;
2766 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002769 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002770 case 0x8c: /* mov r/m, sreg */
2771 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002772 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2773 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002774 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002775 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002776 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002777 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002778 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002779 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002780 case 0x8e: { /* mov seg, r/m16 */
2781 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002782
2783 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002784
Gleb Natapovc6975182010-02-18 12:15:01 +02002785 if (c->modrm_reg == VCPU_SREG_CS ||
2786 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002787 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
2788 goto done;
2789 }
2790
Glauber Costa310b5d32009-05-12 16:21:06 -04002791 if (c->modrm_reg == VCPU_SREG_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002792 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS);
Glauber Costa310b5d32009-05-12 16:21:06 -04002793
Gleb Natapov2e873022010-03-18 15:20:18 +02002794 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002795
2796 c->dst.type = OP_NONE; /* Disable writeback. */
2797 break;
2798 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002800 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002801 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002804 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002805 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2806 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002807 break;
2808 }
2809 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002810 c->src.type = OP_REG;
2811 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002812 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2813 c->src.val = *(c->src.ptr);
2814 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002815 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002816 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002817 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002818 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002819 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002820 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002821 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002822 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002823 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2824 if (rc != X86EMUL_CONTINUE)
2825 goto done;
2826 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002827 case 0xa0 ... 0xa1: /* mov */
2828 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
2829 c->dst.val = c->src.val;
2830 break;
2831 case 0xa2 ... 0xa3: /* mov */
2832 c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
2833 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002835 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002837 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002838 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002839 goto cmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002841 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 break;
2843 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002844 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 case 0xae ... 0xaf: /* scas */
2846 DPRINTF("Urk! I don't handle SCAS.\n");
2847 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002848 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002849 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002850 case 0xc0 ... 0xc1:
2851 emulate_grp2(ctxt);
2852 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002853 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002854 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002855 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002856 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002857 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002858 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2859 mov:
2860 c->dst.val = c->src.val;
2861 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002862 case 0xcb: /* ret far */
2863 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002864 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002865 goto done;
2866 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002867 case 0xd0 ... 0xd1: /* Grp2 */
2868 c->src.val = 1;
2869 emulate_grp2(ctxt);
2870 break;
2871 case 0xd2 ... 0xd3: /* Grp2 */
2872 c->src.val = c->regs[VCPU_REGS_RCX];
2873 emulate_grp2(ctxt);
2874 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002875 case 0xe4: /* inb */
2876 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002877 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002878 case 0xe6: /* outb */
2879 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002880 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002881 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002882 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002883 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002884 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002885 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002886 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002887 }
2888 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002889 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002890 case 0xea: { /* jmp far */
2891 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02002892 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002893 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2894
2895 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002896 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002897
Gleb Natapov414e6272010-04-28 19:15:26 +03002898 c->eip = 0;
2899 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002900 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03002901 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002902 case 0xeb:
2903 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08002904 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02002905 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002906 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002907 case 0xec: /* in al,dx */
2908 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002909 c->src.val = c->regs[VCPU_REGS_RDX];
2910 do_io_in:
2911 c->dst.bytes = min(c->dst.bytes, 4u);
2912 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002913 kvm_inject_gp(ctxt->vcpu, 0);
2914 goto done;
2915 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002916 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
2917 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002918 goto done; /* IO is needed */
2919 break;
2920 case 0xee: /* out al,dx */
2921 case 0xef: /* out (e/r)ax,dx */
2922 c->src.val = c->regs[VCPU_REGS_RDX];
2923 do_io_out:
2924 c->dst.bytes = min(c->dst.bytes, 4u);
2925 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
2926 kvm_inject_gp(ctxt->vcpu, 0);
2927 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002928 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002929 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
2930 ctxt->vcpu);
2931 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002932 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002933 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002934 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03002935 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002936 case 0xf5: /* cmc */
2937 /* complement carry flag from eflags reg */
2938 ctxt->eflags ^= EFLG_CF;
2939 c->dst.type = OP_NONE; /* Disable writeback. */
2940 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002941 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02002942 if (!emulate_grp3(ctxt, ops))
2943 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02002944 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002945 case 0xf8: /* clc */
2946 ctxt->eflags &= ~EFLG_CF;
2947 c->dst.type = OP_NONE; /* Disable writeback. */
2948 break;
2949 case 0xfa: /* cli */
Gleb Natapov9c537242010-03-18 15:20:05 +02002950 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002951 kvm_inject_gp(ctxt->vcpu, 0);
2952 else {
2953 ctxt->eflags &= ~X86_EFLAGS_IF;
2954 c->dst.type = OP_NONE; /* Disable writeback. */
2955 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002956 break;
2957 case 0xfb: /* sti */
Gleb Natapov9c537242010-03-18 15:20:05 +02002958 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002959 kvm_inject_gp(ctxt->vcpu, 0);
2960 else {
Jan Kiszka48005f62010-02-19 19:38:07 +01002961 toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002962 ctxt->eflags |= X86_EFLAGS_IF;
2963 c->dst.type = OP_NONE; /* Disable writeback. */
2964 }
Avi Kivity111de5d2007-11-27 19:14:21 +02002965 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03002966 case 0xfc: /* cld */
2967 ctxt->eflags &= ~EFLG_DF;
2968 c->dst.type = OP_NONE; /* Disable writeback. */
2969 break;
2970 case 0xfd: /* std */
2971 ctxt->eflags |= EFLG_DF;
2972 c->dst.type = OP_NONE; /* Disable writeback. */
2973 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002974 case 0xfe: /* Grp4 */
2975 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02002976 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002977 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002978 goto done;
2979 break;
Gleb Natapovea798492010-02-25 16:36:43 +02002980 case 0xff: /* Grp5 */
2981 if (c->modrm_reg == 5)
2982 goto jump_far;
2983 goto grp45;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 }
Avi Kivity018a98d2007-11-27 19:30:56 +02002985
2986writeback:
2987 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002988 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02002989 goto done;
2990
Gleb Natapov5cd21912010-03-18 15:20:26 +02002991 /*
2992 * restore dst type in case the decoding will be reused
2993 * (happens for string instruction )
2994 */
2995 c->dst.type = saved_dst_type;
2996
Gleb Natapova682e352010-03-18 15:20:21 +02002997 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03002998 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
2999 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003000
3001 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003002 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3003 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003004
Gleb Natapov5cd21912010-03-18 15:20:26 +02003005 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003006 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003007 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003008 /*
3009 * Re-enter guest when pio read ahead buffer is empty or,
3010 * if it is not used, after each 1024 iteration.
3011 */
3012 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3013 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003014 ctxt->restart = false;
3015 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003016 /*
3017 * reset read cache here in case string instruction is restared
3018 * without decoding
3019 */
3020 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003021 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003022
3023done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003024 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025
3026twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003027 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003029 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 u16 size;
3031 unsigned long address;
3032
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003033 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003034 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003035 goto cannot_emulate;
3036
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003037 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003038 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003039 goto done;
3040
Avi Kivity33e38852008-05-21 15:34:25 +03003041 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003042 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003043 /* Disable writeback. */
3044 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003045 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003047 rc = read_descriptor(ctxt, ops, c->src.ptr,
3048 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003049 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 goto done;
3051 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003052 /* Disable writeback. */
3053 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003055 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003056 if (c->modrm_mod == 3) {
3057 switch (c->modrm_rm) {
3058 case 1:
3059 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003060 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003061 goto done;
3062 break;
3063 default:
3064 goto cannot_emulate;
3065 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003066 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003067 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003068 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003069 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003070 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003071 goto done;
3072 realmode_lidt(ctxt->vcpu, size, address);
3073 }
Avi Kivity16286d02008-04-14 14:40:50 +03003074 /* Disable writeback. */
3075 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 break;
3077 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003078 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003079 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 break;
3081 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003082 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3083 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003084 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003086 case 5: /* not defined */
3087 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3088 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003090 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003091 /* Disable writeback. */
3092 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 break;
3094 default:
3095 goto cannot_emulate;
3096 }
3097 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003098 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003099 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003100 if (rc != X86EMUL_CONTINUE)
3101 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003102 else
3103 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003104 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003105 case 0x06:
3106 emulate_clts(ctxt->vcpu);
3107 c->dst.type = OP_NONE;
3108 break;
3109 case 0x08: /* invd */
3110 case 0x09: /* wbinvd */
3111 case 0x0d: /* GrpP (prefetch) */
3112 case 0x18: /* Grp16 (prefetch/nop) */
3113 c->dst.type = OP_NONE;
3114 break;
3115 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003116 switch (c->modrm_reg) {
3117 case 1:
3118 case 5 ... 7:
3119 case 9 ... 15:
3120 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3121 goto done;
3122 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003123 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003124 c->dst.type = OP_NONE; /* no writeback */
3125 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003127 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3128 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3129 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3130 goto done;
3131 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003132 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003133 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003135 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003136 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
3137 kvm_inject_gp(ctxt->vcpu, 0);
3138 goto done;
3139 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003140 c->dst.type = OP_NONE;
3141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003143 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3144 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
3145 kvm_queue_exception(ctxt->vcpu, UD_VECTOR);
3146 goto done;
3147 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003148
Gleb Natapov338dbc92010-04-28 19:15:32 +03003149 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3150 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3151 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3152 /* #UD condition is already handled by the code above */
3153 kvm_inject_gp(ctxt->vcpu, 0);
3154 goto done;
3155 }
3156
Laurent Viviera01af5e2007-09-24 11:10:56 +02003157 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003159 case 0x30:
3160 /* wrmsr */
3161 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3162 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003163 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003164 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003165 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003166 }
3167 rc = X86EMUL_CONTINUE;
3168 c->dst.type = OP_NONE;
3169 break;
3170 case 0x32:
3171 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003172 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003173 kvm_inject_gp(ctxt->vcpu, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003174 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003175 } else {
3176 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3177 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3178 }
3179 rc = X86EMUL_CONTINUE;
3180 c->dst.type = OP_NONE;
3181 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003182 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003183 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003184 if (rc != X86EMUL_CONTINUE)
3185 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003186 else
3187 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003188 break;
3189 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003190 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003191 if (rc != X86EMUL_CONTINUE)
3192 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003193 else
3194 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003195 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003197 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003198 if (!test_cc(c->b, ctxt->eflags))
3199 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003201 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003202 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003203 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003204 c->dst.type = OP_NONE;
3205 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003206 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003207 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003208 break;
3209 case 0xa1: /* pop fs */
3210 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003211 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003212 goto done;
3213 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003214 case 0xa3:
3215 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003216 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003217 /* only subword offset */
3218 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003219 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003220 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003221 case 0xa4: /* shld imm8, r, r/m */
3222 case 0xa5: /* shld cl, r, r/m */
3223 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3224 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003225 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003226 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003227 break;
3228 case 0xa9: /* pop gs */
3229 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003230 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003231 goto done;
3232 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003233 case 0xab:
3234 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003235 /* only subword offset */
3236 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003237 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003238 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003239 case 0xac: /* shrd imm8, r, r/m */
3240 case 0xad: /* shrd cl, r, r/m */
3241 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3242 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003243 case 0xae: /* clflush */
3244 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 case 0xb0 ... 0xb1: /* cmpxchg */
3246 /*
3247 * Save real source value, then compare EAX against
3248 * destination.
3249 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003250 c->src.orig_val = c->src.val;
3251 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003252 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3253 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003255 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 } else {
3257 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003258 c->dst.type = OP_REG;
3259 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 }
3261 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 case 0xb3:
3263 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003264 /* only subword offset */
3265 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003266 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003269 c->dst.bytes = c->op_bytes;
3270 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3271 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003274 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 case 0:
3276 goto bt;
3277 case 1:
3278 goto bts;
3279 case 2:
3280 goto btr;
3281 case 3:
3282 goto btc;
3283 }
3284 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003285 case 0xbb:
3286 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003287 /* only subword offset */
3288 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003289 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003290 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003292 c->dst.bytes = c->op_bytes;
3293 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3294 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003296 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003297 c->dst.bytes = c->op_bytes;
3298 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3299 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003300 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003302 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003303 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003304 goto done;
3305 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 }
3307 goto writeback;
3308
3309cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003310 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 return -1;
3312}