blob: 3624f202b440aaf1251d5a6c29684c2a56bbca00 [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,
503 struct x86_emulate_ops *ops,
504 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300505{
506 if (!c->has_seg_override)
507 return 0;
508
Avi Kivity90de84f2010-11-17 15:28:21 +0200509 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300510}
511
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200512static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
513 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300514{
Avi Kivityda9cb572010-11-22 17:53:21 +0200515 ctxt->exception.vector = vec;
516 ctxt->exception.error_code = error;
517 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300519}
520
Joerg Roedel3b88e412011-04-04 12:39:29 +0200521static int emulate_db(struct x86_emulate_ctxt *ctxt)
522{
523 return emulate_exception(ctxt, DB_VECTOR, 0, false);
524}
525
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200526static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300527{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200528 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300529}
530
Avi Kivity618ff152011-04-03 12:32:09 +0300531static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
532{
533 return emulate_exception(ctxt, SS_VECTOR, err, true);
534}
535
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200536static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300537{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200538 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300539}
540
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200541static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300542{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200543 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300544}
545
Avi Kivity34d1f492010-08-26 11:59:01 +0300546static int emulate_de(struct x86_emulate_ctxt *ctxt)
547{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200548 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300549}
550
Avi Kivity12537912011-03-29 11:41:27 +0200551static int emulate_nm(struct x86_emulate_ctxt *ctxt)
552{
553 return emulate_exception(ctxt, NM_VECTOR, 0, false);
554}
555
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400556static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300557 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400558 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300559 ulong *linear)
560{
561 struct decode_cache *c = &ctxt->decode;
Avi Kivity618ff152011-04-03 12:32:09 +0300562 struct desc_struct desc;
563 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300564 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300565 u32 lim;
566 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300567
568 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300569 switch (ctxt->mode) {
570 case X86EMUL_MODE_REAL:
571 break;
572 case X86EMUL_MODE_PROT64:
573 if (((signed long)la << 16) >> 16 != la)
574 return emulate_gp(ctxt, 0);
575 break;
576 default:
Avi Kivity4bff1e862011-04-20 13:37:53 +0300577 usable = ctxt->ops->get_cached_descriptor(ctxt, &desc, NULL,
578 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300579 if (!usable)
580 goto bad;
581 /* code segment or read-only data segment */
582 if (((desc.type & 8) || !(desc.type & 2)) && write)
583 goto bad;
584 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400585 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300586 goto bad;
587 lim = desc_limit_scaled(&desc);
588 if ((desc.type & 8) || !(desc.type & 4)) {
589 /* expand-up segment */
590 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
591 goto bad;
592 } else {
593 /* exapand-down segment */
594 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
595 goto bad;
596 lim = desc.d ? 0xffffffff : 0xffff;
597 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
598 goto bad;
599 }
Avi Kivity717746e2011-04-20 13:37:53 +0300600 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity4bff1e862011-04-20 13:37:53 +0300601 rpl = ctxt->ops->get_segment_selector(ctxt, addr.seg) & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300602 cpl = max(cpl, rpl);
603 if (!(desc.type & 8)) {
604 /* data segment */
605 if (cpl > desc.dpl)
606 goto bad;
607 } else if ((desc.type & 8) && !(desc.type & 4)) {
608 /* nonconforming code segment */
609 if (cpl != desc.dpl)
610 goto bad;
611 } else if ((desc.type & 8) && (desc.type & 4)) {
612 /* conforming code segment */
613 if (cpl < desc.dpl)
614 goto bad;
615 }
616 break;
617 }
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400618 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300619 la &= (u32)-1;
620 *linear = la;
621 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300622bad:
623 if (addr.seg == VCPU_SREG_SS)
624 return emulate_ss(ctxt, addr.seg);
625 else
626 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300627}
628
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400629static int linearize(struct x86_emulate_ctxt *ctxt,
630 struct segmented_address addr,
631 unsigned size, bool write,
632 ulong *linear)
633{
634 return __linearize(ctxt, addr, size, write, false, linear);
635}
636
637
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200638static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
639 struct segmented_address addr,
640 void *data,
641 unsigned size)
642{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200643 int rc;
644 ulong linear;
645
Avi Kivity83b87952011-04-03 11:31:19 +0300646 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200647 if (rc != X86EMUL_CONTINUE)
648 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300649 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200650}
651
Avi Kivity62266862007-11-20 13:15:52 +0200652static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
653 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300654 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200655{
656 struct fetch_cache *fc = &ctxt->decode.fetch;
657 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300658 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200659
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300660 if (eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400661 unsigned long linear;
662 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300663 cur_size = fc->end - fc->start;
664 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400665 rc = __linearize(ctxt, addr, size, false, true, &linear);
666 if (rc != X86EMUL_CONTINUE)
667 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300668 rc = ops->fetch(ctxt, linear, fc->data + cur_size,
669 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900670 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200671 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300672 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200673 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300674 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900675 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200676}
677
678static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
679 struct x86_emulate_ops *ops,
680 unsigned long eip, void *dest, unsigned size)
681{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900682 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200683
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200684 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200685 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200686 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200687 while (size--) {
688 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900689 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200690 return rc;
691 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900692 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200693}
694
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000695/*
696 * Given the 'reg' portion of a ModRM byte, and a register block, return a
697 * pointer into the block that addresses the relevant register.
698 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
699 */
700static void *decode_register(u8 modrm_reg, unsigned long *regs,
701 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702{
703 void *p;
704
705 p = &regs[modrm_reg];
706 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
707 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
708 return p;
709}
710
711static int read_descriptor(struct x86_emulate_ctxt *ctxt,
712 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200713 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 u16 *size, unsigned long *address, int op_bytes)
715{
716 int rc;
717
718 if (op_bytes == 2)
719 op_bytes = 3;
720 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200721 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900722 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200724 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200725 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 return rc;
727}
728
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300729static int test_cc(unsigned int condition, unsigned int flags)
730{
731 int rc = 0;
732
733 switch ((condition & 15) >> 1) {
734 case 0: /* o */
735 rc |= (flags & EFLG_OF);
736 break;
737 case 1: /* b/c/nae */
738 rc |= (flags & EFLG_CF);
739 break;
740 case 2: /* z/e */
741 rc |= (flags & EFLG_ZF);
742 break;
743 case 3: /* be/na */
744 rc |= (flags & (EFLG_CF|EFLG_ZF));
745 break;
746 case 4: /* s */
747 rc |= (flags & EFLG_SF);
748 break;
749 case 5: /* p/pe */
750 rc |= (flags & EFLG_PF);
751 break;
752 case 7: /* le/ng */
753 rc |= (flags & EFLG_ZF);
754 /* fall through */
755 case 6: /* l/nge */
756 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
757 break;
758 }
759
760 /* Odd condition identifiers (lsb == 1) have inverted sense. */
761 return (!!rc ^ (condition & 1));
762}
763
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300764static void fetch_register_operand(struct operand *op)
765{
766 switch (op->bytes) {
767 case 1:
768 op->val = *(u8 *)op->addr.reg;
769 break;
770 case 2:
771 op->val = *(u16 *)op->addr.reg;
772 break;
773 case 4:
774 op->val = *(u32 *)op->addr.reg;
775 break;
776 case 8:
777 op->val = *(u64 *)op->addr.reg;
778 break;
779 }
780}
781
Avi Kivity12537912011-03-29 11:41:27 +0200782static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
783{
784 ctxt->ops->get_fpu(ctxt);
785 switch (reg) {
786 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
787 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
788 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
789 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
790 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
791 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
792 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
793 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
794#ifdef CONFIG_X86_64
795 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
796 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
797 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
798 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
799 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
800 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
801 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
802 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
803#endif
804 default: BUG();
805 }
806 ctxt->ops->put_fpu(ctxt);
807}
808
809static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
810 int reg)
811{
812 ctxt->ops->get_fpu(ctxt);
813 switch (reg) {
814 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
815 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
816 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
817 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
818 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
819 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
820 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
821 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
822#ifdef CONFIG_X86_64
823 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
824 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
825 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
826 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
827 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
828 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
829 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
830 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
831#endif
832 default: BUG();
833 }
834 ctxt->ops->put_fpu(ctxt);
835}
836
837static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
838 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200839 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200840 int inhibit_bytereg)
841{
Avi Kivity33615aa2007-10-31 11:15:56 +0200842 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200843 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200844
845 if (!(c->d & ModRM))
846 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200847
848 if (c->d & Sse) {
849 op->type = OP_XMM;
850 op->bytes = 16;
851 op->addr.xmm = reg;
852 read_sse_reg(ctxt, &op->vec_val, reg);
853 return;
854 }
855
Avi Kivity3c118e22007-10-31 10:27:04 +0200856 op->type = OP_REG;
857 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300858 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200859 op->bytes = 1;
860 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300861 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200862 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200863 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300864 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200865 op->orig_val = op->val;
866}
867
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200868static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300869 struct x86_emulate_ops *ops,
870 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871{
872 struct decode_cache *c = &ctxt->decode;
873 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700874 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900875 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300876 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877
878 if (c->rex_prefix) {
879 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
880 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
881 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
882 }
883
884 c->modrm = insn_fetch(u8, 1, c->eip);
885 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
886 c->modrm_reg |= (c->modrm & 0x38) >> 3;
887 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300888 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889
890 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300891 op->type = OP_REG;
892 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
893 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300894 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200895 if (c->d & Sse) {
896 op->type = OP_XMM;
897 op->bytes = 16;
898 op->addr.xmm = c->modrm_rm;
899 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
900 return rc;
901 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300902 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200903 return rc;
904 }
905
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300906 op->type = OP_MEM;
907
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200908 if (c->ad_bytes == 2) {
909 unsigned bx = c->regs[VCPU_REGS_RBX];
910 unsigned bp = c->regs[VCPU_REGS_RBP];
911 unsigned si = c->regs[VCPU_REGS_RSI];
912 unsigned di = c->regs[VCPU_REGS_RDI];
913
914 /* 16-bit ModR/M decode. */
915 switch (c->modrm_mod) {
916 case 0:
917 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300918 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200919 break;
920 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300921 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200922 break;
923 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300924 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200925 break;
926 }
927 switch (c->modrm_rm) {
928 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300929 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200930 break;
931 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300932 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200933 break;
934 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300935 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200936 break;
937 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300938 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939 break;
940 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300941 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 break;
943 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300944 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200945 break;
946 case 6:
947 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300948 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 break;
950 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300951 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200952 break;
953 }
954 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
955 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300956 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300957 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200958 } else {
959 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700960 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961 sib = insn_fetch(u8, 1, c->eip);
962 index_reg |= (sib >> 3) & 7;
963 base_reg |= sib & 7;
964 scale = sib >> 6;
965
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700966 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300967 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700968 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300969 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700970 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300971 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700972 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
973 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700974 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700975 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300976 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 switch (c->modrm_mod) {
978 case 0:
979 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300980 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200981 break;
982 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300983 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200984 break;
985 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300986 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200987 break;
988 }
989 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200990 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200991done:
992 return rc;
993}
994
995static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300996 struct x86_emulate_ops *ops,
997 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998{
999 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001000 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001002 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003 switch (c->ad_bytes) {
1004 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +02001005 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006 break;
1007 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +02001008 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001009 break;
1010 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +02001011 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001012 break;
1013 }
1014done:
1015 return rc;
1016}
1017
Wei Yongjun35c843c2010-08-09 11:34:56 +08001018static void fetch_bit_operand(struct decode_cache *c)
1019{
Sheng Yang7129eec2010-09-28 16:33:32 +08001020 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001021
Wei Yongjun3885f182010-08-09 11:37:37 +08001022 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +08001023 mask = ~(c->dst.bytes * 8 - 1);
1024
1025 if (c->src.bytes == 2)
1026 sv = (s16)c->src.val & (s16)mask;
1027 else if (c->src.bytes == 4)
1028 sv = (s32)c->src.val & (s32)mask;
1029
Avi Kivity90de84f2010-11-17 15:28:21 +02001030 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001031 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001032
1033 /* only subword offset */
1034 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001035}
1036
Gleb Natapov9de41572010-04-28 19:15:22 +03001037static int read_emulated(struct x86_emulate_ctxt *ctxt,
1038 struct x86_emulate_ops *ops,
1039 unsigned long addr, void *dest, unsigned size)
1040{
1041 int rc;
1042 struct read_cache *mc = &ctxt->decode.mem_read;
1043
1044 while (size) {
1045 int n = min(size, 8u);
1046 size -= n;
1047 if (mc->pos < mc->end)
1048 goto read_cached;
1049
Avi Kivity0f65dd72011-04-20 13:37:53 +03001050 rc = ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1051 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001052 if (rc != X86EMUL_CONTINUE)
1053 return rc;
1054 mc->end += n;
1055
1056 read_cached:
1057 memcpy(dest, mc->data + mc->pos, n);
1058 mc->pos += n;
1059 dest += n;
1060 addr += n;
1061 }
1062 return X86EMUL_CONTINUE;
1063}
1064
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001065static int segmented_read(struct x86_emulate_ctxt *ctxt,
1066 struct segmented_address addr,
1067 void *data,
1068 unsigned size)
1069{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001070 int rc;
1071 ulong linear;
1072
Avi Kivity83b87952011-04-03 11:31:19 +03001073 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001074 if (rc != X86EMUL_CONTINUE)
1075 return rc;
1076 return read_emulated(ctxt, ctxt->ops, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001077}
1078
1079static int segmented_write(struct x86_emulate_ctxt *ctxt,
1080 struct segmented_address addr,
1081 const void *data,
1082 unsigned size)
1083{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001084 int rc;
1085 ulong linear;
1086
Avi Kivity83b87952011-04-03 11:31:19 +03001087 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001088 if (rc != X86EMUL_CONTINUE)
1089 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001090 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1091 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001092}
1093
1094static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1095 struct segmented_address addr,
1096 const void *orig_data, const void *data,
1097 unsigned size)
1098{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001099 int rc;
1100 ulong linear;
1101
Avi Kivity83b87952011-04-03 11:31:19 +03001102 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001103 if (rc != X86EMUL_CONTINUE)
1104 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001105 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1106 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001107}
1108
Gleb Natapov7b262e92010-03-18 15:20:27 +02001109static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1110 struct x86_emulate_ops *ops,
1111 unsigned int size, unsigned short port,
1112 void *dest)
1113{
1114 struct read_cache *rc = &ctxt->decode.io_read;
1115
1116 if (rc->pos == rc->end) { /* refill pio read ahead */
1117 struct decode_cache *c = &ctxt->decode;
1118 unsigned int in_page, n;
1119 unsigned int count = c->rep_prefix ?
1120 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1121 in_page = (ctxt->eflags & EFLG_DF) ?
1122 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1123 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1124 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1125 count);
1126 if (n == 0)
1127 n = 1;
1128 rc->pos = rc->end = 0;
Avi Kivityca1d4a92011-04-20 13:37:53 +03001129 if (!ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001130 return 0;
1131 rc->end = n * size;
1132 }
1133
1134 memcpy(dest, rc->data + rc->pos, size);
1135 rc->pos += size;
1136 return 1;
1137}
1138
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001139static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1140 struct x86_emulate_ops *ops,
1141 u16 selector, struct desc_ptr *dt)
1142{
1143 if (selector & 1 << 2) {
1144 struct desc_struct desc;
1145 memset (dt, 0, sizeof *dt);
Avi Kivity4bff1e862011-04-20 13:37:53 +03001146 if (!ops->get_cached_descriptor(ctxt, &desc, NULL,
1147 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001148 return;
1149
1150 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1151 dt->address = get_desc_base(&desc);
1152 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001153 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154}
1155
1156/* allowed just for 8 bytes segments */
1157static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1158 struct x86_emulate_ops *ops,
1159 u16 selector, struct desc_struct *desc)
1160{
1161 struct desc_ptr dt;
1162 u16 index = selector >> 3;
1163 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164 ulong addr;
1165
1166 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1167
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001168 if (dt.size < index * 8 + 7)
1169 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001170 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001171 ret = ops->read_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001172
1173 return ret;
1174}
1175
1176/* allowed just for 8 bytes segments */
1177static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1178 struct x86_emulate_ops *ops,
1179 u16 selector, struct desc_struct *desc)
1180{
1181 struct desc_ptr dt;
1182 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001183 ulong addr;
1184 int ret;
1185
1186 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1187
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001188 if (dt.size < index * 8 + 7)
1189 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001190
1191 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001192 ret = ops->write_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193
1194 return ret;
1195}
1196
Gleb Natapov5601d052011-03-07 14:55:06 +02001197/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1199 struct x86_emulate_ops *ops,
1200 u16 selector, int seg)
1201{
1202 struct desc_struct seg_desc;
1203 u8 dpl, rpl, cpl;
1204 unsigned err_vec = GP_VECTOR;
1205 u32 err_code = 0;
1206 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1207 int ret;
1208
1209 memset(&seg_desc, 0, sizeof seg_desc);
1210
1211 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1212 || ctxt->mode == X86EMUL_MODE_REAL) {
1213 /* set real mode segment descriptor */
1214 set_desc_base(&seg_desc, selector << 4);
1215 set_desc_limit(&seg_desc, 0xffff);
1216 seg_desc.type = 3;
1217 seg_desc.p = 1;
1218 seg_desc.s = 1;
1219 goto load;
1220 }
1221
1222 /* NULL selector is not valid for TR, CS and SS */
1223 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1224 && null_selector)
1225 goto exception;
1226
1227 /* TR should be in GDT only */
1228 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1229 goto exception;
1230
1231 if (null_selector) /* for NULL selector skip all following checks */
1232 goto load;
1233
1234 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1235 if (ret != X86EMUL_CONTINUE)
1236 return ret;
1237
1238 err_code = selector & 0xfffc;
1239 err_vec = GP_VECTOR;
1240
1241 /* can't load system descriptor into segment selecor */
1242 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1243 goto exception;
1244
1245 if (!seg_desc.p) {
1246 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1247 goto exception;
1248 }
1249
1250 rpl = selector & 3;
1251 dpl = seg_desc.dpl;
Avi Kivity717746e2011-04-20 13:37:53 +03001252 cpl = ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001253
1254 switch (seg) {
1255 case VCPU_SREG_SS:
1256 /*
1257 * segment is not a writable data segment or segment
1258 * selector's RPL != CPL or segment selector's RPL != CPL
1259 */
1260 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1261 goto exception;
1262 break;
1263 case VCPU_SREG_CS:
1264 if (!(seg_desc.type & 8))
1265 goto exception;
1266
1267 if (seg_desc.type & 4) {
1268 /* conforming */
1269 if (dpl > cpl)
1270 goto exception;
1271 } else {
1272 /* nonconforming */
1273 if (rpl > cpl || dpl != cpl)
1274 goto exception;
1275 }
1276 /* CS(RPL) <- CPL */
1277 selector = (selector & 0xfffc) | cpl;
1278 break;
1279 case VCPU_SREG_TR:
1280 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1281 goto exception;
1282 break;
1283 case VCPU_SREG_LDTR:
1284 if (seg_desc.s || seg_desc.type != 2)
1285 goto exception;
1286 break;
1287 default: /* DS, ES, FS, or GS */
1288 /*
1289 * segment is not a data or readable code segment or
1290 * ((segment is a data or nonconforming code segment)
1291 * and (both RPL and CPL > DPL))
1292 */
1293 if ((seg_desc.type & 0xa) == 0x8 ||
1294 (((seg_desc.type & 0xc) != 0xc) &&
1295 (rpl > dpl && cpl > dpl)))
1296 goto exception;
1297 break;
1298 }
1299
1300 if (seg_desc.s) {
1301 /* mark segment as accessed */
1302 seg_desc.type |= 1;
1303 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1304 if (ret != X86EMUL_CONTINUE)
1305 return ret;
1306 }
1307load:
Avi Kivity4bff1e862011-04-20 13:37:53 +03001308 ops->set_segment_selector(ctxt, selector, seg);
1309 ops->set_cached_descriptor(ctxt, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001310 return X86EMUL_CONTINUE;
1311exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001312 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001313 return X86EMUL_PROPAGATE_FAULT;
1314}
1315
Wei Yongjun31be40b2010-08-17 09:17:30 +08001316static void write_register_operand(struct operand *op)
1317{
1318 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1319 switch (op->bytes) {
1320 case 1:
1321 *(u8 *)op->addr.reg = (u8)op->val;
1322 break;
1323 case 2:
1324 *(u16 *)op->addr.reg = (u16)op->val;
1325 break;
1326 case 4:
1327 *op->addr.reg = (u32)op->val;
1328 break; /* 64b: zero-extend */
1329 case 8:
1330 *op->addr.reg = op->val;
1331 break;
1332 }
1333}
1334
Wei Yongjunc37eda12010-06-15 09:03:33 +08001335static inline int writeback(struct x86_emulate_ctxt *ctxt,
1336 struct x86_emulate_ops *ops)
1337{
1338 int rc;
1339 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001340
1341 switch (c->dst.type) {
1342 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001343 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001344 break;
1345 case OP_MEM:
1346 if (c->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001347 rc = segmented_cmpxchg(ctxt,
1348 c->dst.addr.mem,
1349 &c->dst.orig_val,
1350 &c->dst.val,
1351 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001352 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001353 rc = segmented_write(ctxt,
1354 c->dst.addr.mem,
1355 &c->dst.val,
1356 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001357 if (rc != X86EMUL_CONTINUE)
1358 return rc;
1359 break;
Avi Kivity12537912011-03-29 11:41:27 +02001360 case OP_XMM:
1361 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1362 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001363 case OP_NONE:
1364 /* no writeback */
1365 break;
1366 default:
1367 break;
1368 }
1369 return X86EMUL_CONTINUE;
1370}
1371
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001372static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373{
1374 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001375 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376
Harvey Harrison7a9572752008-02-19 07:40:41 -08001377 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001378 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1379 addr.seg = VCPU_SREG_SS;
1380
1381 /* Disable writeback. */
1382 c->dst.type = OP_NONE;
1383 return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384}
1385
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001386static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001387 struct x86_emulate_ops *ops,
1388 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389{
1390 struct decode_cache *c = &ctxt->decode;
1391 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001392 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393
Avi Kivity90de84f2010-11-17 15:28:21 +02001394 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1395 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001396 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001397 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398 return rc;
1399
Avi Kivity350f69d2009-01-05 11:12:40 +02001400 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001401 return rc;
1402}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001404static int em_pop(struct x86_emulate_ctxt *ctxt)
1405{
1406 struct decode_cache *c = &ctxt->decode;
1407
1408 return emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
1409}
1410
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001411static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1412 struct x86_emulate_ops *ops,
1413 void *dest, int len)
1414{
1415 int rc;
1416 unsigned long val, change_mask;
1417 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03001418 int cpl = ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001419
1420 rc = emulate_pop(ctxt, ops, &val, len);
1421 if (rc != X86EMUL_CONTINUE)
1422 return rc;
1423
1424 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1425 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1426
1427 switch(ctxt->mode) {
1428 case X86EMUL_MODE_PROT64:
1429 case X86EMUL_MODE_PROT32:
1430 case X86EMUL_MODE_PROT16:
1431 if (cpl == 0)
1432 change_mask |= EFLG_IOPL;
1433 if (cpl <= iopl)
1434 change_mask |= EFLG_IF;
1435 break;
1436 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001437 if (iopl < 3)
1438 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001439 change_mask |= EFLG_IF;
1440 break;
1441 default: /* real mode */
1442 change_mask |= (EFLG_IOPL | EFLG_IF);
1443 break;
1444 }
1445
1446 *(unsigned long *)dest =
1447 (ctxt->eflags & ~change_mask) | (val & change_mask);
1448
1449 return rc;
1450}
1451
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001452static int em_popf(struct x86_emulate_ctxt *ctxt)
1453{
1454 struct decode_cache *c = &ctxt->decode;
1455
1456 c->dst.type = OP_REG;
1457 c->dst.addr.reg = &ctxt->eflags;
1458 c->dst.bytes = c->op_bytes;
1459 return emulate_popf(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
1460}
1461
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001462static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1463 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001464{
1465 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001466
Avi Kivity4bff1e862011-04-20 13:37:53 +03001467 c->src.val = ops->get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001468
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001469 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001470}
1471
1472static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1473 struct x86_emulate_ops *ops, int seg)
1474{
1475 struct decode_cache *c = &ctxt->decode;
1476 unsigned long selector;
1477 int rc;
1478
1479 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001480 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001481 return rc;
1482
Gleb Natapov2e873022010-03-18 15:20:18 +02001483 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001484 return rc;
1485}
1486
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001487static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001488{
1489 struct decode_cache *c = &ctxt->decode;
1490 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001491 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001492 int reg = VCPU_REGS_RAX;
1493
1494 while (reg <= VCPU_REGS_RDI) {
1495 (reg == VCPU_REGS_RSP) ?
1496 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1497
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001498 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001499 if (rc != X86EMUL_CONTINUE)
1500 return rc;
1501
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001502 ++reg;
1503 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001504
Wei Yongjunc37eda12010-06-15 09:03:33 +08001505 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001506}
1507
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001508static int em_pushf(struct x86_emulate_ctxt *ctxt)
1509{
1510 struct decode_cache *c = &ctxt->decode;
1511
1512 c->src.val = (unsigned long)ctxt->eflags;
1513 return em_push(ctxt);
1514}
1515
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001516static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001517{
1518 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001519 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001520 int reg = VCPU_REGS_RDI;
1521
1522 while (reg >= VCPU_REGS_RAX) {
1523 if (reg == VCPU_REGS_RSP) {
1524 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1525 c->op_bytes);
1526 --reg;
1527 }
1528
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001529 rc = emulate_pop(ctxt, ctxt->ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001530 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001531 break;
1532 --reg;
1533 }
1534 return rc;
1535}
1536
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001537int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1538 struct x86_emulate_ops *ops, int irq)
1539{
1540 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001541 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001542 struct desc_ptr dt;
1543 gva_t cs_addr;
1544 gva_t eip_addr;
1545 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001546
1547 /* TODO: Add limit checks */
1548 c->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001549 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001552
1553 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1554
Avi Kivity4bff1e862011-04-20 13:37:53 +03001555 c->src.val = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001556 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001557 if (rc != X86EMUL_CONTINUE)
1558 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001559
1560 c->src.val = c->eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001561 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001562 if (rc != X86EMUL_CONTINUE)
1563 return rc;
1564
Avi Kivity4bff1e862011-04-20 13:37:53 +03001565 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001566
1567 eip_addr = dt.address + (irq << 2);
1568 cs_addr = dt.address + (irq << 2) + 2;
1569
Avi Kivity0f65dd72011-04-20 13:37:53 +03001570 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001571 if (rc != X86EMUL_CONTINUE)
1572 return rc;
1573
Avi Kivity0f65dd72011-04-20 13:37:53 +03001574 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001575 if (rc != X86EMUL_CONTINUE)
1576 return rc;
1577
1578 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1579 if (rc != X86EMUL_CONTINUE)
1580 return rc;
1581
1582 c->eip = eip;
1583
1584 return rc;
1585}
1586
1587static int emulate_int(struct x86_emulate_ctxt *ctxt,
1588 struct x86_emulate_ops *ops, int irq)
1589{
1590 switch(ctxt->mode) {
1591 case X86EMUL_MODE_REAL:
1592 return emulate_int_real(ctxt, ops, irq);
1593 case X86EMUL_MODE_VM86:
1594 case X86EMUL_MODE_PROT16:
1595 case X86EMUL_MODE_PROT32:
1596 case X86EMUL_MODE_PROT64:
1597 default:
1598 /* Protected mode interrupts unimplemented yet */
1599 return X86EMUL_UNHANDLEABLE;
1600 }
1601}
1602
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001603static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1604 struct x86_emulate_ops *ops)
1605{
1606 struct decode_cache *c = &ctxt->decode;
1607 int rc = X86EMUL_CONTINUE;
1608 unsigned long temp_eip = 0;
1609 unsigned long temp_eflags = 0;
1610 unsigned long cs = 0;
1611 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1612 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1613 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1614 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1615
1616 /* TODO: Add stack limit check */
1617
1618 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1619
1620 if (rc != X86EMUL_CONTINUE)
1621 return rc;
1622
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001623 if (temp_eip & ~0xffff)
1624 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001625
1626 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1627
1628 if (rc != X86EMUL_CONTINUE)
1629 return rc;
1630
1631 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1632
1633 if (rc != X86EMUL_CONTINUE)
1634 return rc;
1635
1636 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1637
1638 if (rc != X86EMUL_CONTINUE)
1639 return rc;
1640
1641 c->eip = temp_eip;
1642
1643
1644 if (c->op_bytes == 4)
1645 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1646 else if (c->op_bytes == 2) {
1647 ctxt->eflags &= ~0xffff;
1648 ctxt->eflags |= temp_eflags;
1649 }
1650
1651 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1652 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1653
1654 return rc;
1655}
1656
1657static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1658 struct x86_emulate_ops* ops)
1659{
1660 switch(ctxt->mode) {
1661 case X86EMUL_MODE_REAL:
1662 return emulate_iret_real(ctxt, ops);
1663 case X86EMUL_MODE_VM86:
1664 case X86EMUL_MODE_PROT16:
1665 case X86EMUL_MODE_PROT32:
1666 case X86EMUL_MODE_PROT64:
1667 default:
1668 /* iret from protected mode unimplemented yet */
1669 return X86EMUL_UNHANDLEABLE;
1670 }
1671}
1672
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001673static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1674 struct x86_emulate_ops *ops)
1675{
1676 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001677
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001678 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001679}
1680
Laurent Vivier05f086f2007-09-24 11:10:55 +02001681static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001682{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001683 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684 switch (c->modrm_reg) {
1685 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001686 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687 break;
1688 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001689 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690 break;
1691 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001692 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693 break;
1694 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001695 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 break;
1697 case 4: /* sal/shl */
1698 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001699 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700 break;
1701 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001702 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703 break;
1704 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001705 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706 break;
1707 }
1708}
1709
1710static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001711 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001712{
1713 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001714 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1715 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001716 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717
1718 switch (c->modrm_reg) {
1719 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001720 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721 break;
1722 case 2: /* not */
1723 c->dst.val = ~c->dst.val;
1724 break;
1725 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001726 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001727 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001728 case 4: /* mul */
1729 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1730 break;
1731 case 5: /* imul */
1732 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1733 break;
1734 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001735 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1736 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001737 break;
1738 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001739 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1740 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001741 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001742 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001743 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001745 if (de)
1746 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001747 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001748}
1749
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001750static int emulate_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751{
1752 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001753 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754
1755 switch (c->modrm_reg) {
1756 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001757 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001758 break;
1759 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001760 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001762 case 2: /* call near abs */ {
1763 long int old_eip;
1764 old_eip = c->eip;
1765 c->eip = c->src.val;
1766 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001767 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001768 break;
1769 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001771 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 break;
1773 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001774 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001777 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778}
1779
1780static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001781 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782{
1783 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001784 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001785
1786 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1787 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1789 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001790 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001792 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1793 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794
Laurent Vivier05f086f2007-09-24 11:10:55 +02001795 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001797 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001798}
1799
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001800static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1801 struct x86_emulate_ops *ops)
1802{
1803 struct decode_cache *c = &ctxt->decode;
1804 int rc;
1805 unsigned long cs;
1806
1807 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001808 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001809 return rc;
1810 if (c->op_bytes == 4)
1811 c->eip = (u32)c->eip;
1812 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001813 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001814 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001815 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001816 return rc;
1817}
1818
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001819static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1820 struct x86_emulate_ops *ops, int seg)
1821{
1822 struct decode_cache *c = &ctxt->decode;
1823 unsigned short sel;
1824 int rc;
1825
1826 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1827
1828 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1829 if (rc != X86EMUL_CONTINUE)
1830 return rc;
1831
1832 c->dst.val = c->src.val;
1833 return rc;
1834}
1835
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001836static inline void
1837setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001838 struct x86_emulate_ops *ops, struct desc_struct *cs,
1839 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001840{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001841 memset(cs, 0, sizeof(struct desc_struct));
Avi Kivity4bff1e862011-04-20 13:37:53 +03001842 ops->get_cached_descriptor(ctxt, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001844
1845 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001846 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001847 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001848 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849 cs->type = 0x0b; /* Read, Execute, Accessed */
1850 cs->s = 1;
1851 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 cs->p = 1;
1853 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854
Gleb Natapov79168fd2010-04-28 19:15:30 +03001855 set_desc_base(ss, 0); /* flat segment */
1856 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001857 ss->g = 1; /* 4kb granularity */
1858 ss->s = 1;
1859 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001860 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001861 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863}
1864
1865static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001866emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001867{
1868 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001869 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001870 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001871 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001872 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001873
1874 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001875 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001876 ctxt->mode == X86EMUL_MODE_VM86)
1877 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001878
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001879 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881
Avi Kivity717746e2011-04-20 13:37:53 +03001882 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 cs_sel = (u16)(msr_data & 0xfffc);
1885 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001886
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001887 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001888 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889 cs.l = 1;
1890 }
Avi Kivity4bff1e862011-04-20 13:37:53 +03001891 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1892 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1893 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1894 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895
1896 c->regs[VCPU_REGS_RCX] = c->eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001897 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001898#ifdef CONFIG_X86_64
1899 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1900
Avi Kivity717746e2011-04-20 13:37:53 +03001901 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001902 ctxt->mode == X86EMUL_MODE_PROT64 ?
1903 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904 c->eip = msr_data;
1905
Avi Kivity717746e2011-04-20 13:37:53 +03001906 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907 ctxt->eflags &= ~(msr_data | EFLG_RF);
1908#endif
1909 } else {
1910 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001911 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001912 c->eip = (u32)msr_data;
1913
1914 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1915 }
1916
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001917 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001918}
1919
Andre Przywara8c604352009-06-18 12:56:01 +02001920static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001921emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001922{
1923 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001924 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001925 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001926 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001927 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001928
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001929 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001930 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001931 if (ctxt->mode == X86EMUL_MODE_REAL)
1932 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001933
1934 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1935 * Therefore, we inject an #UD.
1936 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001937 if (ctxt->mode == X86EMUL_MODE_PROT64)
1938 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001939
Gleb Natapov79168fd2010-04-28 19:15:30 +03001940 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001941
Avi Kivity717746e2011-04-20 13:37:53 +03001942 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001943 switch (ctxt->mode) {
1944 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001945 if ((msr_data & 0xfffc) == 0x0)
1946 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001947 break;
1948 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001949 if (msr_data == 0x0)
1950 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001951 break;
1952 }
1953
1954 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001955 cs_sel = (u16)msr_data;
1956 cs_sel &= ~SELECTOR_RPL_MASK;
1957 ss_sel = cs_sel + 8;
1958 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001959 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001960 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001961 cs.l = 1;
1962 }
1963
Avi Kivity4bff1e862011-04-20 13:37:53 +03001964 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1965 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1966 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1967 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001968
Avi Kivity717746e2011-04-20 13:37:53 +03001969 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001970 c->eip = msr_data;
1971
Avi Kivity717746e2011-04-20 13:37:53 +03001972 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001973 c->regs[VCPU_REGS_RSP] = msr_data;
1974
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001975 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001976}
1977
Andre Przywara4668f052009-06-18 12:56:02 +02001978static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001979emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001980{
1981 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001983 u64 msr_data;
1984 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001986
Gleb Natapova0044752010-02-10 14:21:31 +02001987 /* inject #GP if in real mode or Virtual 8086 mode */
1988 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001989 ctxt->mode == X86EMUL_MODE_VM86)
1990 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001991
Gleb Natapov79168fd2010-04-28 19:15:30 +03001992 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001993
1994 if ((c->rex_prefix & 0x8) != 0x0)
1995 usermode = X86EMUL_MODE_PROT64;
1996 else
1997 usermode = X86EMUL_MODE_PROT32;
1998
1999 cs.dpl = 3;
2000 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002001 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002002 switch (usermode) {
2003 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002004 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002005 if ((msr_data & 0xfffc) == 0x0)
2006 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002007 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002008 break;
2009 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002011 if (msr_data == 0x0)
2012 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002013 ss_sel = cs_sel + 8;
2014 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002015 cs.l = 1;
2016 break;
2017 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002018 cs_sel |= SELECTOR_RPL_MASK;
2019 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002020
Avi Kivity4bff1e862011-04-20 13:37:53 +03002021 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
2022 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
2023 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
2024 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002025
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002026 c->eip = c->regs[VCPU_REGS_RDX];
2027 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002028
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002029 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002030}
2031
Gleb Natapov9c537242010-03-18 15:20:05 +02002032static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2033 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034{
2035 int iopl;
2036 if (ctxt->mode == X86EMUL_MODE_REAL)
2037 return false;
2038 if (ctxt->mode == X86EMUL_MODE_VM86)
2039 return true;
2040 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03002041 return ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002042}
2043
2044static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2045 struct x86_emulate_ops *ops,
2046 u16 port, u16 len)
2047{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002048 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002049 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002050 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02002051 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002052 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002053 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002054
Avi Kivity4bff1e862011-04-20 13:37:53 +03002055 ops->get_cached_descriptor(ctxt, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002056 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002057 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002058 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002059 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002060 base = get_desc_base(&tr_seg);
2061#ifdef CONFIG_X86_64
2062 base |= ((u64)base3) << 32;
2063#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002064 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002065 if (r != X86EMUL_CONTINUE)
2066 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002067 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002068 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002069 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 if (r != X86EMUL_CONTINUE)
2071 return false;
2072 if ((perm >> bit_idx) & mask)
2073 return false;
2074 return true;
2075}
2076
2077static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2078 struct x86_emulate_ops *ops,
2079 u16 port, u16 len)
2080{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002081 if (ctxt->perm_ok)
2082 return true;
2083
Gleb Natapov9c537242010-03-18 15:20:05 +02002084 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002085 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2086 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002087
2088 ctxt->perm_ok = true;
2089
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002090 return true;
2091}
2092
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002093static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2094 struct x86_emulate_ops *ops,
2095 struct tss_segment_16 *tss)
2096{
2097 struct decode_cache *c = &ctxt->decode;
2098
2099 tss->ip = c->eip;
2100 tss->flag = ctxt->eflags;
2101 tss->ax = c->regs[VCPU_REGS_RAX];
2102 tss->cx = c->regs[VCPU_REGS_RCX];
2103 tss->dx = c->regs[VCPU_REGS_RDX];
2104 tss->bx = c->regs[VCPU_REGS_RBX];
2105 tss->sp = c->regs[VCPU_REGS_RSP];
2106 tss->bp = c->regs[VCPU_REGS_RBP];
2107 tss->si = c->regs[VCPU_REGS_RSI];
2108 tss->di = c->regs[VCPU_REGS_RDI];
2109
Avi Kivity4bff1e862011-04-20 13:37:53 +03002110 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2111 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2112 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2113 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2114 tss->ldt = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002115}
2116
2117static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2118 struct x86_emulate_ops *ops,
2119 struct tss_segment_16 *tss)
2120{
2121 struct decode_cache *c = &ctxt->decode;
2122 int ret;
2123
2124 c->eip = tss->ip;
2125 ctxt->eflags = tss->flag | 2;
2126 c->regs[VCPU_REGS_RAX] = tss->ax;
2127 c->regs[VCPU_REGS_RCX] = tss->cx;
2128 c->regs[VCPU_REGS_RDX] = tss->dx;
2129 c->regs[VCPU_REGS_RBX] = tss->bx;
2130 c->regs[VCPU_REGS_RSP] = tss->sp;
2131 c->regs[VCPU_REGS_RBP] = tss->bp;
2132 c->regs[VCPU_REGS_RSI] = tss->si;
2133 c->regs[VCPU_REGS_RDI] = tss->di;
2134
2135 /*
2136 * SDM says that segment selectors are loaded before segment
2137 * descriptors
2138 */
Avi Kivity4bff1e862011-04-20 13:37:53 +03002139 ops->set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2140 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2141 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2142 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2143 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002144
2145 /*
2146 * Now load segment descriptors. If fault happenes at this stage
2147 * it is handled in a context of new task
2148 */
2149 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2150 if (ret != X86EMUL_CONTINUE)
2151 return ret;
2152 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2153 if (ret != X86EMUL_CONTINUE)
2154 return ret;
2155 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2156 if (ret != X86EMUL_CONTINUE)
2157 return ret;
2158 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2159 if (ret != X86EMUL_CONTINUE)
2160 return ret;
2161 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2162 if (ret != X86EMUL_CONTINUE)
2163 return ret;
2164
2165 return X86EMUL_CONTINUE;
2166}
2167
2168static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2169 struct x86_emulate_ops *ops,
2170 u16 tss_selector, u16 old_tss_sel,
2171 ulong old_tss_base, struct desc_struct *new_desc)
2172{
2173 struct tss_segment_16 tss_seg;
2174 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002175 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176
Avi Kivity0f65dd72011-04-20 13:37:53 +03002177 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002178 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002179 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002181 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182
2183 save_state_to_tss16(ctxt, ops, &tss_seg);
2184
Avi Kivity0f65dd72011-04-20 13:37:53 +03002185 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002186 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190
Avi Kivity0f65dd72011-04-20 13:37:53 +03002191 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002192 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002193 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002194 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002195 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196
2197 if (old_tss_sel != 0xffff) {
2198 tss_seg.prev_task_link = old_tss_sel;
2199
Avi Kivity0f65dd72011-04-20 13:37:53 +03002200 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201 &tss_seg.prev_task_link,
2202 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002203 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002204 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002206 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002207 }
2208
2209 return load_state_from_tss16(ctxt, ops, &tss_seg);
2210}
2211
2212static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2213 struct x86_emulate_ops *ops,
2214 struct tss_segment_32 *tss)
2215{
2216 struct decode_cache *c = &ctxt->decode;
2217
Avi Kivity717746e2011-04-20 13:37:53 +03002218 tss->cr3 = ops->get_cr(ctxt, 3);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219 tss->eip = c->eip;
2220 tss->eflags = ctxt->eflags;
2221 tss->eax = c->regs[VCPU_REGS_RAX];
2222 tss->ecx = c->regs[VCPU_REGS_RCX];
2223 tss->edx = c->regs[VCPU_REGS_RDX];
2224 tss->ebx = c->regs[VCPU_REGS_RBX];
2225 tss->esp = c->regs[VCPU_REGS_RSP];
2226 tss->ebp = c->regs[VCPU_REGS_RBP];
2227 tss->esi = c->regs[VCPU_REGS_RSI];
2228 tss->edi = c->regs[VCPU_REGS_RDI];
2229
Avi Kivity4bff1e862011-04-20 13:37:53 +03002230 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2231 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2232 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2233 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2234 tss->fs = ops->get_segment_selector(ctxt, VCPU_SREG_FS);
2235 tss->gs = ops->get_segment_selector(ctxt, VCPU_SREG_GS);
2236 tss->ldt_selector = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002237}
2238
2239static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2240 struct x86_emulate_ops *ops,
2241 struct tss_segment_32 *tss)
2242{
2243 struct decode_cache *c = &ctxt->decode;
2244 int ret;
2245
Avi Kivity717746e2011-04-20 13:37:53 +03002246 if (ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002247 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002248 c->eip = tss->eip;
2249 ctxt->eflags = tss->eflags | 2;
2250 c->regs[VCPU_REGS_RAX] = tss->eax;
2251 c->regs[VCPU_REGS_RCX] = tss->ecx;
2252 c->regs[VCPU_REGS_RDX] = tss->edx;
2253 c->regs[VCPU_REGS_RBX] = tss->ebx;
2254 c->regs[VCPU_REGS_RSP] = tss->esp;
2255 c->regs[VCPU_REGS_RBP] = tss->ebp;
2256 c->regs[VCPU_REGS_RSI] = tss->esi;
2257 c->regs[VCPU_REGS_RDI] = tss->edi;
2258
2259 /*
2260 * SDM says that segment selectors are loaded before segment
2261 * descriptors
2262 */
Avi Kivity4bff1e862011-04-20 13:37:53 +03002263 ops->set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2264 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2265 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2266 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2267 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2268 ops->set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2269 ops->set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270
2271 /*
2272 * Now load segment descriptors. If fault happenes at this stage
2273 * it is handled in a context of new task
2274 */
2275 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2276 if (ret != X86EMUL_CONTINUE)
2277 return ret;
2278 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2279 if (ret != X86EMUL_CONTINUE)
2280 return ret;
2281 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2282 if (ret != X86EMUL_CONTINUE)
2283 return ret;
2284 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2285 if (ret != X86EMUL_CONTINUE)
2286 return ret;
2287 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2288 if (ret != X86EMUL_CONTINUE)
2289 return ret;
2290 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2291 if (ret != X86EMUL_CONTINUE)
2292 return ret;
2293 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2294 if (ret != X86EMUL_CONTINUE)
2295 return ret;
2296
2297 return X86EMUL_CONTINUE;
2298}
2299
2300static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2301 struct x86_emulate_ops *ops,
2302 u16 tss_selector, u16 old_tss_sel,
2303 ulong old_tss_base, struct desc_struct *new_desc)
2304{
2305 struct tss_segment_32 tss_seg;
2306 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002307 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308
Avi Kivity0f65dd72011-04-20 13:37:53 +03002309 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002310 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002311 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002313 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314
2315 save_state_to_tss32(ctxt, ops, &tss_seg);
2316
Avi Kivity0f65dd72011-04-20 13:37:53 +03002317 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002318 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002319 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002322
Avi Kivity0f65dd72011-04-20 13:37:53 +03002323 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002324 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002325 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328
2329 if (old_tss_sel != 0xffff) {
2330 tss_seg.prev_task_link = old_tss_sel;
2331
Avi Kivity0f65dd72011-04-20 13:37:53 +03002332 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 &tss_seg.prev_task_link,
2334 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002335 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002336 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 }
2340
2341 return load_state_from_tss32(ctxt, ops, &tss_seg);
2342}
2343
2344static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002345 struct x86_emulate_ops *ops,
2346 u16 tss_selector, int reason,
2347 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002348{
2349 struct desc_struct curr_tss_desc, next_tss_desc;
2350 int ret;
Avi Kivity4bff1e862011-04-20 13:37:53 +03002351 u16 old_tss_sel = ops->get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002353 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002354 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002355
2356 /* FIXME: old_tss_base == ~0 ? */
2357
2358 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2359 if (ret != X86EMUL_CONTINUE)
2360 return ret;
2361 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2362 if (ret != X86EMUL_CONTINUE)
2363 return ret;
2364
2365 /* FIXME: check that next_tss_desc is tss */
2366
2367 if (reason != TASK_SWITCH_IRET) {
2368 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002369 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002370 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002371 }
2372
Gleb Natapovceffb452010-03-18 15:20:19 +02002373 desc_limit = desc_limit_scaled(&next_tss_desc);
2374 if (!next_tss_desc.p ||
2375 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2376 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002377 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378 return X86EMUL_PROPAGATE_FAULT;
2379 }
2380
2381 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2382 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2383 write_segment_descriptor(ctxt, ops, old_tss_sel,
2384 &curr_tss_desc);
2385 }
2386
2387 if (reason == TASK_SWITCH_IRET)
2388 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2389
2390 /* set back link to prev task only if NT bit is set in eflags
2391 note that old_tss_sel is not used afetr this point */
2392 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2393 old_tss_sel = 0xffff;
2394
2395 if (next_tss_desc.type & 8)
2396 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2397 old_tss_base, &next_tss_desc);
2398 else
2399 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2400 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002401 if (ret != X86EMUL_CONTINUE)
2402 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403
2404 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2405 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2406
2407 if (reason != TASK_SWITCH_IRET) {
2408 next_tss_desc.type |= (1 << 1); /* set busy flag */
2409 write_segment_descriptor(ctxt, ops, tss_selector,
2410 &next_tss_desc);
2411 }
2412
Avi Kivity717746e2011-04-20 13:37:53 +03002413 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity4bff1e862011-04-20 13:37:53 +03002414 ops->set_cached_descriptor(ctxt, &next_tss_desc, 0, VCPU_SREG_TR);
2415 ops->set_segment_selector(ctxt, tss_selector, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416
Jan Kiszkae269fb22010-04-14 15:51:09 +02002417 if (has_error_code) {
2418 struct decode_cache *c = &ctxt->decode;
2419
2420 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2421 c->lock_prefix = 0;
2422 c->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002423 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002424 }
2425
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426 return ret;
2427}
2428
2429int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002430 u16 tss_selector, int reason,
2431 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432{
Avi Kivity9aabc882010-07-29 15:11:50 +03002433 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 struct decode_cache *c = &ctxt->decode;
2435 int rc;
2436
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002437 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002438 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439
Jan Kiszkae269fb22010-04-14 15:51:09 +02002440 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2441 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002442
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002443 if (rc == X86EMUL_CONTINUE)
2444 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002446 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447}
2448
Avi Kivity90de84f2010-11-17 15:28:21 +02002449static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002450 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002451{
2452 struct decode_cache *c = &ctxt->decode;
2453 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2454
Gleb Natapovd9271122010-03-18 15:20:22 +02002455 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002456 op->addr.mem.ea = register_address(c, c->regs[reg]);
2457 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002458}
2459
Avi Kivity7af04fc2010-08-18 14:16:35 +03002460static int em_das(struct x86_emulate_ctxt *ctxt)
2461{
2462 struct decode_cache *c = &ctxt->decode;
2463 u8 al, old_al;
2464 bool af, cf, old_cf;
2465
2466 cf = ctxt->eflags & X86_EFLAGS_CF;
2467 al = c->dst.val;
2468
2469 old_al = al;
2470 old_cf = cf;
2471 cf = false;
2472 af = ctxt->eflags & X86_EFLAGS_AF;
2473 if ((al & 0x0f) > 9 || af) {
2474 al -= 6;
2475 cf = old_cf | (al >= 250);
2476 af = true;
2477 } else {
2478 af = false;
2479 }
2480 if (old_al > 0x99 || old_cf) {
2481 al -= 0x60;
2482 cf = true;
2483 }
2484
2485 c->dst.val = al;
2486 /* Set PF, ZF, SF */
2487 c->src.type = OP_IMM;
2488 c->src.val = 0;
2489 c->src.bytes = 1;
2490 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2491 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2492 if (cf)
2493 ctxt->eflags |= X86_EFLAGS_CF;
2494 if (af)
2495 ctxt->eflags |= X86_EFLAGS_AF;
2496 return X86EMUL_CONTINUE;
2497}
2498
Avi Kivity0ef753b2010-08-18 14:51:45 +03002499static int em_call_far(struct x86_emulate_ctxt *ctxt)
2500{
2501 struct decode_cache *c = &ctxt->decode;
2502 u16 sel, old_cs;
2503 ulong old_eip;
2504 int rc;
2505
Avi Kivity4bff1e862011-04-20 13:37:53 +03002506 old_cs = ctxt->ops->get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002507 old_eip = c->eip;
2508
2509 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2510 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2511 return X86EMUL_CONTINUE;
2512
2513 c->eip = 0;
2514 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2515
2516 c->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002517 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002518 if (rc != X86EMUL_CONTINUE)
2519 return rc;
2520
2521 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002522 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002523}
2524
Avi Kivity40ece7c2010-08-18 15:12:09 +03002525static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2526{
2527 struct decode_cache *c = &ctxt->decode;
2528 int rc;
2529
2530 c->dst.type = OP_REG;
2531 c->dst.addr.reg = &c->eip;
2532 c->dst.bytes = c->op_bytes;
2533 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2534 if (rc != X86EMUL_CONTINUE)
2535 return rc;
2536 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2537 return X86EMUL_CONTINUE;
2538}
2539
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002540static int em_add(struct x86_emulate_ctxt *ctxt)
2541{
2542 struct decode_cache *c = &ctxt->decode;
2543
2544 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
2545 return X86EMUL_CONTINUE;
2546}
2547
2548static int em_or(struct x86_emulate_ctxt *ctxt)
2549{
2550 struct decode_cache *c = &ctxt->decode;
2551
2552 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2553 return X86EMUL_CONTINUE;
2554}
2555
2556static int em_adc(struct x86_emulate_ctxt *ctxt)
2557{
2558 struct decode_cache *c = &ctxt->decode;
2559
2560 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
2561 return X86EMUL_CONTINUE;
2562}
2563
2564static int em_sbb(struct x86_emulate_ctxt *ctxt)
2565{
2566 struct decode_cache *c = &ctxt->decode;
2567
2568 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
2569 return X86EMUL_CONTINUE;
2570}
2571
2572static int em_and(struct x86_emulate_ctxt *ctxt)
2573{
2574 struct decode_cache *c = &ctxt->decode;
2575
2576 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
2577 return X86EMUL_CONTINUE;
2578}
2579
2580static int em_sub(struct x86_emulate_ctxt *ctxt)
2581{
2582 struct decode_cache *c = &ctxt->decode;
2583
2584 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
2585 return X86EMUL_CONTINUE;
2586}
2587
2588static int em_xor(struct x86_emulate_ctxt *ctxt)
2589{
2590 struct decode_cache *c = &ctxt->decode;
2591
2592 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
2593 return X86EMUL_CONTINUE;
2594}
2595
2596static int em_cmp(struct x86_emulate_ctxt *ctxt)
2597{
2598 struct decode_cache *c = &ctxt->decode;
2599
2600 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2601 /* Disable writeback. */
2602 c->dst.type = OP_NONE;
2603 return X86EMUL_CONTINUE;
2604}
2605
Avi Kivity5c82aa22010-08-18 18:31:43 +03002606static int em_imul(struct x86_emulate_ctxt *ctxt)
2607{
2608 struct decode_cache *c = &ctxt->decode;
2609
2610 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2611 return X86EMUL_CONTINUE;
2612}
2613
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002614static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2615{
2616 struct decode_cache *c = &ctxt->decode;
2617
2618 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002619 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002620}
2621
Avi Kivity61429142010-08-19 15:13:00 +03002622static int em_cwd(struct x86_emulate_ctxt *ctxt)
2623{
2624 struct decode_cache *c = &ctxt->decode;
2625
2626 c->dst.type = OP_REG;
2627 c->dst.bytes = c->src.bytes;
2628 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2629 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2630
2631 return X86EMUL_CONTINUE;
2632}
2633
Avi Kivity48bb5d32010-08-18 18:54:34 +03002634static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2635{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002636 struct decode_cache *c = &ctxt->decode;
2637 u64 tsc = 0;
2638
Avi Kivity717746e2011-04-20 13:37:53 +03002639 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002640 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2641 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2642 return X86EMUL_CONTINUE;
2643}
2644
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002645static int em_mov(struct x86_emulate_ctxt *ctxt)
2646{
2647 struct decode_cache *c = &ctxt->decode;
2648 c->dst.val = c->src.val;
2649 return X86EMUL_CONTINUE;
2650}
2651
Avi Kivityaa97bb42010-01-20 18:09:23 +02002652static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2653{
2654 struct decode_cache *c = &ctxt->decode;
2655 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2656 return X86EMUL_CONTINUE;
2657}
2658
Avi Kivity38503912011-03-31 18:48:09 +02002659static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2660{
2661 struct decode_cache *c = &ctxt->decode;
Avi Kivity9fa088f2011-03-31 18:54:30 +02002662 int rc;
2663 ulong linear;
2664
Avi Kivity83b87952011-04-03 11:31:19 +03002665 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002666 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002667 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002668 /* Disable writeback. */
2669 c->dst.type = OP_NONE;
2670 return X86EMUL_CONTINUE;
2671}
2672
Avi Kivity2d04a052011-04-20 15:32:49 +03002673static int em_clts(struct x86_emulate_ctxt *ctxt)
2674{
2675 ulong cr0;
2676
2677 cr0 = ctxt->ops->get_cr(ctxt, 0);
2678 cr0 &= ~X86_CR0_TS;
2679 ctxt->ops->set_cr(ctxt, 0, cr0);
2680 return X86EMUL_CONTINUE;
2681}
2682
Avi Kivity26d05cc2011-04-21 12:07:59 +03002683static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2684{
2685 struct decode_cache *c = &ctxt->decode;
2686 int rc;
2687
2688 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2689 return X86EMUL_UNHANDLEABLE;
2690
2691 rc = ctxt->ops->fix_hypercall(ctxt);
2692 if (rc != X86EMUL_CONTINUE)
2693 return rc;
2694
2695 /* Let the processor re-execute the fixed hypercall */
2696 c->eip = ctxt->eip;
2697 /* Disable writeback. */
2698 c->dst.type = OP_NONE;
2699 return X86EMUL_CONTINUE;
2700}
2701
2702static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2703{
2704 struct decode_cache *c = &ctxt->decode;
2705 struct desc_ptr desc_ptr;
2706 int rc;
2707
2708 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2709 &desc_ptr.size, &desc_ptr.address,
2710 c->op_bytes);
2711 if (rc != X86EMUL_CONTINUE)
2712 return rc;
2713 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2714 /* Disable writeback. */
2715 c->dst.type = OP_NONE;
2716 return X86EMUL_CONTINUE;
2717}
2718
Avi Kivity5ef39c72011-04-21 12:21:50 +03002719static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002720{
2721 struct decode_cache *c = &ctxt->decode;
2722 int rc;
2723
Avi Kivity5ef39c72011-04-21 12:21:50 +03002724 rc = ctxt->ops->fix_hypercall(ctxt);
2725
Avi Kivity26d05cc2011-04-21 12:07:59 +03002726 /* Disable writeback. */
2727 c->dst.type = OP_NONE;
2728 return rc;
2729}
2730
2731static int em_lidt(struct x86_emulate_ctxt *ctxt)
2732{
2733 struct decode_cache *c = &ctxt->decode;
2734 struct desc_ptr desc_ptr;
2735 int rc;
2736
2737 rc = read_descriptor(ctxt, ctxt->ops, c->src.addr.mem,
2738 &desc_ptr.size,
2739 &desc_ptr.address,
2740 c->op_bytes);
2741 if (rc != X86EMUL_CONTINUE)
2742 return rc;
2743 ctxt->ops->set_idt(ctxt, &desc_ptr);
2744 /* Disable writeback. */
2745 c->dst.type = OP_NONE;
2746 return X86EMUL_CONTINUE;
2747}
2748
2749static int em_smsw(struct x86_emulate_ctxt *ctxt)
2750{
2751 struct decode_cache *c = &ctxt->decode;
2752
2753 c->dst.bytes = 2;
2754 c->dst.val = ctxt->ops->get_cr(ctxt, 0);
2755 return X86EMUL_CONTINUE;
2756}
2757
2758static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2759{
2760 struct decode_cache *c = &ctxt->decode;
2761 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
2762 | (c->src.val & 0x0f));
2763 c->dst.type = OP_NONE;
2764 return X86EMUL_CONTINUE;
2765}
2766
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002767static bool valid_cr(int nr)
2768{
2769 switch (nr) {
2770 case 0:
2771 case 2 ... 4:
2772 case 8:
2773 return true;
2774 default:
2775 return false;
2776 }
2777}
2778
2779static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2780{
2781 struct decode_cache *c = &ctxt->decode;
2782
2783 if (!valid_cr(c->modrm_reg))
2784 return emulate_ud(ctxt);
2785
2786 return X86EMUL_CONTINUE;
2787}
2788
2789static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2790{
2791 struct decode_cache *c = &ctxt->decode;
2792 u64 new_val = c->src.val64;
2793 int cr = c->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002794 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002795
2796 static u64 cr_reserved_bits[] = {
2797 0xffffffff00000000ULL,
2798 0, 0, 0, /* CR3 checked later */
2799 CR4_RESERVED_BITS,
2800 0, 0, 0,
2801 CR8_RESERVED_BITS,
2802 };
2803
2804 if (!valid_cr(cr))
2805 return emulate_ud(ctxt);
2806
2807 if (new_val & cr_reserved_bits[cr])
2808 return emulate_gp(ctxt, 0);
2809
2810 switch (cr) {
2811 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002812 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002813 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2814 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2815 return emulate_gp(ctxt, 0);
2816
Avi Kivity717746e2011-04-20 13:37:53 +03002817 cr4 = ctxt->ops->get_cr(ctxt, 4);
2818 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002819
2820 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2821 !(cr4 & X86_CR4_PAE))
2822 return emulate_gp(ctxt, 0);
2823
2824 break;
2825 }
2826 case 3: {
2827 u64 rsvd = 0;
2828
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002829 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2830 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002831 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002832 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002833 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002834 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002835 rsvd = CR3_NONPAE_RESERVED_BITS;
2836
2837 if (new_val & rsvd)
2838 return emulate_gp(ctxt, 0);
2839
2840 break;
2841 }
2842 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002843 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002844
Avi Kivity717746e2011-04-20 13:37:53 +03002845 cr4 = ctxt->ops->get_cr(ctxt, 4);
2846 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002847
2848 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2849 return emulate_gp(ctxt, 0);
2850
2851 break;
2852 }
2853 }
2854
2855 return X86EMUL_CONTINUE;
2856}
2857
Joerg Roedel3b88e412011-04-04 12:39:29 +02002858static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2859{
2860 unsigned long dr7;
2861
Avi Kivity717746e2011-04-20 13:37:53 +03002862 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002863
2864 /* Check if DR7.Global_Enable is set */
2865 return dr7 & (1 << 13);
2866}
2867
2868static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2869{
2870 struct decode_cache *c = &ctxt->decode;
2871 int dr = c->modrm_reg;
2872 u64 cr4;
2873
2874 if (dr > 7)
2875 return emulate_ud(ctxt);
2876
Avi Kivity717746e2011-04-20 13:37:53 +03002877 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002878 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2879 return emulate_ud(ctxt);
2880
2881 if (check_dr7_gd(ctxt))
2882 return emulate_db(ctxt);
2883
2884 return X86EMUL_CONTINUE;
2885}
2886
2887static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2888{
2889 struct decode_cache *c = &ctxt->decode;
2890 u64 new_val = c->src.val64;
2891 int dr = c->modrm_reg;
2892
2893 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2894 return emulate_gp(ctxt, 0);
2895
2896 return check_dr_read(ctxt);
2897}
2898
Joerg Roedel01de8b02011-04-04 12:39:31 +02002899static int check_svme(struct x86_emulate_ctxt *ctxt)
2900{
2901 u64 efer;
2902
Avi Kivity717746e2011-04-20 13:37:53 +03002903 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002904
2905 if (!(efer & EFER_SVME))
2906 return emulate_ud(ctxt);
2907
2908 return X86EMUL_CONTINUE;
2909}
2910
2911static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2912{
Avi Kivityfe870ab2011-04-20 15:01:23 +03002913 u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002914
2915 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002916 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002917 return emulate_gp(ctxt, 0);
2918
2919 return check_svme(ctxt);
2920}
2921
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002922static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2923{
Avi Kivity717746e2011-04-20 13:37:53 +03002924 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002925
Avi Kivity717746e2011-04-20 13:37:53 +03002926 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002927 return emulate_ud(ctxt);
2928
2929 return X86EMUL_CONTINUE;
2930}
2931
Joerg Roedel80612522011-04-04 12:39:33 +02002932static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2933{
Avi Kivity717746e2011-04-20 13:37:53 +03002934 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivityfe870ab2011-04-20 15:01:23 +03002935 u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002936
Avi Kivity717746e2011-04-20 13:37:53 +03002937 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002938 (rcx > 3))
2939 return emulate_gp(ctxt, 0);
2940
2941 return X86EMUL_CONTINUE;
2942}
2943
Joerg Roedelf6511932011-04-04 12:39:35 +02002944static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2945{
2946 struct decode_cache *c = &ctxt->decode;
2947
2948 c->dst.bytes = min(c->dst.bytes, 4u);
2949 if (!emulator_io_permited(ctxt, ctxt->ops, c->src.val, c->dst.bytes))
2950 return emulate_gp(ctxt, 0);
2951
2952 return X86EMUL_CONTINUE;
2953}
2954
2955static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2956{
2957 struct decode_cache *c = &ctxt->decode;
2958
2959 c->src.bytes = min(c->src.bytes, 4u);
2960 if (!emulator_io_permited(ctxt, ctxt->ops, c->dst.val, c->src.bytes))
2961 return emulate_gp(ctxt, 0);
2962
2963 return X86EMUL_CONTINUE;
2964}
2965
Avi Kivity73fba5f2010-07-29 15:11:53 +03002966#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002967#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002968#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2969 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002970#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002971#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002972#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002973#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002974#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002975#define II(_f, _e, _i) \
2976 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002977#define IIP(_f, _e, _i, _p) \
2978 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2979 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002980#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002981
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002982#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002983#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002984#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2985
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002986#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2987 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2988 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002989
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002990static struct opcode group7_rm1[] = {
2991 DI(SrcNone | ModRM | Priv, monitor),
2992 DI(SrcNone | ModRM | Priv, mwait),
2993 N, N, N, N, N, N,
2994};
2995
Joerg Roedel01de8b02011-04-04 12:39:31 +02002996static struct opcode group7_rm3[] = {
2997 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03002998 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002999 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3000 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3001 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3002 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3003 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3004 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3005};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003006
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003007static struct opcode group7_rm7[] = {
3008 N,
3009 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3010 N, N, N, N, N, N,
3011};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003012
Avi Kivity73fba5f2010-07-29 15:11:53 +03003013static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003014 I(Lock, em_add),
3015 I(Lock, em_or),
3016 I(Lock, em_adc),
3017 I(Lock, em_sbb),
3018 I(Lock, em_and),
3019 I(Lock, em_sub),
3020 I(Lock, em_xor),
3021 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003022};
3023
3024static struct opcode group1A[] = {
3025 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3026};
3027
3028static struct opcode group3[] = {
3029 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3030 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003031 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003032};
3033
3034static struct opcode group4[] = {
3035 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3036 N, N, N, N, N, N,
3037};
3038
3039static struct opcode group5[] = {
3040 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003041 D(SrcMem | ModRM | Stack),
3042 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003043 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3044 D(SrcMem | ModRM | Stack), N,
3045};
3046
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003047static struct opcode group6[] = {
3048 DI(ModRM | Prot, sldt),
3049 DI(ModRM | Prot, str),
3050 DI(ModRM | Prot | Priv, lldt),
3051 DI(ModRM | Prot | Priv, ltr),
3052 N, N, N, N,
3053};
3054
Avi Kivity73fba5f2010-07-29 15:11:53 +03003055static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003056 DI(ModRM | Mov | DstMem | Priv, sgdt),
3057 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003058 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3059 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3060 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3061 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3062 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003063}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003064 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3065 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003066 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003067 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3068 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003069} };
3070
3071static struct opcode group8[] = {
3072 N, N, N, N,
3073 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3074 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3075};
3076
3077static struct group_dual group9 = { {
3078 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3079}, {
3080 N, N, N, N, N, N, N, N,
3081} };
3082
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003083static struct opcode group11[] = {
3084 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3085};
3086
Avi Kivityaa97bb42010-01-20 18:09:23 +02003087static struct gprefix pfx_0f_6f_0f_7f = {
3088 N, N, N, I(Sse, em_movdqu),
3089};
3090
Avi Kivity73fba5f2010-07-29 15:11:53 +03003091static struct opcode opcode_table[256] = {
3092 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003093 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003094 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3095 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003096 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003097 D(ImplicitOps | Stack | No64), N,
3098 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003099 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003100 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3101 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003102 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003103 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3104 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003105 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003106 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003107 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003108 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003109 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003110 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003111 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003112 /* 0x40 - 0x4F */
3113 X16(D(DstReg)),
3114 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003115 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003116 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003117 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003119 I(ImplicitOps | Stack | No64, em_pusha),
3120 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003121 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3122 N, N, N, N,
3123 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003124 I(SrcImm | Mov | Stack, em_push),
3125 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003126 I(SrcImmByte | Mov | Stack, em_push),
3127 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Joerg Roedelf6511932011-04-04 12:39:35 +02003128 D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3129 D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003130 /* 0x70 - 0x7F */
3131 X16(D(SrcImmByte)),
3132 /* 0x80 - 0x87 */
3133 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3134 G(DstMem | SrcImm | ModRM | Group, group1),
3135 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3136 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03003137 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003138 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003139 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3140 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03003141 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003142 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
3143 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003144 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003145 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003146 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003147 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003148 II(ImplicitOps | Stack, em_pushf, pushf),
3149 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003150 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003151 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3152 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3153 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003154 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003155 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03003156 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003157 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3158 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003159 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003160 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003161 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003162 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003163 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003164 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003165 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003166 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
3167 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003168 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003169 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003170 /* 0xC8 - 0xCF */
3171 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003172 D(ImplicitOps), DI(SrcImmByte, intn),
3173 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003174 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003175 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003176 N, N, N, N,
3177 /* 0xD8 - 0xDF */
3178 N, N, N, N, N, N, N, N,
3179 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08003180 X4(D(SrcImmByte)),
Joerg Roedelf6511932011-04-04 12:39:35 +02003181 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3182 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003183 /* 0xE8 - 0xEF */
3184 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
3185 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Joerg Roedelf6511932011-04-04 12:39:35 +02003186 D2bvIP(SrcNone | DstAcc, in, check_perm_in),
3187 D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003188 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003189 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003190 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3191 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003192 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003193 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003194 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3195};
3196
3197static struct opcode twobyte_table[256] = {
3198 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003199 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003200 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003201 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003202 N, D(ImplicitOps | ModRM), N, N,
3203 /* 0x10 - 0x1F */
3204 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3205 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003206 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003207 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003208 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003209 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003210 N, N, N, N,
3211 N, N, N, N, N, N, N, N,
3212 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003213 DI(ImplicitOps | Priv, wrmsr),
3214 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3215 DI(ImplicitOps | Priv, rdmsr),
3216 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02003217 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3218 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003219 N, N, N, N, N, N, N, N,
3220 /* 0x40 - 0x4F */
3221 X16(D(DstReg | SrcMem | ModRM | Mov)),
3222 /* 0x50 - 0x5F */
3223 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3224 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003225 N, N, N, N,
3226 N, N, N, N,
3227 N, N, N, N,
3228 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003229 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003230 N, N, N, N,
3231 N, N, N, N,
3232 N, N, N, N,
3233 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003234 /* 0x80 - 0x8F */
3235 X16(D(SrcImm)),
3236 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003237 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003238 /* 0xA0 - 0xA7 */
3239 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003240 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003241 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3242 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3243 /* 0xA8 - 0xAF */
3244 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003245 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003246 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3247 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003248 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003249 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003250 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003251 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3252 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3253 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003254 /* 0xB8 - 0xBF */
3255 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003256 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003257 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3258 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003259 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003260 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003261 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 N, N, N, GD(0, &group9),
3263 N, N, N, N, N, N, N, N,
3264 /* 0xD0 - 0xDF */
3265 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3266 /* 0xE0 - 0xEF */
3267 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3268 /* 0xF0 - 0xFF */
3269 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3270};
3271
3272#undef D
3273#undef N
3274#undef G
3275#undef GD
3276#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003277#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003278#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003279
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003280#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003281#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003282#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003283#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003284
Avi Kivity39f21ee2010-08-18 19:20:21 +03003285static unsigned imm_size(struct decode_cache *c)
3286{
3287 unsigned size;
3288
3289 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3290 if (size == 8)
3291 size = 4;
3292 return size;
3293}
3294
3295static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3296 unsigned size, bool sign_extension)
3297{
3298 struct decode_cache *c = &ctxt->decode;
3299 struct x86_emulate_ops *ops = ctxt->ops;
3300 int rc = X86EMUL_CONTINUE;
3301
3302 op->type = OP_IMM;
3303 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02003304 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003305 /* NB. Immediates are sign-extended as necessary. */
3306 switch (op->bytes) {
3307 case 1:
3308 op->val = insn_fetch(s8, 1, c->eip);
3309 break;
3310 case 2:
3311 op->val = insn_fetch(s16, 2, c->eip);
3312 break;
3313 case 4:
3314 op->val = insn_fetch(s32, 4, c->eip);
3315 break;
3316 }
3317 if (!sign_extension) {
3318 switch (op->bytes) {
3319 case 1:
3320 op->val &= 0xff;
3321 break;
3322 case 2:
3323 op->val &= 0xffff;
3324 break;
3325 case 4:
3326 op->val &= 0xffffffff;
3327 break;
3328 }
3329 }
3330done:
3331 return rc;
3332}
3333
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003334int
Andre Przywaradc25e892010-12-21 11:12:07 +01003335x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003336{
3337 struct x86_emulate_ops *ops = ctxt->ops;
3338 struct decode_cache *c = &ctxt->decode;
3339 int rc = X86EMUL_CONTINUE;
3340 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003341 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003342 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003343 struct opcode opcode;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003344 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003345
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003346 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01003347 c->fetch.start = c->eip;
3348 c->fetch.end = c->fetch.start + insn_len;
3349 if (insn_len > 0)
3350 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003351
3352 switch (mode) {
3353 case X86EMUL_MODE_REAL:
3354 case X86EMUL_MODE_VM86:
3355 case X86EMUL_MODE_PROT16:
3356 def_op_bytes = def_ad_bytes = 2;
3357 break;
3358 case X86EMUL_MODE_PROT32:
3359 def_op_bytes = def_ad_bytes = 4;
3360 break;
3361#ifdef CONFIG_X86_64
3362 case X86EMUL_MODE_PROT64:
3363 def_op_bytes = 4;
3364 def_ad_bytes = 8;
3365 break;
3366#endif
3367 default:
3368 return -1;
3369 }
3370
3371 c->op_bytes = def_op_bytes;
3372 c->ad_bytes = def_ad_bytes;
3373
3374 /* Legacy prefixes. */
3375 for (;;) {
3376 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3377 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003378 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003379 /* switch between 2/4 bytes */
3380 c->op_bytes = def_op_bytes ^ 6;
3381 break;
3382 case 0x67: /* address-size override */
3383 if (mode == X86EMUL_MODE_PROT64)
3384 /* switch between 4/8 bytes */
3385 c->ad_bytes = def_ad_bytes ^ 12;
3386 else
3387 /* switch between 2/4 bytes */
3388 c->ad_bytes = def_ad_bytes ^ 6;
3389 break;
3390 case 0x26: /* ES override */
3391 case 0x2e: /* CS override */
3392 case 0x36: /* SS override */
3393 case 0x3e: /* DS override */
3394 set_seg_override(c, (c->b >> 3) & 3);
3395 break;
3396 case 0x64: /* FS override */
3397 case 0x65: /* GS override */
3398 set_seg_override(c, c->b & 7);
3399 break;
3400 case 0x40 ... 0x4f: /* REX */
3401 if (mode != X86EMUL_MODE_PROT64)
3402 goto done_prefixes;
3403 c->rex_prefix = c->b;
3404 continue;
3405 case 0xf0: /* LOCK */
3406 c->lock_prefix = 1;
3407 break;
3408 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003409 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003410 c->rep_prefix = c->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003411 break;
3412 default:
3413 goto done_prefixes;
3414 }
3415
3416 /* Any legacy prefix after a REX prefix nullifies its effect. */
3417
3418 c->rex_prefix = 0;
3419 }
3420
3421done_prefixes:
3422
3423 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003424 if (c->rex_prefix & 8)
3425 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003426
3427 /* Opcode byte(s). */
3428 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003429 /* Two-byte opcode? */
3430 if (c->b == 0x0f) {
3431 c->twobyte = 1;
3432 c->b = insn_fetch(u8, 1, c->eip);
3433 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003434 }
3435 c->d = opcode.flags;
3436
Avi Kivity46561642011-04-24 14:09:59 +03003437 while (c->d & GroupMask) {
3438 switch (c->d & GroupMask) {
3439 case Group:
3440 c->modrm = insn_fetch(u8, 1, c->eip);
3441 --c->eip;
3442 goffset = (c->modrm >> 3) & 7;
3443 opcode = opcode.u.group[goffset];
3444 break;
3445 case GroupDual:
3446 c->modrm = insn_fetch(u8, 1, c->eip);
3447 --c->eip;
3448 goffset = (c->modrm >> 3) & 7;
3449 if ((c->modrm >> 6) == 3)
3450 opcode = opcode.u.gdual->mod3[goffset];
3451 else
3452 opcode = opcode.u.gdual->mod012[goffset];
3453 break;
3454 case RMExt:
Joerg Roedel01de8b02011-04-04 12:39:31 +02003455 goffset = c->modrm & 7;
3456 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003457 break;
3458 case Prefix:
3459 if (c->rep_prefix && op_prefix)
3460 return X86EMUL_UNHANDLEABLE;
3461 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3462 switch (simd_prefix) {
3463 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3464 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3465 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3466 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3467 }
3468 break;
3469 default:
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003470 return X86EMUL_UNHANDLEABLE;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003471 }
Avi Kivity46561642011-04-24 14:09:59 +03003472
3473 c->d &= ~GroupMask;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003474 c->d |= opcode.flags;
3475 }
3476
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003477 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003478 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003479 c->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003480
3481 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003482 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003483 return -1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003484
Avi Kivityd8671622011-02-01 16:32:03 +02003485 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3486 return -1;
3487
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003488 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3489 c->op_bytes = 8;
3490
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003491 if (c->d & Op3264) {
3492 if (mode == X86EMUL_MODE_PROT64)
3493 c->op_bytes = 8;
3494 else
3495 c->op_bytes = 4;
3496 }
3497
Avi Kivity12537912011-03-29 11:41:27 +02003498 if (c->d & Sse)
3499 c->op_bytes = 16;
3500
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003501 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003502 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003503 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003504 if (!c->has_seg_override)
3505 set_seg_override(c, c->modrm_seg);
3506 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003507 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003508 if (rc != X86EMUL_CONTINUE)
3509 goto done;
3510
3511 if (!c->has_seg_override)
3512 set_seg_override(c, VCPU_SREG_DS);
3513
Avi Kivity90de84f2010-11-17 15:28:21 +02003514 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003515
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003516 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003517 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003518
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003519 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003520 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003521
3522 /*
3523 * Decode and fetch the source operand: register, memory
3524 * or immediate.
3525 */
3526 switch (c->d & SrcMask) {
3527 case SrcNone:
3528 break;
3529 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003530 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003531 break;
3532 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003533 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003534 goto srcmem_common;
3535 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003536 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003537 goto srcmem_common;
3538 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003539 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003540 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003541 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003542 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003543 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003544 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003545 rc = decode_imm(ctxt, &c->src, 2, false);
3546 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003547 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003548 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3549 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003550 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003551 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003552 break;
3553 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003554 rc = decode_imm(ctxt, &c->src, 1, true);
3555 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003556 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003557 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003558 break;
3559 case SrcAcc:
3560 c->src.type = OP_REG;
3561 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003562 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003563 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003564 break;
3565 case SrcOne:
3566 c->src.bytes = 1;
3567 c->src.val = 1;
3568 break;
3569 case SrcSI:
3570 c->src.type = OP_MEM;
3571 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003572 c->src.addr.mem.ea =
3573 register_address(c, c->regs[VCPU_REGS_RSI]);
3574 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003575 c->src.val = 0;
3576 break;
3577 case SrcImmFAddr:
3578 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003579 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003580 c->src.bytes = c->op_bytes + 2;
3581 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3582 break;
3583 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003584 memop.bytes = c->op_bytes + 2;
3585 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003586 break;
3587 }
3588
Avi Kivity39f21ee2010-08-18 19:20:21 +03003589 if (rc != X86EMUL_CONTINUE)
3590 goto done;
3591
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003592 /*
3593 * Decode and fetch the second source operand: register, memory
3594 * or immediate.
3595 */
3596 switch (c->d & Src2Mask) {
3597 case Src2None:
3598 break;
3599 case Src2CL:
3600 c->src2.bytes = 1;
3601 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3602 break;
3603 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003604 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003605 break;
3606 case Src2One:
3607 c->src2.bytes = 1;
3608 c->src2.val = 1;
3609 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003610 case Src2Imm:
3611 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3612 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003613 }
3614
Avi Kivity39f21ee2010-08-18 19:20:21 +03003615 if (rc != X86EMUL_CONTINUE)
3616 goto done;
3617
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003618 /* Decode and fetch the destination operand: register or memory. */
3619 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003620 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003621 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003622 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3623 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003624 case DstImmUByte:
3625 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003626 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003627 c->dst.bytes = 1;
3628 c->dst.val = insn_fetch(u8, 1, c->eip);
3629 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003630 case DstMem:
3631 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003632 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003633 if ((c->d & DstMask) == DstMem64)
3634 c->dst.bytes = 8;
3635 else
3636 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003637 if (c->d & BitOp)
3638 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003639 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003640 break;
3641 case DstAcc:
3642 c->dst.type = OP_REG;
3643 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003644 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003645 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003646 c->dst.orig_val = c->dst.val;
3647 break;
3648 case DstDI:
3649 c->dst.type = OP_MEM;
3650 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003651 c->dst.addr.mem.ea =
3652 register_address(c, c->regs[VCPU_REGS_RDI]);
3653 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003654 c->dst.val = 0;
3655 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003656 case ImplicitOps:
3657 /* Special instructions do their own operand decoding. */
3658 default:
3659 c->dst.type = OP_NONE; /* Disable writeback. */
3660 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003661 }
3662
3663done:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003664 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003665}
3666
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003667static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3668{
3669 struct decode_cache *c = &ctxt->decode;
3670
3671 /* The second termination condition only applies for REPE
3672 * and REPNE. Test if the repeat string operation prefix is
3673 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3674 * corresponding termination condition according to:
3675 * - if REPE/REPZ and ZF = 0 then done
3676 * - if REPNE/REPNZ and ZF = 1 then done
3677 */
3678 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3679 (c->b == 0xae) || (c->b == 0xaf))
3680 && (((c->rep_prefix == REPE_PREFIX) &&
3681 ((ctxt->eflags & EFLG_ZF) == 0))
3682 || ((c->rep_prefix == REPNE_PREFIX) &&
3683 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3684 return true;
3685
3686 return false;
3687}
3688
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003689int
Avi Kivity9aabc882010-07-29 15:11:50 +03003690x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003691{
Avi Kivity9aabc882010-07-29 15:11:50 +03003692 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003693 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003694 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003695 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003696 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003697 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003698
Gleb Natapov9de41572010-04-28 19:15:22 +03003699 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003700
Gleb Natapov11616242010-02-11 14:43:14 +02003701 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003702 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003703 goto done;
3704 }
3705
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003706 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003707 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003708 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003709 goto done;
3710 }
3711
Avi Kivity081bca02010-08-26 11:06:15 +03003712 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003713 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003714 goto done;
3715 }
3716
Avi Kivity12537912011-03-29 11:41:27 +02003717 if ((c->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003718 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3719 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003720 rc = emulate_ud(ctxt);
3721 goto done;
3722 }
3723
Avi Kivity717746e2011-04-20 13:37:53 +03003724 if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003725 rc = emulate_nm(ctxt);
3726 goto done;
3727 }
3728
Avi Kivityc4f035c2011-04-04 12:39:22 +02003729 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003730 rc = emulator_check_intercept(ctxt, c->intercept,
3731 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003732 if (rc != X86EMUL_CONTINUE)
3733 goto done;
3734 }
3735
Gleb Natapove92805a2010-02-10 14:21:35 +02003736 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity717746e2011-04-20 13:37:53 +03003737 if ((c->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003738 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003739 goto done;
3740 }
3741
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003742 /* Instruction can only be executed in protected mode */
3743 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3744 rc = emulate_ud(ctxt);
3745 goto done;
3746 }
3747
Joerg Roedeld09beab2011-04-04 12:39:25 +02003748 /* Do instruction specific permission checks */
3749 if (c->check_perm) {
3750 rc = c->check_perm(ctxt);
3751 if (rc != X86EMUL_CONTINUE)
3752 goto done;
3753 }
3754
Avi Kivityc4f035c2011-04-04 12:39:22 +02003755 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003756 rc = emulator_check_intercept(ctxt, c->intercept,
3757 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003758 if (rc != X86EMUL_CONTINUE)
3759 goto done;
3760 }
3761
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003762 if (c->rep_prefix && (c->d & String)) {
3763 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003764 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003765 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003766 goto done;
3767 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003768 }
3769
Wei Yongjunc483c022010-08-06 15:36:36 +08003770 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003771 rc = segmented_read(ctxt, c->src.addr.mem,
3772 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003773 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003774 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003775 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003776 }
3777
Gleb Natapove35b7b92010-02-25 16:36:42 +02003778 if (c->src2.type == OP_MEM) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003779 rc = segmented_read(ctxt, c->src2.addr.mem,
3780 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003781 if (rc != X86EMUL_CONTINUE)
3782 goto done;
3783 }
3784
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003785 if ((c->d & DstMask) == ImplicitOps)
3786 goto special_insn;
3787
3788
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003789 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3790 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003791 rc = segmented_read(ctxt, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003792 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003793 if (rc != X86EMUL_CONTINUE)
3794 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003795 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003796 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003797
Avi Kivity018a98d2007-11-27 19:30:56 +02003798special_insn:
3799
Avi Kivityc4f035c2011-04-04 12:39:22 +02003800 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003801 rc = emulator_check_intercept(ctxt, c->intercept,
3802 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003803 if (rc != X86EMUL_CONTINUE)
3804 goto done;
3805 }
3806
Avi Kivityef65c882010-07-29 15:11:51 +03003807 if (c->execute) {
3808 rc = c->execute(ctxt);
3809 if (rc != X86EMUL_CONTINUE)
3810 goto done;
3811 goto writeback;
3812 }
3813
Laurent Viviere4e03de2007-09-18 11:52:50 +02003814 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 goto twobyte_insn;
3816
Laurent Viviere4e03de2007-09-18 11:52:50 +02003817 switch (c->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003818 case 0x06: /* push es */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003819 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003820 break;
3821 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003822 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003823 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003824 case 0x0e: /* push cs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003825 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003826 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003827 case 0x16: /* push ss */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003828 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003829 break;
3830 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003831 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003832 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003833 case 0x1e: /* push ds */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003834 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 break;
3836 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003837 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003839 case 0x40 ... 0x47: /* inc r16/r32 */
3840 emulate_1op("inc", c->dst, ctxt->eflags);
3841 break;
3842 case 0x48 ... 0x4f: /* dec r16/r32 */
3843 emulate_1op("dec", c->dst, ctxt->eflags);
3844 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003846 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003848 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003850 case 0x6c: /* insb */
3851 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003852 c->src.val = c->regs[VCPU_REGS_RDX];
3853 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003854 case 0x6e: /* outsb */
3855 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003856 c->dst.val = c->regs[VCPU_REGS_RDX];
3857 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003858 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003859 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003860 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003861 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003864 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003865 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 break;
3867 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003868 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003870 c->src.val = c->dst.val;
3871 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 /*
3873 * Write back the memory destination with implicit LOCK
3874 * prefix.
3875 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003876 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003877 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003879 case 0x8c: /* mov r/m, sreg */
3880 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003881 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003882 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003883 }
Avi Kivity4bff1e862011-04-20 13:37:53 +03003884 c->dst.val = ops->get_segment_selector(ctxt, c->modrm_reg);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003885 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003886 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003887 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003888 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003889 case 0x8e: { /* mov seg, r/m16 */
3890 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003891
3892 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003893
Gleb Natapovc6975182010-02-18 12:15:01 +02003894 if (c->modrm_reg == VCPU_SREG_CS ||
3895 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003896 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003897 goto done;
3898 }
3899
Glauber Costa310b5d32009-05-12 16:21:06 -04003900 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003901 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003902
Gleb Natapov2e873022010-03-18 15:20:18 +02003903 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003904
3905 c->dst.type = OP_NONE; /* Disable writeback. */
3906 break;
3907 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003909 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003911 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3912 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003913 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003914 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003915 case 0x98: /* cbw/cwde/cdqe */
3916 switch (c->op_bytes) {
3917 case 2: c->dst.val = (s8)c->dst.val; break;
3918 case 4: c->dst.val = (s16)c->dst.val; break;
3919 case 8: c->dst.val = (s32)c->dst.val; break;
3920 }
3921 break;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003922 case 0xa8 ... 0xa9: /* test ax, imm */
3923 goto test;
Avi Kivity018a98d2007-11-27 19:30:56 +02003924 case 0xc0 ... 0xc1:
3925 emulate_grp2(ctxt);
3926 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003927 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003928 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003929 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003930 c->dst.bytes = c->op_bytes;
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003931 rc = em_pop(ctxt);
3932 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003933 case 0xc4: /* les */
3934 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003935 break;
3936 case 0xc5: /* lds */
3937 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003938 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003939 case 0xcb: /* ret far */
3940 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003941 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003942 case 0xcc: /* int3 */
3943 irq = 3;
3944 goto do_interrupt;
3945 case 0xcd: /* int n */
3946 irq = c->src.val;
3947 do_interrupt:
3948 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003949 break;
3950 case 0xce: /* into */
3951 if (ctxt->eflags & EFLG_OF) {
3952 irq = 4;
3953 goto do_interrupt;
3954 }
3955 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003956 case 0xcf: /* iret */
3957 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003958 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003959 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003960 emulate_grp2(ctxt);
3961 break;
3962 case 0xd2 ... 0xd3: /* Grp2 */
3963 c->src.val = c->regs[VCPU_REGS_RCX];
3964 emulate_grp2(ctxt);
3965 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003966 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3967 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3968 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3969 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3970 jmp_rel(c, c->src.val);
3971 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003972 case 0xe3: /* jcxz/jecxz/jrcxz */
3973 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3974 jmp_rel(c, c->src.val);
3975 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003976 case 0xe4: /* inb */
3977 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003978 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003979 case 0xe6: /* outb */
3980 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003981 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003982 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003983 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003984 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003985 jmp_rel(c, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003986 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003987 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003988 }
3989 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003990 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003991 case 0xea: { /* jmp far */
3992 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003993 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003994 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3995
Gleb Natapov4947e7cd2011-04-12 12:36:23 +03003996 rc = load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS);
3997 if (rc != X86EMUL_CONTINUE)
Gleb Natapovc6975182010-02-18 12:15:01 +02003998 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003999
Gleb Natapov414e6272010-04-28 19:15:26 +03004000 c->eip = 0;
4001 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004002 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03004003 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004004 case 0xeb:
4005 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08004006 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02004007 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004008 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03004009 case 0xec: /* in al,dx */
4010 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004011 c->src.val = c->regs[VCPU_REGS_RDX];
4012 do_io_in:
Gleb Natapov7b262e92010-03-18 15:20:27 +02004013 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
4014 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004015 goto done; /* IO is needed */
4016 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08004017 case 0xee: /* out dx,al */
4018 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08004019 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004020 do_io_out:
Avi Kivityca1d4a92011-04-20 13:37:53 +03004021 ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
4022 &c->src.val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004023 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004024 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004025 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004026 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004027 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004028 case 0xf5: /* cmc */
4029 /* complement carry flag from eflags reg */
4030 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004031 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004032 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03004033 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02004034 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004035 case 0xf8: /* clc */
4036 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004037 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004038 case 0xf9: /* stc */
4039 ctxt->eflags |= EFLG_CF;
4040 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004041 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004042 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004043 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004044 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08004045 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004046 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004047 break;
4048 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004049 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004050 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004051 goto done;
4052 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004053 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004054 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004055 }
Avi Kivity111de5d2007-11-27 19:14:21 +02004056 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004057 case 0xfc: /* cld */
4058 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004059 break;
4060 case 0xfd: /* std */
4061 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004062 break;
Gleb Natapovea798492010-02-25 16:36:43 +02004063 case 0xfe: /* Grp4 */
4064 grp45:
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09004065 rc = emulate_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004066 break;
Gleb Natapovea798492010-02-25 16:36:43 +02004067 case 0xff: /* Grp5 */
4068 if (c->modrm_reg == 5)
4069 goto jump_far;
4070 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03004071 default:
4072 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004074
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004075 if (rc != X86EMUL_CONTINUE)
4076 goto done;
4077
Avi Kivity018a98d2007-11-27 19:30:56 +02004078writeback:
4079 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004080 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004081 goto done;
4082
Gleb Natapov5cd21912010-03-18 15:20:26 +02004083 /*
4084 * restore dst type in case the decoding will be reused
4085 * (happens for string instruction )
4086 */
4087 c->dst.type = saved_dst_type;
4088
Gleb Natapova682e352010-03-18 15:20:21 +02004089 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004090 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03004091 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004092
4093 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004094 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03004095 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004096
Gleb Natapov5cd21912010-03-18 15:20:26 +02004097 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03004098 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02004099 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004100
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004101 if (!string_insn_completed(ctxt)) {
4102 /*
4103 * Re-enter guest when pio read ahead buffer is empty
4104 * or, if it is not used, after each 1024 iteration.
4105 */
4106 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
4107 (r->end == 0 || r->end != r->pos)) {
4108 /*
4109 * Reset read cache. Usually happens before
4110 * decode, but since instruction is restarted
4111 * we have to do it here.
4112 */
4113 ctxt->decode.mem_read.end = 0;
4114 return EMULATION_RESTART;
4115 }
4116 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004117 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004118 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004119
4120 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004121
4122done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004123 if (rc == X86EMUL_PROPAGATE_FAULT)
4124 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004125 if (rc == X86EMUL_INTERCEPTED)
4126 return EMULATION_INTERCEPTED;
4127
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004128 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129
4130twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02004131 switch (c->b) {
Andre Przywarae99f0502009-06-17 15:50:33 +02004132 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004133 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004134 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004135 case 0x06:
Avi Kivity2d04a052011-04-20 15:32:49 +03004136 rc = em_clts(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004137 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004138 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004139 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004140 break;
4141 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004142 case 0x0d: /* GrpP (prefetch) */
4143 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004144 break;
4145 case 0x20: /* mov cr, reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004146 c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004147 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148 case 0x21: /* mov from dr to reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004149 ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004151 case 0x22: /* mov reg, cr */
Avi Kivity717746e2011-04-20 13:37:53 +03004152 if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004153 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004154 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004155 goto done;
4156 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004157 c->dst.type = OP_NONE;
4158 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 case 0x23: /* mov from reg to dr */
Avi Kivity717746e2011-04-20 13:37:53 +03004160 if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004161 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004162 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004163 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004164 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004165 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004166 goto done;
4167 }
4168
Laurent Viviera01af5e2007-09-24 11:10:56 +02004169 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004171 case 0x30:
4172 /* wrmsr */
4173 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4174 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Avi Kivity717746e2011-04-20 13:37:53 +03004175 if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004176 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004177 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004178 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004179 }
4180 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004181 break;
4182 case 0x32:
4183 /* rdmsr */
Avi Kivity717746e2011-04-20 13:37:53 +03004184 if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004185 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004186 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004187 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004188 } else {
4189 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4190 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4191 }
4192 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004193 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004194 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004195 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004196 break;
4197 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004198 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004199 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004200 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004201 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004202 if (!test_cc(c->b, ctxt->eflags))
4203 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004205 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004206 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004207 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004208 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004209 case 0x90 ... 0x9f: /* setcc r/m8 */
4210 c->dst.val = test_cc(c->b, ctxt->eflags);
4211 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004212 case 0xa0: /* push fs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004213 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004214 break;
4215 case 0xa1: /* pop fs */
4216 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004217 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004218 case 0xa3:
4219 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004220 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004221 /* only subword offset */
4222 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004223 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004224 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004225 case 0xa4: /* shld imm8, r, r/m */
4226 case 0xa5: /* shld cl, r, r/m */
4227 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4228 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004229 case 0xa8: /* push gs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004230 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004231 break;
4232 case 0xa9: /* pop gs */
4233 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004234 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004235 case 0xab:
4236 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004237 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004238 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004239 case 0xac: /* shrd imm8, r, r/m */
4240 case 0xad: /* shrd cl, r, r/m */
4241 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4242 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004243 case 0xae: /* clflush */
4244 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 case 0xb0 ... 0xb1: /* cmpxchg */
4246 /*
4247 * Save real source value, then compare EAX against
4248 * destination.
4249 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004250 c->src.orig_val = c->src.val;
4251 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004252 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4253 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004255 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256 } else {
4257 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004258 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03004259 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260 }
4261 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004262 case 0xb2: /* lss */
4263 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004264 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265 case 0xb3:
4266 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004267 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004269 case 0xb4: /* lfs */
4270 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004271 break;
4272 case 0xb5: /* lgs */
4273 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004274 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004276 c->dst.bytes = c->op_bytes;
4277 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4278 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004281 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004282 case 0:
4283 goto bt;
4284 case 1:
4285 goto bts;
4286 case 2:
4287 goto btr;
4288 case 3:
4289 goto btc;
4290 }
4291 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004292 case 0xbb:
4293 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004294 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004295 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004296 case 0xbc: { /* bsf */
4297 u8 zf;
4298 __asm__ ("bsf %2, %0; setz %1"
4299 : "=r"(c->dst.val), "=q"(zf)
4300 : "r"(c->src.val));
4301 ctxt->eflags &= ~X86_EFLAGS_ZF;
4302 if (zf) {
4303 ctxt->eflags |= X86_EFLAGS_ZF;
4304 c->dst.type = OP_NONE; /* Disable writeback. */
4305 }
4306 break;
4307 }
4308 case 0xbd: { /* bsr */
4309 u8 zf;
4310 __asm__ ("bsr %2, %0; setz %1"
4311 : "=r"(c->dst.val), "=q"(zf)
4312 : "r"(c->src.val));
4313 ctxt->eflags &= ~X86_EFLAGS_ZF;
4314 if (zf) {
4315 ctxt->eflags |= X86_EFLAGS_ZF;
4316 c->dst.type = OP_NONE; /* Disable writeback. */
4317 }
4318 break;
4319 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004321 c->dst.bytes = c->op_bytes;
4322 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4323 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004325 case 0xc0 ... 0xc1: /* xadd */
4326 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4327 /* Write back the register source. */
4328 c->src.val = c->dst.orig_val;
4329 write_register_operand(&c->src);
4330 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004331 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004332 c->dst.bytes = c->op_bytes;
4333 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4334 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004335 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004337 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004338 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004339 default:
4340 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004342
4343 if (rc != X86EMUL_CONTINUE)
4344 goto done;
4345
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346 goto writeback;
4347
4348cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004349 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350}