blob: 3d0e5ac3a0c1826438f4e8b01dfb115b52a54403 [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
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b122010-07-29 15:11:49 +030050#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080051/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcReg (1<<4) /* Register operand. */
54#define SrcMem (2<<4) /* Memory operand. */
55#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57#define SrcImm (5<<4) /* Immediate operand. */
58#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010059#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030060#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivity46561642011-04-24 14:09:59 +030076#define GroupMask (7<<14) /* Opcode uses one of the group mechanisms */
Avi Kivitye09d0822008-01-18 12:38:59 +020077#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
Avi Kivity46561642011-04-24 14:09:59 +030078#define GroupDual (2<<14) /* Alternate decoding of mod == 3 */
79#define Prefix (3<<14) /* Instruction varies with 66/f2/f3 prefix */
80#define RMExt (4<<14) /* Opcode extension in ModRM r/m if mod == 3 */
Avi Kivity12537912011-03-29 11:41:27 +020081#define Sse (1<<17) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030082/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020083#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020084#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030085#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030086#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030087#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020088#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020089#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030090#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091/* Source 2 operand type */
92#define Src2None (0<<29)
93#define Src2CL (1<<29)
94#define Src2ImmByte (2<<29)
95#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030096#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010097#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
Avi Kivityd0e53322010-07-29 15:11:54 +030099#define X2(x...) x, x
100#define X3(x...) X2(x), x
101#define X4(x...) X2(x), X2(x)
102#define X5(x...) X4(x), x
103#define X6(x...) X4(x), X2(x)
104#define X7(x...) X4(x), X3(x)
105#define X8(x...) X4(x), X4(x)
106#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300107
Avi Kivityd65b1de2010-07-29 15:11:35 +0300108struct opcode {
109 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200110 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300111 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300112 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300113 struct opcode *group;
114 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200115 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300116 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200117 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300118};
119
120struct group_dual {
121 struct opcode mod012[8];
122 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300123};
124
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200125struct gprefix {
126 struct opcode pfx_no;
127 struct opcode pfx_66;
128 struct opcode pfx_f2;
129 struct opcode pfx_f3;
130};
131
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200133#define EFLG_ID (1<<21)
134#define EFLG_VIP (1<<20)
135#define EFLG_VIF (1<<19)
136#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200137#define EFLG_VM (1<<17)
138#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200139#define EFLG_IOPL (3<<12)
140#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141#define EFLG_OF (1<<11)
142#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200143#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200144#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145#define EFLG_SF (1<<7)
146#define EFLG_ZF (1<<6)
147#define EFLG_AF (1<<4)
148#define EFLG_PF (1<<2)
149#define EFLG_CF (1<<0)
150
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300151#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
152#define EFLG_RESERVED_ONE_MASK 2
153
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154/*
155 * Instruction emulation:
156 * Most instructions are emulated directly via a fragment of inline assembly
157 * code. This allows us to save/restore EFLAGS and thus very easily pick up
158 * any modified flags.
159 */
160
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800161#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162#define _LO32 "k" /* force 32-bit operand */
163#define _STK "%%rsp" /* stack pointer */
164#elif defined(__i386__)
165#define _LO32 "" /* force 32-bit operand */
166#define _STK "%%esp" /* stack pointer */
167#endif
168
169/*
170 * These EFLAGS bits are restored from saved value during emulation, and
171 * any changes are written back to the saved value after emulation.
172 */
173#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
174
175/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200176#define _PRE_EFLAGS(_sav, _msk, _tmp) \
177 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
178 "movl %"_sav",%"_LO32 _tmp"; " \
179 "push %"_tmp"; " \
180 "push %"_tmp"; " \
181 "movl %"_msk",%"_LO32 _tmp"; " \
182 "andl %"_LO32 _tmp",("_STK"); " \
183 "pushf; " \
184 "notl %"_LO32 _tmp"; " \
185 "andl %"_LO32 _tmp",("_STK"); " \
186 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
187 "pop %"_tmp"; " \
188 "orl %"_LO32 _tmp",("_STK"); " \
189 "popf; " \
190 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191
192/* After executing instruction: write-back necessary bits in EFLAGS. */
193#define _POST_EFLAGS(_sav, _msk, _tmp) \
194 /* _sav |= EFLAGS & _msk; */ \
195 "pushf; " \
196 "pop %"_tmp"; " \
197 "andl %"_msk",%"_LO32 _tmp"; " \
198 "orl %"_LO32 _tmp",%"_sav"; "
199
Avi Kivitydda96d82008-11-26 15:14:10 +0200200#ifdef CONFIG_X86_64
201#define ON64(x) x
202#else
203#define ON64(x)
204#endif
205
Avi Kivityb3b3d252010-08-16 17:49:52 +0300206#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200207 do { \
208 __asm__ __volatile__ ( \
209 _PRE_EFLAGS("0", "4", "2") \
210 _op _suffix " %"_x"3,%1; " \
211 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300212 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200213 "=&r" (_tmp) \
214 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200215 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200216
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218/* Raw emulation: instruction has two explicit operands. */
219#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200220 do { \
221 unsigned long _tmp; \
222 \
223 switch ((_dst).bytes) { \
224 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300225 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200226 break; \
227 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300228 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 break; \
230 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300231 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200232 break; \
233 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 } while (0)
235
236#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
237 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200238 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400239 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300241 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 break; \
243 default: \
244 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
245 _wx, _wy, _lx, _ly, _qx, _qy); \
246 break; \
247 } \
248 } while (0)
249
250/* Source operand is byte-sized and may be restricted to just %cl. */
251#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
252 __emulate_2op(_op, _src, _dst, _eflags, \
253 "b", "c", "b", "c", "b", "c", "b", "c")
254
255/* Source operand is byte, word, long or quad sized. */
256#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
257 __emulate_2op(_op, _src, _dst, _eflags, \
258 "b", "q", "w", "r", _LO32, "r", "", "r")
259
260/* Source operand is word, long or quad sized. */
261#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
262 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
263 "w", "r", _LO32, "r", "", "r")
264
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100265/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity72952612011-04-20 13:12:27 +0300266#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
267 do { \
268 unsigned long _tmp; \
269 _type _clv = (_cl).val; \
270 _type _srcv = (_src).val; \
271 _type _dstv = (_dst).val; \
272 \
273 __asm__ __volatile__ ( \
274 _PRE_EFLAGS("0", "5", "2") \
275 _op _suffix " %4,%1 \n" \
276 _POST_EFLAGS("0", "5", "2") \
277 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
278 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
279 ); \
280 \
281 (_cl).val = (unsigned long) _clv; \
282 (_src).val = (unsigned long) _srcv; \
283 (_dst).val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100284 } while (0)
285
Avi Kivity72952612011-04-20 13:12:27 +0300286#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
287 do { \
288 switch ((_dst).bytes) { \
289 case 2: \
290 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
291 "w", unsigned short); \
292 break; \
293 case 4: \
294 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
295 "l", unsigned int); \
296 break; \
297 case 8: \
298 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
299 "q", unsigned long)); \
300 break; \
301 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100302 } while (0)
303
Avi Kivitydda96d82008-11-26 15:14:10 +0200304#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 do { \
306 unsigned long _tmp; \
307 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200308 __asm__ __volatile__ ( \
309 _PRE_EFLAGS("0", "3", "2") \
310 _op _suffix " %1; " \
311 _POST_EFLAGS("0", "3", "2") \
312 : "=m" (_eflags), "+m" ((_dst).val), \
313 "=&r" (_tmp) \
314 : "i" (EFLAGS_MASK)); \
315 } while (0)
316
317/* Instruction has only one explicit operand (no source operand). */
318#define emulate_1op(_op, _dst, _eflags) \
319 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400320 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200321 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
322 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
323 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
324 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 } \
326 } while (0)
327
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300328#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
329 do { \
330 unsigned long _tmp; \
331 \
332 __asm__ __volatile__ ( \
333 _PRE_EFLAGS("0", "4", "1") \
334 _op _suffix " %5; " \
335 _POST_EFLAGS("0", "4", "1") \
336 : "=m" (_eflags), "=&r" (_tmp), \
337 "+a" (_rax), "+d" (_rdx) \
338 : "i" (EFLAGS_MASK), "m" ((_src).val), \
339 "a" (_rax), "d" (_rdx)); \
340 } while (0)
341
Avi Kivityf6b35972010-08-26 11:59:00 +0300342#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
343 do { \
344 unsigned long _tmp; \
345 \
346 __asm__ __volatile__ ( \
347 _PRE_EFLAGS("0", "5", "1") \
348 "1: \n\t" \
349 _op _suffix " %6; " \
350 "2: \n\t" \
351 _POST_EFLAGS("0", "5", "1") \
352 ".pushsection .fixup,\"ax\" \n\t" \
353 "3: movb $1, %4 \n\t" \
354 "jmp 2b \n\t" \
355 ".popsection \n\t" \
356 _ASM_EXTABLE(1b, 3b) \
357 : "=m" (_eflags), "=&r" (_tmp), \
358 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
359 : "i" (EFLAGS_MASK), "m" ((_src).val), \
360 "a" (_rax), "d" (_rdx)); \
361 } while (0)
362
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300363/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivity72952612011-04-20 13:12:27 +0300364#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
365 do { \
366 switch((_src).bytes) { \
367 case 1: \
368 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
369 _eflags, "b"); \
370 break; \
371 case 2: \
372 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
373 _eflags, "w"); \
374 break; \
375 case 4: \
376 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
377 _eflags, "l"); \
378 break; \
379 case 8: \
380 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
381 _eflags, "q")); \
382 break; \
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300383 } \
384 } while (0)
385
Avi Kivityf6b35972010-08-26 11:59:00 +0300386#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
387 do { \
388 switch((_src).bytes) { \
389 case 1: \
390 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
391 _eflags, "b", _ex); \
392 break; \
393 case 2: \
394 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
395 _eflags, "w", _ex); \
396 break; \
397 case 4: \
398 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
399 _eflags, "l", _ex); \
400 break; \
401 case 8: ON64( \
402 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
403 _eflags, "q", _ex)); \
404 break; \
405 } \
406 } while (0)
407
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408/* Fetch next part of the instruction being emulated. */
409#define insn_fetch(_type, _size, _eip) \
410({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200411 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200412 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 goto done; \
414 (_eip) += (_size); \
415 (_type)_x; \
416})
417
Avi Kivity72952612011-04-20 13:12:27 +0300418#define insn_fetch_arr(_arr, _size, _eip) \
Gleb Natapov414e6272010-04-28 19:15:26 +0300419({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
420 if (rc != X86EMUL_CONTINUE) \
421 goto done; \
422 (_eip) += (_size); \
423})
424
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200425static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
426 enum x86_intercept intercept,
427 enum x86_intercept_stage stage)
428{
429 struct x86_instruction_info info = {
430 .intercept = intercept,
431 .rep_prefix = ctxt->decode.rep_prefix,
432 .modrm_mod = ctxt->decode.modrm_mod,
433 .modrm_reg = ctxt->decode.modrm_reg,
434 .modrm_rm = ctxt->decode.modrm_rm,
435 .src_val = ctxt->decode.src.val64,
436 .src_bytes = ctxt->decode.src.bytes,
437 .dst_bytes = ctxt->decode.dst.bytes,
438 .ad_bytes = ctxt->decode.ad_bytes,
439 .next_rip = ctxt->eip,
440 };
441
Avi Kivity29535382011-04-20 13:37:53 +0300442 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200443}
444
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800445static inline unsigned long ad_mask(struct decode_cache *c)
446{
447 return (1UL << (c->ad_bytes << 3)) - 1;
448}
449
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800451static inline unsigned long
452address_mask(struct decode_cache *c, unsigned long reg)
453{
454 if (c->ad_bytes == sizeof(unsigned long))
455 return reg;
456 else
457 return reg & ad_mask(c);
458}
459
460static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200461register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800462{
Avi Kivity90de84f2010-11-17 15:28:21 +0200463 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800464}
465
Harvey Harrison7a9572752008-02-19 07:40:41 -0800466static inline void
467register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
468{
469 if (c->ad_bytes == sizeof(unsigned long))
470 *reg += inc;
471 else
472 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
473}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474
Harvey Harrison7a9572752008-02-19 07:40:41 -0800475static inline void jmp_rel(struct decode_cache *c, int rel)
476{
477 register_address_increment(c, &c->eip, rel);
478}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300479
Avi Kivity56697682011-04-03 14:08:51 +0300480static u32 desc_limit_scaled(struct desc_struct *desc)
481{
482 u32 limit = get_desc_limit(desc);
483
484 return desc->g ? (limit << 12) | 0xfff : limit;
485}
486
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487static void set_seg_override(struct decode_cache *c, int seg)
488{
489 c->has_seg_override = true;
490 c->seg_override = seg;
491}
492
Gleb Natapov79168fd2010-04-28 19:15:30 +0300493static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
494 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300495{
496 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
497 return 0;
498
Avi Kivity4bff1e862011-04-20 13:37:53 +0300499 return ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300500}
501
Avi Kivity90de84f2010-11-17 15:28:21 +0200502static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200503 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300504{
505 if (!c->has_seg_override)
506 return 0;
507
Avi Kivity90de84f2010-11-17 15:28:21 +0200508 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300509}
510
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200511static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
512 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300513{
Avi Kivityda9cb572010-11-22 17:53:21 +0200514 ctxt->exception.vector = vec;
515 ctxt->exception.error_code = error;
516 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200517 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300518}
519
Joerg Roedel3b88e412011-04-04 12:39:29 +0200520static int emulate_db(struct x86_emulate_ctxt *ctxt)
521{
522 return emulate_exception(ctxt, DB_VECTOR, 0, false);
523}
524
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300526{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300528}
529
Avi Kivity618ff152011-04-03 12:32:09 +0300530static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
531{
532 return emulate_exception(ctxt, SS_VECTOR, err, true);
533}
534
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300536{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300538}
539
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300541{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200542 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300543}
544
Avi Kivity34d1f492010-08-26 11:59:01 +0300545static int emulate_de(struct x86_emulate_ctxt *ctxt)
546{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300548}
549
Avi Kivity12537912011-03-29 11:41:27 +0200550static int emulate_nm(struct x86_emulate_ctxt *ctxt)
551{
552 return emulate_exception(ctxt, NM_VECTOR, 0, false);
553}
554
Avi Kivity1aa36612011-04-27 13:20:30 +0300555static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
556{
557 u16 selector;
558 struct desc_struct desc;
559
560 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
561 return selector;
562}
563
564static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
565 unsigned seg)
566{
567 u16 dummy;
568 u32 base3;
569 struct desc_struct desc;
570
571 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
572 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
573}
574
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400575static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300576 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400577 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300578 ulong *linear)
579{
580 struct decode_cache *c = &ctxt->decode;
Avi Kivity618ff152011-04-03 12:32:09 +0300581 struct desc_struct desc;
582 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300583 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300584 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300585 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300586 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300587
588 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300589 switch (ctxt->mode) {
590 case X86EMUL_MODE_REAL:
591 break;
592 case X86EMUL_MODE_PROT64:
593 if (((signed long)la << 16) >> 16 != la)
594 return emulate_gp(ctxt, 0);
595 break;
596 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300597 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
598 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300599 if (!usable)
600 goto bad;
601 /* code segment or read-only data segment */
602 if (((desc.type & 8) || !(desc.type & 2)) && write)
603 goto bad;
604 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400605 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300606 goto bad;
607 lim = desc_limit_scaled(&desc);
608 if ((desc.type & 8) || !(desc.type & 4)) {
609 /* expand-up segment */
610 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
611 goto bad;
612 } else {
613 /* exapand-down segment */
614 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
615 goto bad;
616 lim = desc.d ? 0xffffffff : 0xffff;
617 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
618 goto bad;
619 }
Avi Kivity717746e2011-04-20 13:37:53 +0300620 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300621 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300622 cpl = max(cpl, rpl);
623 if (!(desc.type & 8)) {
624 /* data segment */
625 if (cpl > desc.dpl)
626 goto bad;
627 } else if ((desc.type & 8) && !(desc.type & 4)) {
628 /* nonconforming code segment */
629 if (cpl != desc.dpl)
630 goto bad;
631 } else if ((desc.type & 8) && (desc.type & 4)) {
632 /* conforming code segment */
633 if (cpl < desc.dpl)
634 goto bad;
635 }
636 break;
637 }
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400638 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300639 la &= (u32)-1;
640 *linear = la;
641 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300642bad:
643 if (addr.seg == VCPU_SREG_SS)
644 return emulate_ss(ctxt, addr.seg);
645 else
646 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300647}
648
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400649static int linearize(struct x86_emulate_ctxt *ctxt,
650 struct segmented_address addr,
651 unsigned size, bool write,
652 ulong *linear)
653{
654 return __linearize(ctxt, addr, size, write, false, linear);
655}
656
657
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200658static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
659 struct segmented_address addr,
660 void *data,
661 unsigned size)
662{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200663 int rc;
664 ulong linear;
665
Avi Kivity83b87952011-04-03 11:31:19 +0300666 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200667 if (rc != X86EMUL_CONTINUE)
668 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300669 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200670}
671
Avi Kivity62266862007-11-20 13:15:52 +0200672static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
673 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300674 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200675{
676 struct fetch_cache *fc = &ctxt->decode.fetch;
677 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300678 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200679
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300680 if (eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400681 unsigned long linear;
682 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300683 cur_size = fc->end - fc->start;
684 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400685 rc = __linearize(ctxt, addr, size, false, true, &linear);
686 if (rc != X86EMUL_CONTINUE)
687 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300688 rc = ops->fetch(ctxt, linear, fc->data + cur_size,
689 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900690 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200691 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300692 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200693 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300694 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900695 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200696}
697
698static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
699 struct x86_emulate_ops *ops,
700 unsigned long eip, void *dest, unsigned size)
701{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900702 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200703
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200704 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200705 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200706 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200707 while (size--) {
708 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900709 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200710 return rc;
711 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900712 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200713}
714
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000715/*
716 * Given the 'reg' portion of a ModRM byte, and a register block, return a
717 * pointer into the block that addresses the relevant register.
718 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
719 */
720static void *decode_register(u8 modrm_reg, unsigned long *regs,
721 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722{
723 void *p;
724
725 p = &regs[modrm_reg];
726 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
727 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
728 return p;
729}
730
731static int read_descriptor(struct x86_emulate_ctxt *ctxt,
732 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200733 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 u16 *size, unsigned long *address, int op_bytes)
735{
736 int rc;
737
738 if (op_bytes == 2)
739 op_bytes = 3;
740 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200741 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900742 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200744 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200745 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 return rc;
747}
748
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300749static int test_cc(unsigned int condition, unsigned int flags)
750{
751 int rc = 0;
752
753 switch ((condition & 15) >> 1) {
754 case 0: /* o */
755 rc |= (flags & EFLG_OF);
756 break;
757 case 1: /* b/c/nae */
758 rc |= (flags & EFLG_CF);
759 break;
760 case 2: /* z/e */
761 rc |= (flags & EFLG_ZF);
762 break;
763 case 3: /* be/na */
764 rc |= (flags & (EFLG_CF|EFLG_ZF));
765 break;
766 case 4: /* s */
767 rc |= (flags & EFLG_SF);
768 break;
769 case 5: /* p/pe */
770 rc |= (flags & EFLG_PF);
771 break;
772 case 7: /* le/ng */
773 rc |= (flags & EFLG_ZF);
774 /* fall through */
775 case 6: /* l/nge */
776 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
777 break;
778 }
779
780 /* Odd condition identifiers (lsb == 1) have inverted sense. */
781 return (!!rc ^ (condition & 1));
782}
783
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300784static void fetch_register_operand(struct operand *op)
785{
786 switch (op->bytes) {
787 case 1:
788 op->val = *(u8 *)op->addr.reg;
789 break;
790 case 2:
791 op->val = *(u16 *)op->addr.reg;
792 break;
793 case 4:
794 op->val = *(u32 *)op->addr.reg;
795 break;
796 case 8:
797 op->val = *(u64 *)op->addr.reg;
798 break;
799 }
800}
801
Avi Kivity12537912011-03-29 11:41:27 +0200802static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
803{
804 ctxt->ops->get_fpu(ctxt);
805 switch (reg) {
806 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
807 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
808 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
809 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
810 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
811 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
812 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
813 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
814#ifdef CONFIG_X86_64
815 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
816 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
817 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
818 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
819 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
820 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
821 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
822 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
823#endif
824 default: BUG();
825 }
826 ctxt->ops->put_fpu(ctxt);
827}
828
829static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
830 int reg)
831{
832 ctxt->ops->get_fpu(ctxt);
833 switch (reg) {
834 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
835 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
836 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
837 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
838 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
839 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
840 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
841 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
842#ifdef CONFIG_X86_64
843 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
844 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
845 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
846 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
847 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
848 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
849 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
850 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
851#endif
852 default: BUG();
853 }
854 ctxt->ops->put_fpu(ctxt);
855}
856
857static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
858 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200859 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200860 int inhibit_bytereg)
861{
Avi Kivity33615aa2007-10-31 11:15:56 +0200862 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200863 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200864
865 if (!(c->d & ModRM))
866 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200867
868 if (c->d & Sse) {
869 op->type = OP_XMM;
870 op->bytes = 16;
871 op->addr.xmm = reg;
872 read_sse_reg(ctxt, &op->vec_val, reg);
873 return;
874 }
875
Avi Kivity3c118e22007-10-31 10:27:04 +0200876 op->type = OP_REG;
877 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300878 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200879 op->bytes = 1;
880 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300881 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200882 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200883 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300884 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200885 op->orig_val = op->val;
886}
887
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300889 struct x86_emulate_ops *ops,
890 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891{
892 struct decode_cache *c = &ctxt->decode;
893 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700894 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900895 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300896 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897
898 if (c->rex_prefix) {
899 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
900 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
901 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
902 }
903
904 c->modrm = insn_fetch(u8, 1, c->eip);
905 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
906 c->modrm_reg |= (c->modrm & 0x38) >> 3;
907 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300908 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200909
910 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300911 op->type = OP_REG;
912 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
913 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300914 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200915 if (c->d & Sse) {
916 op->type = OP_XMM;
917 op->bytes = 16;
918 op->addr.xmm = c->modrm_rm;
919 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
920 return rc;
921 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300922 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923 return rc;
924 }
925
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300926 op->type = OP_MEM;
927
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200928 if (c->ad_bytes == 2) {
929 unsigned bx = c->regs[VCPU_REGS_RBX];
930 unsigned bp = c->regs[VCPU_REGS_RBP];
931 unsigned si = c->regs[VCPU_REGS_RSI];
932 unsigned di = c->regs[VCPU_REGS_RDI];
933
934 /* 16-bit ModR/M decode. */
935 switch (c->modrm_mod) {
936 case 0:
937 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300938 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939 break;
940 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300941 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 break;
943 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300944 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200945 break;
946 }
947 switch (c->modrm_rm) {
948 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300949 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200950 break;
951 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300952 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200953 break;
954 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200956 break;
957 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300958 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200959 break;
960 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300961 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 break;
963 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300964 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 break;
966 case 6:
967 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300968 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 break;
970 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300971 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972 break;
973 }
974 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
975 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300976 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300977 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200978 } else {
979 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700980 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200981 sib = insn_fetch(u8, 1, c->eip);
982 index_reg |= (sib >> 3) & 7;
983 base_reg |= sib & 7;
984 scale = sib >> 6;
985
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700986 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300987 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700988 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300989 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700990 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300991 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700992 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
993 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700994 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700995 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300996 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200997 switch (c->modrm_mod) {
998 case 0:
999 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001000 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 break;
1002 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001003 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 break;
1005 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001006 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 break;
1008 }
1009 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001010 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011done:
1012 return rc;
1013}
1014
1015static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001016 struct x86_emulate_ops *ops,
1017 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001018{
1019 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001020 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001022 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001023 switch (c->ad_bytes) {
1024 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +02001025 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001026 break;
1027 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +02001028 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001029 break;
1030 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +02001031 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001032 break;
1033 }
1034done:
1035 return rc;
1036}
1037
Wei Yongjun35c843c2010-08-09 11:34:56 +08001038static void fetch_bit_operand(struct decode_cache *c)
1039{
Sheng Yang7129eec2010-09-28 16:33:32 +08001040 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001041
Wei Yongjun3885f182010-08-09 11:37:37 +08001042 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +08001043 mask = ~(c->dst.bytes * 8 - 1);
1044
1045 if (c->src.bytes == 2)
1046 sv = (s16)c->src.val & (s16)mask;
1047 else if (c->src.bytes == 4)
1048 sv = (s32)c->src.val & (s32)mask;
1049
Avi Kivity90de84f2010-11-17 15:28:21 +02001050 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001051 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001052
1053 /* only subword offset */
1054 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001055}
1056
Gleb Natapov9de41572010-04-28 19:15:22 +03001057static int read_emulated(struct x86_emulate_ctxt *ctxt,
1058 struct x86_emulate_ops *ops,
1059 unsigned long addr, void *dest, unsigned size)
1060{
1061 int rc;
1062 struct read_cache *mc = &ctxt->decode.mem_read;
1063
1064 while (size) {
1065 int n = min(size, 8u);
1066 size -= n;
1067 if (mc->pos < mc->end)
1068 goto read_cached;
1069
Avi Kivity0f65dd72011-04-20 13:37:53 +03001070 rc = ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1071 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001072 if (rc != X86EMUL_CONTINUE)
1073 return rc;
1074 mc->end += n;
1075
1076 read_cached:
1077 memcpy(dest, mc->data + mc->pos, n);
1078 mc->pos += n;
1079 dest += n;
1080 addr += n;
1081 }
1082 return X86EMUL_CONTINUE;
1083}
1084
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001085static int segmented_read(struct x86_emulate_ctxt *ctxt,
1086 struct segmented_address addr,
1087 void *data,
1088 unsigned size)
1089{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001090 int rc;
1091 ulong linear;
1092
Avi Kivity83b87952011-04-03 11:31:19 +03001093 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001094 if (rc != X86EMUL_CONTINUE)
1095 return rc;
1096 return read_emulated(ctxt, ctxt->ops, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001097}
1098
1099static int segmented_write(struct x86_emulate_ctxt *ctxt,
1100 struct segmented_address addr,
1101 const void *data,
1102 unsigned size)
1103{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001104 int rc;
1105 ulong linear;
1106
Avi Kivity83b87952011-04-03 11:31:19 +03001107 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001108 if (rc != X86EMUL_CONTINUE)
1109 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001110 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1111 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001112}
1113
1114static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1115 struct segmented_address addr,
1116 const void *orig_data, const void *data,
1117 unsigned size)
1118{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001119 int rc;
1120 ulong linear;
1121
Avi Kivity83b87952011-04-03 11:31:19 +03001122 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001123 if (rc != X86EMUL_CONTINUE)
1124 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001125 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1126 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001127}
1128
Gleb Natapov7b262e92010-03-18 15:20:27 +02001129static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1130 struct x86_emulate_ops *ops,
1131 unsigned int size, unsigned short port,
1132 void *dest)
1133{
1134 struct read_cache *rc = &ctxt->decode.io_read;
1135
1136 if (rc->pos == rc->end) { /* refill pio read ahead */
1137 struct decode_cache *c = &ctxt->decode;
1138 unsigned int in_page, n;
1139 unsigned int count = c->rep_prefix ?
1140 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1141 in_page = (ctxt->eflags & EFLG_DF) ?
1142 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1143 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1144 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1145 count);
1146 if (n == 0)
1147 n = 1;
1148 rc->pos = rc->end = 0;
Avi Kivityca1d4a92011-04-20 13:37:53 +03001149 if (!ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001150 return 0;
1151 rc->end = n * size;
1152 }
1153
1154 memcpy(dest, rc->data + rc->pos, size);
1155 rc->pos += size;
1156 return 1;
1157}
1158
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001159static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1160 struct x86_emulate_ops *ops,
1161 u16 selector, struct desc_ptr *dt)
1162{
1163 if (selector & 1 << 2) {
1164 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001165 u16 sel;
1166
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001167 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001168 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001169 return;
1170
1171 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1172 dt->address = get_desc_base(&desc);
1173 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001174 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001175}
1176
1177/* allowed just for 8 bytes segments */
1178static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1179 struct x86_emulate_ops *ops,
1180 u16 selector, struct desc_struct *desc)
1181{
1182 struct desc_ptr dt;
1183 u16 index = selector >> 3;
1184 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001185 ulong addr;
1186
1187 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1188
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001189 if (dt.size < index * 8 + 7)
1190 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001191 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001192 ret = ops->read_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193
1194 return ret;
1195}
1196
1197/* allowed just for 8 bytes segments */
1198static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1199 struct x86_emulate_ops *ops,
1200 u16 selector, struct desc_struct *desc)
1201{
1202 struct desc_ptr dt;
1203 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001204 ulong addr;
1205 int ret;
1206
1207 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1208
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001209 if (dt.size < index * 8 + 7)
1210 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001211
1212 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001213 ret = ops->write_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001214
1215 return ret;
1216}
1217
Gleb Natapov5601d052011-03-07 14:55:06 +02001218/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001219static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1220 struct x86_emulate_ops *ops,
1221 u16 selector, int seg)
1222{
1223 struct desc_struct seg_desc;
1224 u8 dpl, rpl, cpl;
1225 unsigned err_vec = GP_VECTOR;
1226 u32 err_code = 0;
1227 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1228 int ret;
1229
1230 memset(&seg_desc, 0, sizeof seg_desc);
1231
1232 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1233 || ctxt->mode == X86EMUL_MODE_REAL) {
1234 /* set real mode segment descriptor */
1235 set_desc_base(&seg_desc, selector << 4);
1236 set_desc_limit(&seg_desc, 0xffff);
1237 seg_desc.type = 3;
1238 seg_desc.p = 1;
1239 seg_desc.s = 1;
1240 goto load;
1241 }
1242
1243 /* NULL selector is not valid for TR, CS and SS */
1244 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1245 && null_selector)
1246 goto exception;
1247
1248 /* TR should be in GDT only */
1249 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1250 goto exception;
1251
1252 if (null_selector) /* for NULL selector skip all following checks */
1253 goto load;
1254
1255 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1256 if (ret != X86EMUL_CONTINUE)
1257 return ret;
1258
1259 err_code = selector & 0xfffc;
1260 err_vec = GP_VECTOR;
1261
1262 /* can't load system descriptor into segment selecor */
1263 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1264 goto exception;
1265
1266 if (!seg_desc.p) {
1267 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1268 goto exception;
1269 }
1270
1271 rpl = selector & 3;
1272 dpl = seg_desc.dpl;
Avi Kivity717746e2011-04-20 13:37:53 +03001273 cpl = ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001274
1275 switch (seg) {
1276 case VCPU_SREG_SS:
1277 /*
1278 * segment is not a writable data segment or segment
1279 * selector's RPL != CPL or segment selector's RPL != CPL
1280 */
1281 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1282 goto exception;
1283 break;
1284 case VCPU_SREG_CS:
1285 if (!(seg_desc.type & 8))
1286 goto exception;
1287
1288 if (seg_desc.type & 4) {
1289 /* conforming */
1290 if (dpl > cpl)
1291 goto exception;
1292 } else {
1293 /* nonconforming */
1294 if (rpl > cpl || dpl != cpl)
1295 goto exception;
1296 }
1297 /* CS(RPL) <- CPL */
1298 selector = (selector & 0xfffc) | cpl;
1299 break;
1300 case VCPU_SREG_TR:
1301 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1302 goto exception;
1303 break;
1304 case VCPU_SREG_LDTR:
1305 if (seg_desc.s || seg_desc.type != 2)
1306 goto exception;
1307 break;
1308 default: /* DS, ES, FS, or GS */
1309 /*
1310 * segment is not a data or readable code segment or
1311 * ((segment is a data or nonconforming code segment)
1312 * and (both RPL and CPL > DPL))
1313 */
1314 if ((seg_desc.type & 0xa) == 0x8 ||
1315 (((seg_desc.type & 0xc) != 0xc) &&
1316 (rpl > dpl && cpl > dpl)))
1317 goto exception;
1318 break;
1319 }
1320
1321 if (seg_desc.s) {
1322 /* mark segment as accessed */
1323 seg_desc.type |= 1;
1324 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1325 if (ret != X86EMUL_CONTINUE)
1326 return ret;
1327 }
1328load:
Avi Kivity1aa36612011-04-27 13:20:30 +03001329 ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001330 return X86EMUL_CONTINUE;
1331exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001332 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001333 return X86EMUL_PROPAGATE_FAULT;
1334}
1335
Wei Yongjun31be40b2010-08-17 09:17:30 +08001336static void write_register_operand(struct operand *op)
1337{
1338 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1339 switch (op->bytes) {
1340 case 1:
1341 *(u8 *)op->addr.reg = (u8)op->val;
1342 break;
1343 case 2:
1344 *(u16 *)op->addr.reg = (u16)op->val;
1345 break;
1346 case 4:
1347 *op->addr.reg = (u32)op->val;
1348 break; /* 64b: zero-extend */
1349 case 8:
1350 *op->addr.reg = op->val;
1351 break;
1352 }
1353}
1354
Wei Yongjunc37eda12010-06-15 09:03:33 +08001355static inline int writeback(struct x86_emulate_ctxt *ctxt,
1356 struct x86_emulate_ops *ops)
1357{
1358 int rc;
1359 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001360
1361 switch (c->dst.type) {
1362 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001363 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001364 break;
1365 case OP_MEM:
1366 if (c->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001367 rc = segmented_cmpxchg(ctxt,
1368 c->dst.addr.mem,
1369 &c->dst.orig_val,
1370 &c->dst.val,
1371 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001372 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001373 rc = segmented_write(ctxt,
1374 c->dst.addr.mem,
1375 &c->dst.val,
1376 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001377 if (rc != X86EMUL_CONTINUE)
1378 return rc;
1379 break;
Avi Kivity12537912011-03-29 11:41:27 +02001380 case OP_XMM:
1381 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1382 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001383 case OP_NONE:
1384 /* no writeback */
1385 break;
1386 default:
1387 break;
1388 }
1389 return X86EMUL_CONTINUE;
1390}
1391
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001392static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393{
1394 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001395 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396
Harvey Harrison7a9572752008-02-19 07:40:41 -08001397 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001398 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1399 addr.seg = VCPU_SREG_SS;
1400
1401 /* Disable writeback. */
1402 c->dst.type = OP_NONE;
1403 return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001404}
1405
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001406static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001407 struct x86_emulate_ops *ops,
1408 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001409{
1410 struct decode_cache *c = &ctxt->decode;
1411 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001412 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413
Avi Kivity90de84f2010-11-17 15:28:21 +02001414 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1415 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001416 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001417 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001418 return rc;
1419
Avi Kivity350f69d2009-01-05 11:12:40 +02001420 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001421 return rc;
1422}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001423
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001424static int em_pop(struct x86_emulate_ctxt *ctxt)
1425{
1426 struct decode_cache *c = &ctxt->decode;
1427
1428 return emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
1429}
1430
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001431static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1432 struct x86_emulate_ops *ops,
1433 void *dest, int len)
1434{
1435 int rc;
1436 unsigned long val, change_mask;
1437 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03001438 int cpl = ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001439
1440 rc = emulate_pop(ctxt, ops, &val, len);
1441 if (rc != X86EMUL_CONTINUE)
1442 return rc;
1443
1444 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1445 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1446
1447 switch(ctxt->mode) {
1448 case X86EMUL_MODE_PROT64:
1449 case X86EMUL_MODE_PROT32:
1450 case X86EMUL_MODE_PROT16:
1451 if (cpl == 0)
1452 change_mask |= EFLG_IOPL;
1453 if (cpl <= iopl)
1454 change_mask |= EFLG_IF;
1455 break;
1456 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001457 if (iopl < 3)
1458 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001459 change_mask |= EFLG_IF;
1460 break;
1461 default: /* real mode */
1462 change_mask |= (EFLG_IOPL | EFLG_IF);
1463 break;
1464 }
1465
1466 *(unsigned long *)dest =
1467 (ctxt->eflags & ~change_mask) | (val & change_mask);
1468
1469 return rc;
1470}
1471
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001472static int em_popf(struct x86_emulate_ctxt *ctxt)
1473{
1474 struct decode_cache *c = &ctxt->decode;
1475
1476 c->dst.type = OP_REG;
1477 c->dst.addr.reg = &ctxt->eflags;
1478 c->dst.bytes = c->op_bytes;
1479 return emulate_popf(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
1480}
1481
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001482static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1483 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001484{
1485 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001486
Avi Kivity1aa36612011-04-27 13:20:30 +03001487 c->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001488
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001489 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001490}
1491
1492static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1493 struct x86_emulate_ops *ops, int seg)
1494{
1495 struct decode_cache *c = &ctxt->decode;
1496 unsigned long selector;
1497 int rc;
1498
1499 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001500 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001501 return rc;
1502
Gleb Natapov2e873022010-03-18 15:20:18 +02001503 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001504 return rc;
1505}
1506
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001507static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001508{
1509 struct decode_cache *c = &ctxt->decode;
1510 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001511 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001512 int reg = VCPU_REGS_RAX;
1513
1514 while (reg <= VCPU_REGS_RDI) {
1515 (reg == VCPU_REGS_RSP) ?
1516 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1517
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001518 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001519 if (rc != X86EMUL_CONTINUE)
1520 return rc;
1521
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001522 ++reg;
1523 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001524
Wei Yongjunc37eda12010-06-15 09:03:33 +08001525 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001526}
1527
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001528static int em_pushf(struct x86_emulate_ctxt *ctxt)
1529{
1530 struct decode_cache *c = &ctxt->decode;
1531
1532 c->src.val = (unsigned long)ctxt->eflags;
1533 return em_push(ctxt);
1534}
1535
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001536static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001537{
1538 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001539 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001540 int reg = VCPU_REGS_RDI;
1541
1542 while (reg >= VCPU_REGS_RAX) {
1543 if (reg == VCPU_REGS_RSP) {
1544 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1545 c->op_bytes);
1546 --reg;
1547 }
1548
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001549 rc = emulate_pop(ctxt, ctxt->ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001550 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001551 break;
1552 --reg;
1553 }
1554 return rc;
1555}
1556
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001557int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1558 struct x86_emulate_ops *ops, int irq)
1559{
1560 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001561 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001562 struct desc_ptr dt;
1563 gva_t cs_addr;
1564 gva_t eip_addr;
1565 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001566
1567 /* TODO: Add limit checks */
1568 c->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001569 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001570 if (rc != X86EMUL_CONTINUE)
1571 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001572
1573 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1574
Avi Kivity1aa36612011-04-27 13:20:30 +03001575 c->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001576 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001577 if (rc != X86EMUL_CONTINUE)
1578 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001579
1580 c->src.val = c->eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001581 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001582 if (rc != X86EMUL_CONTINUE)
1583 return rc;
1584
Avi Kivity4bff1e862011-04-20 13:37:53 +03001585 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001586
1587 eip_addr = dt.address + (irq << 2);
1588 cs_addr = dt.address + (irq << 2) + 2;
1589
Avi Kivity0f65dd72011-04-20 13:37:53 +03001590 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001591 if (rc != X86EMUL_CONTINUE)
1592 return rc;
1593
Avi Kivity0f65dd72011-04-20 13:37:53 +03001594 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001595 if (rc != X86EMUL_CONTINUE)
1596 return rc;
1597
1598 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1599 if (rc != X86EMUL_CONTINUE)
1600 return rc;
1601
1602 c->eip = eip;
1603
1604 return rc;
1605}
1606
1607static int emulate_int(struct x86_emulate_ctxt *ctxt,
1608 struct x86_emulate_ops *ops, int irq)
1609{
1610 switch(ctxt->mode) {
1611 case X86EMUL_MODE_REAL:
1612 return emulate_int_real(ctxt, ops, irq);
1613 case X86EMUL_MODE_VM86:
1614 case X86EMUL_MODE_PROT16:
1615 case X86EMUL_MODE_PROT32:
1616 case X86EMUL_MODE_PROT64:
1617 default:
1618 /* Protected mode interrupts unimplemented yet */
1619 return X86EMUL_UNHANDLEABLE;
1620 }
1621}
1622
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001623static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1624 struct x86_emulate_ops *ops)
1625{
1626 struct decode_cache *c = &ctxt->decode;
1627 int rc = X86EMUL_CONTINUE;
1628 unsigned long temp_eip = 0;
1629 unsigned long temp_eflags = 0;
1630 unsigned long cs = 0;
1631 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1632 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1633 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1634 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1635
1636 /* TODO: Add stack limit check */
1637
1638 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1639
1640 if (rc != X86EMUL_CONTINUE)
1641 return rc;
1642
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001643 if (temp_eip & ~0xffff)
1644 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001645
1646 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1647
1648 if (rc != X86EMUL_CONTINUE)
1649 return rc;
1650
1651 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1652
1653 if (rc != X86EMUL_CONTINUE)
1654 return rc;
1655
1656 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1657
1658 if (rc != X86EMUL_CONTINUE)
1659 return rc;
1660
1661 c->eip = temp_eip;
1662
1663
1664 if (c->op_bytes == 4)
1665 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1666 else if (c->op_bytes == 2) {
1667 ctxt->eflags &= ~0xffff;
1668 ctxt->eflags |= temp_eflags;
1669 }
1670
1671 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1672 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1673
1674 return rc;
1675}
1676
1677static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1678 struct x86_emulate_ops* ops)
1679{
1680 switch(ctxt->mode) {
1681 case X86EMUL_MODE_REAL:
1682 return emulate_iret_real(ctxt, ops);
1683 case X86EMUL_MODE_VM86:
1684 case X86EMUL_MODE_PROT16:
1685 case X86EMUL_MODE_PROT32:
1686 case X86EMUL_MODE_PROT64:
1687 default:
1688 /* iret from protected mode unimplemented yet */
1689 return X86EMUL_UNHANDLEABLE;
1690 }
1691}
1692
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001693static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1694 struct x86_emulate_ops *ops)
1695{
1696 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001697
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001698 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699}
1700
Laurent Vivier05f086f2007-09-24 11:10:55 +02001701static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001703 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704 switch (c->modrm_reg) {
1705 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001706 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707 break;
1708 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001709 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710 break;
1711 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001712 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001713 break;
1714 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001715 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716 break;
1717 case 4: /* sal/shl */
1718 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001719 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001720 break;
1721 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001722 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723 break;
1724 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001725 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726 break;
1727 }
1728}
1729
1730static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001731 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001732{
1733 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001734 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1735 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001736 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737
1738 switch (c->modrm_reg) {
1739 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001740 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741 break;
1742 case 2: /* not */
1743 c->dst.val = ~c->dst.val;
1744 break;
1745 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001746 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001748 case 4: /* mul */
1749 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1750 break;
1751 case 5: /* imul */
1752 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1753 break;
1754 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001755 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1756 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001757 break;
1758 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001759 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1760 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001761 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001762 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001763 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001764 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001765 if (de)
1766 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001767 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001768}
1769
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001770static int emulate_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771{
1772 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001773 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774
1775 switch (c->modrm_reg) {
1776 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001777 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778 break;
1779 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001780 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001782 case 2: /* call near abs */ {
1783 long int old_eip;
1784 old_eip = c->eip;
1785 c->eip = c->src.val;
1786 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001787 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001788 break;
1789 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001791 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792 break;
1793 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001794 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001797 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001798}
1799
1800static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001801 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802{
1803 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001804 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001805
1806 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1807 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001808 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1809 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001810 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001811 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001812 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1813 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001814
Laurent Vivier05f086f2007-09-24 11:10:55 +02001815 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001816 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001817 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001818}
1819
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001820static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1821 struct x86_emulate_ops *ops)
1822{
1823 struct decode_cache *c = &ctxt->decode;
1824 int rc;
1825 unsigned long cs;
1826
1827 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001828 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001829 return rc;
1830 if (c->op_bytes == 4)
1831 c->eip = (u32)c->eip;
1832 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001833 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001834 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001835 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001836 return rc;
1837}
1838
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001839static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1840 struct x86_emulate_ops *ops, int seg)
1841{
1842 struct decode_cache *c = &ctxt->decode;
1843 unsigned short sel;
1844 int rc;
1845
1846 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1847
1848 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1849 if (rc != X86EMUL_CONTINUE)
1850 return rc;
1851
1852 c->dst.val = c->src.val;
1853 return rc;
1854}
1855
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856static inline void
1857setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001858 struct x86_emulate_ops *ops, struct desc_struct *cs,
1859 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860{
Avi Kivity1aa36612011-04-27 13:20:30 +03001861 u16 selector;
1862
Gleb Natapov79168fd2010-04-28 19:15:30 +03001863 memset(cs, 0, sizeof(struct desc_struct));
Avi Kivity1aa36612011-04-27 13:20:30 +03001864 ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001865 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866
1867 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001868 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001869 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001871 cs->type = 0x0b; /* Read, Execute, Accessed */
1872 cs->s = 1;
1873 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001874 cs->p = 1;
1875 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001876
Gleb Natapov79168fd2010-04-28 19:15:30 +03001877 set_desc_base(ss, 0); /* flat segment */
1878 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879 ss->g = 1; /* 4kb granularity */
1880 ss->s = 1;
1881 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885}
1886
1887static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001888emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889{
1890 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001891 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001892 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001893 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001894 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895
1896 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001897 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001898 ctxt->mode == X86EMUL_MODE_VM86)
1899 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001900
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001901 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001902 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001903
Avi Kivity717746e2011-04-20 13:37:53 +03001904 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001905 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001906 cs_sel = (u16)(msr_data & 0xfffc);
1907 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001908
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001909 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001910 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911 cs.l = 1;
1912 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001913 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1914 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001915
1916 c->regs[VCPU_REGS_RCX] = c->eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001917 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001918#ifdef CONFIG_X86_64
1919 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1920
Avi Kivity717746e2011-04-20 13:37:53 +03001921 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001922 ctxt->mode == X86EMUL_MODE_PROT64 ?
1923 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001924 c->eip = msr_data;
1925
Avi Kivity717746e2011-04-20 13:37:53 +03001926 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001927 ctxt->eflags &= ~(msr_data | EFLG_RF);
1928#endif
1929 } else {
1930 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001931 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001932 c->eip = (u32)msr_data;
1933
1934 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1935 }
1936
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001937 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001938}
1939
Andre Przywara8c604352009-06-18 12:56:01 +02001940static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001941emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001942{
1943 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001944 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001945 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001946 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001947 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001948
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001949 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001950 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001951 if (ctxt->mode == X86EMUL_MODE_REAL)
1952 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001953
1954 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1955 * Therefore, we inject an #UD.
1956 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001957 if (ctxt->mode == X86EMUL_MODE_PROT64)
1958 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001959
Gleb Natapov79168fd2010-04-28 19:15:30 +03001960 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001961
Avi Kivity717746e2011-04-20 13:37:53 +03001962 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001963 switch (ctxt->mode) {
1964 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001965 if ((msr_data & 0xfffc) == 0x0)
1966 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001967 break;
1968 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001969 if (msr_data == 0x0)
1970 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001971 break;
1972 }
1973
1974 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001975 cs_sel = (u16)msr_data;
1976 cs_sel &= ~SELECTOR_RPL_MASK;
1977 ss_sel = cs_sel + 8;
1978 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001979 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001980 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001981 cs.l = 1;
1982 }
1983
Avi Kivity1aa36612011-04-27 13:20:30 +03001984 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1985 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001986
Avi Kivity717746e2011-04-20 13:37:53 +03001987 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001988 c->eip = msr_data;
1989
Avi Kivity717746e2011-04-20 13:37:53 +03001990 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001991 c->regs[VCPU_REGS_RSP] = msr_data;
1992
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001993 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001994}
1995
Andre Przywara4668f052009-06-18 12:56:02 +02001996static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001997emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001998{
1999 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002000 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002001 u64 msr_data;
2002 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002003 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02002004
Gleb Natapova0044752010-02-10 14:21:31 +02002005 /* inject #GP if in real mode or Virtual 8086 mode */
2006 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002007 ctxt->mode == X86EMUL_MODE_VM86)
2008 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002009
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002011
2012 if ((c->rex_prefix & 0x8) != 0x0)
2013 usermode = X86EMUL_MODE_PROT64;
2014 else
2015 usermode = X86EMUL_MODE_PROT32;
2016
2017 cs.dpl = 3;
2018 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002019 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002020 switch (usermode) {
2021 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002022 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002023 if ((msr_data & 0xfffc) == 0x0)
2024 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002025 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002026 break;
2027 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002028 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002029 if (msr_data == 0x0)
2030 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002031 ss_sel = cs_sel + 8;
2032 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002033 cs.l = 1;
2034 break;
2035 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002036 cs_sel |= SELECTOR_RPL_MASK;
2037 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002038
Avi Kivity1aa36612011-04-27 13:20:30 +03002039 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2040 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002041
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002042 c->eip = c->regs[VCPU_REGS_RDX];
2043 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002044
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002045 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002046}
2047
Gleb Natapov9c537242010-03-18 15:20:05 +02002048static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2049 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002050{
2051 int iopl;
2052 if (ctxt->mode == X86EMUL_MODE_REAL)
2053 return false;
2054 if (ctxt->mode == X86EMUL_MODE_VM86)
2055 return true;
2056 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03002057 return ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058}
2059
2060static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2061 struct x86_emulate_ops *ops,
2062 u16 port, u16 len)
2063{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002064 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002065 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002066 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002067 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002068 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002069 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070
Avi Kivity1aa36612011-04-27 13:20:30 +03002071 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002072 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002073 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002074 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002075 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002076 base = get_desc_base(&tr_seg);
2077#ifdef CONFIG_X86_64
2078 base |= ((u64)base3) << 32;
2079#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002080 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002081 if (r != X86EMUL_CONTINUE)
2082 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002083 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002084 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002085 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002086 if (r != X86EMUL_CONTINUE)
2087 return false;
2088 if ((perm >> bit_idx) & mask)
2089 return false;
2090 return true;
2091}
2092
2093static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2094 struct x86_emulate_ops *ops,
2095 u16 port, u16 len)
2096{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002097 if (ctxt->perm_ok)
2098 return true;
2099
Gleb Natapov9c537242010-03-18 15:20:05 +02002100 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002101 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2102 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002103
2104 ctxt->perm_ok = true;
2105
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002106 return true;
2107}
2108
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002109static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2110 struct x86_emulate_ops *ops,
2111 struct tss_segment_16 *tss)
2112{
2113 struct decode_cache *c = &ctxt->decode;
2114
2115 tss->ip = c->eip;
2116 tss->flag = ctxt->eflags;
2117 tss->ax = c->regs[VCPU_REGS_RAX];
2118 tss->cx = c->regs[VCPU_REGS_RCX];
2119 tss->dx = c->regs[VCPU_REGS_RDX];
2120 tss->bx = c->regs[VCPU_REGS_RBX];
2121 tss->sp = c->regs[VCPU_REGS_RSP];
2122 tss->bp = c->regs[VCPU_REGS_RBP];
2123 tss->si = c->regs[VCPU_REGS_RSI];
2124 tss->di = c->regs[VCPU_REGS_RDI];
2125
Avi Kivity1aa36612011-04-27 13:20:30 +03002126 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2127 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2128 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2129 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2130 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002131}
2132
2133static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2134 struct x86_emulate_ops *ops,
2135 struct tss_segment_16 *tss)
2136{
2137 struct decode_cache *c = &ctxt->decode;
2138 int ret;
2139
2140 c->eip = tss->ip;
2141 ctxt->eflags = tss->flag | 2;
2142 c->regs[VCPU_REGS_RAX] = tss->ax;
2143 c->regs[VCPU_REGS_RCX] = tss->cx;
2144 c->regs[VCPU_REGS_RDX] = tss->dx;
2145 c->regs[VCPU_REGS_RBX] = tss->bx;
2146 c->regs[VCPU_REGS_RSP] = tss->sp;
2147 c->regs[VCPU_REGS_RBP] = tss->bp;
2148 c->regs[VCPU_REGS_RSI] = tss->si;
2149 c->regs[VCPU_REGS_RDI] = tss->di;
2150
2151 /*
2152 * SDM says that segment selectors are loaded before segment
2153 * descriptors
2154 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002155 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2156 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2157 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2158 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2159 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160
2161 /*
2162 * Now load segment descriptors. If fault happenes at this stage
2163 * it is handled in a context of new task
2164 */
2165 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2166 if (ret != X86EMUL_CONTINUE)
2167 return ret;
2168 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2169 if (ret != X86EMUL_CONTINUE)
2170 return ret;
2171 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2172 if (ret != X86EMUL_CONTINUE)
2173 return ret;
2174 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2175 if (ret != X86EMUL_CONTINUE)
2176 return ret;
2177 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2178 if (ret != X86EMUL_CONTINUE)
2179 return ret;
2180
2181 return X86EMUL_CONTINUE;
2182}
2183
2184static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2185 struct x86_emulate_ops *ops,
2186 u16 tss_selector, u16 old_tss_sel,
2187 ulong old_tss_base, struct desc_struct *new_desc)
2188{
2189 struct tss_segment_16 tss_seg;
2190 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002191 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192
Avi Kivity0f65dd72011-04-20 13:37:53 +03002193 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002194 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002195 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002197 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002198
2199 save_state_to_tss16(ctxt, ops, &tss_seg);
2200
Avi Kivity0f65dd72011-04-20 13:37:53 +03002201 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002202 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002203 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002204 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002206
Avi Kivity0f65dd72011-04-20 13:37:53 +03002207 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002208 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002209 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002210 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002211 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212
2213 if (old_tss_sel != 0xffff) {
2214 tss_seg.prev_task_link = old_tss_sel;
2215
Avi Kivity0f65dd72011-04-20 13:37:53 +03002216 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002217 &tss_seg.prev_task_link,
2218 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002219 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002220 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002221 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002222 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223 }
2224
2225 return load_state_from_tss16(ctxt, ops, &tss_seg);
2226}
2227
2228static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2229 struct x86_emulate_ops *ops,
2230 struct tss_segment_32 *tss)
2231{
2232 struct decode_cache *c = &ctxt->decode;
2233
Avi Kivity717746e2011-04-20 13:37:53 +03002234 tss->cr3 = ops->get_cr(ctxt, 3);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002235 tss->eip = c->eip;
2236 tss->eflags = ctxt->eflags;
2237 tss->eax = c->regs[VCPU_REGS_RAX];
2238 tss->ecx = c->regs[VCPU_REGS_RCX];
2239 tss->edx = c->regs[VCPU_REGS_RDX];
2240 tss->ebx = c->regs[VCPU_REGS_RBX];
2241 tss->esp = c->regs[VCPU_REGS_RSP];
2242 tss->ebp = c->regs[VCPU_REGS_RBP];
2243 tss->esi = c->regs[VCPU_REGS_RSI];
2244 tss->edi = c->regs[VCPU_REGS_RDI];
2245
Avi Kivity1aa36612011-04-27 13:20:30 +03002246 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2247 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2248 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2249 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2250 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2251 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2252 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002253}
2254
2255static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2256 struct x86_emulate_ops *ops,
2257 struct tss_segment_32 *tss)
2258{
2259 struct decode_cache *c = &ctxt->decode;
2260 int ret;
2261
Avi Kivity717746e2011-04-20 13:37:53 +03002262 if (ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002263 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264 c->eip = tss->eip;
2265 ctxt->eflags = tss->eflags | 2;
2266 c->regs[VCPU_REGS_RAX] = tss->eax;
2267 c->regs[VCPU_REGS_RCX] = tss->ecx;
2268 c->regs[VCPU_REGS_RDX] = tss->edx;
2269 c->regs[VCPU_REGS_RBX] = tss->ebx;
2270 c->regs[VCPU_REGS_RSP] = tss->esp;
2271 c->regs[VCPU_REGS_RBP] = tss->ebp;
2272 c->regs[VCPU_REGS_RSI] = tss->esi;
2273 c->regs[VCPU_REGS_RDI] = tss->edi;
2274
2275 /*
2276 * SDM says that segment selectors are loaded before segment
2277 * descriptors
2278 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002279 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2280 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2281 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2282 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2283 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2284 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2285 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286
2287 /*
2288 * Now load segment descriptors. If fault happenes at this stage
2289 * it is handled in a context of new task
2290 */
2291 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2292 if (ret != X86EMUL_CONTINUE)
2293 return ret;
2294 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2295 if (ret != X86EMUL_CONTINUE)
2296 return ret;
2297 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2298 if (ret != X86EMUL_CONTINUE)
2299 return ret;
2300 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2301 if (ret != X86EMUL_CONTINUE)
2302 return ret;
2303 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2304 if (ret != X86EMUL_CONTINUE)
2305 return ret;
2306 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2307 if (ret != X86EMUL_CONTINUE)
2308 return ret;
2309 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2310 if (ret != X86EMUL_CONTINUE)
2311 return ret;
2312
2313 return X86EMUL_CONTINUE;
2314}
2315
2316static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2317 struct x86_emulate_ops *ops,
2318 u16 tss_selector, u16 old_tss_sel,
2319 ulong old_tss_base, struct desc_struct *new_desc)
2320{
2321 struct tss_segment_32 tss_seg;
2322 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002323 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002324
Avi Kivity0f65dd72011-04-20 13:37:53 +03002325 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002326 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002327 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002330
2331 save_state_to_tss32(ctxt, ops, &tss_seg);
2332
Avi Kivity0f65dd72011-04-20 13:37:53 +03002333 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002334 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002335 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338
Avi Kivity0f65dd72011-04-20 13:37:53 +03002339 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002340 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002341 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002344
2345 if (old_tss_sel != 0xffff) {
2346 tss_seg.prev_task_link = old_tss_sel;
2347
Avi Kivity0f65dd72011-04-20 13:37:53 +03002348 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002349 &tss_seg.prev_task_link,
2350 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002351 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002352 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002353 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002355 }
2356
2357 return load_state_from_tss32(ctxt, ops, &tss_seg);
2358}
2359
2360static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002361 struct x86_emulate_ops *ops,
2362 u16 tss_selector, int reason,
2363 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002364{
2365 struct desc_struct curr_tss_desc, next_tss_desc;
2366 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002367 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002369 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002370 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002371
2372 /* FIXME: old_tss_base == ~0 ? */
2373
2374 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2375 if (ret != X86EMUL_CONTINUE)
2376 return ret;
2377 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2378 if (ret != X86EMUL_CONTINUE)
2379 return ret;
2380
2381 /* FIXME: check that next_tss_desc is tss */
2382
2383 if (reason != TASK_SWITCH_IRET) {
2384 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002385 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002386 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 }
2388
Gleb Natapovceffb452010-03-18 15:20:19 +02002389 desc_limit = desc_limit_scaled(&next_tss_desc);
2390 if (!next_tss_desc.p ||
2391 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2392 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002393 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394 return X86EMUL_PROPAGATE_FAULT;
2395 }
2396
2397 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2398 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2399 write_segment_descriptor(ctxt, ops, old_tss_sel,
2400 &curr_tss_desc);
2401 }
2402
2403 if (reason == TASK_SWITCH_IRET)
2404 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2405
2406 /* set back link to prev task only if NT bit is set in eflags
2407 note that old_tss_sel is not used afetr this point */
2408 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2409 old_tss_sel = 0xffff;
2410
2411 if (next_tss_desc.type & 8)
2412 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2413 old_tss_base, &next_tss_desc);
2414 else
2415 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2416 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002417 if (ret != X86EMUL_CONTINUE)
2418 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419
2420 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2421 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2422
2423 if (reason != TASK_SWITCH_IRET) {
2424 next_tss_desc.type |= (1 << 1); /* set busy flag */
2425 write_segment_descriptor(ctxt, ops, tss_selector,
2426 &next_tss_desc);
2427 }
2428
Avi Kivity717746e2011-04-20 13:37:53 +03002429 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002430 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431
Jan Kiszkae269fb22010-04-14 15:51:09 +02002432 if (has_error_code) {
2433 struct decode_cache *c = &ctxt->decode;
2434
2435 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2436 c->lock_prefix = 0;
2437 c->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002438 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002439 }
2440
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441 return ret;
2442}
2443
2444int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002445 u16 tss_selector, int reason,
2446 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447{
Avi Kivity9aabc882010-07-29 15:11:50 +03002448 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449 struct decode_cache *c = &ctxt->decode;
2450 int rc;
2451
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002452 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002453 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454
Jan Kiszkae269fb22010-04-14 15:51:09 +02002455 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2456 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002458 if (rc == X86EMUL_CONTINUE)
2459 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002460
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002461 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002462}
2463
Avi Kivity90de84f2010-11-17 15:28:21 +02002464static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002465 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002466{
2467 struct decode_cache *c = &ctxt->decode;
2468 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2469
Gleb Natapovd9271122010-03-18 15:20:22 +02002470 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002471 op->addr.mem.ea = register_address(c, c->regs[reg]);
2472 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002473}
2474
Avi Kivity7af04fc2010-08-18 14:16:35 +03002475static int em_das(struct x86_emulate_ctxt *ctxt)
2476{
2477 struct decode_cache *c = &ctxt->decode;
2478 u8 al, old_al;
2479 bool af, cf, old_cf;
2480
2481 cf = ctxt->eflags & X86_EFLAGS_CF;
2482 al = c->dst.val;
2483
2484 old_al = al;
2485 old_cf = cf;
2486 cf = false;
2487 af = ctxt->eflags & X86_EFLAGS_AF;
2488 if ((al & 0x0f) > 9 || af) {
2489 al -= 6;
2490 cf = old_cf | (al >= 250);
2491 af = true;
2492 } else {
2493 af = false;
2494 }
2495 if (old_al > 0x99 || old_cf) {
2496 al -= 0x60;
2497 cf = true;
2498 }
2499
2500 c->dst.val = al;
2501 /* Set PF, ZF, SF */
2502 c->src.type = OP_IMM;
2503 c->src.val = 0;
2504 c->src.bytes = 1;
2505 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2506 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2507 if (cf)
2508 ctxt->eflags |= X86_EFLAGS_CF;
2509 if (af)
2510 ctxt->eflags |= X86_EFLAGS_AF;
2511 return X86EMUL_CONTINUE;
2512}
2513
Avi Kivity0ef753b2010-08-18 14:51:45 +03002514static int em_call_far(struct x86_emulate_ctxt *ctxt)
2515{
2516 struct decode_cache *c = &ctxt->decode;
2517 u16 sel, old_cs;
2518 ulong old_eip;
2519 int rc;
2520
Avi Kivity1aa36612011-04-27 13:20:30 +03002521 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002522 old_eip = c->eip;
2523
2524 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2525 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2526 return X86EMUL_CONTINUE;
2527
2528 c->eip = 0;
2529 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2530
2531 c->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002532 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002533 if (rc != X86EMUL_CONTINUE)
2534 return rc;
2535
2536 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002537 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002538}
2539
Avi Kivity40ece7c2010-08-18 15:12:09 +03002540static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2541{
2542 struct decode_cache *c = &ctxt->decode;
2543 int rc;
2544
2545 c->dst.type = OP_REG;
2546 c->dst.addr.reg = &c->eip;
2547 c->dst.bytes = c->op_bytes;
2548 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2549 if (rc != X86EMUL_CONTINUE)
2550 return rc;
2551 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2552 return X86EMUL_CONTINUE;
2553}
2554
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002555static int em_add(struct x86_emulate_ctxt *ctxt)
2556{
2557 struct decode_cache *c = &ctxt->decode;
2558
2559 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
2560 return X86EMUL_CONTINUE;
2561}
2562
2563static int em_or(struct x86_emulate_ctxt *ctxt)
2564{
2565 struct decode_cache *c = &ctxt->decode;
2566
2567 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2568 return X86EMUL_CONTINUE;
2569}
2570
2571static int em_adc(struct x86_emulate_ctxt *ctxt)
2572{
2573 struct decode_cache *c = &ctxt->decode;
2574
2575 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
2576 return X86EMUL_CONTINUE;
2577}
2578
2579static int em_sbb(struct x86_emulate_ctxt *ctxt)
2580{
2581 struct decode_cache *c = &ctxt->decode;
2582
2583 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
2584 return X86EMUL_CONTINUE;
2585}
2586
2587static int em_and(struct x86_emulate_ctxt *ctxt)
2588{
2589 struct decode_cache *c = &ctxt->decode;
2590
2591 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
2592 return X86EMUL_CONTINUE;
2593}
2594
2595static int em_sub(struct x86_emulate_ctxt *ctxt)
2596{
2597 struct decode_cache *c = &ctxt->decode;
2598
2599 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
2600 return X86EMUL_CONTINUE;
2601}
2602
2603static int em_xor(struct x86_emulate_ctxt *ctxt)
2604{
2605 struct decode_cache *c = &ctxt->decode;
2606
2607 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
2608 return X86EMUL_CONTINUE;
2609}
2610
2611static int em_cmp(struct x86_emulate_ctxt *ctxt)
2612{
2613 struct decode_cache *c = &ctxt->decode;
2614
2615 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2616 /* Disable writeback. */
2617 c->dst.type = OP_NONE;
2618 return X86EMUL_CONTINUE;
2619}
2620
Avi Kivity5c82aa22010-08-18 18:31:43 +03002621static int em_imul(struct x86_emulate_ctxt *ctxt)
2622{
2623 struct decode_cache *c = &ctxt->decode;
2624
2625 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2626 return X86EMUL_CONTINUE;
2627}
2628
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002629static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2630{
2631 struct decode_cache *c = &ctxt->decode;
2632
2633 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002634 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002635}
2636
Avi Kivity61429142010-08-19 15:13:00 +03002637static int em_cwd(struct x86_emulate_ctxt *ctxt)
2638{
2639 struct decode_cache *c = &ctxt->decode;
2640
2641 c->dst.type = OP_REG;
2642 c->dst.bytes = c->src.bytes;
2643 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2644 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2645
2646 return X86EMUL_CONTINUE;
2647}
2648
Avi Kivity48bb5d32010-08-18 18:54:34 +03002649static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2650{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002651 struct decode_cache *c = &ctxt->decode;
2652 u64 tsc = 0;
2653
Avi Kivity717746e2011-04-20 13:37:53 +03002654 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002655 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2656 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2657 return X86EMUL_CONTINUE;
2658}
2659
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002660static int em_mov(struct x86_emulate_ctxt *ctxt)
2661{
2662 struct decode_cache *c = &ctxt->decode;
2663 c->dst.val = c->src.val;
2664 return X86EMUL_CONTINUE;
2665}
2666
Avi Kivityaa97bb42010-01-20 18:09:23 +02002667static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2668{
2669 struct decode_cache *c = &ctxt->decode;
2670 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2671 return X86EMUL_CONTINUE;
2672}
2673
Avi Kivity38503912011-03-31 18:48:09 +02002674static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2675{
2676 struct decode_cache *c = &ctxt->decode;
Avi Kivity9fa088f2011-03-31 18:54:30 +02002677 int rc;
2678 ulong linear;
2679
Avi Kivity83b87952011-04-03 11:31:19 +03002680 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002681 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002682 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002683 /* Disable writeback. */
2684 c->dst.type = OP_NONE;
2685 return X86EMUL_CONTINUE;
2686}
2687
Avi Kivity2d04a052011-04-20 15:32:49 +03002688static int em_clts(struct x86_emulate_ctxt *ctxt)
2689{
2690 ulong cr0;
2691
2692 cr0 = ctxt->ops->get_cr(ctxt, 0);
2693 cr0 &= ~X86_CR0_TS;
2694 ctxt->ops->set_cr(ctxt, 0, cr0);
2695 return X86EMUL_CONTINUE;
2696}
2697
Avi Kivity26d05cc2011-04-21 12:07:59 +03002698static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2699{
2700 struct decode_cache *c = &ctxt->decode;
2701 int rc;
2702
2703 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2704 return X86EMUL_UNHANDLEABLE;
2705
2706 rc = ctxt->ops->fix_hypercall(ctxt);
2707 if (rc != X86EMUL_CONTINUE)
2708 return rc;
2709
2710 /* Let the processor re-execute the fixed hypercall */
2711 c->eip = ctxt->eip;
2712 /* Disable writeback. */
2713 c->dst.type = OP_NONE;
2714 return X86EMUL_CONTINUE;
2715}
2716
2717static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2718{
2719 struct decode_cache *c = &ctxt->decode;
2720 struct desc_ptr desc_ptr;
2721 int rc;
2722
2723 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2724 &desc_ptr.size, &desc_ptr.address,
2725 c->op_bytes);
2726 if (rc != X86EMUL_CONTINUE)
2727 return rc;
2728 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2729 /* Disable writeback. */
2730 c->dst.type = OP_NONE;
2731 return X86EMUL_CONTINUE;
2732}
2733
Avi Kivity5ef39c72011-04-21 12:21:50 +03002734static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002735{
2736 struct decode_cache *c = &ctxt->decode;
2737 int rc;
2738
Avi Kivity5ef39c72011-04-21 12:21:50 +03002739 rc = ctxt->ops->fix_hypercall(ctxt);
2740
Avi Kivity26d05cc2011-04-21 12:07:59 +03002741 /* Disable writeback. */
2742 c->dst.type = OP_NONE;
2743 return rc;
2744}
2745
2746static int em_lidt(struct x86_emulate_ctxt *ctxt)
2747{
2748 struct decode_cache *c = &ctxt->decode;
2749 struct desc_ptr desc_ptr;
2750 int rc;
2751
2752 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2753 &desc_ptr.size,
2754 &desc_ptr.address,
2755 c->op_bytes);
2756 if (rc != X86EMUL_CONTINUE)
2757 return rc;
2758 ctxt->ops->set_idt(ctxt, &desc_ptr);
2759 /* Disable writeback. */
2760 c->dst.type = OP_NONE;
2761 return X86EMUL_CONTINUE;
2762}
2763
2764static int em_smsw(struct x86_emulate_ctxt *ctxt)
2765{
2766 struct decode_cache *c = &ctxt->decode;
2767
2768 c->dst.bytes = 2;
2769 c->dst.val = ctxt->ops->get_cr(ctxt, 0);
2770 return X86EMUL_CONTINUE;
2771}
2772
2773static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2774{
2775 struct decode_cache *c = &ctxt->decode;
2776 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
2777 | (c->src.val & 0x0f));
2778 c->dst.type = OP_NONE;
2779 return X86EMUL_CONTINUE;
2780}
2781
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002782static bool valid_cr(int nr)
2783{
2784 switch (nr) {
2785 case 0:
2786 case 2 ... 4:
2787 case 8:
2788 return true;
2789 default:
2790 return false;
2791 }
2792}
2793
2794static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2795{
2796 struct decode_cache *c = &ctxt->decode;
2797
2798 if (!valid_cr(c->modrm_reg))
2799 return emulate_ud(ctxt);
2800
2801 return X86EMUL_CONTINUE;
2802}
2803
2804static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2805{
2806 struct decode_cache *c = &ctxt->decode;
2807 u64 new_val = c->src.val64;
2808 int cr = c->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002809 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002810
2811 static u64 cr_reserved_bits[] = {
2812 0xffffffff00000000ULL,
2813 0, 0, 0, /* CR3 checked later */
2814 CR4_RESERVED_BITS,
2815 0, 0, 0,
2816 CR8_RESERVED_BITS,
2817 };
2818
2819 if (!valid_cr(cr))
2820 return emulate_ud(ctxt);
2821
2822 if (new_val & cr_reserved_bits[cr])
2823 return emulate_gp(ctxt, 0);
2824
2825 switch (cr) {
2826 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002827 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002828 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2829 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2830 return emulate_gp(ctxt, 0);
2831
Avi Kivity717746e2011-04-20 13:37:53 +03002832 cr4 = ctxt->ops->get_cr(ctxt, 4);
2833 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002834
2835 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2836 !(cr4 & X86_CR4_PAE))
2837 return emulate_gp(ctxt, 0);
2838
2839 break;
2840 }
2841 case 3: {
2842 u64 rsvd = 0;
2843
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002844 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2845 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002846 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002847 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002848 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002849 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002850 rsvd = CR3_NONPAE_RESERVED_BITS;
2851
2852 if (new_val & rsvd)
2853 return emulate_gp(ctxt, 0);
2854
2855 break;
2856 }
2857 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002858 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002859
Avi Kivity717746e2011-04-20 13:37:53 +03002860 cr4 = ctxt->ops->get_cr(ctxt, 4);
2861 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002862
2863 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2864 return emulate_gp(ctxt, 0);
2865
2866 break;
2867 }
2868 }
2869
2870 return X86EMUL_CONTINUE;
2871}
2872
Joerg Roedel3b88e412011-04-04 12:39:29 +02002873static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2874{
2875 unsigned long dr7;
2876
Avi Kivity717746e2011-04-20 13:37:53 +03002877 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002878
2879 /* Check if DR7.Global_Enable is set */
2880 return dr7 & (1 << 13);
2881}
2882
2883static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2884{
2885 struct decode_cache *c = &ctxt->decode;
2886 int dr = c->modrm_reg;
2887 u64 cr4;
2888
2889 if (dr > 7)
2890 return emulate_ud(ctxt);
2891
Avi Kivity717746e2011-04-20 13:37:53 +03002892 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002893 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2894 return emulate_ud(ctxt);
2895
2896 if (check_dr7_gd(ctxt))
2897 return emulate_db(ctxt);
2898
2899 return X86EMUL_CONTINUE;
2900}
2901
2902static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2903{
2904 struct decode_cache *c = &ctxt->decode;
2905 u64 new_val = c->src.val64;
2906 int dr = c->modrm_reg;
2907
2908 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2909 return emulate_gp(ctxt, 0);
2910
2911 return check_dr_read(ctxt);
2912}
2913
Joerg Roedel01de8b02011-04-04 12:39:31 +02002914static int check_svme(struct x86_emulate_ctxt *ctxt)
2915{
2916 u64 efer;
2917
Avi Kivity717746e2011-04-20 13:37:53 +03002918 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002919
2920 if (!(efer & EFER_SVME))
2921 return emulate_ud(ctxt);
2922
2923 return X86EMUL_CONTINUE;
2924}
2925
2926static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivityfe870ab2011-04-20 15:01:23 +03002928 u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002929
2930 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002931 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002932 return emulate_gp(ctxt, 0);
2933
2934 return check_svme(ctxt);
2935}
2936
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002937static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2938{
Avi Kivity717746e2011-04-20 13:37:53 +03002939 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002940
Avi Kivity717746e2011-04-20 13:37:53 +03002941 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002942 return emulate_ud(ctxt);
2943
2944 return X86EMUL_CONTINUE;
2945}
2946
Joerg Roedel80612522011-04-04 12:39:33 +02002947static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2948{
Avi Kivity717746e2011-04-20 13:37:53 +03002949 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivityfe870ab2011-04-20 15:01:23 +03002950 u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002951
Avi Kivity717746e2011-04-20 13:37:53 +03002952 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002953 (rcx > 3))
2954 return emulate_gp(ctxt, 0);
2955
2956 return X86EMUL_CONTINUE;
2957}
2958
Joerg Roedelf6511932011-04-04 12:39:35 +02002959static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2960{
2961 struct decode_cache *c = &ctxt->decode;
2962
2963 c->dst.bytes = min(c->dst.bytes, 4u);
2964 if (!emulator_io_permited(ctxt, ctxt->ops, c->src.val, c->dst.bytes))
2965 return emulate_gp(ctxt, 0);
2966
2967 return X86EMUL_CONTINUE;
2968}
2969
2970static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2971{
2972 struct decode_cache *c = &ctxt->decode;
2973
2974 c->src.bytes = min(c->src.bytes, 4u);
2975 if (!emulator_io_permited(ctxt, ctxt->ops, c->dst.val, c->src.bytes))
2976 return emulate_gp(ctxt, 0);
2977
2978 return X86EMUL_CONTINUE;
2979}
2980
Avi Kivity73fba5f2010-07-29 15:11:53 +03002981#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002982#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002983#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2984 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002985#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002986#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002987#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002988#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002989#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002990#define II(_f, _e, _i) \
2991 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002992#define IIP(_f, _e, _i, _p) \
2993 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2994 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002995#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002996
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002997#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002998#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002999#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
3000
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003001#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3002 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3003 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003004
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003005static struct opcode group7_rm1[] = {
3006 DI(SrcNone | ModRM | Priv, monitor),
3007 DI(SrcNone | ModRM | Priv, mwait),
3008 N, N, N, N, N, N,
3009};
3010
Joerg Roedel01de8b02011-04-04 12:39:31 +02003011static struct opcode group7_rm3[] = {
3012 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003013 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003014 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3015 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3016 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3017 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3018 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3019 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3020};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003021
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003022static struct opcode group7_rm7[] = {
3023 N,
3024 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3025 N, N, N, N, N, N,
3026};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003027
Avi Kivity73fba5f2010-07-29 15:11:53 +03003028static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003029 I(Lock, em_add),
3030 I(Lock, em_or),
3031 I(Lock, em_adc),
3032 I(Lock, em_sbb),
3033 I(Lock, em_and),
3034 I(Lock, em_sub),
3035 I(Lock, em_xor),
3036 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003037};
3038
3039static struct opcode group1A[] = {
3040 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3041};
3042
3043static struct opcode group3[] = {
3044 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3045 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003046 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003047};
3048
3049static struct opcode group4[] = {
3050 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3051 N, N, N, N, N, N,
3052};
3053
3054static struct opcode group5[] = {
3055 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003056 D(SrcMem | ModRM | Stack),
3057 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003058 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3059 D(SrcMem | ModRM | Stack), N,
3060};
3061
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003062static struct opcode group6[] = {
3063 DI(ModRM | Prot, sldt),
3064 DI(ModRM | Prot, str),
3065 DI(ModRM | Prot | Priv, lldt),
3066 DI(ModRM | Prot | Priv, ltr),
3067 N, N, N, N,
3068};
3069
Avi Kivity73fba5f2010-07-29 15:11:53 +03003070static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003071 DI(ModRM | Mov | DstMem | Priv, sgdt),
3072 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003073 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3074 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3075 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3076 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3077 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003078}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003079 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3080 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003081 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003082 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3083 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003084} };
3085
3086static struct opcode group8[] = {
3087 N, N, N, N,
3088 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3089 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3090};
3091
3092static struct group_dual group9 = { {
3093 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3094}, {
3095 N, N, N, N, N, N, N, N,
3096} };
3097
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003098static struct opcode group11[] = {
3099 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3100};
3101
Avi Kivityaa97bb42010-01-20 18:09:23 +02003102static struct gprefix pfx_0f_6f_0f_7f = {
3103 N, N, N, I(Sse, em_movdqu),
3104};
3105
Avi Kivity73fba5f2010-07-29 15:11:53 +03003106static struct opcode opcode_table[256] = {
3107 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003108 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003109 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3110 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003111 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003112 D(ImplicitOps | Stack | No64), N,
3113 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003114 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003115 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3116 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003117 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3119 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003120 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003121 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003122 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003123 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003124 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003125 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003126 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003127 /* 0x40 - 0x4F */
3128 X16(D(DstReg)),
3129 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003130 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003131 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003132 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003133 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003134 I(ImplicitOps | Stack | No64, em_pusha),
3135 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003136 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3137 N, N, N, N,
3138 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003139 I(SrcImm | Mov | Stack, em_push),
3140 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003141 I(SrcImmByte | Mov | Stack, em_push),
3142 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Joerg Roedelf6511932011-04-04 12:39:35 +02003143 D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3144 D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003145 /* 0x70 - 0x7F */
3146 X16(D(SrcImmByte)),
3147 /* 0x80 - 0x87 */
3148 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3149 G(DstMem | SrcImm | ModRM | Group, group1),
3150 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3151 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03003152 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003153 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003154 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3155 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03003156 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003157 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
3158 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003159 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003160 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003161 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003162 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003163 II(ImplicitOps | Stack, em_pushf, pushf),
3164 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003165 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003166 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3167 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3168 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003169 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003170 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03003171 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003172 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3173 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003174 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003175 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003176 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003177 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003178 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003179 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003180 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003181 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
3182 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003183 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003184 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003185 /* 0xC8 - 0xCF */
3186 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003187 D(ImplicitOps), DI(SrcImmByte, intn),
3188 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003189 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003190 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003191 N, N, N, N,
3192 /* 0xD8 - 0xDF */
3193 N, N, N, N, N, N, N, N,
3194 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08003195 X4(D(SrcImmByte)),
Joerg Roedelf6511932011-04-04 12:39:35 +02003196 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3197 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003198 /* 0xE8 - 0xEF */
3199 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
3200 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Joerg Roedelf6511932011-04-04 12:39:35 +02003201 D2bvIP(SrcNone | DstAcc, in, check_perm_in),
3202 D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003203 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003204 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003205 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3206 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003207 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003208 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003209 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3210};
3211
3212static struct opcode twobyte_table[256] = {
3213 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003214 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003215 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003216 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003217 N, D(ImplicitOps | ModRM), N, N,
3218 /* 0x10 - 0x1F */
3219 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3220 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003221 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003222 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003223 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003224 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003225 N, N, N, N,
3226 N, N, N, N, N, N, N, N,
3227 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003228 DI(ImplicitOps | Priv, wrmsr),
3229 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3230 DI(ImplicitOps | Priv, rdmsr),
3231 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02003232 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3233 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003234 N, N, N, N, N, N, N, N,
3235 /* 0x40 - 0x4F */
3236 X16(D(DstReg | SrcMem | ModRM | Mov)),
3237 /* 0x50 - 0x5F */
3238 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3239 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003240 N, N, N, N,
3241 N, N, N, N,
3242 N, N, N, N,
3243 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003244 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003245 N, N, N, N,
3246 N, N, N, N,
3247 N, N, N, N,
3248 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003249 /* 0x80 - 0x8F */
3250 X16(D(SrcImm)),
3251 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003252 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003253 /* 0xA0 - 0xA7 */
3254 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003255 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003256 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3257 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3258 /* 0xA8 - 0xAF */
3259 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003260 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003261 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3262 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003263 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003264 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003265 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003266 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3267 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3268 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003269 /* 0xB8 - 0xBF */
3270 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003271 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003272 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3273 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003274 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003275 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003276 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003277 N, N, N, GD(0, &group9),
3278 N, N, N, N, N, N, N, N,
3279 /* 0xD0 - 0xDF */
3280 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3281 /* 0xE0 - 0xEF */
3282 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3283 /* 0xF0 - 0xFF */
3284 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3285};
3286
3287#undef D
3288#undef N
3289#undef G
3290#undef GD
3291#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003292#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003293#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003294
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003295#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003296#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003297#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003298#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003299
Avi Kivity39f21ee2010-08-18 19:20:21 +03003300static unsigned imm_size(struct decode_cache *c)
3301{
3302 unsigned size;
3303
3304 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3305 if (size == 8)
3306 size = 4;
3307 return size;
3308}
3309
3310static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3311 unsigned size, bool sign_extension)
3312{
3313 struct decode_cache *c = &ctxt->decode;
3314 struct x86_emulate_ops *ops = ctxt->ops;
3315 int rc = X86EMUL_CONTINUE;
3316
3317 op->type = OP_IMM;
3318 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02003319 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003320 /* NB. Immediates are sign-extended as necessary. */
3321 switch (op->bytes) {
3322 case 1:
3323 op->val = insn_fetch(s8, 1, c->eip);
3324 break;
3325 case 2:
3326 op->val = insn_fetch(s16, 2, c->eip);
3327 break;
3328 case 4:
3329 op->val = insn_fetch(s32, 4, c->eip);
3330 break;
3331 }
3332 if (!sign_extension) {
3333 switch (op->bytes) {
3334 case 1:
3335 op->val &= 0xff;
3336 break;
3337 case 2:
3338 op->val &= 0xffff;
3339 break;
3340 case 4:
3341 op->val &= 0xffffffff;
3342 break;
3343 }
3344 }
3345done:
3346 return rc;
3347}
3348
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003349int
Andre Przywaradc25e892010-12-21 11:12:07 +01003350x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003351{
3352 struct x86_emulate_ops *ops = ctxt->ops;
3353 struct decode_cache *c = &ctxt->decode;
3354 int rc = X86EMUL_CONTINUE;
3355 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003356 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003357 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003358 struct opcode opcode;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003359 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003360
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003361 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01003362 c->fetch.start = c->eip;
3363 c->fetch.end = c->fetch.start + insn_len;
3364 if (insn_len > 0)
3365 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003366
3367 switch (mode) {
3368 case X86EMUL_MODE_REAL:
3369 case X86EMUL_MODE_VM86:
3370 case X86EMUL_MODE_PROT16:
3371 def_op_bytes = def_ad_bytes = 2;
3372 break;
3373 case X86EMUL_MODE_PROT32:
3374 def_op_bytes = def_ad_bytes = 4;
3375 break;
3376#ifdef CONFIG_X86_64
3377 case X86EMUL_MODE_PROT64:
3378 def_op_bytes = 4;
3379 def_ad_bytes = 8;
3380 break;
3381#endif
3382 default:
3383 return -1;
3384 }
3385
3386 c->op_bytes = def_op_bytes;
3387 c->ad_bytes = def_ad_bytes;
3388
3389 /* Legacy prefixes. */
3390 for (;;) {
3391 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3392 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003393 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003394 /* switch between 2/4 bytes */
3395 c->op_bytes = def_op_bytes ^ 6;
3396 break;
3397 case 0x67: /* address-size override */
3398 if (mode == X86EMUL_MODE_PROT64)
3399 /* switch between 4/8 bytes */
3400 c->ad_bytes = def_ad_bytes ^ 12;
3401 else
3402 /* switch between 2/4 bytes */
3403 c->ad_bytes = def_ad_bytes ^ 6;
3404 break;
3405 case 0x26: /* ES override */
3406 case 0x2e: /* CS override */
3407 case 0x36: /* SS override */
3408 case 0x3e: /* DS override */
3409 set_seg_override(c, (c->b >> 3) & 3);
3410 break;
3411 case 0x64: /* FS override */
3412 case 0x65: /* GS override */
3413 set_seg_override(c, c->b & 7);
3414 break;
3415 case 0x40 ... 0x4f: /* REX */
3416 if (mode != X86EMUL_MODE_PROT64)
3417 goto done_prefixes;
3418 c->rex_prefix = c->b;
3419 continue;
3420 case 0xf0: /* LOCK */
3421 c->lock_prefix = 1;
3422 break;
3423 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003424 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003425 c->rep_prefix = c->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003426 break;
3427 default:
3428 goto done_prefixes;
3429 }
3430
3431 /* Any legacy prefix after a REX prefix nullifies its effect. */
3432
3433 c->rex_prefix = 0;
3434 }
3435
3436done_prefixes:
3437
3438 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003439 if (c->rex_prefix & 8)
3440 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003441
3442 /* Opcode byte(s). */
3443 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003444 /* Two-byte opcode? */
3445 if (c->b == 0x0f) {
3446 c->twobyte = 1;
3447 c->b = insn_fetch(u8, 1, c->eip);
3448 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003449 }
3450 c->d = opcode.flags;
3451
Avi Kivity46561642011-04-24 14:09:59 +03003452 while (c->d & GroupMask) {
3453 switch (c->d & GroupMask) {
3454 case Group:
3455 c->modrm = insn_fetch(u8, 1, c->eip);
3456 --c->eip;
3457 goffset = (c->modrm >> 3) & 7;
3458 opcode = opcode.u.group[goffset];
3459 break;
3460 case GroupDual:
3461 c->modrm = insn_fetch(u8, 1, c->eip);
3462 --c->eip;
3463 goffset = (c->modrm >> 3) & 7;
3464 if ((c->modrm >> 6) == 3)
3465 opcode = opcode.u.gdual->mod3[goffset];
3466 else
3467 opcode = opcode.u.gdual->mod012[goffset];
3468 break;
3469 case RMExt:
Joerg Roedel01de8b02011-04-04 12:39:31 +02003470 goffset = c->modrm & 7;
3471 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003472 break;
3473 case Prefix:
3474 if (c->rep_prefix && op_prefix)
3475 return X86EMUL_UNHANDLEABLE;
3476 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3477 switch (simd_prefix) {
3478 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3479 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3480 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3481 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3482 }
3483 break;
3484 default:
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003485 return X86EMUL_UNHANDLEABLE;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003486 }
Avi Kivity46561642011-04-24 14:09:59 +03003487
3488 c->d &= ~GroupMask;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003489 c->d |= opcode.flags;
3490 }
3491
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003492 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003493 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003494 c->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003495
3496 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003497 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003498 return -1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003499
Avi Kivityd8671622011-02-01 16:32:03 +02003500 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3501 return -1;
3502
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003503 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3504 c->op_bytes = 8;
3505
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003506 if (c->d & Op3264) {
3507 if (mode == X86EMUL_MODE_PROT64)
3508 c->op_bytes = 8;
3509 else
3510 c->op_bytes = 4;
3511 }
3512
Avi Kivity12537912011-03-29 11:41:27 +02003513 if (c->d & Sse)
3514 c->op_bytes = 16;
3515
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003516 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003517 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003518 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003519 if (!c->has_seg_override)
3520 set_seg_override(c, c->modrm_seg);
3521 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003522 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003523 if (rc != X86EMUL_CONTINUE)
3524 goto done;
3525
3526 if (!c->has_seg_override)
3527 set_seg_override(c, VCPU_SREG_DS);
3528
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09003529 memop.addr.mem.seg = seg_override(ctxt, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003530
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003531 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003532 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003533
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003534 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003535 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003536
3537 /*
3538 * Decode and fetch the source operand: register, memory
3539 * or immediate.
3540 */
3541 switch (c->d & SrcMask) {
3542 case SrcNone:
3543 break;
3544 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003545 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003546 break;
3547 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003548 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003549 goto srcmem_common;
3550 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003551 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003552 goto srcmem_common;
3553 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003554 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003555 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003556 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003557 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003558 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003559 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003560 rc = decode_imm(ctxt, &c->src, 2, false);
3561 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003562 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003563 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3564 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003565 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003566 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003567 break;
3568 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003569 rc = decode_imm(ctxt, &c->src, 1, true);
3570 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003571 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003572 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003573 break;
3574 case SrcAcc:
3575 c->src.type = OP_REG;
3576 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003577 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003578 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003579 break;
3580 case SrcOne:
3581 c->src.bytes = 1;
3582 c->src.val = 1;
3583 break;
3584 case SrcSI:
3585 c->src.type = OP_MEM;
3586 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003587 c->src.addr.mem.ea =
3588 register_address(c, c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09003589 c->src.addr.mem.seg = seg_override(ctxt, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003590 c->src.val = 0;
3591 break;
3592 case SrcImmFAddr:
3593 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003594 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003595 c->src.bytes = c->op_bytes + 2;
3596 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3597 break;
3598 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003599 memop.bytes = c->op_bytes + 2;
3600 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003601 break;
3602 }
3603
Avi Kivity39f21ee2010-08-18 19:20:21 +03003604 if (rc != X86EMUL_CONTINUE)
3605 goto done;
3606
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003607 /*
3608 * Decode and fetch the second source operand: register, memory
3609 * or immediate.
3610 */
3611 switch (c->d & Src2Mask) {
3612 case Src2None:
3613 break;
3614 case Src2CL:
3615 c->src2.bytes = 1;
3616 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3617 break;
3618 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003619 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003620 break;
3621 case Src2One:
3622 c->src2.bytes = 1;
3623 c->src2.val = 1;
3624 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003625 case Src2Imm:
3626 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3627 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003628 }
3629
Avi Kivity39f21ee2010-08-18 19:20:21 +03003630 if (rc != X86EMUL_CONTINUE)
3631 goto done;
3632
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003633 /* Decode and fetch the destination operand: register or memory. */
3634 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003635 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003636 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003637 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3638 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003639 case DstImmUByte:
3640 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003641 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003642 c->dst.bytes = 1;
3643 c->dst.val = insn_fetch(u8, 1, c->eip);
3644 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003645 case DstMem:
3646 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003647 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003648 if ((c->d & DstMask) == DstMem64)
3649 c->dst.bytes = 8;
3650 else
3651 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003652 if (c->d & BitOp)
3653 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003654 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003655 break;
3656 case DstAcc:
3657 c->dst.type = OP_REG;
3658 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003659 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003660 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003661 c->dst.orig_val = c->dst.val;
3662 break;
3663 case DstDI:
3664 c->dst.type = OP_MEM;
3665 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003666 c->dst.addr.mem.ea =
3667 register_address(c, c->regs[VCPU_REGS_RDI]);
3668 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003669 c->dst.val = 0;
3670 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003671 case ImplicitOps:
3672 /* Special instructions do their own operand decoding. */
3673 default:
3674 c->dst.type = OP_NONE; /* Disable writeback. */
3675 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003676 }
3677
3678done:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003679 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003680}
3681
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003682static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3683{
3684 struct decode_cache *c = &ctxt->decode;
3685
3686 /* The second termination condition only applies for REPE
3687 * and REPNE. Test if the repeat string operation prefix is
3688 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3689 * corresponding termination condition according to:
3690 * - if REPE/REPZ and ZF = 0 then done
3691 * - if REPNE/REPNZ and ZF = 1 then done
3692 */
3693 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3694 (c->b == 0xae) || (c->b == 0xaf))
3695 && (((c->rep_prefix == REPE_PREFIX) &&
3696 ((ctxt->eflags & EFLG_ZF) == 0))
3697 || ((c->rep_prefix == REPNE_PREFIX) &&
3698 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3699 return true;
3700
3701 return false;
3702}
3703
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003704int
Avi Kivity9aabc882010-07-29 15:11:50 +03003705x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003706{
Avi Kivity9aabc882010-07-29 15:11:50 +03003707 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003708 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003709 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003710 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003711 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003712 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003713
Gleb Natapov9de41572010-04-28 19:15:22 +03003714 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003715
Gleb Natapov11616242010-02-11 14:43:14 +02003716 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003717 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003718 goto done;
3719 }
3720
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003721 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003722 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003723 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003724 goto done;
3725 }
3726
Avi Kivity081bca02010-08-26 11:06:15 +03003727 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003728 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003729 goto done;
3730 }
3731
Avi Kivity12537912011-03-29 11:41:27 +02003732 if ((c->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003733 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3734 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003735 rc = emulate_ud(ctxt);
3736 goto done;
3737 }
3738
Avi Kivity717746e2011-04-20 13:37:53 +03003739 if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003740 rc = emulate_nm(ctxt);
3741 goto done;
3742 }
3743
Avi Kivityc4f035c2011-04-04 12:39:22 +02003744 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003745 rc = emulator_check_intercept(ctxt, c->intercept,
3746 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003747 if (rc != X86EMUL_CONTINUE)
3748 goto done;
3749 }
3750
Gleb Natapove92805a2010-02-10 14:21:35 +02003751 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity717746e2011-04-20 13:37:53 +03003752 if ((c->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003753 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003754 goto done;
3755 }
3756
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003757 /* Instruction can only be executed in protected mode */
3758 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3759 rc = emulate_ud(ctxt);
3760 goto done;
3761 }
3762
Joerg Roedeld09beab2011-04-04 12:39:25 +02003763 /* Do instruction specific permission checks */
3764 if (c->check_perm) {
3765 rc = c->check_perm(ctxt);
3766 if (rc != X86EMUL_CONTINUE)
3767 goto done;
3768 }
3769
Avi Kivityc4f035c2011-04-04 12:39:22 +02003770 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003771 rc = emulator_check_intercept(ctxt, c->intercept,
3772 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003773 if (rc != X86EMUL_CONTINUE)
3774 goto done;
3775 }
3776
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003777 if (c->rep_prefix && (c->d & String)) {
3778 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003779 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003780 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003781 goto done;
3782 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003783 }
3784
Wei Yongjunc483c022010-08-06 15:36:36 +08003785 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003786 rc = segmented_read(ctxt, c->src.addr.mem,
3787 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003788 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003789 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003790 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003791 }
3792
Gleb Natapove35b7b92010-02-25 16:36:42 +02003793 if (c->src2.type == OP_MEM) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003794 rc = segmented_read(ctxt, c->src2.addr.mem,
3795 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003796 if (rc != X86EMUL_CONTINUE)
3797 goto done;
3798 }
3799
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003800 if ((c->d & DstMask) == ImplicitOps)
3801 goto special_insn;
3802
3803
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003804 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3805 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003806 rc = segmented_read(ctxt, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003807 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003808 if (rc != X86EMUL_CONTINUE)
3809 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003810 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003811 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003812
Avi Kivity018a98d2007-11-27 19:30:56 +02003813special_insn:
3814
Avi Kivityc4f035c2011-04-04 12:39:22 +02003815 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003816 rc = emulator_check_intercept(ctxt, c->intercept,
3817 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003818 if (rc != X86EMUL_CONTINUE)
3819 goto done;
3820 }
3821
Avi Kivityef65c882010-07-29 15:11:51 +03003822 if (c->execute) {
3823 rc = c->execute(ctxt);
3824 if (rc != X86EMUL_CONTINUE)
3825 goto done;
3826 goto writeback;
3827 }
3828
Laurent Viviere4e03de2007-09-18 11:52:50 +02003829 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 goto twobyte_insn;
3831
Laurent Viviere4e03de2007-09-18 11:52:50 +02003832 switch (c->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003833 case 0x06: /* push es */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003834 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 break;
3836 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003837 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003839 case 0x0e: /* push cs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003840 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003841 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003842 case 0x16: /* push ss */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003843 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 break;
3845 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003846 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003847 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003848 case 0x1e: /* push ds */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003849 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003850 break;
3851 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003852 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003853 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003854 case 0x40 ... 0x47: /* inc r16/r32 */
3855 emulate_1op("inc", c->dst, ctxt->eflags);
3856 break;
3857 case 0x48 ... 0x4f: /* dec r16/r32 */
3858 emulate_1op("dec", c->dst, ctxt->eflags);
3859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003861 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003863 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003865 case 0x6c: /* insb */
3866 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003867 c->src.val = c->regs[VCPU_REGS_RDX];
3868 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003869 case 0x6e: /* outsb */
3870 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003871 c->dst.val = c->regs[VCPU_REGS_RDX];
3872 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003873 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003874 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003875 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003876 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003879 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003880 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 break;
3882 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003883 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003885 c->src.val = c->dst.val;
3886 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887 /*
3888 * Write back the memory destination with implicit LOCK
3889 * prefix.
3890 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003891 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003892 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003894 case 0x8c: /* mov r/m, sreg */
3895 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003896 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003897 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003898 }
Avi Kivity1aa36612011-04-27 13:20:30 +03003899 c->dst.val = get_segment_selector(ctxt, c->modrm_reg);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003900 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003901 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003902 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003903 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003904 case 0x8e: { /* mov seg, r/m16 */
3905 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003906
3907 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003908
Gleb Natapovc6975182010-02-18 12:15:01 +02003909 if (c->modrm_reg == VCPU_SREG_CS ||
3910 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003911 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003912 goto done;
3913 }
3914
Glauber Costa310b5d32009-05-12 16:21:06 -04003915 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003916 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003917
Gleb Natapov2e873022010-03-18 15:20:18 +02003918 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003919
3920 c->dst.type = OP_NONE; /* Disable writeback. */
3921 break;
3922 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003924 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003926 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3927 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003928 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003929 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003930 case 0x98: /* cbw/cwde/cdqe */
3931 switch (c->op_bytes) {
3932 case 2: c->dst.val = (s8)c->dst.val; break;
3933 case 4: c->dst.val = (s16)c->dst.val; break;
3934 case 8: c->dst.val = (s32)c->dst.val; break;
3935 }
3936 break;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003937 case 0xa8 ... 0xa9: /* test ax, imm */
3938 goto test;
Avi Kivity018a98d2007-11-27 19:30:56 +02003939 case 0xc0 ... 0xc1:
3940 emulate_grp2(ctxt);
3941 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003942 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003943 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003944 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003945 c->dst.bytes = c->op_bytes;
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003946 rc = em_pop(ctxt);
3947 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003948 case 0xc4: /* les */
3949 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003950 break;
3951 case 0xc5: /* lds */
3952 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003953 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003954 case 0xcb: /* ret far */
3955 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003956 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003957 case 0xcc: /* int3 */
3958 irq = 3;
3959 goto do_interrupt;
3960 case 0xcd: /* int n */
3961 irq = c->src.val;
3962 do_interrupt:
3963 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003964 break;
3965 case 0xce: /* into */
3966 if (ctxt->eflags & EFLG_OF) {
3967 irq = 4;
3968 goto do_interrupt;
3969 }
3970 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003971 case 0xcf: /* iret */
3972 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003973 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003974 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003975 emulate_grp2(ctxt);
3976 break;
3977 case 0xd2 ... 0xd3: /* Grp2 */
3978 c->src.val = c->regs[VCPU_REGS_RCX];
3979 emulate_grp2(ctxt);
3980 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003981 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3982 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3983 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3984 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3985 jmp_rel(c, c->src.val);
3986 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003987 case 0xe3: /* jcxz/jecxz/jrcxz */
3988 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3989 jmp_rel(c, c->src.val);
3990 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003991 case 0xe4: /* inb */
3992 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003993 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003994 case 0xe6: /* outb */
3995 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003996 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003997 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003998 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003999 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08004000 jmp_rel(c, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09004001 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004002 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004003 }
4004 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004005 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03004006 case 0xea: { /* jmp far */
4007 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02004008 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03004009 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
4010
Gleb Natapov4947e7cd2011-04-12 12:36:23 +03004011 rc = load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS);
4012 if (rc != X86EMUL_CONTINUE)
Gleb Natapovc6975182010-02-18 12:15:01 +02004013 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004014
Gleb Natapov414e6272010-04-28 19:15:26 +03004015 c->eip = 0;
4016 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004017 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03004018 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004019 case 0xeb:
4020 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08004021 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02004022 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004023 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03004024 case 0xec: /* in al,dx */
4025 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004026 c->src.val = c->regs[VCPU_REGS_RDX];
4027 do_io_in:
Gleb Natapov7b262e92010-03-18 15:20:27 +02004028 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
4029 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004030 goto done; /* IO is needed */
4031 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08004032 case 0xee: /* out dx,al */
4033 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08004034 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004035 do_io_out:
Avi Kivityca1d4a92011-04-20 13:37:53 +03004036 ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
4037 &c->src.val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004038 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004039 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004040 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004041 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004042 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004043 case 0xf5: /* cmc */
4044 /* complement carry flag from eflags reg */
4045 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004046 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004047 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03004048 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02004049 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004050 case 0xf8: /* clc */
4051 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004052 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004053 case 0xf9: /* stc */
4054 ctxt->eflags |= EFLG_CF;
4055 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004056 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004057 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004058 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004059 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08004060 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004061 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004062 break;
4063 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004064 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004065 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004066 goto done;
4067 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004068 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004069 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004070 }
Avi Kivity111de5d2007-11-27 19:14:21 +02004071 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004072 case 0xfc: /* cld */
4073 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004074 break;
4075 case 0xfd: /* std */
4076 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004077 break;
Gleb Natapovea798492010-02-25 16:36:43 +02004078 case 0xfe: /* Grp4 */
4079 grp45:
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09004080 rc = emulate_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004081 break;
Gleb Natapovea798492010-02-25 16:36:43 +02004082 case 0xff: /* Grp5 */
4083 if (c->modrm_reg == 5)
4084 goto jump_far;
4085 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03004086 default:
4087 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004089
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004090 if (rc != X86EMUL_CONTINUE)
4091 goto done;
4092
Avi Kivity018a98d2007-11-27 19:30:56 +02004093writeback:
4094 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004095 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004096 goto done;
4097
Gleb Natapov5cd21912010-03-18 15:20:26 +02004098 /*
4099 * restore dst type in case the decoding will be reused
4100 * (happens for string instruction )
4101 */
4102 c->dst.type = saved_dst_type;
4103
Gleb Natapova682e352010-03-18 15:20:21 +02004104 if ((c->d & SrcMask) == SrcSI)
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09004105 string_addr_inc(ctxt, seg_override(ctxt, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03004106 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004107
4108 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004109 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03004110 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004111
Gleb Natapov5cd21912010-03-18 15:20:26 +02004112 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03004113 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02004114 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004115
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004116 if (!string_insn_completed(ctxt)) {
4117 /*
4118 * Re-enter guest when pio read ahead buffer is empty
4119 * or, if it is not used, after each 1024 iteration.
4120 */
4121 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
4122 (r->end == 0 || r->end != r->pos)) {
4123 /*
4124 * Reset read cache. Usually happens before
4125 * decode, but since instruction is restarted
4126 * we have to do it here.
4127 */
4128 ctxt->decode.mem_read.end = 0;
4129 return EMULATION_RESTART;
4130 }
4131 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004132 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004133 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004134
4135 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004136
4137done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004138 if (rc == X86EMUL_PROPAGATE_FAULT)
4139 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004140 if (rc == X86EMUL_INTERCEPTED)
4141 return EMULATION_INTERCEPTED;
4142
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004143 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144
4145twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02004146 switch (c->b) {
Andre Przywarae99f0502009-06-17 15:50:33 +02004147 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004148 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004149 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004150 case 0x06:
Avi Kivity2d04a052011-04-20 15:32:49 +03004151 rc = em_clts(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004152 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004153 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004154 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004155 break;
4156 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004157 case 0x0d: /* GrpP (prefetch) */
4158 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004159 break;
4160 case 0x20: /* mov cr, reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004161 c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004162 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 case 0x21: /* mov from dr to reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004164 ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004166 case 0x22: /* mov reg, cr */
Avi Kivity717746e2011-04-20 13:37:53 +03004167 if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004168 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004169 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004170 goto done;
4171 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004172 c->dst.type = OP_NONE;
4173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 case 0x23: /* mov from reg to dr */
Avi Kivity717746e2011-04-20 13:37:53 +03004175 if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004176 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004177 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004178 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004179 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004180 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004181 goto done;
4182 }
4183
Laurent Viviera01af5e2007-09-24 11:10:56 +02004184 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004186 case 0x30:
4187 /* wrmsr */
4188 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4189 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Avi Kivity717746e2011-04-20 13:37:53 +03004190 if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004191 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004192 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004193 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004194 }
4195 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004196 break;
4197 case 0x32:
4198 /* rdmsr */
Avi Kivity717746e2011-04-20 13:37:53 +03004199 if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004200 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004201 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004202 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004203 } else {
4204 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4205 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4206 }
4207 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004208 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004209 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004210 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004211 break;
4212 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004213 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004214 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004216 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004217 if (!test_cc(c->b, ctxt->eflags))
4218 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004220 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004221 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004222 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004223 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004224 case 0x90 ... 0x9f: /* setcc r/m8 */
4225 c->dst.val = test_cc(c->b, ctxt->eflags);
4226 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004227 case 0xa0: /* push fs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004228 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004229 break;
4230 case 0xa1: /* pop fs */
4231 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004232 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004233 case 0xa3:
4234 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004235 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004236 /* only subword offset */
4237 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004238 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004239 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004240 case 0xa4: /* shld imm8, r, r/m */
4241 case 0xa5: /* shld cl, r, r/m */
4242 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4243 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004244 case 0xa8: /* push gs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004245 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004246 break;
4247 case 0xa9: /* pop gs */
4248 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004249 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004250 case 0xab:
4251 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004252 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004253 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004254 case 0xac: /* shrd imm8, r, r/m */
4255 case 0xad: /* shrd cl, r, r/m */
4256 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4257 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004258 case 0xae: /* clflush */
4259 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260 case 0xb0 ... 0xb1: /* cmpxchg */
4261 /*
4262 * Save real source value, then compare EAX against
4263 * destination.
4264 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004265 c->src.orig_val = c->src.val;
4266 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004267 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4268 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004270 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004271 } else {
4272 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004273 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03004274 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275 }
4276 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004277 case 0xb2: /* lss */
4278 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280 case 0xb3:
4281 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004282 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004284 case 0xb4: /* lfs */
4285 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004286 break;
4287 case 0xb5: /* lgs */
4288 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004289 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004290 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004291 c->dst.bytes = c->op_bytes;
4292 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4293 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004294 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004295 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004296 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004297 case 0:
4298 goto bt;
4299 case 1:
4300 goto bts;
4301 case 2:
4302 goto btr;
4303 case 3:
4304 goto btc;
4305 }
4306 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004307 case 0xbb:
4308 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004309 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004310 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004311 case 0xbc: { /* bsf */
4312 u8 zf;
4313 __asm__ ("bsf %2, %0; setz %1"
4314 : "=r"(c->dst.val), "=q"(zf)
4315 : "r"(c->src.val));
4316 ctxt->eflags &= ~X86_EFLAGS_ZF;
4317 if (zf) {
4318 ctxt->eflags |= X86_EFLAGS_ZF;
4319 c->dst.type = OP_NONE; /* Disable writeback. */
4320 }
4321 break;
4322 }
4323 case 0xbd: { /* bsr */
4324 u8 zf;
4325 __asm__ ("bsr %2, %0; setz %1"
4326 : "=r"(c->dst.val), "=q"(zf)
4327 : "r"(c->src.val));
4328 ctxt->eflags &= ~X86_EFLAGS_ZF;
4329 if (zf) {
4330 ctxt->eflags |= X86_EFLAGS_ZF;
4331 c->dst.type = OP_NONE; /* Disable writeback. */
4332 }
4333 break;
4334 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004336 c->dst.bytes = c->op_bytes;
4337 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4338 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004340 case 0xc0 ... 0xc1: /* xadd */
4341 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4342 /* Write back the register source. */
4343 c->src.val = c->dst.orig_val;
4344 write_register_operand(&c->src);
4345 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004346 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004347 c->dst.bytes = c->op_bytes;
4348 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4349 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004350 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004352 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004353 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004354 default:
4355 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004357
4358 if (rc != X86EMUL_CONTINUE)
4359 goto done;
4360
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 goto writeback;
4362
4363cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004364 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}