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