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