Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 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 Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 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 Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 28 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 29 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 30 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | /* |
| 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 Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 41 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | /* Destination operand type. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 43 | #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 Yongjun | 943858e | 2010-08-06 11:36:51 +0800 | [diff] [blame] | 49 | #define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 50 | #define DstMask (7<<1) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | /* Source operand type. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 52 | #define SrcNone (0<<4) /* No source operand. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 53 | #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 Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 59 | #define SrcOne (7<<4) /* Implied '1' */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 60 | #define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */ |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 61 | #define SrcImmU (9<<4) /* Immediate operand, unsigned */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 62 | #define SrcSI (0xa<<4) /* Source is in the DS:RSI */ |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 63 | #define SrcImmFAddr (0xb<<4) /* Source is immediate far address */ |
| 64 | #define SrcMemFAddr (0xc<<4) /* Source is far address in memory */ |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 65 | #define SrcAcc (0xd<<4) /* Source Accumulator */ |
Avi Kivity | b250e60 | 2010-08-18 15:11:24 +0300 | [diff] [blame] | 66 | #define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 67 | #define SrcMask (0xf<<4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 68 | /* Generic ModRM decode. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 69 | #define ModRM (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 70 | /* Destination is only written; never read. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 71 | #define Mov (1<<9) |
| 72 | #define BitOp (1<<10) |
| 73 | #define MemAbs (1<<11) /* Memory operand is absolute displacement */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 74 | #define String (1<<12) /* String instruction (rep capable) */ |
| 75 | #define Stack (1<<13) /* Stack instruction (push/pop) */ |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 76 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ |
| 77 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 78 | #define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */ |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 79 | #define Sse (1<<17) /* SSE Vector instruction */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 80 | /* Misc flags */ |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 81 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 82 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 83 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 84 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 85 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 86 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 87 | #define No64 (1<<28) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 88 | /* Source 2 operand type */ |
| 89 | #define Src2None (0<<29) |
| 90 | #define Src2CL (1<<29) |
| 91 | #define Src2ImmByte (2<<29) |
| 92 | #define Src2One (3<<29) |
Avi Kivity | 7db41eb | 2010-08-18 19:25:28 +0300 | [diff] [blame] | 93 | #define Src2Imm (4<<29) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 94 | #define Src2Mask (7<<29) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 95 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 96 | #define X2(x...) x, x |
| 97 | #define X3(x...) X2(x), x |
| 98 | #define X4(x...) X2(x), X2(x) |
| 99 | #define X5(x...) X4(x), x |
| 100 | #define X6(x...) X4(x), X2(x) |
| 101 | #define X7(x...) X4(x), X3(x) |
| 102 | #define X8(x...) X4(x), X4(x) |
| 103 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 104 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 105 | struct opcode { |
| 106 | u32 flags; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 107 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 108 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 109 | struct opcode *group; |
| 110 | struct group_dual *gdual; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 111 | struct gprefix *gprefix; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 112 | } u; |
| 113 | }; |
| 114 | |
| 115 | struct group_dual { |
| 116 | struct opcode mod012[8]; |
| 117 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 118 | }; |
| 119 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 120 | struct gprefix { |
| 121 | struct opcode pfx_no; |
| 122 | struct opcode pfx_66; |
| 123 | struct opcode pfx_f2; |
| 124 | struct opcode pfx_f3; |
| 125 | }; |
| 126 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 127 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 128 | #define EFLG_ID (1<<21) |
| 129 | #define EFLG_VIP (1<<20) |
| 130 | #define EFLG_VIF (1<<19) |
| 131 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 132 | #define EFLG_VM (1<<17) |
| 133 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 134 | #define EFLG_IOPL (3<<12) |
| 135 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 136 | #define EFLG_OF (1<<11) |
| 137 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 138 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 139 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 140 | #define EFLG_SF (1<<7) |
| 141 | #define EFLG_ZF (1<<6) |
| 142 | #define EFLG_AF (1<<4) |
| 143 | #define EFLG_PF (1<<2) |
| 144 | #define EFLG_CF (1<<0) |
| 145 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 146 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 147 | #define EFLG_RESERVED_ONE_MASK 2 |
| 148 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 149 | /* |
| 150 | * Instruction emulation: |
| 151 | * Most instructions are emulated directly via a fragment of inline assembly |
| 152 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 153 | * any modified flags. |
| 154 | */ |
| 155 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 156 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 157 | #define _LO32 "k" /* force 32-bit operand */ |
| 158 | #define _STK "%%rsp" /* stack pointer */ |
| 159 | #elif defined(__i386__) |
| 160 | #define _LO32 "" /* force 32-bit operand */ |
| 161 | #define _STK "%%esp" /* stack pointer */ |
| 162 | #endif |
| 163 | |
| 164 | /* |
| 165 | * These EFLAGS bits are restored from saved value during emulation, and |
| 166 | * any changes are written back to the saved value after emulation. |
| 167 | */ |
| 168 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 169 | |
| 170 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 171 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 172 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 173 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 174 | "push %"_tmp"; " \ |
| 175 | "push %"_tmp"; " \ |
| 176 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 177 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 178 | "pushf; " \ |
| 179 | "notl %"_LO32 _tmp"; " \ |
| 180 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 181 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 182 | "pop %"_tmp"; " \ |
| 183 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 184 | "popf; " \ |
| 185 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 186 | |
| 187 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 188 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 189 | /* _sav |= EFLAGS & _msk; */ \ |
| 190 | "pushf; " \ |
| 191 | "pop %"_tmp"; " \ |
| 192 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 193 | "orl %"_LO32 _tmp",%"_sav"; " |
| 194 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_X86_64 |
| 196 | #define ON64(x) x |
| 197 | #else |
| 198 | #define ON64(x) |
| 199 | #endif |
| 200 | |
Avi Kivity | b3b3d25 | 2010-08-16 17:49:52 +0300 | [diff] [blame] | 201 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 202 | do { \ |
| 203 | __asm__ __volatile__ ( \ |
| 204 | _PRE_EFLAGS("0", "4", "2") \ |
| 205 | _op _suffix " %"_x"3,%1; " \ |
| 206 | _POST_EFLAGS("0", "4", "2") \ |
Avi Kivity | fb2c264 | 2010-08-16 17:50:56 +0300 | [diff] [blame] | 207 | : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 208 | "=&r" (_tmp) \ |
| 209 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 210 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 211 | |
| 212 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | /* Raw emulation: instruction has two explicit operands. */ |
| 214 | #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 215 | do { \ |
| 216 | unsigned long _tmp; \ |
| 217 | \ |
| 218 | switch ((_dst).bytes) { \ |
| 219 | case 2: \ |
Avi Kivity | b3b3d25 | 2010-08-16 17:49:52 +0300 | [diff] [blame] | 220 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 221 | break; \ |
| 222 | case 4: \ |
Avi Kivity | b3b3d25 | 2010-08-16 17:49:52 +0300 | [diff] [blame] | 223 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 224 | break; \ |
| 225 | case 8: \ |
Avi Kivity | b3b3d25 | 2010-08-16 17:49:52 +0300 | [diff] [blame] | 226 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 227 | break; \ |
| 228 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 229 | } while (0) |
| 230 | |
| 231 | #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 232 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 233 | unsigned long _tmp; \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 234 | switch ((_dst).bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 235 | case 1: \ |
Avi Kivity | b3b3d25 | 2010-08-16 17:49:52 +0300 | [diff] [blame] | 236 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 237 | break; \ |
| 238 | default: \ |
| 239 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 240 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 241 | break; \ |
| 242 | } \ |
| 243 | } while (0) |
| 244 | |
| 245 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
| 246 | #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \ |
| 247 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 248 | "b", "c", "b", "c", "b", "c", "b", "c") |
| 249 | |
| 250 | /* Source operand is byte, word, long or quad sized. */ |
| 251 | #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \ |
| 252 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 253 | "b", "q", "w", "r", _LO32, "r", "", "r") |
| 254 | |
| 255 | /* Source operand is word, long or quad sized. */ |
| 256 | #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \ |
| 257 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 258 | "w", "r", _LO32, "r", "", "r") |
| 259 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 260 | /* Instruction has three operands and one operand is stored in ECX register */ |
| 261 | #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \ |
| 262 | do { \ |
| 263 | unsigned long _tmp; \ |
| 264 | _type _clv = (_cl).val; \ |
| 265 | _type _srcv = (_src).val; \ |
| 266 | _type _dstv = (_dst).val; \ |
| 267 | \ |
| 268 | __asm__ __volatile__ ( \ |
| 269 | _PRE_EFLAGS("0", "5", "2") \ |
| 270 | _op _suffix " %4,%1 \n" \ |
| 271 | _POST_EFLAGS("0", "5", "2") \ |
| 272 | : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \ |
| 273 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 274 | ); \ |
| 275 | \ |
| 276 | (_cl).val = (unsigned long) _clv; \ |
| 277 | (_src).val = (unsigned long) _srcv; \ |
| 278 | (_dst).val = (unsigned long) _dstv; \ |
| 279 | } while (0) |
| 280 | |
| 281 | #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \ |
| 282 | do { \ |
| 283 | switch ((_dst).bytes) { \ |
| 284 | case 2: \ |
| 285 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 286 | "w", unsigned short); \ |
| 287 | break; \ |
| 288 | case 4: \ |
| 289 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 290 | "l", unsigned int); \ |
| 291 | break; \ |
| 292 | case 8: \ |
| 293 | ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 294 | "q", unsigned long)); \ |
| 295 | break; \ |
| 296 | } \ |
| 297 | } while (0) |
| 298 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 299 | #define __emulate_1op(_op, _dst, _eflags, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 300 | do { \ |
| 301 | unsigned long _tmp; \ |
| 302 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 303 | __asm__ __volatile__ ( \ |
| 304 | _PRE_EFLAGS("0", "3", "2") \ |
| 305 | _op _suffix " %1; " \ |
| 306 | _POST_EFLAGS("0", "3", "2") \ |
| 307 | : "=m" (_eflags), "+m" ((_dst).val), \ |
| 308 | "=&r" (_tmp) \ |
| 309 | : "i" (EFLAGS_MASK)); \ |
| 310 | } while (0) |
| 311 | |
| 312 | /* Instruction has only one explicit operand (no source operand). */ |
| 313 | #define emulate_1op(_op, _dst, _eflags) \ |
| 314 | do { \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 315 | switch ((_dst).bytes) { \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 316 | case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \ |
| 317 | case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \ |
| 318 | case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \ |
| 319 | case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 320 | } \ |
| 321 | } while (0) |
| 322 | |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 323 | #define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \ |
| 324 | do { \ |
| 325 | unsigned long _tmp; \ |
| 326 | \ |
| 327 | __asm__ __volatile__ ( \ |
| 328 | _PRE_EFLAGS("0", "4", "1") \ |
| 329 | _op _suffix " %5; " \ |
| 330 | _POST_EFLAGS("0", "4", "1") \ |
| 331 | : "=m" (_eflags), "=&r" (_tmp), \ |
| 332 | "+a" (_rax), "+d" (_rdx) \ |
| 333 | : "i" (EFLAGS_MASK), "m" ((_src).val), \ |
| 334 | "a" (_rax), "d" (_rdx)); \ |
| 335 | } while (0) |
| 336 | |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 337 | #define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \ |
| 338 | do { \ |
| 339 | unsigned long _tmp; \ |
| 340 | \ |
| 341 | __asm__ __volatile__ ( \ |
| 342 | _PRE_EFLAGS("0", "5", "1") \ |
| 343 | "1: \n\t" \ |
| 344 | _op _suffix " %6; " \ |
| 345 | "2: \n\t" \ |
| 346 | _POST_EFLAGS("0", "5", "1") \ |
| 347 | ".pushsection .fixup,\"ax\" \n\t" \ |
| 348 | "3: movb $1, %4 \n\t" \ |
| 349 | "jmp 2b \n\t" \ |
| 350 | ".popsection \n\t" \ |
| 351 | _ASM_EXTABLE(1b, 3b) \ |
| 352 | : "=m" (_eflags), "=&r" (_tmp), \ |
| 353 | "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \ |
| 354 | : "i" (EFLAGS_MASK), "m" ((_src).val), \ |
| 355 | "a" (_rax), "d" (_rdx)); \ |
| 356 | } while (0) |
| 357 | |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 358 | /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */ |
| 359 | #define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \ |
| 360 | do { \ |
| 361 | switch((_src).bytes) { \ |
| 362 | case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \ |
| 363 | case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \ |
| 364 | case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \ |
| 365 | case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \ |
| 366 | } \ |
| 367 | } while (0) |
| 368 | |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 369 | #define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \ |
| 370 | do { \ |
| 371 | switch((_src).bytes) { \ |
| 372 | case 1: \ |
| 373 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ |
| 374 | _eflags, "b", _ex); \ |
| 375 | break; \ |
| 376 | case 2: \ |
| 377 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ |
| 378 | _eflags, "w", _ex); \ |
| 379 | break; \ |
| 380 | case 4: \ |
| 381 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ |
| 382 | _eflags, "l", _ex); \ |
| 383 | break; \ |
| 384 | case 8: ON64( \ |
| 385 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ |
| 386 | _eflags, "q", _ex)); \ |
| 387 | break; \ |
| 388 | } \ |
| 389 | } while (0) |
| 390 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 391 | /* Fetch next part of the instruction being emulated. */ |
| 392 | #define insn_fetch(_type, _size, _eip) \ |
| 393 | ({ unsigned long _x; \ |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 394 | rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \ |
Gleb Natapov | af5b4f7 | 2010-03-15 16:38:30 +0200 | [diff] [blame] | 395 | if (rc != X86EMUL_CONTINUE) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 396 | goto done; \ |
| 397 | (_eip) += (_size); \ |
| 398 | (_type)_x; \ |
| 399 | }) |
| 400 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 401 | #define insn_fetch_arr(_arr, _size, _eip) \ |
| 402 | ({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \ |
| 403 | if (rc != X86EMUL_CONTINUE) \ |
| 404 | goto done; \ |
| 405 | (_eip) += (_size); \ |
| 406 | }) |
| 407 | |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 408 | static inline unsigned long ad_mask(struct decode_cache *c) |
| 409 | { |
| 410 | return (1UL << (c->ad_bytes << 3)) - 1; |
| 411 | } |
| 412 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 413 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 414 | static inline unsigned long |
| 415 | address_mask(struct decode_cache *c, unsigned long reg) |
| 416 | { |
| 417 | if (c->ad_bytes == sizeof(unsigned long)) |
| 418 | return reg; |
| 419 | else |
| 420 | return reg & ad_mask(c); |
| 421 | } |
| 422 | |
| 423 | static inline unsigned long |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 424 | register_address(struct decode_cache *c, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 425 | { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 426 | return address_mask(c, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 429 | static inline void |
| 430 | register_address_increment(struct decode_cache *c, unsigned long *reg, int inc) |
| 431 | { |
| 432 | if (c->ad_bytes == sizeof(unsigned long)) |
| 433 | *reg += inc; |
| 434 | else |
| 435 | *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c)); |
| 436 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 437 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 438 | static inline void jmp_rel(struct decode_cache *c, int rel) |
| 439 | { |
| 440 | register_address_increment(c, &c->eip, rel); |
| 441 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 442 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 443 | static void set_seg_override(struct decode_cache *c, int seg) |
| 444 | { |
| 445 | c->has_seg_override = true; |
| 446 | c->seg_override = seg; |
| 447 | } |
| 448 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 449 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, |
| 450 | struct x86_emulate_ops *ops, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 451 | { |
| 452 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 453 | return 0; |
| 454 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 455 | return ops->get_cached_segment_base(seg, ctxt->vcpu); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 456 | } |
| 457 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 458 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt, |
| 459 | struct x86_emulate_ops *ops, |
| 460 | struct decode_cache *c) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 461 | { |
| 462 | if (!c->has_seg_override) |
| 463 | return 0; |
| 464 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 465 | return c->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 466 | } |
| 467 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 468 | static ulong linear(struct x86_emulate_ctxt *ctxt, |
| 469 | struct segmented_address addr) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 470 | { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 471 | struct decode_cache *c = &ctxt->decode; |
| 472 | ulong la; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 473 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 474 | la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea; |
| 475 | if (c->ad_bytes != 8) |
| 476 | la &= (u32)-1; |
| 477 | return la; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 478 | } |
| 479 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 480 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 481 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 482 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 483 | ctxt->exception.vector = vec; |
| 484 | ctxt->exception.error_code = error; |
| 485 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 486 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 489 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 490 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 491 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 492 | } |
| 493 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 494 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 495 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 496 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 497 | } |
| 498 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 499 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 500 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 501 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 502 | } |
| 503 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 504 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 505 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 506 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 507 | } |
| 508 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 509 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 510 | { |
| 511 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 512 | } |
| 513 | |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 514 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, |
| 515 | struct x86_emulate_ops *ops, |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 516 | unsigned long eip, u8 *dest) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 517 | { |
| 518 | struct fetch_cache *fc = &ctxt->decode.fetch; |
| 519 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 520 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 521 | |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 522 | if (eip == fc->end) { |
| 523 | cur_size = fc->end - fc->start; |
| 524 | size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip)); |
| 525 | rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 526 | size, ctxt->vcpu, &ctxt->exception); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 527 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 528 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 529 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 530 | } |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 531 | *dest = fc->data[eip - fc->start]; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 532 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
| 536 | struct x86_emulate_ops *ops, |
| 537 | unsigned long eip, void *dest, unsigned size) |
| 538 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 539 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 540 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 541 | /* x86 instructions are limited to 15 bytes. */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 542 | if (eip + size - ctxt->eip > 15) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 543 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 544 | while (size--) { |
| 545 | rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 546 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 547 | return rc; |
| 548 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 549 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 552 | /* |
| 553 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 554 | * pointer into the block that addresses the relevant register. |
| 555 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 556 | */ |
| 557 | static void *decode_register(u8 modrm_reg, unsigned long *regs, |
| 558 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 559 | { |
| 560 | void *p; |
| 561 | |
| 562 | p = ®s[modrm_reg]; |
| 563 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
| 564 | p = (unsigned char *)®s[modrm_reg & 3] + 1; |
| 565 | return p; |
| 566 | } |
| 567 | |
| 568 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
| 569 | struct x86_emulate_ops *ops, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 570 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 571 | u16 *size, unsigned long *address, int op_bytes) |
| 572 | { |
| 573 | int rc; |
| 574 | |
| 575 | if (op_bytes == 2) |
| 576 | op_bytes = 3; |
| 577 | *address = 0; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 578 | rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 579 | ctxt->vcpu, &ctxt->exception); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 580 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 581 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 582 | addr.ea += 2; |
| 583 | rc = ops->read_std(linear(ctxt, addr), address, op_bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 584 | ctxt->vcpu, &ctxt->exception); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 585 | return rc; |
| 586 | } |
| 587 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 588 | static int test_cc(unsigned int condition, unsigned int flags) |
| 589 | { |
| 590 | int rc = 0; |
| 591 | |
| 592 | switch ((condition & 15) >> 1) { |
| 593 | case 0: /* o */ |
| 594 | rc |= (flags & EFLG_OF); |
| 595 | break; |
| 596 | case 1: /* b/c/nae */ |
| 597 | rc |= (flags & EFLG_CF); |
| 598 | break; |
| 599 | case 2: /* z/e */ |
| 600 | rc |= (flags & EFLG_ZF); |
| 601 | break; |
| 602 | case 3: /* be/na */ |
| 603 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 604 | break; |
| 605 | case 4: /* s */ |
| 606 | rc |= (flags & EFLG_SF); |
| 607 | break; |
| 608 | case 5: /* p/pe */ |
| 609 | rc |= (flags & EFLG_PF); |
| 610 | break; |
| 611 | case 7: /* le/ng */ |
| 612 | rc |= (flags & EFLG_ZF); |
| 613 | /* fall through */ |
| 614 | case 6: /* l/nge */ |
| 615 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 620 | return (!!rc ^ (condition & 1)); |
| 621 | } |
| 622 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 623 | static void fetch_register_operand(struct operand *op) |
| 624 | { |
| 625 | switch (op->bytes) { |
| 626 | case 1: |
| 627 | op->val = *(u8 *)op->addr.reg; |
| 628 | break; |
| 629 | case 2: |
| 630 | op->val = *(u16 *)op->addr.reg; |
| 631 | break; |
| 632 | case 4: |
| 633 | op->val = *(u32 *)op->addr.reg; |
| 634 | break; |
| 635 | case 8: |
| 636 | op->val = *(u64 *)op->addr.reg; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 641 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 642 | { |
| 643 | ctxt->ops->get_fpu(ctxt); |
| 644 | switch (reg) { |
| 645 | case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break; |
| 646 | case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break; |
| 647 | case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break; |
| 648 | case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break; |
| 649 | case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break; |
| 650 | case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break; |
| 651 | case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break; |
| 652 | case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break; |
| 653 | #ifdef CONFIG_X86_64 |
| 654 | case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break; |
| 655 | case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break; |
| 656 | case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break; |
| 657 | case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break; |
| 658 | case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break; |
| 659 | case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break; |
| 660 | case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break; |
| 661 | case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break; |
| 662 | #endif |
| 663 | default: BUG(); |
| 664 | } |
| 665 | ctxt->ops->put_fpu(ctxt); |
| 666 | } |
| 667 | |
| 668 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 669 | int reg) |
| 670 | { |
| 671 | ctxt->ops->get_fpu(ctxt); |
| 672 | switch (reg) { |
| 673 | case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break; |
| 674 | case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break; |
| 675 | case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break; |
| 676 | case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break; |
| 677 | case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break; |
| 678 | case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break; |
| 679 | case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break; |
| 680 | case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break; |
| 681 | #ifdef CONFIG_X86_64 |
| 682 | case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break; |
| 683 | case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break; |
| 684 | case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break; |
| 685 | case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break; |
| 686 | case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break; |
| 687 | case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break; |
| 688 | case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break; |
| 689 | case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break; |
| 690 | #endif |
| 691 | default: BUG(); |
| 692 | } |
| 693 | ctxt->ops->put_fpu(ctxt); |
| 694 | } |
| 695 | |
| 696 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
| 697 | struct operand *op, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 698 | struct decode_cache *c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 699 | int inhibit_bytereg) |
| 700 | { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 701 | unsigned reg = c->modrm_reg; |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 702 | int highbyte_regs = c->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 703 | |
| 704 | if (!(c->d & ModRM)) |
| 705 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 706 | |
| 707 | if (c->d & Sse) { |
| 708 | op->type = OP_XMM; |
| 709 | op->bytes = 16; |
| 710 | op->addr.xmm = reg; |
| 711 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 712 | return; |
| 713 | } |
| 714 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 715 | op->type = OP_REG; |
| 716 | if ((c->d & ByteOp) && !inhibit_bytereg) { |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 717 | op->addr.reg = decode_register(reg, c->regs, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 718 | op->bytes = 1; |
| 719 | } else { |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 720 | op->addr.reg = decode_register(reg, c->regs, 0); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 721 | op->bytes = c->op_bytes; |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 722 | } |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 723 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 724 | op->orig_val = op->val; |
| 725 | } |
| 726 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 727 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 728 | struct x86_emulate_ops *ops, |
| 729 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 730 | { |
| 731 | struct decode_cache *c = &ctxt->decode; |
| 732 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 733 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 734 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 735 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 736 | |
| 737 | if (c->rex_prefix) { |
| 738 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ |
| 739 | index_reg = (c->rex_prefix & 2) << 2; /* REX.X */ |
| 740 | c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */ |
| 741 | } |
| 742 | |
| 743 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 744 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
| 745 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
| 746 | c->modrm_rm |= (c->modrm & 0x07); |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 747 | c->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 748 | |
| 749 | if (c->modrm_mod == 3) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 750 | op->type = OP_REG; |
| 751 | op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 752 | op->addr.reg = decode_register(c->modrm_rm, |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 753 | c->regs, c->d & ByteOp); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 754 | if (c->d & Sse) { |
| 755 | op->type = OP_XMM; |
| 756 | op->bytes = 16; |
| 757 | op->addr.xmm = c->modrm_rm; |
| 758 | read_sse_reg(ctxt, &op->vec_val, c->modrm_rm); |
| 759 | return rc; |
| 760 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 761 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 762 | return rc; |
| 763 | } |
| 764 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 765 | op->type = OP_MEM; |
| 766 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 767 | if (c->ad_bytes == 2) { |
| 768 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
| 769 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
| 770 | unsigned si = c->regs[VCPU_REGS_RSI]; |
| 771 | unsigned di = c->regs[VCPU_REGS_RDI]; |
| 772 | |
| 773 | /* 16-bit ModR/M decode. */ |
| 774 | switch (c->modrm_mod) { |
| 775 | case 0: |
| 776 | if (c->modrm_rm == 6) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 777 | modrm_ea += insn_fetch(u16, 2, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 778 | break; |
| 779 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 780 | modrm_ea += insn_fetch(s8, 1, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 781 | break; |
| 782 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 783 | modrm_ea += insn_fetch(u16, 2, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | switch (c->modrm_rm) { |
| 787 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 788 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 789 | break; |
| 790 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 791 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 792 | break; |
| 793 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 794 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 795 | break; |
| 796 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 797 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 798 | break; |
| 799 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 800 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 801 | break; |
| 802 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 803 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 804 | break; |
| 805 | case 6: |
| 806 | if (c->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 807 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 808 | break; |
| 809 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 810 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
| 814 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 815 | c->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 816 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 817 | } else { |
| 818 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 819 | if ((c->modrm_rm & 7) == 4) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 820 | sib = insn_fetch(u8, 1, c->eip); |
| 821 | index_reg |= (sib >> 3) & 7; |
| 822 | base_reg |= sib & 7; |
| 823 | scale = sib >> 6; |
| 824 | |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 825 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 826 | modrm_ea += insn_fetch(s32, 4, c->eip); |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 827 | else |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 828 | modrm_ea += c->regs[base_reg]; |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 829 | if (index_reg != 4) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 830 | modrm_ea += c->regs[index_reg] << scale; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 831 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { |
| 832 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 833 | c->rip_relative = 1; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 834 | } else |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 835 | modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 836 | switch (c->modrm_mod) { |
| 837 | case 0: |
| 838 | if (c->modrm_rm == 5) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 839 | modrm_ea += insn_fetch(s32, 4, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 840 | break; |
| 841 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 842 | modrm_ea += insn_fetch(s8, 1, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 843 | break; |
| 844 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 845 | modrm_ea += insn_fetch(s32, 4, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 849 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 850 | done: |
| 851 | return rc; |
| 852 | } |
| 853 | |
| 854 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 855 | struct x86_emulate_ops *ops, |
| 856 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 857 | { |
| 858 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 859 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 860 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 861 | op->type = OP_MEM; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 862 | switch (c->ad_bytes) { |
| 863 | case 2: |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 864 | op->addr.mem.ea = insn_fetch(u16, 2, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 865 | break; |
| 866 | case 4: |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 867 | op->addr.mem.ea = insn_fetch(u32, 4, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 868 | break; |
| 869 | case 8: |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 870 | op->addr.mem.ea = insn_fetch(u64, 8, c->eip); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | done: |
| 874 | return rc; |
| 875 | } |
| 876 | |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 877 | static void fetch_bit_operand(struct decode_cache *c) |
| 878 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 879 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 880 | |
Wei Yongjun | 3885f18 | 2010-08-09 11:37:37 +0800 | [diff] [blame] | 881 | if (c->dst.type == OP_MEM && c->src.type == OP_REG) { |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 882 | mask = ~(c->dst.bytes * 8 - 1); |
| 883 | |
| 884 | if (c->src.bytes == 2) |
| 885 | sv = (s16)c->src.val & (s16)mask; |
| 886 | else if (c->src.bytes == 4) |
| 887 | sv = (s32)c->src.val & (s32)mask; |
| 888 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 889 | c->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 890 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 891 | |
| 892 | /* only subword offset */ |
| 893 | c->src.val &= (c->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 894 | } |
| 895 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 896 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
| 897 | struct x86_emulate_ops *ops, |
| 898 | unsigned long addr, void *dest, unsigned size) |
| 899 | { |
| 900 | int rc; |
| 901 | struct read_cache *mc = &ctxt->decode.mem_read; |
| 902 | |
| 903 | while (size) { |
| 904 | int n = min(size, 8u); |
| 905 | size -= n; |
| 906 | if (mc->pos < mc->end) |
| 907 | goto read_cached; |
| 908 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 909 | rc = ops->read_emulated(addr, mc->data + mc->end, n, |
| 910 | &ctxt->exception, ctxt->vcpu); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 911 | if (rc != X86EMUL_CONTINUE) |
| 912 | return rc; |
| 913 | mc->end += n; |
| 914 | |
| 915 | read_cached: |
| 916 | memcpy(dest, mc->data + mc->pos, n); |
| 917 | mc->pos += n; |
| 918 | dest += n; |
| 919 | addr += n; |
| 920 | } |
| 921 | return X86EMUL_CONTINUE; |
| 922 | } |
| 923 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 924 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
| 925 | struct x86_emulate_ops *ops, |
| 926 | unsigned int size, unsigned short port, |
| 927 | void *dest) |
| 928 | { |
| 929 | struct read_cache *rc = &ctxt->decode.io_read; |
| 930 | |
| 931 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
| 932 | struct decode_cache *c = &ctxt->decode; |
| 933 | unsigned int in_page, n; |
| 934 | unsigned int count = c->rep_prefix ? |
| 935 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1; |
| 936 | in_page = (ctxt->eflags & EFLG_DF) ? |
| 937 | offset_in_page(c->regs[VCPU_REGS_RDI]) : |
| 938 | PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]); |
| 939 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 940 | count); |
| 941 | if (n == 0) |
| 942 | n = 1; |
| 943 | rc->pos = rc->end = 0; |
| 944 | if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu)) |
| 945 | return 0; |
| 946 | rc->end = n * size; |
| 947 | } |
| 948 | |
| 949 | memcpy(dest, rc->data + rc->pos, size); |
| 950 | rc->pos += size; |
| 951 | return 1; |
| 952 | } |
| 953 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 954 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 955 | { |
| 956 | u32 limit = get_desc_limit(desc); |
| 957 | |
| 958 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 959 | } |
| 960 | |
| 961 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
| 962 | struct x86_emulate_ops *ops, |
| 963 | u16 selector, struct desc_ptr *dt) |
| 964 | { |
| 965 | if (selector & 1 << 2) { |
| 966 | struct desc_struct desc; |
| 967 | memset (dt, 0, sizeof *dt); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 968 | if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR, |
| 969 | ctxt->vcpu)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 970 | return; |
| 971 | |
| 972 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 973 | dt->address = get_desc_base(&desc); |
| 974 | } else |
| 975 | ops->get_gdt(dt, ctxt->vcpu); |
| 976 | } |
| 977 | |
| 978 | /* allowed just for 8 bytes segments */ |
| 979 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 980 | struct x86_emulate_ops *ops, |
| 981 | u16 selector, struct desc_struct *desc) |
| 982 | { |
| 983 | struct desc_ptr dt; |
| 984 | u16 index = selector >> 3; |
| 985 | int ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 986 | ulong addr; |
| 987 | |
| 988 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 989 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 990 | if (dt.size < index * 8 + 7) |
| 991 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 992 | addr = dt.address + index * 8; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 993 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, |
| 994 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 995 | |
| 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | /* allowed just for 8 bytes segments */ |
| 1000 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1001 | struct x86_emulate_ops *ops, |
| 1002 | u16 selector, struct desc_struct *desc) |
| 1003 | { |
| 1004 | struct desc_ptr dt; |
| 1005 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1006 | ulong addr; |
| 1007 | int ret; |
| 1008 | |
| 1009 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1010 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1011 | if (dt.size < index * 8 + 7) |
| 1012 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1013 | |
| 1014 | addr = dt.address + index * 8; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1015 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, |
| 1016 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1021 | /* Does not support long mode */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1022 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1023 | struct x86_emulate_ops *ops, |
| 1024 | u16 selector, int seg) |
| 1025 | { |
| 1026 | struct desc_struct seg_desc; |
| 1027 | u8 dpl, rpl, cpl; |
| 1028 | unsigned err_vec = GP_VECTOR; |
| 1029 | u32 err_code = 0; |
| 1030 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 1031 | int ret; |
| 1032 | |
| 1033 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1034 | |
| 1035 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1036 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1037 | /* set real mode segment descriptor */ |
| 1038 | set_desc_base(&seg_desc, selector << 4); |
| 1039 | set_desc_limit(&seg_desc, 0xffff); |
| 1040 | seg_desc.type = 3; |
| 1041 | seg_desc.p = 1; |
| 1042 | seg_desc.s = 1; |
| 1043 | goto load; |
| 1044 | } |
| 1045 | |
| 1046 | /* NULL selector is not valid for TR, CS and SS */ |
| 1047 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 1048 | && null_selector) |
| 1049 | goto exception; |
| 1050 | |
| 1051 | /* TR should be in GDT only */ |
| 1052 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1053 | goto exception; |
| 1054 | |
| 1055 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1056 | goto load; |
| 1057 | |
| 1058 | ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1059 | if (ret != X86EMUL_CONTINUE) |
| 1060 | return ret; |
| 1061 | |
| 1062 | err_code = selector & 0xfffc; |
| 1063 | err_vec = GP_VECTOR; |
| 1064 | |
| 1065 | /* can't load system descriptor into segment selecor */ |
| 1066 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1067 | goto exception; |
| 1068 | |
| 1069 | if (!seg_desc.p) { |
| 1070 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1071 | goto exception; |
| 1072 | } |
| 1073 | |
| 1074 | rpl = selector & 3; |
| 1075 | dpl = seg_desc.dpl; |
| 1076 | cpl = ops->cpl(ctxt->vcpu); |
| 1077 | |
| 1078 | switch (seg) { |
| 1079 | case VCPU_SREG_SS: |
| 1080 | /* |
| 1081 | * segment is not a writable data segment or segment |
| 1082 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1083 | */ |
| 1084 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1085 | goto exception; |
| 1086 | break; |
| 1087 | case VCPU_SREG_CS: |
| 1088 | if (!(seg_desc.type & 8)) |
| 1089 | goto exception; |
| 1090 | |
| 1091 | if (seg_desc.type & 4) { |
| 1092 | /* conforming */ |
| 1093 | if (dpl > cpl) |
| 1094 | goto exception; |
| 1095 | } else { |
| 1096 | /* nonconforming */ |
| 1097 | if (rpl > cpl || dpl != cpl) |
| 1098 | goto exception; |
| 1099 | } |
| 1100 | /* CS(RPL) <- CPL */ |
| 1101 | selector = (selector & 0xfffc) | cpl; |
| 1102 | break; |
| 1103 | case VCPU_SREG_TR: |
| 1104 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1105 | goto exception; |
| 1106 | break; |
| 1107 | case VCPU_SREG_LDTR: |
| 1108 | if (seg_desc.s || seg_desc.type != 2) |
| 1109 | goto exception; |
| 1110 | break; |
| 1111 | default: /* DS, ES, FS, or GS */ |
| 1112 | /* |
| 1113 | * segment is not a data or readable code segment or |
| 1114 | * ((segment is a data or nonconforming code segment) |
| 1115 | * and (both RPL and CPL > DPL)) |
| 1116 | */ |
| 1117 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1118 | (((seg_desc.type & 0xc) != 0xc) && |
| 1119 | (rpl > dpl && cpl > dpl))) |
| 1120 | goto exception; |
| 1121 | break; |
| 1122 | } |
| 1123 | |
| 1124 | if (seg_desc.s) { |
| 1125 | /* mark segment as accessed */ |
| 1126 | seg_desc.type |= 1; |
| 1127 | ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1128 | if (ret != X86EMUL_CONTINUE) |
| 1129 | return ret; |
| 1130 | } |
| 1131 | load: |
| 1132 | ops->set_segment_selector(selector, seg, ctxt->vcpu); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1133 | ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1134 | return X86EMUL_CONTINUE; |
| 1135 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1136 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1137 | return X86EMUL_PROPAGATE_FAULT; |
| 1138 | } |
| 1139 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1140 | static void write_register_operand(struct operand *op) |
| 1141 | { |
| 1142 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1143 | switch (op->bytes) { |
| 1144 | case 1: |
| 1145 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1146 | break; |
| 1147 | case 2: |
| 1148 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1149 | break; |
| 1150 | case 4: |
| 1151 | *op->addr.reg = (u32)op->val; |
| 1152 | break; /* 64b: zero-extend */ |
| 1153 | case 8: |
| 1154 | *op->addr.reg = op->val; |
| 1155 | break; |
| 1156 | } |
| 1157 | } |
| 1158 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1159 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1160 | struct x86_emulate_ops *ops) |
| 1161 | { |
| 1162 | int rc; |
| 1163 | struct decode_cache *c = &ctxt->decode; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1164 | |
| 1165 | switch (c->dst.type) { |
| 1166 | case OP_REG: |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1167 | write_register_operand(&c->dst); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1168 | break; |
| 1169 | case OP_MEM: |
| 1170 | if (c->lock_prefix) |
| 1171 | rc = ops->cmpxchg_emulated( |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1172 | linear(ctxt, c->dst.addr.mem), |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1173 | &c->dst.orig_val, |
| 1174 | &c->dst.val, |
| 1175 | c->dst.bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1176 | &ctxt->exception, |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1177 | ctxt->vcpu); |
| 1178 | else |
| 1179 | rc = ops->write_emulated( |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1180 | linear(ctxt, c->dst.addr.mem), |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1181 | &c->dst.val, |
| 1182 | c->dst.bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1183 | &ctxt->exception, |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1184 | ctxt->vcpu); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1185 | if (rc != X86EMUL_CONTINUE) |
| 1186 | return rc; |
| 1187 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1188 | case OP_XMM: |
| 1189 | write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm); |
| 1190 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1191 | case OP_NONE: |
| 1192 | /* no writeback */ |
| 1193 | break; |
| 1194 | default: |
| 1195 | break; |
| 1196 | } |
| 1197 | return X86EMUL_CONTINUE; |
| 1198 | } |
| 1199 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1200 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt, |
| 1201 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1202 | { |
| 1203 | struct decode_cache *c = &ctxt->decode; |
| 1204 | |
| 1205 | c->dst.type = OP_MEM; |
| 1206 | c->dst.bytes = c->op_bytes; |
| 1207 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1208 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1209 | c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]); |
| 1210 | c->dst.addr.mem.seg = VCPU_SREG_SS; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1211 | } |
| 1212 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1213 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1214 | struct x86_emulate_ops *ops, |
| 1215 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1216 | { |
| 1217 | struct decode_cache *c = &ctxt->decode; |
| 1218 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1219 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1220 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1221 | addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]); |
| 1222 | addr.seg = VCPU_SREG_SS; |
| 1223 | rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1224 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1225 | return rc; |
| 1226 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1227 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1228 | return rc; |
| 1229 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1230 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1231 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1232 | struct x86_emulate_ops *ops, |
| 1233 | void *dest, int len) |
| 1234 | { |
| 1235 | int rc; |
| 1236 | unsigned long val, change_mask; |
| 1237 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1238 | int cpl = ops->cpl(ctxt->vcpu); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1239 | |
| 1240 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1241 | if (rc != X86EMUL_CONTINUE) |
| 1242 | return rc; |
| 1243 | |
| 1244 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1245 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1246 | |
| 1247 | switch(ctxt->mode) { |
| 1248 | case X86EMUL_MODE_PROT64: |
| 1249 | case X86EMUL_MODE_PROT32: |
| 1250 | case X86EMUL_MODE_PROT16: |
| 1251 | if (cpl == 0) |
| 1252 | change_mask |= EFLG_IOPL; |
| 1253 | if (cpl <= iopl) |
| 1254 | change_mask |= EFLG_IF; |
| 1255 | break; |
| 1256 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1257 | if (iopl < 3) |
| 1258 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1259 | change_mask |= EFLG_IF; |
| 1260 | break; |
| 1261 | default: /* real mode */ |
| 1262 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1263 | break; |
| 1264 | } |
| 1265 | |
| 1266 | *(unsigned long *)dest = |
| 1267 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1268 | |
| 1269 | return rc; |
| 1270 | } |
| 1271 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1272 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, |
| 1273 | struct x86_emulate_ops *ops, int seg) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1274 | { |
| 1275 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1276 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1277 | c->src.val = ops->get_segment_selector(seg, ctxt->vcpu); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1278 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1279 | emulate_push(ctxt, ops); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1283 | struct x86_emulate_ops *ops, int seg) |
| 1284 | { |
| 1285 | struct decode_cache *c = &ctxt->decode; |
| 1286 | unsigned long selector; |
| 1287 | int rc; |
| 1288 | |
| 1289 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1290 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1291 | return rc; |
| 1292 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1293 | rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1294 | return rc; |
| 1295 | } |
| 1296 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1297 | static int emulate_pusha(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1298 | struct x86_emulate_ops *ops) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1299 | { |
| 1300 | struct decode_cache *c = &ctxt->decode; |
| 1301 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1302 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1303 | int reg = VCPU_REGS_RAX; |
| 1304 | |
| 1305 | while (reg <= VCPU_REGS_RDI) { |
| 1306 | (reg == VCPU_REGS_RSP) ? |
| 1307 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1308 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1309 | emulate_push(ctxt, ops); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1310 | |
| 1311 | rc = writeback(ctxt, ops); |
| 1312 | if (rc != X86EMUL_CONTINUE) |
| 1313 | return rc; |
| 1314 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1315 | ++reg; |
| 1316 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1317 | |
| 1318 | /* Disable writeback. */ |
| 1319 | c->dst.type = OP_NONE; |
| 1320 | |
| 1321 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1325 | struct x86_emulate_ops *ops) |
| 1326 | { |
| 1327 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1328 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1329 | int reg = VCPU_REGS_RDI; |
| 1330 | |
| 1331 | while (reg >= VCPU_REGS_RAX) { |
| 1332 | if (reg == VCPU_REGS_RSP) { |
| 1333 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1334 | c->op_bytes); |
| 1335 | --reg; |
| 1336 | } |
| 1337 | |
| 1338 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1339 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1340 | break; |
| 1341 | --reg; |
| 1342 | } |
| 1343 | return rc; |
| 1344 | } |
| 1345 | |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1346 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, |
| 1347 | struct x86_emulate_ops *ops, int irq) |
| 1348 | { |
| 1349 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1350 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1351 | struct desc_ptr dt; |
| 1352 | gva_t cs_addr; |
| 1353 | gva_t eip_addr; |
| 1354 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1355 | |
| 1356 | /* TODO: Add limit checks */ |
| 1357 | c->src.val = ctxt->eflags; |
| 1358 | emulate_push(ctxt, ops); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1359 | rc = writeback(ctxt, ops); |
| 1360 | if (rc != X86EMUL_CONTINUE) |
| 1361 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1362 | |
| 1363 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1364 | |
| 1365 | c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 1366 | emulate_push(ctxt, ops); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1367 | rc = writeback(ctxt, ops); |
| 1368 | if (rc != X86EMUL_CONTINUE) |
| 1369 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1370 | |
| 1371 | c->src.val = c->eip; |
| 1372 | emulate_push(ctxt, ops); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1373 | rc = writeback(ctxt, ops); |
| 1374 | if (rc != X86EMUL_CONTINUE) |
| 1375 | return rc; |
| 1376 | |
| 1377 | c->dst.type = OP_NONE; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1378 | |
| 1379 | ops->get_idt(&dt, ctxt->vcpu); |
| 1380 | |
| 1381 | eip_addr = dt.address + (irq << 2); |
| 1382 | cs_addr = dt.address + (irq << 2) + 2; |
| 1383 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1384 | rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1385 | if (rc != X86EMUL_CONTINUE) |
| 1386 | return rc; |
| 1387 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1388 | rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1389 | if (rc != X86EMUL_CONTINUE) |
| 1390 | return rc; |
| 1391 | |
| 1392 | rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS); |
| 1393 | if (rc != X86EMUL_CONTINUE) |
| 1394 | return rc; |
| 1395 | |
| 1396 | c->eip = eip; |
| 1397 | |
| 1398 | return rc; |
| 1399 | } |
| 1400 | |
| 1401 | static int emulate_int(struct x86_emulate_ctxt *ctxt, |
| 1402 | struct x86_emulate_ops *ops, int irq) |
| 1403 | { |
| 1404 | switch(ctxt->mode) { |
| 1405 | case X86EMUL_MODE_REAL: |
| 1406 | return emulate_int_real(ctxt, ops, irq); |
| 1407 | case X86EMUL_MODE_VM86: |
| 1408 | case X86EMUL_MODE_PROT16: |
| 1409 | case X86EMUL_MODE_PROT32: |
| 1410 | case X86EMUL_MODE_PROT64: |
| 1411 | default: |
| 1412 | /* Protected mode interrupts unimplemented yet */ |
| 1413 | return X86EMUL_UNHANDLEABLE; |
| 1414 | } |
| 1415 | } |
| 1416 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1417 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, |
| 1418 | struct x86_emulate_ops *ops) |
| 1419 | { |
| 1420 | struct decode_cache *c = &ctxt->decode; |
| 1421 | int rc = X86EMUL_CONTINUE; |
| 1422 | unsigned long temp_eip = 0; |
| 1423 | unsigned long temp_eflags = 0; |
| 1424 | unsigned long cs = 0; |
| 1425 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1426 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1427 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1428 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1429 | |
| 1430 | /* TODO: Add stack limit check */ |
| 1431 | |
| 1432 | rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); |
| 1433 | |
| 1434 | if (rc != X86EMUL_CONTINUE) |
| 1435 | return rc; |
| 1436 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1437 | if (temp_eip & ~0xffff) |
| 1438 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1439 | |
| 1440 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
| 1441 | |
| 1442 | if (rc != X86EMUL_CONTINUE) |
| 1443 | return rc; |
| 1444 | |
| 1445 | rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); |
| 1446 | |
| 1447 | if (rc != X86EMUL_CONTINUE) |
| 1448 | return rc; |
| 1449 | |
| 1450 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
| 1451 | |
| 1452 | if (rc != X86EMUL_CONTINUE) |
| 1453 | return rc; |
| 1454 | |
| 1455 | c->eip = temp_eip; |
| 1456 | |
| 1457 | |
| 1458 | if (c->op_bytes == 4) |
| 1459 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
| 1460 | else if (c->op_bytes == 2) { |
| 1461 | ctxt->eflags &= ~0xffff; |
| 1462 | ctxt->eflags |= temp_eflags; |
| 1463 | } |
| 1464 | |
| 1465 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1466 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1467 | |
| 1468 | return rc; |
| 1469 | } |
| 1470 | |
| 1471 | static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, |
| 1472 | struct x86_emulate_ops* ops) |
| 1473 | { |
| 1474 | switch(ctxt->mode) { |
| 1475 | case X86EMUL_MODE_REAL: |
| 1476 | return emulate_iret_real(ctxt, ops); |
| 1477 | case X86EMUL_MODE_VM86: |
| 1478 | case X86EMUL_MODE_PROT16: |
| 1479 | case X86EMUL_MODE_PROT32: |
| 1480 | case X86EMUL_MODE_PROT64: |
| 1481 | default: |
| 1482 | /* iret from protected mode unimplemented yet */ |
| 1483 | return X86EMUL_UNHANDLEABLE; |
| 1484 | } |
| 1485 | } |
| 1486 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1487 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1488 | struct x86_emulate_ops *ops) |
| 1489 | { |
| 1490 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1491 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1492 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1495 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1496 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1497 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1498 | switch (c->modrm_reg) { |
| 1499 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1500 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1501 | break; |
| 1502 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1503 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1504 | break; |
| 1505 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1506 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1507 | break; |
| 1508 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1509 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1510 | break; |
| 1511 | case 4: /* sal/shl */ |
| 1512 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1513 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1514 | break; |
| 1515 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1516 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1517 | break; |
| 1518 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1519 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1520 | break; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1525 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1526 | { |
| 1527 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1528 | unsigned long *rax = &c->regs[VCPU_REGS_RAX]; |
| 1529 | unsigned long *rdx = &c->regs[VCPU_REGS_RDX]; |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1530 | u8 de = 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1531 | |
| 1532 | switch (c->modrm_reg) { |
| 1533 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1534 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1535 | break; |
| 1536 | case 2: /* not */ |
| 1537 | c->dst.val = ~c->dst.val; |
| 1538 | break; |
| 1539 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1540 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1541 | break; |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1542 | case 4: /* mul */ |
| 1543 | emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags); |
| 1544 | break; |
| 1545 | case 5: /* imul */ |
| 1546 | emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags); |
| 1547 | break; |
| 1548 | case 6: /* div */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1549 | emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx, |
| 1550 | ctxt->eflags, de); |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1551 | break; |
| 1552 | case 7: /* idiv */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1553 | emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx, |
| 1554 | ctxt->eflags, de); |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1555 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1556 | default: |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 1557 | return X86EMUL_UNHANDLEABLE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1558 | } |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1559 | if (de) |
| 1560 | return emulate_de(ctxt); |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 1561 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1565 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1566 | { |
| 1567 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1568 | |
| 1569 | switch (c->modrm_reg) { |
| 1570 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1571 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1572 | break; |
| 1573 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1574 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1575 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1576 | case 2: /* call near abs */ { |
| 1577 | long int old_eip; |
| 1578 | old_eip = c->eip; |
| 1579 | c->eip = c->src.val; |
| 1580 | c->src.val = old_eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1581 | emulate_push(ctxt, ops); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1582 | break; |
| 1583 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1584 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1585 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1586 | break; |
| 1587 | case 6: /* push */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1588 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1589 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1590 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1591 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1595 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1596 | { |
| 1597 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1598 | u64 old = c->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1599 | |
| 1600 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1601 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1602 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1603 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1604 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1605 | } else { |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1606 | c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1607 | (u32) c->regs[VCPU_REGS_RBX]; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1608 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1609 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1610 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1611 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1614 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1615 | struct x86_emulate_ops *ops) |
| 1616 | { |
| 1617 | struct decode_cache *c = &ctxt->decode; |
| 1618 | int rc; |
| 1619 | unsigned long cs; |
| 1620 | |
| 1621 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1622 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1623 | return rc; |
| 1624 | if (c->op_bytes == 4) |
| 1625 | c->eip = (u32)c->eip; |
| 1626 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1627 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1628 | return rc; |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1629 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1630 | return rc; |
| 1631 | } |
| 1632 | |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 1633 | static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, |
| 1634 | struct x86_emulate_ops *ops, int seg) |
| 1635 | { |
| 1636 | struct decode_cache *c = &ctxt->decode; |
| 1637 | unsigned short sel; |
| 1638 | int rc; |
| 1639 | |
| 1640 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 1641 | |
| 1642 | rc = load_segment_descriptor(ctxt, ops, sel, seg); |
| 1643 | if (rc != X86EMUL_CONTINUE) |
| 1644 | return rc; |
| 1645 | |
| 1646 | c->dst.val = c->src.val; |
| 1647 | return rc; |
| 1648 | } |
| 1649 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1650 | static inline void |
| 1651 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1652 | struct x86_emulate_ops *ops, struct desc_struct *cs, |
| 1653 | struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1654 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1655 | memset(cs, 0, sizeof(struct desc_struct)); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1656 | ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1657 | memset(ss, 0, sizeof(struct desc_struct)); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1658 | |
| 1659 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1660 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1661 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1662 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1663 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1664 | cs->s = 1; |
| 1665 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1666 | cs->p = 1; |
| 1667 | cs->d = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1668 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1669 | set_desc_base(ss, 0); /* flat segment */ |
| 1670 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1671 | ss->g = 1; /* 4kb granularity */ |
| 1672 | ss->s = 1; |
| 1673 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1674 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1675 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1676 | ss->p = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1680 | emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1681 | { |
| 1682 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1683 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1684 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1685 | u16 cs_sel, ss_sel; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1686 | |
| 1687 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1688 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1689 | ctxt->mode == X86EMUL_MODE_VM86) |
| 1690 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1691 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1692 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1693 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1694 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1695 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1696 | cs_sel = (u16)(msr_data & 0xfffc); |
| 1697 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1698 | |
| 1699 | if (is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1700 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1701 | cs.l = 1; |
| 1702 | } |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1703 | ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1704 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1705 | ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1706 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1707 | |
| 1708 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 1709 | if (is_long_mode(ctxt->vcpu)) { |
| 1710 | #ifdef CONFIG_X86_64 |
| 1711 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 1712 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1713 | ops->get_msr(ctxt->vcpu, |
| 1714 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 1715 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1716 | c->eip = msr_data; |
| 1717 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1718 | ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1719 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 1720 | #endif |
| 1721 | } else { |
| 1722 | /* legacy mode */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1723 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1724 | c->eip = (u32)msr_data; |
| 1725 | |
| 1726 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1727 | } |
| 1728 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1729 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1732 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1733 | emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1734 | { |
| 1735 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1736 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1737 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1738 | u16 cs_sel, ss_sel; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1739 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1740 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1741 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1742 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1743 | |
| 1744 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 1745 | * Therefore, we inject an #UD. |
| 1746 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1747 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 1748 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1749 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1750 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1751 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1752 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1753 | switch (ctxt->mode) { |
| 1754 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1755 | if ((msr_data & 0xfffc) == 0x0) |
| 1756 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1757 | break; |
| 1758 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1759 | if (msr_data == 0x0) |
| 1760 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1761 | break; |
| 1762 | } |
| 1763 | |
| 1764 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1765 | cs_sel = (u16)msr_data; |
| 1766 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 1767 | ss_sel = cs_sel + 8; |
| 1768 | ss_sel &= ~SELECTOR_RPL_MASK; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1769 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 1770 | || is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1771 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1772 | cs.l = 1; |
| 1773 | } |
| 1774 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1775 | ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1776 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1777 | ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1778 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1779 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1780 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1781 | c->eip = msr_data; |
| 1782 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1783 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1784 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 1785 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1786 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1789 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1790 | emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1791 | { |
| 1792 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1793 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1794 | u64 msr_data; |
| 1795 | int usermode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1796 | u16 cs_sel, ss_sel; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1797 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1798 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 1799 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1800 | ctxt->mode == X86EMUL_MODE_VM86) |
| 1801 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1802 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1803 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1804 | |
| 1805 | if ((c->rex_prefix & 0x8) != 0x0) |
| 1806 | usermode = X86EMUL_MODE_PROT64; |
| 1807 | else |
| 1808 | usermode = X86EMUL_MODE_PROT32; |
| 1809 | |
| 1810 | cs.dpl = 3; |
| 1811 | ss.dpl = 3; |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1812 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1813 | switch (usermode) { |
| 1814 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1815 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1816 | if ((msr_data & 0xfffc) == 0x0) |
| 1817 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1818 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1819 | break; |
| 1820 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1821 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1822 | if (msr_data == 0x0) |
| 1823 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1824 | ss_sel = cs_sel + 8; |
| 1825 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1826 | cs.l = 1; |
| 1827 | break; |
| 1828 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1829 | cs_sel |= SELECTOR_RPL_MASK; |
| 1830 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1831 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1832 | ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1833 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1834 | ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1835 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1836 | |
Gleb Natapov | bdb475a | 2010-04-28 19:15:41 +0300 | [diff] [blame] | 1837 | c->eip = c->regs[VCPU_REGS_RDX]; |
| 1838 | c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX]; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1839 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1840 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1843 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt, |
| 1844 | struct x86_emulate_ops *ops) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1845 | { |
| 1846 | int iopl; |
| 1847 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1848 | return false; |
| 1849 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 1850 | return true; |
| 1851 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1852 | return ops->cpl(ctxt->vcpu) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 1856 | struct x86_emulate_ops *ops, |
| 1857 | u16 port, u16 len) |
| 1858 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1859 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1860 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1861 | int r; |
Gleb Natapov | 399a40c | 2011-03-07 14:55:07 +0200 | [diff] [blame] | 1862 | u16 io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1863 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1864 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1865 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1866 | ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1867 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1868 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1869 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1870 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1871 | base = get_desc_base(&tr_seg); |
| 1872 | #ifdef CONFIG_X86_64 |
| 1873 | base |= ((u64)base3) << 32; |
| 1874 | #endif |
| 1875 | r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1876 | if (r != X86EMUL_CONTINUE) |
| 1877 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1878 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1879 | return false; |
Gleb Natapov | 399a40c | 2011-03-07 14:55:07 +0200 | [diff] [blame] | 1880 | r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu, |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1881 | NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1882 | if (r != X86EMUL_CONTINUE) |
| 1883 | return false; |
| 1884 | if ((perm >> bit_idx) & mask) |
| 1885 | return false; |
| 1886 | return true; |
| 1887 | } |
| 1888 | |
| 1889 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 1890 | struct x86_emulate_ops *ops, |
| 1891 | u16 port, u16 len) |
| 1892 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 1893 | if (ctxt->perm_ok) |
| 1894 | return true; |
| 1895 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1896 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1897 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 1898 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 1899 | |
| 1900 | ctxt->perm_ok = true; |
| 1901 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1902 | return true; |
| 1903 | } |
| 1904 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1905 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
| 1906 | struct x86_emulate_ops *ops, |
| 1907 | struct tss_segment_16 *tss) |
| 1908 | { |
| 1909 | struct decode_cache *c = &ctxt->decode; |
| 1910 | |
| 1911 | tss->ip = c->eip; |
| 1912 | tss->flag = ctxt->eflags; |
| 1913 | tss->ax = c->regs[VCPU_REGS_RAX]; |
| 1914 | tss->cx = c->regs[VCPU_REGS_RCX]; |
| 1915 | tss->dx = c->regs[VCPU_REGS_RDX]; |
| 1916 | tss->bx = c->regs[VCPU_REGS_RBX]; |
| 1917 | tss->sp = c->regs[VCPU_REGS_RSP]; |
| 1918 | tss->bp = c->regs[VCPU_REGS_RBP]; |
| 1919 | tss->si = c->regs[VCPU_REGS_RSI]; |
| 1920 | tss->di = c->regs[VCPU_REGS_RDI]; |
| 1921 | |
| 1922 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 1923 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 1924 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 1925 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 1926 | tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 1927 | } |
| 1928 | |
| 1929 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
| 1930 | struct x86_emulate_ops *ops, |
| 1931 | struct tss_segment_16 *tss) |
| 1932 | { |
| 1933 | struct decode_cache *c = &ctxt->decode; |
| 1934 | int ret; |
| 1935 | |
| 1936 | c->eip = tss->ip; |
| 1937 | ctxt->eflags = tss->flag | 2; |
| 1938 | c->regs[VCPU_REGS_RAX] = tss->ax; |
| 1939 | c->regs[VCPU_REGS_RCX] = tss->cx; |
| 1940 | c->regs[VCPU_REGS_RDX] = tss->dx; |
| 1941 | c->regs[VCPU_REGS_RBX] = tss->bx; |
| 1942 | c->regs[VCPU_REGS_RSP] = tss->sp; |
| 1943 | c->regs[VCPU_REGS_RBP] = tss->bp; |
| 1944 | c->regs[VCPU_REGS_RSI] = tss->si; |
| 1945 | c->regs[VCPU_REGS_RDI] = tss->di; |
| 1946 | |
| 1947 | /* |
| 1948 | * SDM says that segment selectors are loaded before segment |
| 1949 | * descriptors |
| 1950 | */ |
| 1951 | ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu); |
| 1952 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 1953 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1954 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1955 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 1956 | |
| 1957 | /* |
| 1958 | * Now load segment descriptors. If fault happenes at this stage |
| 1959 | * it is handled in a context of new task |
| 1960 | */ |
| 1961 | ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR); |
| 1962 | if (ret != X86EMUL_CONTINUE) |
| 1963 | return ret; |
| 1964 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 1965 | if (ret != X86EMUL_CONTINUE) |
| 1966 | return ret; |
| 1967 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 1968 | if (ret != X86EMUL_CONTINUE) |
| 1969 | return ret; |
| 1970 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 1971 | if (ret != X86EMUL_CONTINUE) |
| 1972 | return ret; |
| 1973 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 1974 | if (ret != X86EMUL_CONTINUE) |
| 1975 | return ret; |
| 1976 | |
| 1977 | return X86EMUL_CONTINUE; |
| 1978 | } |
| 1979 | |
| 1980 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
| 1981 | struct x86_emulate_ops *ops, |
| 1982 | u16 tss_selector, u16 old_tss_sel, |
| 1983 | ulong old_tss_base, struct desc_struct *new_desc) |
| 1984 | { |
| 1985 | struct tss_segment_16 tss_seg; |
| 1986 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1987 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1988 | |
| 1989 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1990 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1991 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1992 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1993 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1994 | |
| 1995 | save_state_to_tss16(ctxt, ops, &tss_seg); |
| 1996 | |
| 1997 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1998 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1999 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2000 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2001 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2002 | |
| 2003 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2004 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2005 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2006 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2007 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2008 | |
| 2009 | if (old_tss_sel != 0xffff) { |
| 2010 | tss_seg.prev_task_link = old_tss_sel; |
| 2011 | |
| 2012 | ret = ops->write_std(new_tss_base, |
| 2013 | &tss_seg.prev_task_link, |
| 2014 | sizeof tss_seg.prev_task_link, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2015 | ctxt->vcpu, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2016 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2017 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2018 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | return load_state_from_tss16(ctxt, ops, &tss_seg); |
| 2022 | } |
| 2023 | |
| 2024 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
| 2025 | struct x86_emulate_ops *ops, |
| 2026 | struct tss_segment_32 *tss) |
| 2027 | { |
| 2028 | struct decode_cache *c = &ctxt->decode; |
| 2029 | |
| 2030 | tss->cr3 = ops->get_cr(3, ctxt->vcpu); |
| 2031 | tss->eip = c->eip; |
| 2032 | tss->eflags = ctxt->eflags; |
| 2033 | tss->eax = c->regs[VCPU_REGS_RAX]; |
| 2034 | tss->ecx = c->regs[VCPU_REGS_RCX]; |
| 2035 | tss->edx = c->regs[VCPU_REGS_RDX]; |
| 2036 | tss->ebx = c->regs[VCPU_REGS_RBX]; |
| 2037 | tss->esp = c->regs[VCPU_REGS_RSP]; |
| 2038 | tss->ebp = c->regs[VCPU_REGS_RBP]; |
| 2039 | tss->esi = c->regs[VCPU_REGS_RSI]; |
| 2040 | tss->edi = c->regs[VCPU_REGS_RDI]; |
| 2041 | |
| 2042 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2043 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2044 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2045 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2046 | tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu); |
| 2047 | tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu); |
| 2048 | tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2049 | } |
| 2050 | |
| 2051 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
| 2052 | struct x86_emulate_ops *ops, |
| 2053 | struct tss_segment_32 *tss) |
| 2054 | { |
| 2055 | struct decode_cache *c = &ctxt->decode; |
| 2056 | int ret; |
| 2057 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2058 | if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) |
| 2059 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2060 | c->eip = tss->eip; |
| 2061 | ctxt->eflags = tss->eflags | 2; |
| 2062 | c->regs[VCPU_REGS_RAX] = tss->eax; |
| 2063 | c->regs[VCPU_REGS_RCX] = tss->ecx; |
| 2064 | c->regs[VCPU_REGS_RDX] = tss->edx; |
| 2065 | c->regs[VCPU_REGS_RBX] = tss->ebx; |
| 2066 | c->regs[VCPU_REGS_RSP] = tss->esp; |
| 2067 | c->regs[VCPU_REGS_RBP] = tss->ebp; |
| 2068 | c->regs[VCPU_REGS_RSI] = tss->esi; |
| 2069 | c->regs[VCPU_REGS_RDI] = tss->edi; |
| 2070 | |
| 2071 | /* |
| 2072 | * SDM says that segment selectors are loaded before segment |
| 2073 | * descriptors |
| 2074 | */ |
| 2075 | ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2076 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2077 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2078 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2079 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2080 | ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu); |
| 2081 | ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu); |
| 2082 | |
| 2083 | /* |
| 2084 | * Now load segment descriptors. If fault happenes at this stage |
| 2085 | * it is handled in a context of new task |
| 2086 | */ |
| 2087 | ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2088 | if (ret != X86EMUL_CONTINUE) |
| 2089 | return ret; |
| 2090 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2091 | if (ret != X86EMUL_CONTINUE) |
| 2092 | return ret; |
| 2093 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2094 | if (ret != X86EMUL_CONTINUE) |
| 2095 | return ret; |
| 2096 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2097 | if (ret != X86EMUL_CONTINUE) |
| 2098 | return ret; |
| 2099 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2100 | if (ret != X86EMUL_CONTINUE) |
| 2101 | return ret; |
| 2102 | ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS); |
| 2103 | if (ret != X86EMUL_CONTINUE) |
| 2104 | return ret; |
| 2105 | ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS); |
| 2106 | if (ret != X86EMUL_CONTINUE) |
| 2107 | return ret; |
| 2108 | |
| 2109 | return X86EMUL_CONTINUE; |
| 2110 | } |
| 2111 | |
| 2112 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
| 2113 | struct x86_emulate_ops *ops, |
| 2114 | u16 tss_selector, u16 old_tss_sel, |
| 2115 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2116 | { |
| 2117 | struct tss_segment_32 tss_seg; |
| 2118 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2119 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2120 | |
| 2121 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2122 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2123 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2124 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2125 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2126 | |
| 2127 | save_state_to_tss32(ctxt, ops, &tss_seg); |
| 2128 | |
| 2129 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2130 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2131 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2132 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2133 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2134 | |
| 2135 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2136 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2137 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2138 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2139 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2140 | |
| 2141 | if (old_tss_sel != 0xffff) { |
| 2142 | tss_seg.prev_task_link = old_tss_sel; |
| 2143 | |
| 2144 | ret = ops->write_std(new_tss_base, |
| 2145 | &tss_seg.prev_task_link, |
| 2146 | sizeof tss_seg.prev_task_link, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2147 | ctxt->vcpu, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2148 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2149 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2150 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | return load_state_from_tss32(ctxt, ops, &tss_seg); |
| 2154 | } |
| 2155 | |
| 2156 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2157 | struct x86_emulate_ops *ops, |
| 2158 | u16 tss_selector, int reason, |
| 2159 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2160 | { |
| 2161 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2162 | int ret; |
| 2163 | u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu); |
| 2164 | ulong old_tss_base = |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 2165 | ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2166 | u32 desc_limit; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2167 | |
| 2168 | /* FIXME: old_tss_base == ~0 ? */ |
| 2169 | |
| 2170 | ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc); |
| 2171 | if (ret != X86EMUL_CONTINUE) |
| 2172 | return ret; |
| 2173 | ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc); |
| 2174 | if (ret != X86EMUL_CONTINUE) |
| 2175 | return ret; |
| 2176 | |
| 2177 | /* FIXME: check that next_tss_desc is tss */ |
| 2178 | |
| 2179 | if (reason != TASK_SWITCH_IRET) { |
| 2180 | if ((tss_selector & 3) > next_tss_desc.dpl || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2181 | ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) |
| 2182 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2185 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2186 | if (!next_tss_desc.p || |
| 2187 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2188 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2189 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2190 | return X86EMUL_PROPAGATE_FAULT; |
| 2191 | } |
| 2192 | |
| 2193 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2194 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
| 2195 | write_segment_descriptor(ctxt, ops, old_tss_sel, |
| 2196 | &curr_tss_desc); |
| 2197 | } |
| 2198 | |
| 2199 | if (reason == TASK_SWITCH_IRET) |
| 2200 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2201 | |
| 2202 | /* set back link to prev task only if NT bit is set in eflags |
| 2203 | note that old_tss_sel is not used afetr this point */ |
| 2204 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2205 | old_tss_sel = 0xffff; |
| 2206 | |
| 2207 | if (next_tss_desc.type & 8) |
| 2208 | ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel, |
| 2209 | old_tss_base, &next_tss_desc); |
| 2210 | else |
| 2211 | ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel, |
| 2212 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2213 | if (ret != X86EMUL_CONTINUE) |
| 2214 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2215 | |
| 2216 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2217 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2218 | |
| 2219 | if (reason != TASK_SWITCH_IRET) { |
| 2220 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
| 2221 | write_segment_descriptor(ctxt, ops, tss_selector, |
| 2222 | &next_tss_desc); |
| 2223 | } |
| 2224 | |
| 2225 | ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu); |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2226 | ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2227 | ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu); |
| 2228 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2229 | if (has_error_code) { |
| 2230 | struct decode_cache *c = &ctxt->decode; |
| 2231 | |
| 2232 | c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2233 | c->lock_prefix = 0; |
| 2234 | c->src.val = (unsigned long) error_code; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2235 | emulate_push(ctxt, ops); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2238 | return ret; |
| 2239 | } |
| 2240 | |
| 2241 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2242 | u16 tss_selector, int reason, |
| 2243 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2244 | { |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 2245 | struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2246 | struct decode_cache *c = &ctxt->decode; |
| 2247 | int rc; |
| 2248 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2249 | c->eip = ctxt->eip; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2250 | c->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2251 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2252 | rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason, |
| 2253 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2254 | |
| 2255 | if (rc == X86EMUL_CONTINUE) { |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2256 | rc = writeback(ctxt, ops); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2257 | if (rc == X86EMUL_CONTINUE) |
| 2258 | ctxt->eip = c->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2259 | } |
| 2260 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 2261 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2262 | } |
| 2263 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2264 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg, |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2265 | int reg, struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2266 | { |
| 2267 | struct decode_cache *c = &ctxt->decode; |
| 2268 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
| 2269 | |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2270 | register_address_increment(c, &c->regs[reg], df * op->bytes); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2271 | op->addr.mem.ea = register_address(c, c->regs[reg]); |
| 2272 | op->addr.mem.seg = seg; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 2275 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 2276 | { |
| 2277 | emulate_push(ctxt, ctxt->ops); |
| 2278 | return X86EMUL_CONTINUE; |
| 2279 | } |
| 2280 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2281 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2282 | { |
| 2283 | struct decode_cache *c = &ctxt->decode; |
| 2284 | u8 al, old_al; |
| 2285 | bool af, cf, old_cf; |
| 2286 | |
| 2287 | cf = ctxt->eflags & X86_EFLAGS_CF; |
| 2288 | al = c->dst.val; |
| 2289 | |
| 2290 | old_al = al; |
| 2291 | old_cf = cf; |
| 2292 | cf = false; |
| 2293 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2294 | if ((al & 0x0f) > 9 || af) { |
| 2295 | al -= 6; |
| 2296 | cf = old_cf | (al >= 250); |
| 2297 | af = true; |
| 2298 | } else { |
| 2299 | af = false; |
| 2300 | } |
| 2301 | if (old_al > 0x99 || old_cf) { |
| 2302 | al -= 0x60; |
| 2303 | cf = true; |
| 2304 | } |
| 2305 | |
| 2306 | c->dst.val = al; |
| 2307 | /* Set PF, ZF, SF */ |
| 2308 | c->src.type = OP_IMM; |
| 2309 | c->src.val = 0; |
| 2310 | c->src.bytes = 1; |
| 2311 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
| 2312 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2313 | if (cf) |
| 2314 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2315 | if (af) |
| 2316 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2317 | return X86EMUL_CONTINUE; |
| 2318 | } |
| 2319 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2320 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2321 | { |
| 2322 | struct decode_cache *c = &ctxt->decode; |
| 2323 | u16 sel, old_cs; |
| 2324 | ulong old_eip; |
| 2325 | int rc; |
| 2326 | |
| 2327 | old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2328 | old_eip = c->eip; |
| 2329 | |
| 2330 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 2331 | if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS)) |
| 2332 | return X86EMUL_CONTINUE; |
| 2333 | |
| 2334 | c->eip = 0; |
| 2335 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
| 2336 | |
| 2337 | c->src.val = old_cs; |
| 2338 | emulate_push(ctxt, ctxt->ops); |
| 2339 | rc = writeback(ctxt, ctxt->ops); |
| 2340 | if (rc != X86EMUL_CONTINUE) |
| 2341 | return rc; |
| 2342 | |
| 2343 | c->src.val = old_eip; |
| 2344 | emulate_push(ctxt, ctxt->ops); |
| 2345 | rc = writeback(ctxt, ctxt->ops); |
| 2346 | if (rc != X86EMUL_CONTINUE) |
| 2347 | return rc; |
| 2348 | |
| 2349 | c->dst.type = OP_NONE; |
| 2350 | |
| 2351 | return X86EMUL_CONTINUE; |
| 2352 | } |
| 2353 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2354 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2355 | { |
| 2356 | struct decode_cache *c = &ctxt->decode; |
| 2357 | int rc; |
| 2358 | |
| 2359 | c->dst.type = OP_REG; |
| 2360 | c->dst.addr.reg = &c->eip; |
| 2361 | c->dst.bytes = c->op_bytes; |
| 2362 | rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes); |
| 2363 | if (rc != X86EMUL_CONTINUE) |
| 2364 | return rc; |
| 2365 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val); |
| 2366 | return X86EMUL_CONTINUE; |
| 2367 | } |
| 2368 | |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2369 | static int em_imul(struct x86_emulate_ctxt *ctxt) |
| 2370 | { |
| 2371 | struct decode_cache *c = &ctxt->decode; |
| 2372 | |
| 2373 | emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags); |
| 2374 | return X86EMUL_CONTINUE; |
| 2375 | } |
| 2376 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2377 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2378 | { |
| 2379 | struct decode_cache *c = &ctxt->decode; |
| 2380 | |
| 2381 | c->dst.val = c->src2.val; |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2382 | return em_imul(ctxt); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2383 | } |
| 2384 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2385 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2386 | { |
| 2387 | struct decode_cache *c = &ctxt->decode; |
| 2388 | |
| 2389 | c->dst.type = OP_REG; |
| 2390 | c->dst.bytes = c->src.bytes; |
| 2391 | c->dst.addr.reg = &c->regs[VCPU_REGS_RDX]; |
| 2392 | c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1); |
| 2393 | |
| 2394 | return X86EMUL_CONTINUE; |
| 2395 | } |
| 2396 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2397 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2398 | { |
| 2399 | unsigned cpl = ctxt->ops->cpl(ctxt->vcpu); |
| 2400 | struct decode_cache *c = &ctxt->decode; |
| 2401 | u64 tsc = 0; |
| 2402 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2403 | if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) |
| 2404 | return emulate_gp(ctxt, 0); |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2405 | ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc); |
| 2406 | c->regs[VCPU_REGS_RAX] = (u32)tsc; |
| 2407 | c->regs[VCPU_REGS_RDX] = tsc >> 32; |
| 2408 | return X86EMUL_CONTINUE; |
| 2409 | } |
| 2410 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2411 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 2412 | { |
| 2413 | struct decode_cache *c = &ctxt->decode; |
| 2414 | c->dst.val = c->src.val; |
| 2415 | return X86EMUL_CONTINUE; |
| 2416 | } |
| 2417 | |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2418 | static int em_movdqu(struct x86_emulate_ctxt *ctxt) |
| 2419 | { |
| 2420 | struct decode_cache *c = &ctxt->decode; |
| 2421 | memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes); |
| 2422 | return X86EMUL_CONTINUE; |
| 2423 | } |
| 2424 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2425 | #define D(_y) { .flags = (_y) } |
| 2426 | #define N D(0) |
| 2427 | #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) } |
| 2428 | #define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) } |
| 2429 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2430 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2431 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2432 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
| 2433 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
| 2434 | |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2435 | #define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \ |
| 2436 | D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ |
| 2437 | D2bv(((_f) & ~Lock) | DstAcc | SrcImm) |
| 2438 | |
| 2439 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2440 | static struct opcode group1[] = { |
| 2441 | X7(D(Lock)), N |
| 2442 | }; |
| 2443 | |
| 2444 | static struct opcode group1A[] = { |
| 2445 | D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N, |
| 2446 | }; |
| 2447 | |
| 2448 | static struct opcode group3[] = { |
| 2449 | D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM), |
| 2450 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 2451 | X4(D(SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2452 | }; |
| 2453 | |
| 2454 | static struct opcode group4[] = { |
| 2455 | D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock), |
| 2456 | N, N, N, N, N, N, |
| 2457 | }; |
| 2458 | |
| 2459 | static struct opcode group5[] = { |
| 2460 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2461 | D(SrcMem | ModRM | Stack), |
| 2462 | I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2463 | D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps), |
| 2464 | D(SrcMem | ModRM | Stack), N, |
| 2465 | }; |
| 2466 | |
| 2467 | static struct group_dual group7 = { { |
| 2468 | N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv), |
| 2469 | D(SrcNone | ModRM | DstMem | Mov), N, |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 2470 | D(SrcMem16 | ModRM | Mov | Priv), |
| 2471 | D(SrcMem | ModRM | ByteOp | Priv | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2472 | }, { |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 2473 | D(SrcNone | ModRM | Priv | VendorSpecific), N, |
| 2474 | N, D(SrcNone | ModRM | Priv | VendorSpecific), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2475 | D(SrcNone | ModRM | DstMem | Mov), N, |
| 2476 | D(SrcMem16 | ModRM | Mov | Priv), N, |
| 2477 | } }; |
| 2478 | |
| 2479 | static struct opcode group8[] = { |
| 2480 | N, N, N, N, |
| 2481 | D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock), |
| 2482 | D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock), |
| 2483 | }; |
| 2484 | |
| 2485 | static struct group_dual group9 = { { |
| 2486 | N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N, |
| 2487 | }, { |
| 2488 | N, N, N, N, N, N, N, N, |
| 2489 | } }; |
| 2490 | |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 2491 | static struct opcode group11[] = { |
| 2492 | I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)), |
| 2493 | }; |
| 2494 | |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2495 | static struct gprefix pfx_0f_6f_0f_7f = { |
| 2496 | N, N, N, I(Sse, em_movdqu), |
| 2497 | }; |
| 2498 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2499 | static struct opcode opcode_table[256] = { |
| 2500 | /* 0x00 - 0x07 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2501 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2502 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2503 | /* 0x08 - 0x0F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2504 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2505 | D(ImplicitOps | Stack | No64), N, |
| 2506 | /* 0x10 - 0x17 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2507 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2508 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2509 | /* 0x18 - 0x1F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2510 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2511 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2512 | /* 0x20 - 0x27 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2513 | D6ALU(Lock), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2514 | /* 0x28 - 0x2F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2515 | D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2516 | /* 0x30 - 0x37 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2517 | D6ALU(Lock), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2518 | /* 0x38 - 0x3F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2519 | D6ALU(0), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2520 | /* 0x40 - 0x4F */ |
| 2521 | X16(D(DstReg)), |
| 2522 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 2523 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2524 | /* 0x58 - 0x5F */ |
| 2525 | X8(D(DstReg | Stack)), |
| 2526 | /* 0x60 - 0x67 */ |
| 2527 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2528 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 2529 | N, N, N, N, |
| 2530 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 2531 | I(SrcImm | Mov | Stack, em_push), |
| 2532 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2533 | I(SrcImmByte | Mov | Stack, em_push), |
| 2534 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Avi Kivity | 48fe67b | 2010-08-26 11:56:08 +0300 | [diff] [blame] | 2535 | D2bv(DstDI | Mov | String), /* insb, insw/insd */ |
| 2536 | D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */ |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2537 | /* 0x70 - 0x7F */ |
| 2538 | X16(D(SrcImmByte)), |
| 2539 | /* 0x80 - 0x87 */ |
| 2540 | G(ByteOp | DstMem | SrcImm | ModRM | Group, group1), |
| 2541 | G(DstMem | SrcImm | ModRM | Group, group1), |
| 2542 | G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1), |
| 2543 | G(DstMem | SrcImmByte | ModRM | Group, group1), |
Avi Kivity | 76e8e68 | 2010-08-26 11:56:09 +0300 | [diff] [blame] | 2544 | D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2545 | /* 0x88 - 0x8F */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2546 | I2bv(DstMem | SrcReg | ModRM | Mov, em_mov), |
| 2547 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Avi Kivity | 342fc63 | 2010-08-01 15:13:22 +0300 | [diff] [blame] | 2548 | D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2549 | D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A), |
| 2550 | /* 0x90 - 0x97 */ |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 2551 | X8(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2552 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2553 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 2554 | I(SrcImmFAddr | No64, em_call_far), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2555 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N, |
| 2556 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2557 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
| 2558 | I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov), |
| 2559 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
| 2560 | D2bv(SrcSI | DstDI | String), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2561 | /* 0xA8 - 0xAF */ |
Avi Kivity | 5074861 | 2010-08-26 11:56:10 +0300 | [diff] [blame] | 2562 | D2bv(DstAcc | SrcImm), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2563 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 2564 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | 48fe67b | 2010-08-26 11:56:08 +0300 | [diff] [blame] | 2565 | D2bv(SrcAcc | DstDI | String), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2566 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2567 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2568 | /* 0xB8 - 0xBF */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2569 | X8(I(DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2570 | /* 0xC0 - 0xC7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 2571 | D2bv(DstMem | SrcImmByte | ModRM), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2572 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
| 2573 | D(ImplicitOps | Stack), |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2574 | D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 2575 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2576 | /* 0xC8 - 0xCF */ |
| 2577 | N, N, N, D(ImplicitOps | Stack), |
| 2578 | D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps), |
| 2579 | /* 0xD0 - 0xD7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 2580 | D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2581 | N, N, N, N, |
| 2582 | /* 0xD8 - 0xDF */ |
| 2583 | N, N, N, N, N, N, N, N, |
| 2584 | /* 0xE0 - 0xE7 */ |
Wei Yongjun | e4abac6 | 2010-08-19 14:25:48 +0800 | [diff] [blame] | 2585 | X4(D(SrcImmByte)), |
Avi Kivity | d269e39 | 2010-08-26 11:56:12 +0300 | [diff] [blame] | 2586 | D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2587 | /* 0xE8 - 0xEF */ |
| 2588 | D(SrcImm | Stack), D(SrcImm | ImplicitOps), |
| 2589 | D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps), |
Avi Kivity | d269e39 | 2010-08-26 11:56:12 +0300 | [diff] [blame] | 2590 | D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2591 | /* 0xF0 - 0xF7 */ |
| 2592 | N, N, N, N, |
| 2593 | D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3), |
| 2594 | /* 0xF8 - 0xFF */ |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 2595 | D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2596 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 2597 | }; |
| 2598 | |
| 2599 | static struct opcode twobyte_table[256] = { |
| 2600 | /* 0x00 - 0x0F */ |
| 2601 | N, GD(0, &group7), N, N, |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 2602 | N, D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2603 | D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N, |
| 2604 | N, D(ImplicitOps | ModRM), N, N, |
| 2605 | /* 0x10 - 0x1F */ |
| 2606 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, |
| 2607 | /* 0x20 - 0x2F */ |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 2608 | D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264), |
| 2609 | D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2610 | N, N, N, N, |
| 2611 | N, N, N, N, N, N, N, N, |
| 2612 | /* 0x30 - 0x3F */ |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2613 | D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc), |
| 2614 | D(ImplicitOps | Priv), N, |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 2615 | D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific), |
| 2616 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2617 | N, N, N, N, N, N, N, N, |
| 2618 | /* 0x40 - 0x4F */ |
| 2619 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 2620 | /* 0x50 - 0x5F */ |
| 2621 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2622 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2623 | N, N, N, N, |
| 2624 | N, N, N, N, |
| 2625 | N, N, N, N, |
| 2626 | N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2627 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2628 | N, N, N, N, |
| 2629 | N, N, N, N, |
| 2630 | N, N, N, N, |
| 2631 | N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2632 | /* 0x80 - 0x8F */ |
| 2633 | X16(D(SrcImm)), |
| 2634 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 2635 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2636 | /* 0xA0 - 0xA7 */ |
| 2637 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 2638 | N, D(DstMem | SrcReg | ModRM | BitOp), |
| 2639 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 2640 | D(DstMem | SrcReg | Src2CL | ModRM), N, N, |
| 2641 | /* 0xA8 - 0xAF */ |
| 2642 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 2643 | N, D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 2644 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 2645 | D(DstMem | SrcReg | Src2CL | ModRM), |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2646 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2647 | /* 0xB0 - 0xB7 */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 2648 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2649 | D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 2650 | D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM), |
| 2651 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2652 | /* 0xB8 - 0xBF */ |
| 2653 | N, N, |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 2654 | G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), |
Wei Yongjun | d9574a2 | 2010-08-10 13:48:22 +0800 | [diff] [blame] | 2655 | D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 2656 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2657 | /* 0xC0 - 0xCF */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 2658 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 2659 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2660 | N, N, N, GD(0, &group9), |
| 2661 | N, N, N, N, N, N, N, N, |
| 2662 | /* 0xD0 - 0xDF */ |
| 2663 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2664 | /* 0xE0 - 0xEF */ |
| 2665 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2666 | /* 0xF0 - 0xFF */ |
| 2667 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 2668 | }; |
| 2669 | |
| 2670 | #undef D |
| 2671 | #undef N |
| 2672 | #undef G |
| 2673 | #undef GD |
| 2674 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame^] | 2675 | #undef GP |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2676 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2677 | #undef D2bv |
| 2678 | #undef I2bv |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2679 | #undef D6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2680 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2681 | static unsigned imm_size(struct decode_cache *c) |
| 2682 | { |
| 2683 | unsigned size; |
| 2684 | |
| 2685 | size = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 2686 | if (size == 8) |
| 2687 | size = 4; |
| 2688 | return size; |
| 2689 | } |
| 2690 | |
| 2691 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 2692 | unsigned size, bool sign_extension) |
| 2693 | { |
| 2694 | struct decode_cache *c = &ctxt->decode; |
| 2695 | struct x86_emulate_ops *ops = ctxt->ops; |
| 2696 | int rc = X86EMUL_CONTINUE; |
| 2697 | |
| 2698 | op->type = OP_IMM; |
| 2699 | op->bytes = size; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2700 | op->addr.mem.ea = c->eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2701 | /* NB. Immediates are sign-extended as necessary. */ |
| 2702 | switch (op->bytes) { |
| 2703 | case 1: |
| 2704 | op->val = insn_fetch(s8, 1, c->eip); |
| 2705 | break; |
| 2706 | case 2: |
| 2707 | op->val = insn_fetch(s16, 2, c->eip); |
| 2708 | break; |
| 2709 | case 4: |
| 2710 | op->val = insn_fetch(s32, 4, c->eip); |
| 2711 | break; |
| 2712 | } |
| 2713 | if (!sign_extension) { |
| 2714 | switch (op->bytes) { |
| 2715 | case 1: |
| 2716 | op->val &= 0xff; |
| 2717 | break; |
| 2718 | case 2: |
| 2719 | op->val &= 0xffff; |
| 2720 | break; |
| 2721 | case 4: |
| 2722 | op->val &= 0xffffffff; |
| 2723 | break; |
| 2724 | } |
| 2725 | } |
| 2726 | done: |
| 2727 | return rc; |
| 2728 | } |
| 2729 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2730 | int |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 2731 | x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2732 | { |
| 2733 | struct x86_emulate_ops *ops = ctxt->ops; |
| 2734 | struct decode_cache *c = &ctxt->decode; |
| 2735 | int rc = X86EMUL_CONTINUE; |
| 2736 | int mode = ctxt->mode; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 2737 | int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix; |
| 2738 | bool op_prefix = false; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2739 | struct opcode opcode, *g_mod012, *g_mod3; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2740 | struct operand memop = { .type = OP_NONE }; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2741 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2742 | c->eip = ctxt->eip; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 2743 | c->fetch.start = c->eip; |
| 2744 | c->fetch.end = c->fetch.start + insn_len; |
| 2745 | if (insn_len > 0) |
| 2746 | memcpy(c->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2747 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); |
| 2748 | |
| 2749 | switch (mode) { |
| 2750 | case X86EMUL_MODE_REAL: |
| 2751 | case X86EMUL_MODE_VM86: |
| 2752 | case X86EMUL_MODE_PROT16: |
| 2753 | def_op_bytes = def_ad_bytes = 2; |
| 2754 | break; |
| 2755 | case X86EMUL_MODE_PROT32: |
| 2756 | def_op_bytes = def_ad_bytes = 4; |
| 2757 | break; |
| 2758 | #ifdef CONFIG_X86_64 |
| 2759 | case X86EMUL_MODE_PROT64: |
| 2760 | def_op_bytes = 4; |
| 2761 | def_ad_bytes = 8; |
| 2762 | break; |
| 2763 | #endif |
| 2764 | default: |
| 2765 | return -1; |
| 2766 | } |
| 2767 | |
| 2768 | c->op_bytes = def_op_bytes; |
| 2769 | c->ad_bytes = def_ad_bytes; |
| 2770 | |
| 2771 | /* Legacy prefixes. */ |
| 2772 | for (;;) { |
| 2773 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
| 2774 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 2775 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2776 | /* switch between 2/4 bytes */ |
| 2777 | c->op_bytes = def_op_bytes ^ 6; |
| 2778 | break; |
| 2779 | case 0x67: /* address-size override */ |
| 2780 | if (mode == X86EMUL_MODE_PROT64) |
| 2781 | /* switch between 4/8 bytes */ |
| 2782 | c->ad_bytes = def_ad_bytes ^ 12; |
| 2783 | else |
| 2784 | /* switch between 2/4 bytes */ |
| 2785 | c->ad_bytes = def_ad_bytes ^ 6; |
| 2786 | break; |
| 2787 | case 0x26: /* ES override */ |
| 2788 | case 0x2e: /* CS override */ |
| 2789 | case 0x36: /* SS override */ |
| 2790 | case 0x3e: /* DS override */ |
| 2791 | set_seg_override(c, (c->b >> 3) & 3); |
| 2792 | break; |
| 2793 | case 0x64: /* FS override */ |
| 2794 | case 0x65: /* GS override */ |
| 2795 | set_seg_override(c, c->b & 7); |
| 2796 | break; |
| 2797 | case 0x40 ... 0x4f: /* REX */ |
| 2798 | if (mode != X86EMUL_MODE_PROT64) |
| 2799 | goto done_prefixes; |
| 2800 | c->rex_prefix = c->b; |
| 2801 | continue; |
| 2802 | case 0xf0: /* LOCK */ |
| 2803 | c->lock_prefix = 1; |
| 2804 | break; |
| 2805 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2806 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 1d6b114 | 2010-01-20 16:00:35 +0200 | [diff] [blame] | 2807 | c->rep_prefix = c->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2808 | break; |
| 2809 | default: |
| 2810 | goto done_prefixes; |
| 2811 | } |
| 2812 | |
| 2813 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 2814 | |
| 2815 | c->rex_prefix = 0; |
| 2816 | } |
| 2817 | |
| 2818 | done_prefixes: |
| 2819 | |
| 2820 | /* REX prefix. */ |
Avi Kivity | 1e87e3e | 2010-08-01 14:42:51 +0300 | [diff] [blame] | 2821 | if (c->rex_prefix & 8) |
| 2822 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2823 | |
| 2824 | /* Opcode byte(s). */ |
| 2825 | opcode = opcode_table[c->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 2826 | /* Two-byte opcode? */ |
| 2827 | if (c->b == 0x0f) { |
| 2828 | c->twobyte = 1; |
| 2829 | c->b = insn_fetch(u8, 1, c->eip); |
| 2830 | opcode = twobyte_table[c->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2831 | } |
| 2832 | c->d = opcode.flags; |
| 2833 | |
| 2834 | if (c->d & Group) { |
| 2835 | dual = c->d & GroupDual; |
| 2836 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 2837 | --c->eip; |
| 2838 | |
| 2839 | if (c->d & GroupDual) { |
| 2840 | g_mod012 = opcode.u.gdual->mod012; |
| 2841 | g_mod3 = opcode.u.gdual->mod3; |
| 2842 | } else |
| 2843 | g_mod012 = g_mod3 = opcode.u.group; |
| 2844 | |
| 2845 | c->d &= ~(Group | GroupDual); |
| 2846 | |
| 2847 | goffset = (c->modrm >> 3) & 7; |
| 2848 | |
| 2849 | if ((c->modrm >> 6) == 3) |
| 2850 | opcode = g_mod3[goffset]; |
| 2851 | else |
| 2852 | opcode = g_mod012[goffset]; |
| 2853 | c->d |= opcode.flags; |
| 2854 | } |
| 2855 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 2856 | if (c->d & Prefix) { |
| 2857 | if (c->rep_prefix && op_prefix) |
| 2858 | return X86EMUL_UNHANDLEABLE; |
| 2859 | simd_prefix = op_prefix ? 0x66 : c->rep_prefix; |
| 2860 | switch (simd_prefix) { |
| 2861 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 2862 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 2863 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 2864 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 2865 | } |
| 2866 | c->d |= opcode.flags; |
| 2867 | } |
| 2868 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2869 | c->execute = opcode.u.execute; |
| 2870 | |
| 2871 | /* Unrecognised? */ |
Avi Kivity | d53db5e | 2010-11-17 13:40:51 +0200 | [diff] [blame] | 2872 | if (c->d == 0 || (c->d & Undefined)) |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2873 | return -1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2874 | |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 2875 | if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
| 2876 | return -1; |
| 2877 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2878 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 2879 | c->op_bytes = 8; |
| 2880 | |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 2881 | if (c->d & Op3264) { |
| 2882 | if (mode == X86EMUL_MODE_PROT64) |
| 2883 | c->op_bytes = 8; |
| 2884 | else |
| 2885 | c->op_bytes = 4; |
| 2886 | } |
| 2887 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 2888 | if (c->d & Sse) |
| 2889 | c->op_bytes = 16; |
| 2890 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2891 | /* ModRM and SIB bytes. */ |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 2892 | if (c->d & ModRM) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2893 | rc = decode_modrm(ctxt, ops, &memop); |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 2894 | if (!c->has_seg_override) |
| 2895 | set_seg_override(c, c->modrm_seg); |
| 2896 | } else if (c->d & MemAbs) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2897 | rc = decode_abs(ctxt, ops, &memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2898 | if (rc != X86EMUL_CONTINUE) |
| 2899 | goto done; |
| 2900 | |
| 2901 | if (!c->has_seg_override) |
| 2902 | set_seg_override(c, VCPU_SREG_DS); |
| 2903 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2904 | memop.addr.mem.seg = seg_override(ctxt, ops, c); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2905 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2906 | if (memop.type == OP_MEM && c->ad_bytes != 8) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2907 | memop.addr.mem.ea = (u32)memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2908 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2909 | if (memop.type == OP_MEM && c->rip_relative) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2910 | memop.addr.mem.ea += c->eip; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2911 | |
| 2912 | /* |
| 2913 | * Decode and fetch the source operand: register, memory |
| 2914 | * or immediate. |
| 2915 | */ |
| 2916 | switch (c->d & SrcMask) { |
| 2917 | case SrcNone: |
| 2918 | break; |
| 2919 | case SrcReg: |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 2920 | decode_register_operand(ctxt, &c->src, c, 0); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2921 | break; |
| 2922 | case SrcMem16: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2923 | memop.bytes = 2; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2924 | goto srcmem_common; |
| 2925 | case SrcMem32: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2926 | memop.bytes = 4; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2927 | goto srcmem_common; |
| 2928 | case SrcMem: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2929 | memop.bytes = (c->d & ByteOp) ? 1 : |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2930 | c->op_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2931 | srcmem_common: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2932 | c->src = memop; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2933 | break; |
Avi Kivity | b250e60 | 2010-08-18 15:11:24 +0300 | [diff] [blame] | 2934 | case SrcImmU16: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2935 | rc = decode_imm(ctxt, &c->src, 2, false); |
| 2936 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2937 | case SrcImm: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2938 | rc = decode_imm(ctxt, &c->src, imm_size(c), true); |
| 2939 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2940 | case SrcImmU: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2941 | rc = decode_imm(ctxt, &c->src, imm_size(c), false); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2942 | break; |
| 2943 | case SrcImmByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2944 | rc = decode_imm(ctxt, &c->src, 1, true); |
| 2945 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2946 | case SrcImmUByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2947 | rc = decode_imm(ctxt, &c->src, 1, false); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2948 | break; |
| 2949 | case SrcAcc: |
| 2950 | c->src.type = OP_REG; |
| 2951 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 2952 | c->src.addr.reg = &c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 2953 | fetch_register_operand(&c->src); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2954 | break; |
| 2955 | case SrcOne: |
| 2956 | c->src.bytes = 1; |
| 2957 | c->src.val = 1; |
| 2958 | break; |
| 2959 | case SrcSI: |
| 2960 | c->src.type = OP_MEM; |
| 2961 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2962 | c->src.addr.mem.ea = |
| 2963 | register_address(c, c->regs[VCPU_REGS_RSI]); |
| 2964 | c->src.addr.mem.seg = seg_override(ctxt, ops, c), |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2965 | c->src.val = 0; |
| 2966 | break; |
| 2967 | case SrcImmFAddr: |
| 2968 | c->src.type = OP_IMM; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2969 | c->src.addr.mem.ea = c->eip; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2970 | c->src.bytes = c->op_bytes + 2; |
| 2971 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); |
| 2972 | break; |
| 2973 | case SrcMemFAddr: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2974 | memop.bytes = c->op_bytes + 2; |
| 2975 | goto srcmem_common; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2976 | break; |
| 2977 | } |
| 2978 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2979 | if (rc != X86EMUL_CONTINUE) |
| 2980 | goto done; |
| 2981 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2982 | /* |
| 2983 | * Decode and fetch the second source operand: register, memory |
| 2984 | * or immediate. |
| 2985 | */ |
| 2986 | switch (c->d & Src2Mask) { |
| 2987 | case Src2None: |
| 2988 | break; |
| 2989 | case Src2CL: |
| 2990 | c->src2.bytes = 1; |
| 2991 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 2992 | break; |
| 2993 | case Src2ImmByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2994 | rc = decode_imm(ctxt, &c->src2, 1, true); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2995 | break; |
| 2996 | case Src2One: |
| 2997 | c->src2.bytes = 1; |
| 2998 | c->src2.val = 1; |
| 2999 | break; |
Avi Kivity | 7db41eb | 2010-08-18 19:25:28 +0300 | [diff] [blame] | 3000 | case Src2Imm: |
| 3001 | rc = decode_imm(ctxt, &c->src2, imm_size(c), true); |
| 3002 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3003 | } |
| 3004 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3005 | if (rc != X86EMUL_CONTINUE) |
| 3006 | goto done; |
| 3007 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3008 | /* Decode and fetch the destination operand: register or memory. */ |
| 3009 | switch (c->d & DstMask) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3010 | case DstReg: |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 3011 | decode_register_operand(ctxt, &c->dst, c, |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3012 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
| 3013 | break; |
Wei Yongjun | 943858e | 2010-08-06 11:36:51 +0800 | [diff] [blame] | 3014 | case DstImmUByte: |
| 3015 | c->dst.type = OP_IMM; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3016 | c->dst.addr.mem.ea = c->eip; |
Wei Yongjun | 943858e | 2010-08-06 11:36:51 +0800 | [diff] [blame] | 3017 | c->dst.bytes = 1; |
| 3018 | c->dst.val = insn_fetch(u8, 1, c->eip); |
| 3019 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3020 | case DstMem: |
| 3021 | case DstMem64: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 3022 | c->dst = memop; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3023 | if ((c->d & DstMask) == DstMem64) |
| 3024 | c->dst.bytes = 8; |
| 3025 | else |
| 3026 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 3027 | if (c->d & BitOp) |
| 3028 | fetch_bit_operand(c); |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 3029 | c->dst.orig_val = c->dst.val; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3030 | break; |
| 3031 | case DstAcc: |
| 3032 | c->dst.type = OP_REG; |
| 3033 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3034 | c->dst.addr.reg = &c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 3035 | fetch_register_operand(&c->dst); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3036 | c->dst.orig_val = c->dst.val; |
| 3037 | break; |
| 3038 | case DstDI: |
| 3039 | c->dst.type = OP_MEM; |
| 3040 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3041 | c->dst.addr.mem.ea = |
| 3042 | register_address(c, c->regs[VCPU_REGS_RDI]); |
| 3043 | c->dst.addr.mem.seg = VCPU_SREG_ES; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3044 | c->dst.val = 0; |
| 3045 | break; |
Wei Yongjun | 36089fe | 2010-08-04 15:38:18 +0800 | [diff] [blame] | 3046 | case ImplicitOps: |
| 3047 | /* Special instructions do their own operand decoding. */ |
| 3048 | default: |
| 3049 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3050 | return 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | done: |
| 3054 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 3055 | } |
| 3056 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 3057 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 3058 | { |
| 3059 | struct decode_cache *c = &ctxt->decode; |
| 3060 | |
| 3061 | /* The second termination condition only applies for REPE |
| 3062 | * and REPNE. Test if the repeat string operation prefix is |
| 3063 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 3064 | * corresponding termination condition according to: |
| 3065 | * - if REPE/REPZ and ZF = 0 then done |
| 3066 | * - if REPNE/REPNZ and ZF = 1 then done |
| 3067 | */ |
| 3068 | if (((c->b == 0xa6) || (c->b == 0xa7) || |
| 3069 | (c->b == 0xae) || (c->b == 0xaf)) |
| 3070 | && (((c->rep_prefix == REPE_PREFIX) && |
| 3071 | ((ctxt->eflags & EFLG_ZF) == 0)) |
| 3072 | || ((c->rep_prefix == REPNE_PREFIX) && |
| 3073 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 3074 | return true; |
| 3075 | |
| 3076 | return false; |
| 3077 | } |
| 3078 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 3079 | int |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 3080 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3081 | { |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 3082 | struct x86_emulate_ops *ops = ctxt->ops; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3083 | u64 msr_data; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3084 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3085 | int rc = X86EMUL_CONTINUE; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3086 | int saved_dst_type = c->dst.type; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 3087 | int irq; /* Used for int 3, int, and into */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3088 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3089 | ctxt->decode.mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3090 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 3091 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3092 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 3093 | goto done; |
| 3094 | } |
| 3095 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 3096 | /* LOCK prefix is allowed only with some instructions */ |
Gleb Natapov | a41ffb75 | 2010-03-18 15:20:14 +0200 | [diff] [blame] | 3097 | if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3098 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 3099 | goto done; |
| 3100 | } |
| 3101 | |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 3102 | if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3103 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 3104 | goto done; |
| 3105 | } |
| 3106 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 3107 | if ((c->d & Sse) |
| 3108 | && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM) |
| 3109 | || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) { |
| 3110 | rc = emulate_ud(ctxt); |
| 3111 | goto done; |
| 3112 | } |
| 3113 | |
| 3114 | if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) { |
| 3115 | rc = emulate_nm(ctxt); |
| 3116 | goto done; |
| 3117 | } |
| 3118 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 3119 | /* Privileged instruction can be executed only in CPL=0 */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 3120 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3121 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 3122 | goto done; |
| 3123 | } |
| 3124 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 3125 | if (c->rep_prefix && (c->d & String)) { |
| 3126 | /* All REP prefixes have the same first termination condition */ |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 3127 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 3128 | ctxt->eip = c->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 3129 | goto done; |
| 3130 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 3131 | } |
| 3132 | |
Wei Yongjun | c483c02 | 2010-08-06 15:36:36 +0800 | [diff] [blame] | 3133 | if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3134 | rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem), |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3135 | c->src.valptr, c->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 3136 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3137 | goto done; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 3138 | c->src.orig_val64 = c->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 3141 | if (c->src2.type == OP_MEM) { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3142 | rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3143 | &c->src2.val, c->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 3144 | if (rc != X86EMUL_CONTINUE) |
| 3145 | goto done; |
| 3146 | } |
| 3147 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3148 | if ((c->d & DstMask) == ImplicitOps) |
| 3149 | goto special_insn; |
| 3150 | |
| 3151 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3152 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
| 3153 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3154 | rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3155 | &c->dst.val, c->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3156 | if (rc != X86EMUL_CONTINUE) |
| 3157 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 3158 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3159 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 3160 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3161 | special_insn: |
| 3162 | |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 3163 | if (c->execute) { |
| 3164 | rc = c->execute(ctxt); |
| 3165 | if (rc != X86EMUL_CONTINUE) |
| 3166 | goto done; |
| 3167 | goto writeback; |
| 3168 | } |
| 3169 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3170 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3171 | goto twobyte_insn; |
| 3172 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3173 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | case 0x00 ... 0x05: |
| 3175 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3176 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3177 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3178 | case 0x06: /* push es */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3179 | emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3180 | break; |
| 3181 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3182 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3183 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3184 | case 0x08 ... 0x0d: |
| 3185 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3186 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3187 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3188 | case 0x0e: /* push cs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3189 | emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3190 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3191 | case 0x10 ... 0x15: |
| 3192 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3193 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3194 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3195 | case 0x16: /* push ss */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3196 | emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3197 | break; |
| 3198 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3199 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3200 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3201 | case 0x18 ... 0x1d: |
| 3202 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3203 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3204 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3205 | case 0x1e: /* push ds */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3206 | emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3207 | break; |
| 3208 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3209 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3210 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 3211 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3212 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3213 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3214 | break; |
| 3215 | case 0x28 ... 0x2d: |
| 3216 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3217 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3218 | break; |
| 3219 | case 0x30 ... 0x35: |
| 3220 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3221 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3222 | break; |
| 3223 | case 0x38 ... 0x3d: |
| 3224 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3225 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3226 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3227 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 3228 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 3229 | break; |
| 3230 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 3231 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 3232 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3233 | case 0x58 ... 0x5f: /* pop reg */ |
| 3234 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 3235 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3236 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3237 | case 0x60: /* pusha */ |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 3238 | rc = emulate_pusha(ctxt, ops); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3239 | break; |
| 3240 | case 0x61: /* popa */ |
| 3241 | rc = emulate_popa(ctxt, ops); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3242 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3243 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3244 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3245 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3246 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3247 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3248 | case 0x6c: /* insb */ |
| 3249 | case 0x6d: /* insw/insd */ |
Wei Yongjun | a13a63f | 2010-08-06 11:46:12 +0800 | [diff] [blame] | 3250 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3251 | goto do_io_in; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3252 | case 0x6e: /* outsb */ |
| 3253 | case 0x6f: /* outsw/outsd */ |
Wei Yongjun | a13a63f | 2010-08-06 11:46:12 +0800 | [diff] [blame] | 3254 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
| 3255 | goto do_io_out; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3256 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3257 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3258 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3259 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3260 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3261 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3262 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3263 | case 0: |
| 3264 | goto add; |
| 3265 | case 1: |
| 3266 | goto or; |
| 3267 | case 2: |
| 3268 | goto adc; |
| 3269 | case 3: |
| 3270 | goto sbb; |
| 3271 | case 4: |
| 3272 | goto and; |
| 3273 | case 5: |
| 3274 | goto sub; |
| 3275 | case 6: |
| 3276 | goto xor; |
| 3277 | case 7: |
| 3278 | goto cmp; |
| 3279 | } |
| 3280 | break; |
| 3281 | case 0x84 ... 0x85: |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 3282 | test: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3283 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3284 | break; |
| 3285 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 3286 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3287 | /* Write back the register source. */ |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 3288 | c->src.val = c->dst.val; |
| 3289 | write_register_operand(&c->src); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3290 | /* |
| 3291 | * Write back the memory destination with implicit LOCK |
| 3292 | * prefix. |
| 3293 | */ |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 3294 | c->dst.val = c->src.orig_val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3295 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3296 | break; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3297 | case 0x8c: /* mov r/m, sreg */ |
| 3298 | if (c->modrm_reg > VCPU_SREG_GS) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3299 | rc = emulate_ud(ctxt); |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 3300 | goto done; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 3301 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3302 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 3303 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 3304 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3305 | c->dst.val = c->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 3306 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3307 | case 0x8e: { /* mov seg, r/m16 */ |
| 3308 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3309 | |
| 3310 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 3311 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3312 | if (c->modrm_reg == VCPU_SREG_CS || |
| 3313 | c->modrm_reg > VCPU_SREG_GS) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3314 | rc = emulate_ud(ctxt); |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 3315 | goto done; |
| 3316 | } |
| 3317 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3318 | if (c->modrm_reg == VCPU_SREG_SS) |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3319 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3320 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 3321 | rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3322 | |
| 3323 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3324 | break; |
| 3325 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3326 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3327 | rc = emulate_grp1a(ctxt, ops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3328 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 3329 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
| 3330 | if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX]) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 3331 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 3332 | goto xchg; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 3333 | case 0x98: /* cbw/cwde/cdqe */ |
| 3334 | switch (c->op_bytes) { |
| 3335 | case 2: c->dst.val = (s8)c->dst.val; break; |
| 3336 | case 4: c->dst.val = (s16)c->dst.val; break; |
| 3337 | case 8: c->dst.val = (s32)c->dst.val; break; |
| 3338 | } |
| 3339 | break; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 3340 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3341 | c->src.val = (unsigned long) ctxt->eflags; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3342 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3343 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 3344 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 3345 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3346 | c->dst.addr.reg = &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 3347 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 3348 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 3349 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3350 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 3351 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3352 | goto cmp; |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 3353 | case 0xa8 ... 0xa9: /* test ax, imm */ |
| 3354 | goto test; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3355 | case 0xae ... 0xaf: /* scas */ |
Avi Kivity | f6b33fc | 2010-08-17 11:20:37 +0300 | [diff] [blame] | 3356 | goto cmp; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3357 | case 0xc0 ... 0xc1: |
| 3358 | emulate_grp2(ctxt); |
| 3359 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3360 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 3361 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3362 | c->dst.addr.reg = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 3363 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3364 | goto pop_instruction; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3365 | case 0xc4: /* les */ |
| 3366 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3367 | break; |
| 3368 | case 0xc5: /* lds */ |
| 3369 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3370 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 3371 | case 0xcb: /* ret far */ |
| 3372 | rc = emulate_ret_far(ctxt, ops); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 3373 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 3374 | case 0xcc: /* int3 */ |
| 3375 | irq = 3; |
| 3376 | goto do_interrupt; |
| 3377 | case 0xcd: /* int n */ |
| 3378 | irq = c->src.val; |
| 3379 | do_interrupt: |
| 3380 | rc = emulate_int(ctxt, ops, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 3381 | break; |
| 3382 | case 0xce: /* into */ |
| 3383 | if (ctxt->eflags & EFLG_OF) { |
| 3384 | irq = 4; |
| 3385 | goto do_interrupt; |
| 3386 | } |
| 3387 | break; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 3388 | case 0xcf: /* iret */ |
| 3389 | rc = emulate_iret(ctxt, ops); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 3390 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3391 | case 0xd0 ... 0xd1: /* Grp2 */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3392 | emulate_grp2(ctxt); |
| 3393 | break; |
| 3394 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 3395 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 3396 | emulate_grp2(ctxt); |
| 3397 | break; |
Wei Yongjun | f2f3184 | 2010-08-18 16:38:21 +0800 | [diff] [blame] | 3398 | case 0xe0 ... 0xe2: /* loop/loopz/loopnz */ |
| 3399 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
| 3400 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 && |
| 3401 | (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags))) |
| 3402 | jmp_rel(c, c->src.val); |
| 3403 | break; |
Wei Yongjun | e4abac6 | 2010-08-19 14:25:48 +0800 | [diff] [blame] | 3404 | case 0xe3: /* jcxz/jecxz/jrcxz */ |
| 3405 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) |
| 3406 | jmp_rel(c, c->src.val); |
| 3407 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3408 | case 0xe4: /* inb */ |
| 3409 | case 0xe5: /* in */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3410 | goto do_io_in; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3411 | case 0xe6: /* outb */ |
| 3412 | case 0xe7: /* out */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3413 | goto do_io_out; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3414 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 3415 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3416 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3417 | jmp_rel(c, rel); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3418 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3419 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3420 | } |
| 3421 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3422 | goto jmp; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3423 | case 0xea: { /* jmp far */ |
| 3424 | unsigned short sel; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3425 | jump_far: |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3426 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 3427 | |
| 3428 | if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS)) |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3429 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3430 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3431 | c->eip = 0; |
| 3432 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3433 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3434 | } |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3435 | case 0xeb: |
| 3436 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3437 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3438 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3439 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3440 | case 0xec: /* in al,dx */ |
| 3441 | case 0xed: /* in (e/r)ax,dx */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3442 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3443 | do_io_in: |
| 3444 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3445 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3446 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3447 | goto done; |
| 3448 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3449 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val, |
| 3450 | &c->dst.val)) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3451 | goto done; /* IO is needed */ |
| 3452 | break; |
Wei Yongjun | ce7a0ad | 2010-07-06 16:50:21 +0800 | [diff] [blame] | 3453 | case 0xee: /* out dx,al */ |
| 3454 | case 0xef: /* out dx,(e/r)ax */ |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3455 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3456 | do_io_out: |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3457 | c->src.bytes = min(c->src.bytes, 4u); |
| 3458 | if (!emulator_io_permited(ctxt, ops, c->dst.val, |
| 3459 | c->src.bytes)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3460 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3461 | goto done; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3462 | } |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3463 | ops->pio_out_emulated(c->src.bytes, c->dst.val, |
| 3464 | &c->src.val, 1, ctxt->vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3465 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 3466 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3467 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3468 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 3469 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3470 | case 0xf5: /* cmc */ |
| 3471 | /* complement carry flag from eflags reg */ |
| 3472 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3473 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3474 | case 0xf6 ... 0xf7: /* Grp3 */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 3475 | rc = emulate_grp3(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3476 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3477 | case 0xf8: /* clc */ |
| 3478 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3479 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 3480 | case 0xf9: /* stc */ |
| 3481 | ctxt->eflags |= EFLG_CF; |
| 3482 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3483 | case 0xfa: /* cli */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3484 | if (emulator_bad_iopl(ctxt, ops)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3485 | rc = emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3486 | goto done; |
Wei Yongjun | 36089fe | 2010-08-04 15:38:18 +0800 | [diff] [blame] | 3487 | } else |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3488 | ctxt->eflags &= ~X86_EFLAGS_IF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3489 | break; |
| 3490 | case 0xfb: /* sti */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3491 | if (emulator_bad_iopl(ctxt, ops)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3492 | rc = emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3493 | goto done; |
| 3494 | } else { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3495 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3496 | ctxt->eflags |= X86_EFLAGS_IF; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3497 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3498 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3499 | case 0xfc: /* cld */ |
| 3500 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3501 | break; |
| 3502 | case 0xfd: /* std */ |
| 3503 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3504 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3505 | case 0xfe: /* Grp4 */ |
| 3506 | grp45: |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3507 | rc = emulate_grp45(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3508 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3509 | case 0xff: /* Grp5 */ |
| 3510 | if (c->modrm_reg == 5) |
| 3511 | goto jump_far; |
| 3512 | goto grp45; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3513 | default: |
| 3514 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3515 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3516 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 3517 | if (rc != X86EMUL_CONTINUE) |
| 3518 | goto done; |
| 3519 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3520 | writeback: |
| 3521 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3522 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3523 | goto done; |
| 3524 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3525 | /* |
| 3526 | * restore dst type in case the decoding will be reused |
| 3527 | * (happens for string instruction ) |
| 3528 | */ |
| 3529 | c->dst.type = saved_dst_type; |
| 3530 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3531 | if ((c->d & SrcMask) == SrcSI) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3532 | string_addr_inc(ctxt, seg_override(ctxt, ops, c), |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3533 | VCPU_REGS_RSI, &c->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3534 | |
| 3535 | if ((c->d & DstMask) == DstDI) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3536 | string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3537 | &c->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3538 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3539 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 6e2fb2c | 2010-08-25 12:47:41 +0300 | [diff] [blame] | 3540 | struct read_cache *r = &ctxt->decode.io_read; |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3541 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 3542 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3543 | if (!string_insn_completed(ctxt)) { |
| 3544 | /* |
| 3545 | * Re-enter guest when pio read ahead buffer is empty |
| 3546 | * or, if it is not used, after each 1024 iteration. |
| 3547 | */ |
| 3548 | if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) && |
| 3549 | (r->end == 0 || r->end != r->pos)) { |
| 3550 | /* |
| 3551 | * Reset read cache. Usually happens before |
| 3552 | * decode, but since instruction is restarted |
| 3553 | * we have to do it here. |
| 3554 | */ |
| 3555 | ctxt->decode.mem_read.end = 0; |
| 3556 | return EMULATION_RESTART; |
| 3557 | } |
| 3558 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 3559 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3560 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3561 | |
| 3562 | ctxt->eip = c->eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3563 | |
| 3564 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3565 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 3566 | ctxt->have_exception = true; |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3567 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3568 | |
| 3569 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3570 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3571 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3572 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3573 | u16 size; |
| 3574 | unsigned long address; |
| 3575 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3576 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3577 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3578 | goto cannot_emulate; |
| 3579 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3580 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3581 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3582 | goto done; |
| 3583 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 3584 | /* Let the processor re-execute the fixed hypercall */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 3585 | c->eip = ctxt->eip; |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3586 | /* Disable writeback. */ |
| 3587 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3588 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3589 | case 2: /* lgdt */ |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3590 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3591 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3592 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3593 | goto done; |
| 3594 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3595 | /* Disable writeback. */ |
| 3596 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3597 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3598 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3599 | if (c->modrm_mod == 3) { |
| 3600 | switch (c->modrm_rm) { |
| 3601 | case 1: |
| 3602 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3603 | break; |
| 3604 | default: |
| 3605 | goto cannot_emulate; |
| 3606 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3607 | } else { |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3608 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3609 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3610 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3611 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3612 | goto done; |
| 3613 | realmode_lidt(ctxt->vcpu, size, address); |
| 3614 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3615 | /* Disable writeback. */ |
| 3616 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3617 | break; |
| 3618 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3619 | c->dst.bytes = 2; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3620 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3621 | break; |
| 3622 | case 6: /* lmsw */ |
Avi Kivity | 9928ff6 | 2010-08-01 18:35:24 +0300 | [diff] [blame] | 3623 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) | |
Gleb Natapov | 93a152b | 2010-03-18 15:20:04 +0200 | [diff] [blame] | 3624 | (c->src.val & 0x0f), ctxt->vcpu); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 3625 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3626 | break; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3627 | case 5: /* not defined */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3628 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3629 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3630 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3631 | case 7: /* invlpg*/ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3632 | emulate_invlpg(ctxt->vcpu, |
| 3633 | linear(ctxt, c->src.addr.mem)); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3634 | /* Disable writeback. */ |
| 3635 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3636 | break; |
| 3637 | default: |
| 3638 | goto cannot_emulate; |
| 3639 | } |
| 3640 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3641 | case 0x05: /* syscall */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3642 | rc = emulate_syscall(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3643 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3644 | case 0x06: |
| 3645 | emulate_clts(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3646 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3647 | case 0x09: /* wbinvd */ |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3648 | kvm_emulate_wbinvd(ctxt->vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3649 | break; |
| 3650 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3651 | case 0x0d: /* GrpP (prefetch) */ |
| 3652 | case 0x18: /* Grp16 (prefetch/nop) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3653 | break; |
| 3654 | case 0x20: /* mov cr, reg */ |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3655 | switch (c->modrm_reg) { |
| 3656 | case 1: |
| 3657 | case 5 ... 7: |
| 3658 | case 9 ... 15: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3659 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3660 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3661 | goto done; |
| 3662 | } |
Avi Kivity | 1a0c7d4 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3663 | c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3664 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3665 | case 0x21: /* mov from dr to reg */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3666 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3667 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3668 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3669 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3670 | goto done; |
| 3671 | } |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3672 | ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3673 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3674 | case 0x22: /* mov reg, cr */ |
Avi Kivity | 1a0c7d4 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3675 | if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3676 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3677 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3678 | goto done; |
| 3679 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3680 | c->dst.type = OP_NONE; |
| 3681 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3682 | case 0x23: /* mov from reg to dr */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3683 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3684 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3685 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3686 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3687 | goto done; |
| 3688 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3689 | |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3690 | if (ops->set_dr(c->modrm_reg, c->src.val & |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3691 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? |
| 3692 | ~0ULL : ~0U), ctxt->vcpu) < 0) { |
| 3693 | /* #UD condition is already handled by the code above */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3694 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3695 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3696 | goto done; |
| 3697 | } |
| 3698 | |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3699 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3700 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3701 | case 0x30: |
| 3702 | /* wrmsr */ |
| 3703 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 3704 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3705 | if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3706 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3707 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3708 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3709 | } |
| 3710 | rc = X86EMUL_CONTINUE; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3711 | break; |
| 3712 | case 0x32: |
| 3713 | /* rdmsr */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3714 | if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3715 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3716 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3717 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3718 | } else { |
| 3719 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 3720 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 3721 | } |
| 3722 | rc = X86EMUL_CONTINUE; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3723 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3724 | case 0x34: /* sysenter */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3725 | rc = emulate_sysenter(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3726 | break; |
| 3727 | case 0x35: /* sysexit */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3728 | rc = emulate_sysexit(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3729 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3730 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3731 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3732 | if (!test_cc(c->b, ctxt->eflags)) |
| 3733 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3734 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3735 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3736 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3737 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3738 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3739 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
| 3740 | c->dst.val = test_cc(c->b, ctxt->eflags); |
| 3741 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3742 | case 0xa0: /* push fs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3743 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3744 | break; |
| 3745 | case 0xa1: /* pop fs */ |
| 3746 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3747 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3748 | case 0xa3: |
| 3749 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 3750 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3751 | /* only subword offset */ |
| 3752 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3753 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3754 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3755 | case 0xa4: /* shld imm8, r, r/m */ |
| 3756 | case 0xa5: /* shld cl, r, r/m */ |
| 3757 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 3758 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3759 | case 0xa8: /* push gs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3760 | emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3761 | break; |
| 3762 | case 0xa9: /* pop gs */ |
| 3763 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3764 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3765 | case 0xab: |
| 3766 | bts: /* bts */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3767 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3768 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3769 | case 0xac: /* shrd imm8, r, r/m */ |
| 3770 | case 0xad: /* shrd cl, r, r/m */ |
| 3771 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 3772 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 3773 | case 0xae: /* clflush */ |
| 3774 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3775 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 3776 | /* |
| 3777 | * Save real source value, then compare EAX against |
| 3778 | * destination. |
| 3779 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3780 | c->src.orig_val = c->src.val; |
| 3781 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3782 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 3783 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3784 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3785 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3786 | } else { |
| 3787 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3788 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3789 | c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3790 | } |
| 3791 | break; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3792 | case 0xb2: /* lss */ |
| 3793 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3794 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3795 | case 0xb3: |
| 3796 | btr: /* btr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3797 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3798 | break; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3799 | case 0xb4: /* lfs */ |
| 3800 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3801 | break; |
| 3802 | case 0xb5: /* lgs */ |
| 3803 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3804 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3805 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3806 | c->dst.bytes = c->op_bytes; |
| 3807 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 3808 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3809 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3810 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3811 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3812 | case 0: |
| 3813 | goto bt; |
| 3814 | case 1: |
| 3815 | goto bts; |
| 3816 | case 2: |
| 3817 | goto btr; |
| 3818 | case 3: |
| 3819 | goto btc; |
| 3820 | } |
| 3821 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3822 | case 0xbb: |
| 3823 | btc: /* btc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3824 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3825 | break; |
Wei Yongjun | d9574a2 | 2010-08-10 13:48:22 +0800 | [diff] [blame] | 3826 | case 0xbc: { /* bsf */ |
| 3827 | u8 zf; |
| 3828 | __asm__ ("bsf %2, %0; setz %1" |
| 3829 | : "=r"(c->dst.val), "=q"(zf) |
| 3830 | : "r"(c->src.val)); |
| 3831 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
| 3832 | if (zf) { |
| 3833 | ctxt->eflags |= X86_EFLAGS_ZF; |
| 3834 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3835 | } |
| 3836 | break; |
| 3837 | } |
| 3838 | case 0xbd: { /* bsr */ |
| 3839 | u8 zf; |
| 3840 | __asm__ ("bsr %2, %0; setz %1" |
| 3841 | : "=r"(c->dst.val), "=q"(zf) |
| 3842 | : "r"(c->src.val)); |
| 3843 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
| 3844 | if (zf) { |
| 3845 | ctxt->eflags |= X86_EFLAGS_ZF; |
| 3846 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3847 | } |
| 3848 | break; |
| 3849 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3850 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3851 | c->dst.bytes = c->op_bytes; |
| 3852 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 3853 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3854 | break; |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3855 | case 0xc0 ... 0xc1: /* xadd */ |
| 3856 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
| 3857 | /* Write back the register source. */ |
| 3858 | c->src.val = c->dst.orig_val; |
| 3859 | write_register_operand(&c->src); |
| 3860 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3861 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3862 | c->dst.bytes = c->op_bytes; |
| 3863 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 3864 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3865 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3866 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3867 | rc = emulate_grp9(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3868 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3869 | default: |
| 3870 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3871 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 3872 | |
| 3873 | if (rc != X86EMUL_CONTINUE) |
| 3874 | goto done; |
| 3875 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3876 | goto writeback; |
| 3877 | |
| 3878 | cannot_emulate: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3879 | return -1; |
| 3880 | } |