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