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