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