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