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 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 469 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 470 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 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; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 475 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 476 | } |
| 477 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 478 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 479 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 480 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 481 | } |
| 482 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 483 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 484 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 485 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 486 | } |
| 487 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 488 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 489 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 490 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 491 | } |
| 492 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 493 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 494 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 495 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 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 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 901 | if (dt.size < index * 8 + 7) |
| 902 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 903 | addr = dt.address + index * 8; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 904 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, |
| 905 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 906 | |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | /* allowed just for 8 bytes segments */ |
| 911 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 912 | struct x86_emulate_ops *ops, |
| 913 | u16 selector, struct desc_struct *desc) |
| 914 | { |
| 915 | struct desc_ptr dt; |
| 916 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 917 | ulong addr; |
| 918 | int ret; |
| 919 | |
| 920 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 921 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 922 | if (dt.size < index * 8 + 7) |
| 923 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 924 | |
| 925 | addr = dt.address + index * 8; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 926 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, |
| 927 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 933 | struct x86_emulate_ops *ops, |
| 934 | u16 selector, int seg) |
| 935 | { |
| 936 | struct desc_struct seg_desc; |
| 937 | u8 dpl, rpl, cpl; |
| 938 | unsigned err_vec = GP_VECTOR; |
| 939 | u32 err_code = 0; |
| 940 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 941 | int ret; |
| 942 | |
| 943 | memset(&seg_desc, 0, sizeof seg_desc); |
| 944 | |
| 945 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 946 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 947 | /* set real mode segment descriptor */ |
| 948 | set_desc_base(&seg_desc, selector << 4); |
| 949 | set_desc_limit(&seg_desc, 0xffff); |
| 950 | seg_desc.type = 3; |
| 951 | seg_desc.p = 1; |
| 952 | seg_desc.s = 1; |
| 953 | goto load; |
| 954 | } |
| 955 | |
| 956 | /* NULL selector is not valid for TR, CS and SS */ |
| 957 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 958 | && null_selector) |
| 959 | goto exception; |
| 960 | |
| 961 | /* TR should be in GDT only */ |
| 962 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 963 | goto exception; |
| 964 | |
| 965 | if (null_selector) /* for NULL selector skip all following checks */ |
| 966 | goto load; |
| 967 | |
| 968 | ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 969 | if (ret != X86EMUL_CONTINUE) |
| 970 | return ret; |
| 971 | |
| 972 | err_code = selector & 0xfffc; |
| 973 | err_vec = GP_VECTOR; |
| 974 | |
| 975 | /* can't load system descriptor into segment selecor */ |
| 976 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 977 | goto exception; |
| 978 | |
| 979 | if (!seg_desc.p) { |
| 980 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 981 | goto exception; |
| 982 | } |
| 983 | |
| 984 | rpl = selector & 3; |
| 985 | dpl = seg_desc.dpl; |
| 986 | cpl = ops->cpl(ctxt->vcpu); |
| 987 | |
| 988 | switch (seg) { |
| 989 | case VCPU_SREG_SS: |
| 990 | /* |
| 991 | * segment is not a writable data segment or segment |
| 992 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 993 | */ |
| 994 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 995 | goto exception; |
| 996 | break; |
| 997 | case VCPU_SREG_CS: |
| 998 | if (!(seg_desc.type & 8)) |
| 999 | goto exception; |
| 1000 | |
| 1001 | if (seg_desc.type & 4) { |
| 1002 | /* conforming */ |
| 1003 | if (dpl > cpl) |
| 1004 | goto exception; |
| 1005 | } else { |
| 1006 | /* nonconforming */ |
| 1007 | if (rpl > cpl || dpl != cpl) |
| 1008 | goto exception; |
| 1009 | } |
| 1010 | /* CS(RPL) <- CPL */ |
| 1011 | selector = (selector & 0xfffc) | cpl; |
| 1012 | break; |
| 1013 | case VCPU_SREG_TR: |
| 1014 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1015 | goto exception; |
| 1016 | break; |
| 1017 | case VCPU_SREG_LDTR: |
| 1018 | if (seg_desc.s || seg_desc.type != 2) |
| 1019 | goto exception; |
| 1020 | break; |
| 1021 | default: /* DS, ES, FS, or GS */ |
| 1022 | /* |
| 1023 | * segment is not a data or readable code segment or |
| 1024 | * ((segment is a data or nonconforming code segment) |
| 1025 | * and (both RPL and CPL > DPL)) |
| 1026 | */ |
| 1027 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1028 | (((seg_desc.type & 0xc) != 0xc) && |
| 1029 | (rpl > dpl && cpl > dpl))) |
| 1030 | goto exception; |
| 1031 | break; |
| 1032 | } |
| 1033 | |
| 1034 | if (seg_desc.s) { |
| 1035 | /* mark segment as accessed */ |
| 1036 | seg_desc.type |= 1; |
| 1037 | ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1038 | if (ret != X86EMUL_CONTINUE) |
| 1039 | return ret; |
| 1040 | } |
| 1041 | load: |
| 1042 | ops->set_segment_selector(selector, seg, ctxt->vcpu); |
| 1043 | ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu); |
| 1044 | return X86EMUL_CONTINUE; |
| 1045 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1046 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1047 | return X86EMUL_PROPAGATE_FAULT; |
| 1048 | } |
| 1049 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1050 | static void write_register_operand(struct operand *op) |
| 1051 | { |
| 1052 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1053 | switch (op->bytes) { |
| 1054 | case 1: |
| 1055 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1056 | break; |
| 1057 | case 2: |
| 1058 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1059 | break; |
| 1060 | case 4: |
| 1061 | *op->addr.reg = (u32)op->val; |
| 1062 | break; /* 64b: zero-extend */ |
| 1063 | case 8: |
| 1064 | *op->addr.reg = op->val; |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1069 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1070 | struct x86_emulate_ops *ops) |
| 1071 | { |
| 1072 | int rc; |
| 1073 | struct decode_cache *c = &ctxt->decode; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1074 | |
| 1075 | switch (c->dst.type) { |
| 1076 | case OP_REG: |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1077 | write_register_operand(&c->dst); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1078 | break; |
| 1079 | case OP_MEM: |
| 1080 | if (c->lock_prefix) |
| 1081 | rc = ops->cmpxchg_emulated( |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1082 | linear(ctxt, c->dst.addr.mem), |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1083 | &c->dst.orig_val, |
| 1084 | &c->dst.val, |
| 1085 | c->dst.bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1086 | &ctxt->exception, |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1087 | ctxt->vcpu); |
| 1088 | else |
| 1089 | rc = ops->write_emulated( |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1090 | linear(ctxt, c->dst.addr.mem), |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1091 | &c->dst.val, |
| 1092 | c->dst.bytes, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1093 | &ctxt->exception, |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1094 | ctxt->vcpu); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1095 | if (rc != X86EMUL_CONTINUE) |
| 1096 | return rc; |
| 1097 | break; |
| 1098 | case OP_NONE: |
| 1099 | /* no writeback */ |
| 1100 | break; |
| 1101 | default: |
| 1102 | break; |
| 1103 | } |
| 1104 | return X86EMUL_CONTINUE; |
| 1105 | } |
| 1106 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1107 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt, |
| 1108 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1109 | { |
| 1110 | struct decode_cache *c = &ctxt->decode; |
| 1111 | |
| 1112 | c->dst.type = OP_MEM; |
| 1113 | c->dst.bytes = c->op_bytes; |
| 1114 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1115 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1116 | c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]); |
| 1117 | c->dst.addr.mem.seg = VCPU_SREG_SS; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1120 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1121 | struct x86_emulate_ops *ops, |
| 1122 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1123 | { |
| 1124 | struct decode_cache *c = &ctxt->decode; |
| 1125 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1126 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1127 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1128 | addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]); |
| 1129 | addr.seg = VCPU_SREG_SS; |
| 1130 | rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1131 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1132 | return rc; |
| 1133 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1134 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1135 | return rc; |
| 1136 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1137 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1138 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1139 | struct x86_emulate_ops *ops, |
| 1140 | void *dest, int len) |
| 1141 | { |
| 1142 | int rc; |
| 1143 | unsigned long val, change_mask; |
| 1144 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1145 | int cpl = ops->cpl(ctxt->vcpu); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1146 | |
| 1147 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1148 | if (rc != X86EMUL_CONTINUE) |
| 1149 | return rc; |
| 1150 | |
| 1151 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1152 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1153 | |
| 1154 | switch(ctxt->mode) { |
| 1155 | case X86EMUL_MODE_PROT64: |
| 1156 | case X86EMUL_MODE_PROT32: |
| 1157 | case X86EMUL_MODE_PROT16: |
| 1158 | if (cpl == 0) |
| 1159 | change_mask |= EFLG_IOPL; |
| 1160 | if (cpl <= iopl) |
| 1161 | change_mask |= EFLG_IF; |
| 1162 | break; |
| 1163 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1164 | if (iopl < 3) |
| 1165 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1166 | change_mask |= EFLG_IF; |
| 1167 | break; |
| 1168 | default: /* real mode */ |
| 1169 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | *(unsigned long *)dest = |
| 1174 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1175 | |
| 1176 | return rc; |
| 1177 | } |
| 1178 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1179 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, |
| 1180 | struct x86_emulate_ops *ops, int seg) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1181 | { |
| 1182 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1183 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1184 | c->src.val = ops->get_segment_selector(seg, ctxt->vcpu); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1185 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1186 | emulate_push(ctxt, ops); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1190 | struct x86_emulate_ops *ops, int seg) |
| 1191 | { |
| 1192 | struct decode_cache *c = &ctxt->decode; |
| 1193 | unsigned long selector; |
| 1194 | int rc; |
| 1195 | |
| 1196 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1197 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1198 | return rc; |
| 1199 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1200 | rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1201 | return rc; |
| 1202 | } |
| 1203 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1204 | static int emulate_pusha(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1205 | struct x86_emulate_ops *ops) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1206 | { |
| 1207 | struct decode_cache *c = &ctxt->decode; |
| 1208 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1209 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1210 | int reg = VCPU_REGS_RAX; |
| 1211 | |
| 1212 | while (reg <= VCPU_REGS_RDI) { |
| 1213 | (reg == VCPU_REGS_RSP) ? |
| 1214 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1215 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1216 | emulate_push(ctxt, ops); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1217 | |
| 1218 | rc = writeback(ctxt, ops); |
| 1219 | if (rc != X86EMUL_CONTINUE) |
| 1220 | return rc; |
| 1221 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1222 | ++reg; |
| 1223 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1224 | |
| 1225 | /* Disable writeback. */ |
| 1226 | c->dst.type = OP_NONE; |
| 1227 | |
| 1228 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1232 | struct x86_emulate_ops *ops) |
| 1233 | { |
| 1234 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1235 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1236 | int reg = VCPU_REGS_RDI; |
| 1237 | |
| 1238 | while (reg >= VCPU_REGS_RAX) { |
| 1239 | if (reg == VCPU_REGS_RSP) { |
| 1240 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1241 | c->op_bytes); |
| 1242 | --reg; |
| 1243 | } |
| 1244 | |
| 1245 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1246 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1247 | break; |
| 1248 | --reg; |
| 1249 | } |
| 1250 | return rc; |
| 1251 | } |
| 1252 | |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1253 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, |
| 1254 | struct x86_emulate_ops *ops, int irq) |
| 1255 | { |
| 1256 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1257 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1258 | struct desc_ptr dt; |
| 1259 | gva_t cs_addr; |
| 1260 | gva_t eip_addr; |
| 1261 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1262 | |
| 1263 | /* TODO: Add limit checks */ |
| 1264 | c->src.val = ctxt->eflags; |
| 1265 | emulate_push(ctxt, ops); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1266 | rc = writeback(ctxt, ops); |
| 1267 | if (rc != X86EMUL_CONTINUE) |
| 1268 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1269 | |
| 1270 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1271 | |
| 1272 | c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 1273 | emulate_push(ctxt, ops); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1274 | rc = writeback(ctxt, ops); |
| 1275 | if (rc != X86EMUL_CONTINUE) |
| 1276 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1277 | |
| 1278 | c->src.val = c->eip; |
| 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; |
| 1283 | |
| 1284 | c->dst.type = OP_NONE; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1285 | |
| 1286 | ops->get_idt(&dt, ctxt->vcpu); |
| 1287 | |
| 1288 | eip_addr = dt.address + (irq << 2); |
| 1289 | cs_addr = dt.address + (irq << 2) + 2; |
| 1290 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1291 | rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1292 | if (rc != X86EMUL_CONTINUE) |
| 1293 | return rc; |
| 1294 | |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1295 | rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1296 | if (rc != X86EMUL_CONTINUE) |
| 1297 | return rc; |
| 1298 | |
| 1299 | rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS); |
| 1300 | if (rc != X86EMUL_CONTINUE) |
| 1301 | return rc; |
| 1302 | |
| 1303 | c->eip = eip; |
| 1304 | |
| 1305 | return rc; |
| 1306 | } |
| 1307 | |
| 1308 | static int emulate_int(struct x86_emulate_ctxt *ctxt, |
| 1309 | struct x86_emulate_ops *ops, int irq) |
| 1310 | { |
| 1311 | switch(ctxt->mode) { |
| 1312 | case X86EMUL_MODE_REAL: |
| 1313 | return emulate_int_real(ctxt, ops, irq); |
| 1314 | case X86EMUL_MODE_VM86: |
| 1315 | case X86EMUL_MODE_PROT16: |
| 1316 | case X86EMUL_MODE_PROT32: |
| 1317 | case X86EMUL_MODE_PROT64: |
| 1318 | default: |
| 1319 | /* Protected mode interrupts unimplemented yet */ |
| 1320 | return X86EMUL_UNHANDLEABLE; |
| 1321 | } |
| 1322 | } |
| 1323 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1324 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, |
| 1325 | struct x86_emulate_ops *ops) |
| 1326 | { |
| 1327 | struct decode_cache *c = &ctxt->decode; |
| 1328 | int rc = X86EMUL_CONTINUE; |
| 1329 | unsigned long temp_eip = 0; |
| 1330 | unsigned long temp_eflags = 0; |
| 1331 | unsigned long cs = 0; |
| 1332 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1333 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1334 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1335 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1336 | |
| 1337 | /* TODO: Add stack limit check */ |
| 1338 | |
| 1339 | rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); |
| 1340 | |
| 1341 | if (rc != X86EMUL_CONTINUE) |
| 1342 | return rc; |
| 1343 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1344 | if (temp_eip & ~0xffff) |
| 1345 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1346 | |
| 1347 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
| 1348 | |
| 1349 | if (rc != X86EMUL_CONTINUE) |
| 1350 | return rc; |
| 1351 | |
| 1352 | rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); |
| 1353 | |
| 1354 | if (rc != X86EMUL_CONTINUE) |
| 1355 | return rc; |
| 1356 | |
| 1357 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
| 1358 | |
| 1359 | if (rc != X86EMUL_CONTINUE) |
| 1360 | return rc; |
| 1361 | |
| 1362 | c->eip = temp_eip; |
| 1363 | |
| 1364 | |
| 1365 | if (c->op_bytes == 4) |
| 1366 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
| 1367 | else if (c->op_bytes == 2) { |
| 1368 | ctxt->eflags &= ~0xffff; |
| 1369 | ctxt->eflags |= temp_eflags; |
| 1370 | } |
| 1371 | |
| 1372 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1373 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1374 | |
| 1375 | return rc; |
| 1376 | } |
| 1377 | |
| 1378 | static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, |
| 1379 | struct x86_emulate_ops* ops) |
| 1380 | { |
| 1381 | switch(ctxt->mode) { |
| 1382 | case X86EMUL_MODE_REAL: |
| 1383 | return emulate_iret_real(ctxt, ops); |
| 1384 | case X86EMUL_MODE_VM86: |
| 1385 | case X86EMUL_MODE_PROT16: |
| 1386 | case X86EMUL_MODE_PROT32: |
| 1387 | case X86EMUL_MODE_PROT64: |
| 1388 | default: |
| 1389 | /* iret from protected mode unimplemented yet */ |
| 1390 | return X86EMUL_UNHANDLEABLE; |
| 1391 | } |
| 1392 | } |
| 1393 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1394 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1395 | struct x86_emulate_ops *ops) |
| 1396 | { |
| 1397 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1398 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1399 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1402 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1403 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1404 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1405 | switch (c->modrm_reg) { |
| 1406 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1407 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1408 | break; |
| 1409 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1410 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1411 | break; |
| 1412 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1413 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1416 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1417 | break; |
| 1418 | case 4: /* sal/shl */ |
| 1419 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1420 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1421 | break; |
| 1422 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1423 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1424 | break; |
| 1425 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1426 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1432 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1433 | { |
| 1434 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1435 | unsigned long *rax = &c->regs[VCPU_REGS_RAX]; |
| 1436 | unsigned long *rdx = &c->regs[VCPU_REGS_RDX]; |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1437 | u8 de = 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1438 | |
| 1439 | switch (c->modrm_reg) { |
| 1440 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1441 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1442 | break; |
| 1443 | case 2: /* not */ |
| 1444 | c->dst.val = ~c->dst.val; |
| 1445 | break; |
| 1446 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1447 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1448 | break; |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1449 | case 4: /* mul */ |
| 1450 | emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags); |
| 1451 | break; |
| 1452 | case 5: /* imul */ |
| 1453 | emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags); |
| 1454 | break; |
| 1455 | case 6: /* div */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1456 | emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx, |
| 1457 | ctxt->eflags, de); |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1458 | break; |
| 1459 | case 7: /* idiv */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1460 | emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx, |
| 1461 | ctxt->eflags, de); |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 1462 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1463 | default: |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 1464 | return X86EMUL_UNHANDLEABLE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1465 | } |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1466 | if (de) |
| 1467 | return emulate_de(ctxt); |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 1468 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1472 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1473 | { |
| 1474 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1475 | |
| 1476 | switch (c->modrm_reg) { |
| 1477 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1478 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1479 | break; |
| 1480 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1481 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1482 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1483 | case 2: /* call near abs */ { |
| 1484 | long int old_eip; |
| 1485 | old_eip = c->eip; |
| 1486 | c->eip = c->src.val; |
| 1487 | c->src.val = old_eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1488 | emulate_push(ctxt, ops); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1489 | break; |
| 1490 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1491 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1492 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1493 | break; |
| 1494 | case 6: /* push */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1495 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1496 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1497 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1498 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1502 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1503 | { |
| 1504 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1505 | u64 old = c->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1506 | |
| 1507 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1508 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1509 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1510 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1511 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1512 | } else { |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1513 | c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1514 | (u32) c->regs[VCPU_REGS_RBX]; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1515 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1516 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1517 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1518 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1521 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1522 | struct x86_emulate_ops *ops) |
| 1523 | { |
| 1524 | struct decode_cache *c = &ctxt->decode; |
| 1525 | int rc; |
| 1526 | unsigned long cs; |
| 1527 | |
| 1528 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1529 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1530 | return rc; |
| 1531 | if (c->op_bytes == 4) |
| 1532 | c->eip = (u32)c->eip; |
| 1533 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1534 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1535 | return rc; |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1536 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1537 | return rc; |
| 1538 | } |
| 1539 | |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 1540 | static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, |
| 1541 | struct x86_emulate_ops *ops, int seg) |
| 1542 | { |
| 1543 | struct decode_cache *c = &ctxt->decode; |
| 1544 | unsigned short sel; |
| 1545 | int rc; |
| 1546 | |
| 1547 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 1548 | |
| 1549 | rc = load_segment_descriptor(ctxt, ops, sel, seg); |
| 1550 | if (rc != X86EMUL_CONTINUE) |
| 1551 | return rc; |
| 1552 | |
| 1553 | c->dst.val = c->src.val; |
| 1554 | return rc; |
| 1555 | } |
| 1556 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1557 | static inline void |
| 1558 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1559 | struct x86_emulate_ops *ops, struct desc_struct *cs, |
| 1560 | struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1561 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1562 | memset(cs, 0, sizeof(struct desc_struct)); |
| 1563 | ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1564 | memset(ss, 0, sizeof(struct desc_struct)); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1565 | |
| 1566 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1567 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1568 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1569 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1570 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1571 | cs->s = 1; |
| 1572 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1573 | cs->p = 1; |
| 1574 | cs->d = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1575 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1576 | set_desc_base(ss, 0); /* flat segment */ |
| 1577 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1578 | ss->g = 1; /* 4kb granularity */ |
| 1579 | ss->s = 1; |
| 1580 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1581 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1582 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1583 | ss->p = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1587 | emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1588 | { |
| 1589 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1590 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1591 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1592 | u16 cs_sel, ss_sel; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1593 | |
| 1594 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1595 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1596 | ctxt->mode == X86EMUL_MODE_VM86) |
| 1597 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1598 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1599 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1600 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1601 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1602 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1603 | cs_sel = (u16)(msr_data & 0xfffc); |
| 1604 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1605 | |
| 1606 | if (is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1607 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1608 | cs.l = 1; |
| 1609 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1610 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1611 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 1612 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1613 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1614 | |
| 1615 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 1616 | if (is_long_mode(ctxt->vcpu)) { |
| 1617 | #ifdef CONFIG_X86_64 |
| 1618 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 1619 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1620 | ops->get_msr(ctxt->vcpu, |
| 1621 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 1622 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1623 | c->eip = msr_data; |
| 1624 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1625 | ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1626 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 1627 | #endif |
| 1628 | } else { |
| 1629 | /* legacy mode */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1630 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1631 | c->eip = (u32)msr_data; |
| 1632 | |
| 1633 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1634 | } |
| 1635 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1636 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1639 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1640 | emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1641 | { |
| 1642 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1643 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1644 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1645 | u16 cs_sel, ss_sel; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1646 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1647 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1648 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1649 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 1652 | * Therefore, we inject an #UD. |
| 1653 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1654 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 1655 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1656 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1657 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1658 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1659 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1660 | switch (ctxt->mode) { |
| 1661 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1662 | if ((msr_data & 0xfffc) == 0x0) |
| 1663 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1664 | break; |
| 1665 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1666 | if (msr_data == 0x0) |
| 1667 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1668 | break; |
| 1669 | } |
| 1670 | |
| 1671 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1672 | cs_sel = (u16)msr_data; |
| 1673 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 1674 | ss_sel = cs_sel + 8; |
| 1675 | ss_sel &= ~SELECTOR_RPL_MASK; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1676 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 1677 | || is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1678 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1679 | cs.l = 1; |
| 1680 | } |
| 1681 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1682 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1683 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 1684 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1685 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1686 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1687 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1688 | c->eip = msr_data; |
| 1689 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1690 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1691 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 1692 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1693 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1696 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1697 | emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1698 | { |
| 1699 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1700 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1701 | u64 msr_data; |
| 1702 | int usermode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1703 | u16 cs_sel, ss_sel; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1704 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1705 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 1706 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1707 | ctxt->mode == X86EMUL_MODE_VM86) |
| 1708 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1709 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1710 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1711 | |
| 1712 | if ((c->rex_prefix & 0x8) != 0x0) |
| 1713 | usermode = X86EMUL_MODE_PROT64; |
| 1714 | else |
| 1715 | usermode = X86EMUL_MODE_PROT32; |
| 1716 | |
| 1717 | cs.dpl = 3; |
| 1718 | ss.dpl = 3; |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 1719 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1720 | switch (usermode) { |
| 1721 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1722 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1723 | if ((msr_data & 0xfffc) == 0x0) |
| 1724 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1725 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1726 | break; |
| 1727 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1728 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1729 | if (msr_data == 0x0) |
| 1730 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1731 | ss_sel = cs_sel + 8; |
| 1732 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1733 | cs.l = 1; |
| 1734 | break; |
| 1735 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1736 | cs_sel |= SELECTOR_RPL_MASK; |
| 1737 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1738 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1739 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1740 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 1741 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1742 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1743 | |
Gleb Natapov | bdb475a | 2010-04-28 19:15:41 +0300 | [diff] [blame] | 1744 | c->eip = c->regs[VCPU_REGS_RDX]; |
| 1745 | c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX]; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1746 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1747 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1750 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt, |
| 1751 | struct x86_emulate_ops *ops) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1752 | { |
| 1753 | int iopl; |
| 1754 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1755 | return false; |
| 1756 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 1757 | return true; |
| 1758 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1759 | return ops->cpl(ctxt->vcpu) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 1763 | struct x86_emulate_ops *ops, |
| 1764 | u16 port, u16 len) |
| 1765 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1766 | struct desc_struct tr_seg; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1767 | int r; |
| 1768 | u16 io_bitmap_ptr; |
| 1769 | u8 perm, bit_idx = port & 0x7; |
| 1770 | unsigned mask = (1 << len) - 1; |
| 1771 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1772 | ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu); |
| 1773 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1774 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1775 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1776 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1777 | r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2, |
| 1778 | ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1779 | if (r != X86EMUL_CONTINUE) |
| 1780 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1781 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1782 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1783 | r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8, |
| 1784 | &perm, 1, ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1785 | if (r != X86EMUL_CONTINUE) |
| 1786 | return false; |
| 1787 | if ((perm >> bit_idx) & mask) |
| 1788 | return false; |
| 1789 | return true; |
| 1790 | } |
| 1791 | |
| 1792 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 1793 | struct x86_emulate_ops *ops, |
| 1794 | u16 port, u16 len) |
| 1795 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 1796 | if (ctxt->perm_ok) |
| 1797 | return true; |
| 1798 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1799 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1800 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 1801 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 1802 | |
| 1803 | ctxt->perm_ok = true; |
| 1804 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1805 | return true; |
| 1806 | } |
| 1807 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1808 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
| 1809 | struct x86_emulate_ops *ops, |
| 1810 | struct tss_segment_16 *tss) |
| 1811 | { |
| 1812 | struct decode_cache *c = &ctxt->decode; |
| 1813 | |
| 1814 | tss->ip = c->eip; |
| 1815 | tss->flag = ctxt->eflags; |
| 1816 | tss->ax = c->regs[VCPU_REGS_RAX]; |
| 1817 | tss->cx = c->regs[VCPU_REGS_RCX]; |
| 1818 | tss->dx = c->regs[VCPU_REGS_RDX]; |
| 1819 | tss->bx = c->regs[VCPU_REGS_RBX]; |
| 1820 | tss->sp = c->regs[VCPU_REGS_RSP]; |
| 1821 | tss->bp = c->regs[VCPU_REGS_RBP]; |
| 1822 | tss->si = c->regs[VCPU_REGS_RSI]; |
| 1823 | tss->di = c->regs[VCPU_REGS_RDI]; |
| 1824 | |
| 1825 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 1826 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 1827 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 1828 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 1829 | tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 1830 | } |
| 1831 | |
| 1832 | static int load_state_from_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 | int ret; |
| 1838 | |
| 1839 | c->eip = tss->ip; |
| 1840 | ctxt->eflags = tss->flag | 2; |
| 1841 | c->regs[VCPU_REGS_RAX] = tss->ax; |
| 1842 | c->regs[VCPU_REGS_RCX] = tss->cx; |
| 1843 | c->regs[VCPU_REGS_RDX] = tss->dx; |
| 1844 | c->regs[VCPU_REGS_RBX] = tss->bx; |
| 1845 | c->regs[VCPU_REGS_RSP] = tss->sp; |
| 1846 | c->regs[VCPU_REGS_RBP] = tss->bp; |
| 1847 | c->regs[VCPU_REGS_RSI] = tss->si; |
| 1848 | c->regs[VCPU_REGS_RDI] = tss->di; |
| 1849 | |
| 1850 | /* |
| 1851 | * SDM says that segment selectors are loaded before segment |
| 1852 | * descriptors |
| 1853 | */ |
| 1854 | ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu); |
| 1855 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 1856 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1857 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1858 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 1859 | |
| 1860 | /* |
| 1861 | * Now load segment descriptors. If fault happenes at this stage |
| 1862 | * it is handled in a context of new task |
| 1863 | */ |
| 1864 | ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR); |
| 1865 | if (ret != X86EMUL_CONTINUE) |
| 1866 | return ret; |
| 1867 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 1868 | if (ret != X86EMUL_CONTINUE) |
| 1869 | return ret; |
| 1870 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 1871 | if (ret != X86EMUL_CONTINUE) |
| 1872 | return ret; |
| 1873 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 1874 | if (ret != X86EMUL_CONTINUE) |
| 1875 | return ret; |
| 1876 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 1877 | if (ret != X86EMUL_CONTINUE) |
| 1878 | return ret; |
| 1879 | |
| 1880 | return X86EMUL_CONTINUE; |
| 1881 | } |
| 1882 | |
| 1883 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
| 1884 | struct x86_emulate_ops *ops, |
| 1885 | u16 tss_selector, u16 old_tss_sel, |
| 1886 | ulong old_tss_base, struct desc_struct *new_desc) |
| 1887 | { |
| 1888 | struct tss_segment_16 tss_seg; |
| 1889 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1890 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1891 | |
| 1892 | 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] | 1893 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1894 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1895 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1896 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1897 | |
| 1898 | save_state_to_tss16(ctxt, ops, &tss_seg); |
| 1899 | |
| 1900 | 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] | 1901 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1902 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1903 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1904 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1905 | |
| 1906 | 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] | 1907 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1908 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1909 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1910 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1911 | |
| 1912 | if (old_tss_sel != 0xffff) { |
| 1913 | tss_seg.prev_task_link = old_tss_sel; |
| 1914 | |
| 1915 | ret = ops->write_std(new_tss_base, |
| 1916 | &tss_seg.prev_task_link, |
| 1917 | sizeof tss_seg.prev_task_link, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 1918 | ctxt->vcpu, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 1919 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1920 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1921 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | return load_state_from_tss16(ctxt, ops, &tss_seg); |
| 1925 | } |
| 1926 | |
| 1927 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
| 1928 | struct x86_emulate_ops *ops, |
| 1929 | struct tss_segment_32 *tss) |
| 1930 | { |
| 1931 | struct decode_cache *c = &ctxt->decode; |
| 1932 | |
| 1933 | tss->cr3 = ops->get_cr(3, ctxt->vcpu); |
| 1934 | tss->eip = c->eip; |
| 1935 | tss->eflags = ctxt->eflags; |
| 1936 | tss->eax = c->regs[VCPU_REGS_RAX]; |
| 1937 | tss->ecx = c->regs[VCPU_REGS_RCX]; |
| 1938 | tss->edx = c->regs[VCPU_REGS_RDX]; |
| 1939 | tss->ebx = c->regs[VCPU_REGS_RBX]; |
| 1940 | tss->esp = c->regs[VCPU_REGS_RSP]; |
| 1941 | tss->ebp = c->regs[VCPU_REGS_RBP]; |
| 1942 | tss->esi = c->regs[VCPU_REGS_RSI]; |
| 1943 | tss->edi = c->regs[VCPU_REGS_RDI]; |
| 1944 | |
| 1945 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 1946 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 1947 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 1948 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 1949 | tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu); |
| 1950 | tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu); |
| 1951 | tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 1952 | } |
| 1953 | |
| 1954 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
| 1955 | struct x86_emulate_ops *ops, |
| 1956 | struct tss_segment_32 *tss) |
| 1957 | { |
| 1958 | struct decode_cache *c = &ctxt->decode; |
| 1959 | int ret; |
| 1960 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 1961 | if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) |
| 1962 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1963 | c->eip = tss->eip; |
| 1964 | ctxt->eflags = tss->eflags | 2; |
| 1965 | c->regs[VCPU_REGS_RAX] = tss->eax; |
| 1966 | c->regs[VCPU_REGS_RCX] = tss->ecx; |
| 1967 | c->regs[VCPU_REGS_RDX] = tss->edx; |
| 1968 | c->regs[VCPU_REGS_RBX] = tss->ebx; |
| 1969 | c->regs[VCPU_REGS_RSP] = tss->esp; |
| 1970 | c->regs[VCPU_REGS_RBP] = tss->ebp; |
| 1971 | c->regs[VCPU_REGS_RSI] = tss->esi; |
| 1972 | c->regs[VCPU_REGS_RDI] = tss->edi; |
| 1973 | |
| 1974 | /* |
| 1975 | * SDM says that segment selectors are loaded before segment |
| 1976 | * descriptors |
| 1977 | */ |
| 1978 | ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu); |
| 1979 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 1980 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1981 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 1982 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 1983 | ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu); |
| 1984 | ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu); |
| 1985 | |
| 1986 | /* |
| 1987 | * Now load segment descriptors. If fault happenes at this stage |
| 1988 | * it is handled in a context of new task |
| 1989 | */ |
| 1990 | ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR); |
| 1991 | if (ret != X86EMUL_CONTINUE) |
| 1992 | return ret; |
| 1993 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 1994 | if (ret != X86EMUL_CONTINUE) |
| 1995 | return ret; |
| 1996 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 1997 | if (ret != X86EMUL_CONTINUE) |
| 1998 | return ret; |
| 1999 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2000 | if (ret != X86EMUL_CONTINUE) |
| 2001 | return ret; |
| 2002 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2003 | if (ret != X86EMUL_CONTINUE) |
| 2004 | return ret; |
| 2005 | ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS); |
| 2006 | if (ret != X86EMUL_CONTINUE) |
| 2007 | return ret; |
| 2008 | ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS); |
| 2009 | if (ret != X86EMUL_CONTINUE) |
| 2010 | return ret; |
| 2011 | |
| 2012 | return X86EMUL_CONTINUE; |
| 2013 | } |
| 2014 | |
| 2015 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
| 2016 | struct x86_emulate_ops *ops, |
| 2017 | u16 tss_selector, u16 old_tss_sel, |
| 2018 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2019 | { |
| 2020 | struct tss_segment_32 tss_seg; |
| 2021 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2022 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2023 | |
| 2024 | 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] | 2025 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2026 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2027 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2028 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2029 | |
| 2030 | save_state_to_tss32(ctxt, ops, &tss_seg); |
| 2031 | |
| 2032 | 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] | 2033 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2034 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2035 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2036 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2037 | |
| 2038 | 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] | 2039 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2040 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2041 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2042 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (old_tss_sel != 0xffff) { |
| 2045 | tss_seg.prev_task_link = old_tss_sel; |
| 2046 | |
| 2047 | ret = ops->write_std(new_tss_base, |
| 2048 | &tss_seg.prev_task_link, |
| 2049 | sizeof tss_seg.prev_task_link, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2050 | ctxt->vcpu, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2051 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2052 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2053 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | return load_state_from_tss32(ctxt, ops, &tss_seg); |
| 2057 | } |
| 2058 | |
| 2059 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2060 | struct x86_emulate_ops *ops, |
| 2061 | u16 tss_selector, int reason, |
| 2062 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2063 | { |
| 2064 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2065 | int ret; |
| 2066 | u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu); |
| 2067 | ulong old_tss_base = |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 2068 | ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2069 | u32 desc_limit; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2070 | |
| 2071 | /* FIXME: old_tss_base == ~0 ? */ |
| 2072 | |
| 2073 | ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc); |
| 2074 | if (ret != X86EMUL_CONTINUE) |
| 2075 | return ret; |
| 2076 | ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc); |
| 2077 | if (ret != X86EMUL_CONTINUE) |
| 2078 | return ret; |
| 2079 | |
| 2080 | /* FIXME: check that next_tss_desc is tss */ |
| 2081 | |
| 2082 | if (reason != TASK_SWITCH_IRET) { |
| 2083 | if ((tss_selector & 3) > next_tss_desc.dpl || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2084 | ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) |
| 2085 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2088 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2089 | if (!next_tss_desc.p || |
| 2090 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2091 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2092 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2093 | return X86EMUL_PROPAGATE_FAULT; |
| 2094 | } |
| 2095 | |
| 2096 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2097 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
| 2098 | write_segment_descriptor(ctxt, ops, old_tss_sel, |
| 2099 | &curr_tss_desc); |
| 2100 | } |
| 2101 | |
| 2102 | if (reason == TASK_SWITCH_IRET) |
| 2103 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2104 | |
| 2105 | /* set back link to prev task only if NT bit is set in eflags |
| 2106 | note that old_tss_sel is not used afetr this point */ |
| 2107 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2108 | old_tss_sel = 0xffff; |
| 2109 | |
| 2110 | if (next_tss_desc.type & 8) |
| 2111 | ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel, |
| 2112 | old_tss_base, &next_tss_desc); |
| 2113 | else |
| 2114 | ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel, |
| 2115 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2116 | if (ret != X86EMUL_CONTINUE) |
| 2117 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2118 | |
| 2119 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2120 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2121 | |
| 2122 | if (reason != TASK_SWITCH_IRET) { |
| 2123 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
| 2124 | write_segment_descriptor(ctxt, ops, tss_selector, |
| 2125 | &next_tss_desc); |
| 2126 | } |
| 2127 | |
| 2128 | ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu); |
| 2129 | ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu); |
| 2130 | ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu); |
| 2131 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2132 | if (has_error_code) { |
| 2133 | struct decode_cache *c = &ctxt->decode; |
| 2134 | |
| 2135 | c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2136 | c->lock_prefix = 0; |
| 2137 | c->src.val = (unsigned long) error_code; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2138 | emulate_push(ctxt, ops); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2141 | return ret; |
| 2142 | } |
| 2143 | |
| 2144 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2145 | u16 tss_selector, int reason, |
| 2146 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2147 | { |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 2148 | struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2149 | struct decode_cache *c = &ctxt->decode; |
| 2150 | int rc; |
| 2151 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2152 | c->eip = ctxt->eip; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2153 | c->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2154 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2155 | rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason, |
| 2156 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2157 | |
| 2158 | if (rc == X86EMUL_CONTINUE) { |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2159 | rc = writeback(ctxt, ops); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2160 | if (rc == X86EMUL_CONTINUE) |
| 2161 | ctxt->eip = c->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2162 | } |
| 2163 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 2164 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2167 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg, |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2168 | int reg, struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2169 | { |
| 2170 | struct decode_cache *c = &ctxt->decode; |
| 2171 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
| 2172 | |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2173 | register_address_increment(c, &c->regs[reg], df * op->bytes); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2174 | op->addr.mem.ea = register_address(c, c->regs[reg]); |
| 2175 | op->addr.mem.seg = seg; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 2178 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 2179 | { |
| 2180 | emulate_push(ctxt, ctxt->ops); |
| 2181 | return X86EMUL_CONTINUE; |
| 2182 | } |
| 2183 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2184 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2185 | { |
| 2186 | struct decode_cache *c = &ctxt->decode; |
| 2187 | u8 al, old_al; |
| 2188 | bool af, cf, old_cf; |
| 2189 | |
| 2190 | cf = ctxt->eflags & X86_EFLAGS_CF; |
| 2191 | al = c->dst.val; |
| 2192 | |
| 2193 | old_al = al; |
| 2194 | old_cf = cf; |
| 2195 | cf = false; |
| 2196 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2197 | if ((al & 0x0f) > 9 || af) { |
| 2198 | al -= 6; |
| 2199 | cf = old_cf | (al >= 250); |
| 2200 | af = true; |
| 2201 | } else { |
| 2202 | af = false; |
| 2203 | } |
| 2204 | if (old_al > 0x99 || old_cf) { |
| 2205 | al -= 0x60; |
| 2206 | cf = true; |
| 2207 | } |
| 2208 | |
| 2209 | c->dst.val = al; |
| 2210 | /* Set PF, ZF, SF */ |
| 2211 | c->src.type = OP_IMM; |
| 2212 | c->src.val = 0; |
| 2213 | c->src.bytes = 1; |
| 2214 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
| 2215 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2216 | if (cf) |
| 2217 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2218 | if (af) |
| 2219 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2220 | return X86EMUL_CONTINUE; |
| 2221 | } |
| 2222 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2223 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2224 | { |
| 2225 | struct decode_cache *c = &ctxt->decode; |
| 2226 | u16 sel, old_cs; |
| 2227 | ulong old_eip; |
| 2228 | int rc; |
| 2229 | |
| 2230 | old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2231 | old_eip = c->eip; |
| 2232 | |
| 2233 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 2234 | if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS)) |
| 2235 | return X86EMUL_CONTINUE; |
| 2236 | |
| 2237 | c->eip = 0; |
| 2238 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
| 2239 | |
| 2240 | c->src.val = old_cs; |
| 2241 | emulate_push(ctxt, ctxt->ops); |
| 2242 | rc = writeback(ctxt, ctxt->ops); |
| 2243 | if (rc != X86EMUL_CONTINUE) |
| 2244 | return rc; |
| 2245 | |
| 2246 | c->src.val = old_eip; |
| 2247 | emulate_push(ctxt, ctxt->ops); |
| 2248 | rc = writeback(ctxt, ctxt->ops); |
| 2249 | if (rc != X86EMUL_CONTINUE) |
| 2250 | return rc; |
| 2251 | |
| 2252 | c->dst.type = OP_NONE; |
| 2253 | |
| 2254 | return X86EMUL_CONTINUE; |
| 2255 | } |
| 2256 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2257 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2258 | { |
| 2259 | struct decode_cache *c = &ctxt->decode; |
| 2260 | int rc; |
| 2261 | |
| 2262 | c->dst.type = OP_REG; |
| 2263 | c->dst.addr.reg = &c->eip; |
| 2264 | c->dst.bytes = c->op_bytes; |
| 2265 | rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes); |
| 2266 | if (rc != X86EMUL_CONTINUE) |
| 2267 | return rc; |
| 2268 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val); |
| 2269 | return X86EMUL_CONTINUE; |
| 2270 | } |
| 2271 | |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2272 | static int em_imul(struct x86_emulate_ctxt *ctxt) |
| 2273 | { |
| 2274 | struct decode_cache *c = &ctxt->decode; |
| 2275 | |
| 2276 | emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags); |
| 2277 | return X86EMUL_CONTINUE; |
| 2278 | } |
| 2279 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2280 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2281 | { |
| 2282 | struct decode_cache *c = &ctxt->decode; |
| 2283 | |
| 2284 | c->dst.val = c->src2.val; |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2285 | return em_imul(ctxt); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2286 | } |
| 2287 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2288 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2289 | { |
| 2290 | struct decode_cache *c = &ctxt->decode; |
| 2291 | |
| 2292 | c->dst.type = OP_REG; |
| 2293 | c->dst.bytes = c->src.bytes; |
| 2294 | c->dst.addr.reg = &c->regs[VCPU_REGS_RDX]; |
| 2295 | c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1); |
| 2296 | |
| 2297 | return X86EMUL_CONTINUE; |
| 2298 | } |
| 2299 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2300 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2301 | { |
| 2302 | unsigned cpl = ctxt->ops->cpl(ctxt->vcpu); |
| 2303 | struct decode_cache *c = &ctxt->decode; |
| 2304 | u64 tsc = 0; |
| 2305 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2306 | if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) |
| 2307 | return emulate_gp(ctxt, 0); |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2308 | ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc); |
| 2309 | c->regs[VCPU_REGS_RAX] = (u32)tsc; |
| 2310 | c->regs[VCPU_REGS_RDX] = tsc >> 32; |
| 2311 | return X86EMUL_CONTINUE; |
| 2312 | } |
| 2313 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2314 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 2315 | { |
| 2316 | struct decode_cache *c = &ctxt->decode; |
| 2317 | c->dst.val = c->src.val; |
| 2318 | return X86EMUL_CONTINUE; |
| 2319 | } |
| 2320 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2321 | #define D(_y) { .flags = (_y) } |
| 2322 | #define N D(0) |
| 2323 | #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) } |
| 2324 | #define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) } |
| 2325 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
| 2326 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2327 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
| 2328 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
| 2329 | |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2330 | #define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \ |
| 2331 | D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ |
| 2332 | D2bv(((_f) & ~Lock) | DstAcc | SrcImm) |
| 2333 | |
| 2334 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2335 | static struct opcode group1[] = { |
| 2336 | X7(D(Lock)), N |
| 2337 | }; |
| 2338 | |
| 2339 | static struct opcode group1A[] = { |
| 2340 | D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N, |
| 2341 | }; |
| 2342 | |
| 2343 | static struct opcode group3[] = { |
| 2344 | D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM), |
| 2345 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 2346 | X4(D(SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2347 | }; |
| 2348 | |
| 2349 | static struct opcode group4[] = { |
| 2350 | D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock), |
| 2351 | N, N, N, N, N, N, |
| 2352 | }; |
| 2353 | |
| 2354 | static struct opcode group5[] = { |
| 2355 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2356 | D(SrcMem | ModRM | Stack), |
| 2357 | I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2358 | D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps), |
| 2359 | D(SrcMem | ModRM | Stack), N, |
| 2360 | }; |
| 2361 | |
| 2362 | static struct group_dual group7 = { { |
| 2363 | N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv), |
| 2364 | D(SrcNone | ModRM | DstMem | Mov), N, |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 2365 | D(SrcMem16 | ModRM | Mov | Priv), |
| 2366 | D(SrcMem | ModRM | ByteOp | Priv | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2367 | }, { |
| 2368 | D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv), |
| 2369 | D(SrcNone | ModRM | DstMem | Mov), N, |
| 2370 | D(SrcMem16 | ModRM | Mov | Priv), N, |
| 2371 | } }; |
| 2372 | |
| 2373 | static struct opcode group8[] = { |
| 2374 | N, N, N, N, |
| 2375 | D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock), |
| 2376 | D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock), |
| 2377 | }; |
| 2378 | |
| 2379 | static struct group_dual group9 = { { |
| 2380 | N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N, |
| 2381 | }, { |
| 2382 | N, N, N, N, N, N, N, N, |
| 2383 | } }; |
| 2384 | |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 2385 | static struct opcode group11[] = { |
| 2386 | I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)), |
| 2387 | }; |
| 2388 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2389 | static struct opcode opcode_table[256] = { |
| 2390 | /* 0x00 - 0x07 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2391 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2392 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2393 | /* 0x08 - 0x0F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2394 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2395 | D(ImplicitOps | Stack | No64), N, |
| 2396 | /* 0x10 - 0x17 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2397 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2398 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2399 | /* 0x18 - 0x1F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2400 | D6ALU(Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2401 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2402 | /* 0x20 - 0x27 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2403 | D6ALU(Lock), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2404 | /* 0x28 - 0x2F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2405 | D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2406 | /* 0x30 - 0x37 */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2407 | D6ALU(Lock), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2408 | /* 0x38 - 0x3F */ |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2409 | D6ALU(0), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2410 | /* 0x40 - 0x4F */ |
| 2411 | X16(D(DstReg)), |
| 2412 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 2413 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2414 | /* 0x58 - 0x5F */ |
| 2415 | X8(D(DstReg | Stack)), |
| 2416 | /* 0x60 - 0x67 */ |
| 2417 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 2418 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 2419 | N, N, N, N, |
| 2420 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 2421 | I(SrcImm | Mov | Stack, em_push), |
| 2422 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2423 | I(SrcImmByte | Mov | Stack, em_push), |
| 2424 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Avi Kivity | 48fe67b | 2010-08-26 11:56:08 +0300 | [diff] [blame] | 2425 | D2bv(DstDI | Mov | String), /* insb, insw/insd */ |
| 2426 | D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */ |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2427 | /* 0x70 - 0x7F */ |
| 2428 | X16(D(SrcImmByte)), |
| 2429 | /* 0x80 - 0x87 */ |
| 2430 | G(ByteOp | DstMem | SrcImm | ModRM | Group, group1), |
| 2431 | G(DstMem | SrcImm | ModRM | Group, group1), |
| 2432 | G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1), |
| 2433 | G(DstMem | SrcImmByte | ModRM | Group, group1), |
Avi Kivity | 76e8e68 | 2010-08-26 11:56:09 +0300 | [diff] [blame] | 2434 | D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2435 | /* 0x88 - 0x8F */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2436 | I2bv(DstMem | SrcReg | ModRM | Mov, em_mov), |
| 2437 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Avi Kivity | 342fc63 | 2010-08-01 15:13:22 +0300 | [diff] [blame] | 2438 | D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2439 | D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A), |
| 2440 | /* 0x90 - 0x97 */ |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 2441 | X8(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2442 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2443 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 2444 | I(SrcImmFAddr | No64, em_call_far), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2445 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N, |
| 2446 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2447 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
| 2448 | I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov), |
| 2449 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
| 2450 | D2bv(SrcSI | DstDI | String), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2451 | /* 0xA8 - 0xAF */ |
Avi Kivity | 5074861 | 2010-08-26 11:56:10 +0300 | [diff] [blame] | 2452 | D2bv(DstAcc | SrcImm), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2453 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 2454 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | 48fe67b | 2010-08-26 11:56:08 +0300 | [diff] [blame] | 2455 | D2bv(SrcAcc | DstDI | String), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2456 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2457 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2458 | /* 0xB8 - 0xBF */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2459 | X8(I(DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2460 | /* 0xC0 - 0xC7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 2461 | D2bv(DstMem | SrcImmByte | ModRM), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2462 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
| 2463 | D(ImplicitOps | Stack), |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2464 | D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 2465 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2466 | /* 0xC8 - 0xCF */ |
| 2467 | N, N, N, D(ImplicitOps | Stack), |
| 2468 | D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps), |
| 2469 | /* 0xD0 - 0xD7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 2470 | D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2471 | N, N, N, N, |
| 2472 | /* 0xD8 - 0xDF */ |
| 2473 | N, N, N, N, N, N, N, N, |
| 2474 | /* 0xE0 - 0xE7 */ |
Wei Yongjun | e4abac6 | 2010-08-19 14:25:48 +0800 | [diff] [blame] | 2475 | X4(D(SrcImmByte)), |
Avi Kivity | d269e39 | 2010-08-26 11:56:12 +0300 | [diff] [blame] | 2476 | D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2477 | /* 0xE8 - 0xEF */ |
| 2478 | D(SrcImm | Stack), D(SrcImm | ImplicitOps), |
| 2479 | D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps), |
Avi Kivity | d269e39 | 2010-08-26 11:56:12 +0300 | [diff] [blame] | 2480 | D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2481 | /* 0xF0 - 0xF7 */ |
| 2482 | N, N, N, N, |
| 2483 | D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3), |
| 2484 | /* 0xF8 - 0xFF */ |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 2485 | D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2486 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 2487 | }; |
| 2488 | |
| 2489 | static struct opcode twobyte_table[256] = { |
| 2490 | /* 0x00 - 0x0F */ |
| 2491 | N, GD(0, &group7), N, N, |
| 2492 | N, D(ImplicitOps), D(ImplicitOps | Priv), N, |
| 2493 | D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N, |
| 2494 | N, D(ImplicitOps | ModRM), N, N, |
| 2495 | /* 0x10 - 0x1F */ |
| 2496 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, |
| 2497 | /* 0x20 - 0x2F */ |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 2498 | D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264), |
| 2499 | D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2500 | N, N, N, N, |
| 2501 | N, N, N, N, N, N, N, N, |
| 2502 | /* 0x30 - 0x3F */ |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2503 | D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc), |
| 2504 | D(ImplicitOps | Priv), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2505 | D(ImplicitOps), D(ImplicitOps | Priv), N, N, |
| 2506 | N, N, N, N, N, N, N, N, |
| 2507 | /* 0x40 - 0x4F */ |
| 2508 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 2509 | /* 0x50 - 0x5F */ |
| 2510 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2511 | /* 0x60 - 0x6F */ |
| 2512 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2513 | /* 0x70 - 0x7F */ |
| 2514 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2515 | /* 0x80 - 0x8F */ |
| 2516 | X16(D(SrcImm)), |
| 2517 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 2518 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2519 | /* 0xA0 - 0xA7 */ |
| 2520 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 2521 | N, D(DstMem | SrcReg | ModRM | BitOp), |
| 2522 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 2523 | D(DstMem | SrcReg | Src2CL | ModRM), N, N, |
| 2524 | /* 0xA8 - 0xAF */ |
| 2525 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 2526 | N, D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 2527 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 2528 | D(DstMem | SrcReg | Src2CL | ModRM), |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2529 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2530 | /* 0xB0 - 0xB7 */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 2531 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2532 | D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 2533 | D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM), |
| 2534 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2535 | /* 0xB8 - 0xBF */ |
| 2536 | N, N, |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 2537 | G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), |
Wei Yongjun | d9574a2 | 2010-08-10 13:48:22 +0800 | [diff] [blame] | 2538 | D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 2539 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2540 | /* 0xC0 - 0xCF */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 2541 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 2542 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 2543 | N, N, N, GD(0, &group9), |
| 2544 | N, N, N, N, N, N, N, N, |
| 2545 | /* 0xD0 - 0xDF */ |
| 2546 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2547 | /* 0xE0 - 0xEF */ |
| 2548 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 2549 | /* 0xF0 - 0xFF */ |
| 2550 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 2551 | }; |
| 2552 | |
| 2553 | #undef D |
| 2554 | #undef N |
| 2555 | #undef G |
| 2556 | #undef GD |
| 2557 | #undef I |
| 2558 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2559 | #undef D2bv |
| 2560 | #undef I2bv |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 2561 | #undef D6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 2562 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2563 | static unsigned imm_size(struct decode_cache *c) |
| 2564 | { |
| 2565 | unsigned size; |
| 2566 | |
| 2567 | size = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 2568 | if (size == 8) |
| 2569 | size = 4; |
| 2570 | return size; |
| 2571 | } |
| 2572 | |
| 2573 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 2574 | unsigned size, bool sign_extension) |
| 2575 | { |
| 2576 | struct decode_cache *c = &ctxt->decode; |
| 2577 | struct x86_emulate_ops *ops = ctxt->ops; |
| 2578 | int rc = X86EMUL_CONTINUE; |
| 2579 | |
| 2580 | op->type = OP_IMM; |
| 2581 | op->bytes = size; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2582 | op->addr.mem.ea = c->eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2583 | /* NB. Immediates are sign-extended as necessary. */ |
| 2584 | switch (op->bytes) { |
| 2585 | case 1: |
| 2586 | op->val = insn_fetch(s8, 1, c->eip); |
| 2587 | break; |
| 2588 | case 2: |
| 2589 | op->val = insn_fetch(s16, 2, c->eip); |
| 2590 | break; |
| 2591 | case 4: |
| 2592 | op->val = insn_fetch(s32, 4, c->eip); |
| 2593 | break; |
| 2594 | } |
| 2595 | if (!sign_extension) { |
| 2596 | switch (op->bytes) { |
| 2597 | case 1: |
| 2598 | op->val &= 0xff; |
| 2599 | break; |
| 2600 | case 2: |
| 2601 | op->val &= 0xffff; |
| 2602 | break; |
| 2603 | case 4: |
| 2604 | op->val &= 0xffffffff; |
| 2605 | break; |
| 2606 | } |
| 2607 | } |
| 2608 | done: |
| 2609 | return rc; |
| 2610 | } |
| 2611 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2612 | int |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2613 | x86_decode_insn(struct x86_emulate_ctxt *ctxt) |
| 2614 | { |
| 2615 | struct x86_emulate_ops *ops = ctxt->ops; |
| 2616 | struct decode_cache *c = &ctxt->decode; |
| 2617 | int rc = X86EMUL_CONTINUE; |
| 2618 | int mode = ctxt->mode; |
| 2619 | int def_op_bytes, def_ad_bytes, dual, goffset; |
| 2620 | struct opcode opcode, *g_mod012, *g_mod3; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2621 | struct operand memop = { .type = OP_NONE }; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2622 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2623 | c->eip = ctxt->eip; |
| 2624 | c->fetch.start = c->fetch.end = c->eip; |
| 2625 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); |
| 2626 | |
| 2627 | switch (mode) { |
| 2628 | case X86EMUL_MODE_REAL: |
| 2629 | case X86EMUL_MODE_VM86: |
| 2630 | case X86EMUL_MODE_PROT16: |
| 2631 | def_op_bytes = def_ad_bytes = 2; |
| 2632 | break; |
| 2633 | case X86EMUL_MODE_PROT32: |
| 2634 | def_op_bytes = def_ad_bytes = 4; |
| 2635 | break; |
| 2636 | #ifdef CONFIG_X86_64 |
| 2637 | case X86EMUL_MODE_PROT64: |
| 2638 | def_op_bytes = 4; |
| 2639 | def_ad_bytes = 8; |
| 2640 | break; |
| 2641 | #endif |
| 2642 | default: |
| 2643 | return -1; |
| 2644 | } |
| 2645 | |
| 2646 | c->op_bytes = def_op_bytes; |
| 2647 | c->ad_bytes = def_ad_bytes; |
| 2648 | |
| 2649 | /* Legacy prefixes. */ |
| 2650 | for (;;) { |
| 2651 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
| 2652 | case 0x66: /* operand-size override */ |
| 2653 | /* switch between 2/4 bytes */ |
| 2654 | c->op_bytes = def_op_bytes ^ 6; |
| 2655 | break; |
| 2656 | case 0x67: /* address-size override */ |
| 2657 | if (mode == X86EMUL_MODE_PROT64) |
| 2658 | /* switch between 4/8 bytes */ |
| 2659 | c->ad_bytes = def_ad_bytes ^ 12; |
| 2660 | else |
| 2661 | /* switch between 2/4 bytes */ |
| 2662 | c->ad_bytes = def_ad_bytes ^ 6; |
| 2663 | break; |
| 2664 | case 0x26: /* ES override */ |
| 2665 | case 0x2e: /* CS override */ |
| 2666 | case 0x36: /* SS override */ |
| 2667 | case 0x3e: /* DS override */ |
| 2668 | set_seg_override(c, (c->b >> 3) & 3); |
| 2669 | break; |
| 2670 | case 0x64: /* FS override */ |
| 2671 | case 0x65: /* GS override */ |
| 2672 | set_seg_override(c, c->b & 7); |
| 2673 | break; |
| 2674 | case 0x40 ... 0x4f: /* REX */ |
| 2675 | if (mode != X86EMUL_MODE_PROT64) |
| 2676 | goto done_prefixes; |
| 2677 | c->rex_prefix = c->b; |
| 2678 | continue; |
| 2679 | case 0xf0: /* LOCK */ |
| 2680 | c->lock_prefix = 1; |
| 2681 | break; |
| 2682 | case 0xf2: /* REPNE/REPNZ */ |
| 2683 | c->rep_prefix = REPNE_PREFIX; |
| 2684 | break; |
| 2685 | case 0xf3: /* REP/REPE/REPZ */ |
| 2686 | c->rep_prefix = REPE_PREFIX; |
| 2687 | break; |
| 2688 | default: |
| 2689 | goto done_prefixes; |
| 2690 | } |
| 2691 | |
| 2692 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 2693 | |
| 2694 | c->rex_prefix = 0; |
| 2695 | } |
| 2696 | |
| 2697 | done_prefixes: |
| 2698 | |
| 2699 | /* REX prefix. */ |
Avi Kivity | 1e87e3e | 2010-08-01 14:42:51 +0300 | [diff] [blame] | 2700 | if (c->rex_prefix & 8) |
| 2701 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2702 | |
| 2703 | /* Opcode byte(s). */ |
| 2704 | opcode = opcode_table[c->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 2705 | /* Two-byte opcode? */ |
| 2706 | if (c->b == 0x0f) { |
| 2707 | c->twobyte = 1; |
| 2708 | c->b = insn_fetch(u8, 1, c->eip); |
| 2709 | opcode = twobyte_table[c->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2710 | } |
| 2711 | c->d = opcode.flags; |
| 2712 | |
| 2713 | if (c->d & Group) { |
| 2714 | dual = c->d & GroupDual; |
| 2715 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 2716 | --c->eip; |
| 2717 | |
| 2718 | if (c->d & GroupDual) { |
| 2719 | g_mod012 = opcode.u.gdual->mod012; |
| 2720 | g_mod3 = opcode.u.gdual->mod3; |
| 2721 | } else |
| 2722 | g_mod012 = g_mod3 = opcode.u.group; |
| 2723 | |
| 2724 | c->d &= ~(Group | GroupDual); |
| 2725 | |
| 2726 | goffset = (c->modrm >> 3) & 7; |
| 2727 | |
| 2728 | if ((c->modrm >> 6) == 3) |
| 2729 | opcode = g_mod3[goffset]; |
| 2730 | else |
| 2731 | opcode = g_mod012[goffset]; |
| 2732 | c->d |= opcode.flags; |
| 2733 | } |
| 2734 | |
| 2735 | c->execute = opcode.u.execute; |
| 2736 | |
| 2737 | /* Unrecognised? */ |
Avi Kivity | d53db5e | 2010-11-17 13:40:51 +0200 | [diff] [blame] | 2738 | if (c->d == 0 || (c->d & Undefined)) |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2739 | return -1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2740 | |
| 2741 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 2742 | c->op_bytes = 8; |
| 2743 | |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 2744 | if (c->d & Op3264) { |
| 2745 | if (mode == X86EMUL_MODE_PROT64) |
| 2746 | c->op_bytes = 8; |
| 2747 | else |
| 2748 | c->op_bytes = 4; |
| 2749 | } |
| 2750 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2751 | /* ModRM and SIB bytes. */ |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 2752 | if (c->d & ModRM) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2753 | rc = decode_modrm(ctxt, ops, &memop); |
Avi Kivity | 09ee57c | 2010-08-01 12:07:29 +0300 | [diff] [blame] | 2754 | if (!c->has_seg_override) |
| 2755 | set_seg_override(c, c->modrm_seg); |
| 2756 | } else if (c->d & MemAbs) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2757 | rc = decode_abs(ctxt, ops, &memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2758 | if (rc != X86EMUL_CONTINUE) |
| 2759 | goto done; |
| 2760 | |
| 2761 | if (!c->has_seg_override) |
| 2762 | set_seg_override(c, VCPU_SREG_DS); |
| 2763 | |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2764 | memop.addr.mem.seg = seg_override(ctxt, ops, c); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2765 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2766 | if (memop.type == OP_MEM && c->ad_bytes != 8) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2767 | memop.addr.mem.ea = (u32)memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2768 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2769 | if (memop.type == OP_MEM && c->rip_relative) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2770 | memop.addr.mem.ea += c->eip; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2771 | |
| 2772 | /* |
| 2773 | * Decode and fetch the source operand: register, memory |
| 2774 | * or immediate. |
| 2775 | */ |
| 2776 | switch (c->d & SrcMask) { |
| 2777 | case SrcNone: |
| 2778 | break; |
| 2779 | case SrcReg: |
| 2780 | decode_register_operand(&c->src, c, 0); |
| 2781 | break; |
| 2782 | case SrcMem16: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2783 | memop.bytes = 2; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2784 | goto srcmem_common; |
| 2785 | case SrcMem32: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2786 | memop.bytes = 4; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2787 | goto srcmem_common; |
| 2788 | case SrcMem: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2789 | memop.bytes = (c->d & ByteOp) ? 1 : |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2790 | c->op_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2791 | srcmem_common: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2792 | c->src = memop; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2793 | break; |
Avi Kivity | b250e60 | 2010-08-18 15:11:24 +0300 | [diff] [blame] | 2794 | case SrcImmU16: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2795 | rc = decode_imm(ctxt, &c->src, 2, false); |
| 2796 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2797 | case SrcImm: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2798 | rc = decode_imm(ctxt, &c->src, imm_size(c), true); |
| 2799 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2800 | case SrcImmU: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2801 | rc = decode_imm(ctxt, &c->src, imm_size(c), false); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2802 | break; |
| 2803 | case SrcImmByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2804 | rc = decode_imm(ctxt, &c->src, 1, true); |
| 2805 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2806 | case SrcImmUByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2807 | rc = decode_imm(ctxt, &c->src, 1, false); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2808 | break; |
| 2809 | case SrcAcc: |
| 2810 | c->src.type = OP_REG; |
| 2811 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 2812 | c->src.addr.reg = &c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 2813 | fetch_register_operand(&c->src); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2814 | break; |
| 2815 | case SrcOne: |
| 2816 | c->src.bytes = 1; |
| 2817 | c->src.val = 1; |
| 2818 | break; |
| 2819 | case SrcSI: |
| 2820 | c->src.type = OP_MEM; |
| 2821 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2822 | c->src.addr.mem.ea = |
| 2823 | register_address(c, c->regs[VCPU_REGS_RSI]); |
| 2824 | c->src.addr.mem.seg = seg_override(ctxt, ops, c), |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2825 | c->src.val = 0; |
| 2826 | break; |
| 2827 | case SrcImmFAddr: |
| 2828 | c->src.type = OP_IMM; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2829 | c->src.addr.mem.ea = c->eip; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2830 | c->src.bytes = c->op_bytes + 2; |
| 2831 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); |
| 2832 | break; |
| 2833 | case SrcMemFAddr: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2834 | memop.bytes = c->op_bytes + 2; |
| 2835 | goto srcmem_common; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2836 | break; |
| 2837 | } |
| 2838 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2839 | if (rc != X86EMUL_CONTINUE) |
| 2840 | goto done; |
| 2841 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2842 | /* |
| 2843 | * Decode and fetch the second source operand: register, memory |
| 2844 | * or immediate. |
| 2845 | */ |
| 2846 | switch (c->d & Src2Mask) { |
| 2847 | case Src2None: |
| 2848 | break; |
| 2849 | case Src2CL: |
| 2850 | c->src2.bytes = 1; |
| 2851 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 2852 | break; |
| 2853 | case Src2ImmByte: |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2854 | rc = decode_imm(ctxt, &c->src2, 1, true); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2855 | break; |
| 2856 | case Src2One: |
| 2857 | c->src2.bytes = 1; |
| 2858 | c->src2.val = 1; |
| 2859 | break; |
Avi Kivity | 7db41eb | 2010-08-18 19:25:28 +0300 | [diff] [blame] | 2860 | case Src2Imm: |
| 2861 | rc = decode_imm(ctxt, &c->src2, imm_size(c), true); |
| 2862 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2863 | } |
| 2864 | |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 2865 | if (rc != X86EMUL_CONTINUE) |
| 2866 | goto done; |
| 2867 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2868 | /* Decode and fetch the destination operand: register or memory. */ |
| 2869 | switch (c->d & DstMask) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2870 | case DstReg: |
| 2871 | decode_register_operand(&c->dst, c, |
| 2872 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
| 2873 | break; |
Wei Yongjun | 943858e | 2010-08-06 11:36:51 +0800 | [diff] [blame] | 2874 | case DstImmUByte: |
| 2875 | c->dst.type = OP_IMM; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2876 | c->dst.addr.mem.ea = c->eip; |
Wei Yongjun | 943858e | 2010-08-06 11:36:51 +0800 | [diff] [blame] | 2877 | c->dst.bytes = 1; |
| 2878 | c->dst.val = insn_fetch(u8, 1, c->eip); |
| 2879 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2880 | case DstMem: |
| 2881 | case DstMem64: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2882 | c->dst = memop; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2883 | if ((c->d & DstMask) == DstMem64) |
| 2884 | c->dst.bytes = 8; |
| 2885 | else |
| 2886 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 2887 | if (c->d & BitOp) |
| 2888 | fetch_bit_operand(c); |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 2889 | c->dst.orig_val = c->dst.val; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2890 | break; |
| 2891 | case DstAcc: |
| 2892 | c->dst.type = OP_REG; |
| 2893 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 2894 | c->dst.addr.reg = &c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 2895 | fetch_register_operand(&c->dst); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2896 | c->dst.orig_val = c->dst.val; |
| 2897 | break; |
| 2898 | case DstDI: |
| 2899 | c->dst.type = OP_MEM; |
| 2900 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2901 | c->dst.addr.mem.ea = |
| 2902 | register_address(c, c->regs[VCPU_REGS_RDI]); |
| 2903 | c->dst.addr.mem.seg = VCPU_SREG_ES; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2904 | c->dst.val = 0; |
| 2905 | break; |
Wei Yongjun | 36089fe | 2010-08-04 15:38:18 +0800 | [diff] [blame] | 2906 | case ImplicitOps: |
| 2907 | /* Special instructions do their own operand decoding. */ |
| 2908 | default: |
| 2909 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2910 | return 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | done: |
| 2914 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 2915 | } |
| 2916 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 2917 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 2918 | { |
| 2919 | struct decode_cache *c = &ctxt->decode; |
| 2920 | |
| 2921 | /* The second termination condition only applies for REPE |
| 2922 | * and REPNE. Test if the repeat string operation prefix is |
| 2923 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 2924 | * corresponding termination condition according to: |
| 2925 | * - if REPE/REPZ and ZF = 0 then done |
| 2926 | * - if REPNE/REPNZ and ZF = 1 then done |
| 2927 | */ |
| 2928 | if (((c->b == 0xa6) || (c->b == 0xa7) || |
| 2929 | (c->b == 0xae) || (c->b == 0xaf)) |
| 2930 | && (((c->rep_prefix == REPE_PREFIX) && |
| 2931 | ((ctxt->eflags & EFLG_ZF) == 0)) |
| 2932 | || ((c->rep_prefix == REPNE_PREFIX) && |
| 2933 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 2934 | return true; |
| 2935 | |
| 2936 | return false; |
| 2937 | } |
| 2938 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 2939 | int |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 2940 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2941 | { |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 2942 | struct x86_emulate_ops *ops = ctxt->ops; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2943 | u64 msr_data; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2944 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2945 | int rc = X86EMUL_CONTINUE; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2946 | int saved_dst_type = c->dst.type; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2947 | int irq; /* Used for int 3, int, and into */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2948 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2949 | ctxt->decode.mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2950 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2951 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2952 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2953 | goto done; |
| 2954 | } |
| 2955 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2956 | /* LOCK prefix is allowed only with some instructions */ |
Gleb Natapov | a41ffb75 | 2010-03-18 15:20:14 +0200 | [diff] [blame] | 2957 | if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2958 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2959 | goto done; |
| 2960 | } |
| 2961 | |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 2962 | if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2963 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 2964 | goto done; |
| 2965 | } |
| 2966 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2967 | /* Privileged instruction can be executed only in CPL=0 */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2968 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 2969 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2970 | goto done; |
| 2971 | } |
| 2972 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2973 | if (c->rep_prefix && (c->d & String)) { |
| 2974 | /* All REP prefixes have the same first termination condition */ |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 2975 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2976 | ctxt->eip = c->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2977 | goto done; |
| 2978 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2979 | } |
| 2980 | |
Wei Yongjun | c483c02 | 2010-08-06 15:36:36 +0800 | [diff] [blame] | 2981 | if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2982 | rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem), |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 2983 | c->src.valptr, c->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2984 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2985 | goto done; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 2986 | c->src.orig_val64 = c->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2989 | if (c->src2.type == OP_MEM) { |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 2990 | rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2991 | &c->src2.val, c->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2992 | if (rc != X86EMUL_CONTINUE) |
| 2993 | goto done; |
| 2994 | } |
| 2995 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2996 | if ((c->d & DstMask) == ImplicitOps) |
| 2997 | goto special_insn; |
| 2998 | |
| 2999 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3000 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
| 3001 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3002 | rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3003 | &c->dst.val, c->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3004 | if (rc != X86EMUL_CONTINUE) |
| 3005 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 3006 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3007 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 3008 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3009 | special_insn: |
| 3010 | |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 3011 | if (c->execute) { |
| 3012 | rc = c->execute(ctxt); |
| 3013 | if (rc != X86EMUL_CONTINUE) |
| 3014 | goto done; |
| 3015 | goto writeback; |
| 3016 | } |
| 3017 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3018 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3019 | goto twobyte_insn; |
| 3020 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3021 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3022 | case 0x00 ... 0x05: |
| 3023 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3024 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3025 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3026 | case 0x06: /* push es */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3027 | emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3028 | break; |
| 3029 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3030 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3031 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3032 | case 0x08 ... 0x0d: |
| 3033 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3034 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3035 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3036 | case 0x0e: /* push cs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3037 | emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3038 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3039 | case 0x10 ... 0x15: |
| 3040 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3041 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3042 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3043 | case 0x16: /* push ss */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3044 | emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3045 | break; |
| 3046 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3047 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3048 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3049 | case 0x18 ... 0x1d: |
| 3050 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3051 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3052 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3053 | case 0x1e: /* push ds */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3054 | emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3055 | break; |
| 3056 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3057 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3058 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 3059 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3060 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3061 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3062 | break; |
| 3063 | case 0x28 ... 0x2d: |
| 3064 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3065 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3066 | break; |
| 3067 | case 0x30 ... 0x35: |
| 3068 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3069 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3070 | break; |
| 3071 | case 0x38 ... 0x3d: |
| 3072 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3073 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3074 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3075 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 3076 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 3077 | break; |
| 3078 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 3079 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 3080 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3081 | case 0x58 ... 0x5f: /* pop reg */ |
| 3082 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 3083 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 3084 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3085 | case 0x60: /* pusha */ |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 3086 | rc = emulate_pusha(ctxt, ops); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3087 | break; |
| 3088 | case 0x61: /* popa */ |
| 3089 | rc = emulate_popa(ctxt, ops); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 3090 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3091 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 3092 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3093 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3094 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3095 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3096 | case 0x6c: /* insb */ |
| 3097 | case 0x6d: /* insw/insd */ |
Wei Yongjun | a13a63f | 2010-08-06 11:46:12 +0800 | [diff] [blame] | 3098 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3099 | goto do_io_in; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3100 | case 0x6e: /* outsb */ |
| 3101 | case 0x6f: /* outsw/outsd */ |
Wei Yongjun | a13a63f | 2010-08-06 11:46:12 +0800 | [diff] [blame] | 3102 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
| 3103 | goto do_io_out; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3104 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3105 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3106 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3107 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3108 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3109 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3110 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3111 | case 0: |
| 3112 | goto add; |
| 3113 | case 1: |
| 3114 | goto or; |
| 3115 | case 2: |
| 3116 | goto adc; |
| 3117 | case 3: |
| 3118 | goto sbb; |
| 3119 | case 4: |
| 3120 | goto and; |
| 3121 | case 5: |
| 3122 | goto sub; |
| 3123 | case 6: |
| 3124 | goto xor; |
| 3125 | case 7: |
| 3126 | goto cmp; |
| 3127 | } |
| 3128 | break; |
| 3129 | case 0x84 ... 0x85: |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 3130 | test: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3131 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3132 | break; |
| 3133 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 3134 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3135 | /* Write back the register source. */ |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 3136 | c->src.val = c->dst.val; |
| 3137 | write_register_operand(&c->src); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3138 | /* |
| 3139 | * Write back the memory destination with implicit LOCK |
| 3140 | * prefix. |
| 3141 | */ |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 3142 | c->dst.val = c->src.orig_val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3143 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3144 | break; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3145 | case 0x8c: /* mov r/m, sreg */ |
| 3146 | if (c->modrm_reg > VCPU_SREG_GS) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3147 | rc = emulate_ud(ctxt); |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 3148 | goto done; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 3149 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3150 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 3151 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 3152 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3153 | c->dst.val = c->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 3154 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3155 | case 0x8e: { /* mov seg, r/m16 */ |
| 3156 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3157 | |
| 3158 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 3159 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3160 | if (c->modrm_reg == VCPU_SREG_CS || |
| 3161 | c->modrm_reg > VCPU_SREG_GS) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3162 | rc = emulate_ud(ctxt); |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 3163 | goto done; |
| 3164 | } |
| 3165 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3166 | if (c->modrm_reg == VCPU_SREG_SS) |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3167 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3168 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 3169 | rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 3170 | |
| 3171 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3172 | break; |
| 3173 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3175 | rc = emulate_grp1a(ctxt, ops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3176 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 3177 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
| 3178 | if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX]) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 3179 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 3180 | goto xchg; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 3181 | case 0x98: /* cbw/cwde/cdqe */ |
| 3182 | switch (c->op_bytes) { |
| 3183 | case 2: c->dst.val = (s8)c->dst.val; break; |
| 3184 | case 4: c->dst.val = (s16)c->dst.val; break; |
| 3185 | case 8: c->dst.val = (s32)c->dst.val; break; |
| 3186 | } |
| 3187 | break; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 3188 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3189 | c->src.val = (unsigned long) ctxt->eflags; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3190 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3191 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 3192 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 3193 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3194 | c->dst.addr.reg = &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 3195 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 3196 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 3197 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3198 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 3199 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3200 | goto cmp; |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 3201 | case 0xa8 ... 0xa9: /* test ax, imm */ |
| 3202 | goto test; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3203 | case 0xae ... 0xaf: /* scas */ |
Avi Kivity | f6b33fc | 2010-08-17 11:20:37 +0300 | [diff] [blame] | 3204 | goto cmp; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3205 | case 0xc0 ... 0xc1: |
| 3206 | emulate_grp2(ctxt); |
| 3207 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3208 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 3209 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3210 | c->dst.addr.reg = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 3211 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3212 | goto pop_instruction; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3213 | case 0xc4: /* les */ |
| 3214 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3215 | break; |
| 3216 | case 0xc5: /* lds */ |
| 3217 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3218 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 3219 | case 0xcb: /* ret far */ |
| 3220 | rc = emulate_ret_far(ctxt, ops); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 3221 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 3222 | case 0xcc: /* int3 */ |
| 3223 | irq = 3; |
| 3224 | goto do_interrupt; |
| 3225 | case 0xcd: /* int n */ |
| 3226 | irq = c->src.val; |
| 3227 | do_interrupt: |
| 3228 | rc = emulate_int(ctxt, ops, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 3229 | break; |
| 3230 | case 0xce: /* into */ |
| 3231 | if (ctxt->eflags & EFLG_OF) { |
| 3232 | irq = 4; |
| 3233 | goto do_interrupt; |
| 3234 | } |
| 3235 | break; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 3236 | case 0xcf: /* iret */ |
| 3237 | rc = emulate_iret(ctxt, ops); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 3238 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3239 | case 0xd0 ... 0xd1: /* Grp2 */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3240 | emulate_grp2(ctxt); |
| 3241 | break; |
| 3242 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 3243 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 3244 | emulate_grp2(ctxt); |
| 3245 | break; |
Wei Yongjun | f2f3184 | 2010-08-18 16:38:21 +0800 | [diff] [blame] | 3246 | case 0xe0 ... 0xe2: /* loop/loopz/loopnz */ |
| 3247 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
| 3248 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 && |
| 3249 | (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags))) |
| 3250 | jmp_rel(c, c->src.val); |
| 3251 | break; |
Wei Yongjun | e4abac6 | 2010-08-19 14:25:48 +0800 | [diff] [blame] | 3252 | case 0xe3: /* jcxz/jecxz/jrcxz */ |
| 3253 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) |
| 3254 | jmp_rel(c, c->src.val); |
| 3255 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3256 | case 0xe4: /* inb */ |
| 3257 | case 0xe5: /* in */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3258 | goto do_io_in; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3259 | case 0xe6: /* outb */ |
| 3260 | case 0xe7: /* out */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3261 | goto do_io_out; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3262 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 3263 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3264 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3265 | jmp_rel(c, rel); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3266 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3267 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3268 | } |
| 3269 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3270 | goto jmp; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3271 | case 0xea: { /* jmp far */ |
| 3272 | unsigned short sel; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3273 | jump_far: |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3274 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 3275 | |
| 3276 | if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS)) |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3277 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3278 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3279 | c->eip = 0; |
| 3280 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3281 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3282 | } |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3283 | case 0xeb: |
| 3284 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3285 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3286 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3287 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3288 | case 0xec: /* in al,dx */ |
| 3289 | case 0xed: /* in (e/r)ax,dx */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3290 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3291 | do_io_in: |
| 3292 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3293 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3294 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3295 | goto done; |
| 3296 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3297 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val, |
| 3298 | &c->dst.val)) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3299 | goto done; /* IO is needed */ |
| 3300 | break; |
Wei Yongjun | ce7a0ad | 2010-07-06 16:50:21 +0800 | [diff] [blame] | 3301 | case 0xee: /* out dx,al */ |
| 3302 | case 0xef: /* out dx,(e/r)ax */ |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3303 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3304 | do_io_out: |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3305 | c->src.bytes = min(c->src.bytes, 4u); |
| 3306 | if (!emulator_io_permited(ctxt, ops, c->dst.val, |
| 3307 | c->src.bytes)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3308 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3309 | goto done; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3310 | } |
Wei Yongjun | 41167be | 2010-08-06 11:45:12 +0800 | [diff] [blame] | 3311 | ops->pio_out_emulated(c->src.bytes, c->dst.val, |
| 3312 | &c->src.val, 1, ctxt->vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3313 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 3314 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3315 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3316 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 3317 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3318 | case 0xf5: /* cmc */ |
| 3319 | /* complement carry flag from eflags reg */ |
| 3320 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3321 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3322 | case 0xf6 ... 0xf7: /* Grp3 */ |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 3323 | rc = emulate_grp3(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3324 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3325 | case 0xf8: /* clc */ |
| 3326 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3327 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 3328 | case 0xf9: /* stc */ |
| 3329 | ctxt->eflags |= EFLG_CF; |
| 3330 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3331 | case 0xfa: /* cli */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3332 | if (emulator_bad_iopl(ctxt, ops)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3333 | rc = emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3334 | goto done; |
Wei Yongjun | 36089fe | 2010-08-04 15:38:18 +0800 | [diff] [blame] | 3335 | } else |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3336 | ctxt->eflags &= ~X86_EFLAGS_IF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3337 | break; |
| 3338 | case 0xfb: /* sti */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3339 | if (emulator_bad_iopl(ctxt, ops)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame^] | 3340 | rc = emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3341 | goto done; |
| 3342 | } else { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3343 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3344 | ctxt->eflags |= X86_EFLAGS_IF; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3345 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3346 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3347 | case 0xfc: /* cld */ |
| 3348 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3349 | break; |
| 3350 | case 0xfd: /* std */ |
| 3351 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3352 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3353 | case 0xfe: /* Grp4 */ |
| 3354 | grp45: |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3355 | rc = emulate_grp45(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3356 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3357 | case 0xff: /* Grp5 */ |
| 3358 | if (c->modrm_reg == 5) |
| 3359 | goto jump_far; |
| 3360 | goto grp45; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3361 | default: |
| 3362 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3363 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3364 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 3365 | if (rc != X86EMUL_CONTINUE) |
| 3366 | goto done; |
| 3367 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3368 | writeback: |
| 3369 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3370 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3371 | goto done; |
| 3372 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3373 | /* |
| 3374 | * restore dst type in case the decoding will be reused |
| 3375 | * (happens for string instruction ) |
| 3376 | */ |
| 3377 | c->dst.type = saved_dst_type; |
| 3378 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3379 | if ((c->d & SrcMask) == SrcSI) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3380 | string_addr_inc(ctxt, seg_override(ctxt, ops, c), |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3381 | VCPU_REGS_RSI, &c->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3382 | |
| 3383 | if ((c->d & DstMask) == DstDI) |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3384 | string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3385 | &c->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3386 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3387 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 6e2fb2c | 2010-08-25 12:47:41 +0300 | [diff] [blame] | 3388 | struct read_cache *r = &ctxt->decode.io_read; |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3389 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 3390 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3391 | if (!string_insn_completed(ctxt)) { |
| 3392 | /* |
| 3393 | * Re-enter guest when pio read ahead buffer is empty |
| 3394 | * or, if it is not used, after each 1024 iteration. |
| 3395 | */ |
| 3396 | if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) && |
| 3397 | (r->end == 0 || r->end != r->pos)) { |
| 3398 | /* |
| 3399 | * Reset read cache. Usually happens before |
| 3400 | * decode, but since instruction is restarted |
| 3401 | * we have to do it here. |
| 3402 | */ |
| 3403 | ctxt->decode.mem_read.end = 0; |
| 3404 | return EMULATION_RESTART; |
| 3405 | } |
| 3406 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 3407 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3408 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3409 | |
| 3410 | ctxt->eip = c->eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3411 | |
| 3412 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3413 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 3414 | ctxt->have_exception = true; |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 3415 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3416 | |
| 3417 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3418 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3419 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3420 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3421 | u16 size; |
| 3422 | unsigned long address; |
| 3423 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3424 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3425 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3426 | goto cannot_emulate; |
| 3427 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3428 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3429 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3430 | goto done; |
| 3431 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 3432 | /* Let the processor re-execute the fixed hypercall */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 3433 | c->eip = ctxt->eip; |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3434 | /* Disable writeback. */ |
| 3435 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3436 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3437 | case 2: /* lgdt */ |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3438 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3439 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3440 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3441 | goto done; |
| 3442 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3443 | /* Disable writeback. */ |
| 3444 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3445 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3446 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3447 | if (c->modrm_mod == 3) { |
| 3448 | switch (c->modrm_rm) { |
| 3449 | case 1: |
| 3450 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3451 | break; |
| 3452 | default: |
| 3453 | goto cannot_emulate; |
| 3454 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3455 | } else { |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3456 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3457 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3458 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3459 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3460 | goto done; |
| 3461 | realmode_lidt(ctxt->vcpu, size, address); |
| 3462 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3463 | /* Disable writeback. */ |
| 3464 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3465 | break; |
| 3466 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3467 | c->dst.bytes = 2; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3468 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3469 | break; |
| 3470 | case 6: /* lmsw */ |
Avi Kivity | 9928ff6 | 2010-08-01 18:35:24 +0300 | [diff] [blame] | 3471 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) | |
Gleb Natapov | 93a152b | 2010-03-18 15:20:04 +0200 | [diff] [blame] | 3472 | (c->src.val & 0x0f), ctxt->vcpu); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 3473 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3474 | break; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3475 | case 5: /* not defined */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3476 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3477 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3478 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3479 | case 7: /* invlpg*/ |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 3480 | emulate_invlpg(ctxt->vcpu, |
| 3481 | linear(ctxt, c->src.addr.mem)); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3482 | /* Disable writeback. */ |
| 3483 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3484 | break; |
| 3485 | default: |
| 3486 | goto cannot_emulate; |
| 3487 | } |
| 3488 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3489 | case 0x05: /* syscall */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3490 | rc = emulate_syscall(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3491 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3492 | case 0x06: |
| 3493 | emulate_clts(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3494 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3495 | case 0x09: /* wbinvd */ |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3496 | kvm_emulate_wbinvd(ctxt->vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3497 | break; |
| 3498 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3499 | case 0x0d: /* GrpP (prefetch) */ |
| 3500 | case 0x18: /* Grp16 (prefetch/nop) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3501 | break; |
| 3502 | case 0x20: /* mov cr, reg */ |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3503 | switch (c->modrm_reg) { |
| 3504 | case 1: |
| 3505 | case 5 ... 7: |
| 3506 | case 9 ... 15: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3507 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3508 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3509 | goto done; |
| 3510 | } |
Avi Kivity | 1a0c7d4 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3511 | c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3512 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3513 | case 0x21: /* mov from dr to reg */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3514 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3515 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
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 | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3518 | goto done; |
| 3519 | } |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3520 | ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3521 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3522 | case 0x22: /* mov reg, cr */ |
Avi Kivity | 1a0c7d4 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3523 | if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3524 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3525 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3526 | goto done; |
| 3527 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3528 | c->dst.type = OP_NONE; |
| 3529 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3530 | case 0x23: /* mov from reg to dr */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3531 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3532 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3533 | emulate_ud(ctxt); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3534 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3535 | goto done; |
| 3536 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3537 | |
Avi Kivity | b27f385 | 2010-08-01 14:25:22 +0300 | [diff] [blame] | 3538 | if (ops->set_dr(c->modrm_reg, c->src.val & |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3539 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? |
| 3540 | ~0ULL : ~0U), ctxt->vcpu) < 0) { |
| 3541 | /* #UD condition is already handled by the code above */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3542 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3543 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3544 | goto done; |
| 3545 | } |
| 3546 | |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3547 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3548 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3549 | case 0x30: |
| 3550 | /* wrmsr */ |
| 3551 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 3552 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3553 | 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] | 3554 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3555 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3556 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3557 | } |
| 3558 | rc = X86EMUL_CONTINUE; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3559 | break; |
| 3560 | case 0x32: |
| 3561 | /* rdmsr */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3562 | 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] | 3563 | emulate_gp(ctxt, 0); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 3564 | rc = X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3565 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3566 | } else { |
| 3567 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 3568 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 3569 | } |
| 3570 | rc = X86EMUL_CONTINUE; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3571 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3572 | case 0x34: /* sysenter */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3573 | rc = emulate_sysenter(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3574 | break; |
| 3575 | case 0x35: /* sysexit */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3576 | rc = emulate_sysexit(ctxt, ops); |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3577 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3578 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3579 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3580 | if (!test_cc(c->b, ctxt->eflags)) |
| 3581 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3582 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3583 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3584 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3585 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3586 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3587 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
| 3588 | c->dst.val = test_cc(c->b, ctxt->eflags); |
| 3589 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3590 | case 0xa0: /* push fs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3591 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3592 | break; |
| 3593 | case 0xa1: /* pop fs */ |
| 3594 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3595 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3596 | case 0xa3: |
| 3597 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 3598 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3599 | /* only subword offset */ |
| 3600 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3601 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3602 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3603 | case 0xa4: /* shld imm8, r, r/m */ |
| 3604 | case 0xa5: /* shld cl, r, r/m */ |
| 3605 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 3606 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3607 | case 0xa8: /* push gs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3608 | emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3609 | break; |
| 3610 | case 0xa9: /* pop gs */ |
| 3611 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3612 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3613 | case 0xab: |
| 3614 | bts: /* bts */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3615 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3616 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3617 | case 0xac: /* shrd imm8, r, r/m */ |
| 3618 | case 0xad: /* shrd cl, r, r/m */ |
| 3619 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 3620 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 3621 | case 0xae: /* clflush */ |
| 3622 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3623 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 3624 | /* |
| 3625 | * Save real source value, then compare EAX against |
| 3626 | * destination. |
| 3627 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3628 | c->src.orig_val = c->src.val; |
| 3629 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3630 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 3631 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3632 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3633 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3634 | } else { |
| 3635 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3636 | c->dst.type = OP_REG; |
Avi Kivity | 1a6440aef | 2010-08-01 12:35:10 +0300 | [diff] [blame] | 3637 | c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3638 | } |
| 3639 | break; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3640 | case 0xb2: /* lss */ |
| 3641 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3642 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3643 | case 0xb3: |
| 3644 | btr: /* btr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3645 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3646 | break; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3647 | case 0xb4: /* lfs */ |
| 3648 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3649 | break; |
| 3650 | case 0xb5: /* lgs */ |
| 3651 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 3652 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3653 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3654 | c->dst.bytes = c->op_bytes; |
| 3655 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 3656 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3657 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3658 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3659 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3660 | case 0: |
| 3661 | goto bt; |
| 3662 | case 1: |
| 3663 | goto bts; |
| 3664 | case 2: |
| 3665 | goto btr; |
| 3666 | case 3: |
| 3667 | goto btc; |
| 3668 | } |
| 3669 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3670 | case 0xbb: |
| 3671 | btc: /* btc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3672 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3673 | break; |
Wei Yongjun | d9574a2 | 2010-08-10 13:48:22 +0800 | [diff] [blame] | 3674 | case 0xbc: { /* bsf */ |
| 3675 | u8 zf; |
| 3676 | __asm__ ("bsf %2, %0; setz %1" |
| 3677 | : "=r"(c->dst.val), "=q"(zf) |
| 3678 | : "r"(c->src.val)); |
| 3679 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
| 3680 | if (zf) { |
| 3681 | ctxt->eflags |= X86_EFLAGS_ZF; |
| 3682 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3683 | } |
| 3684 | break; |
| 3685 | } |
| 3686 | case 0xbd: { /* bsr */ |
| 3687 | u8 zf; |
| 3688 | __asm__ ("bsr %2, %0; setz %1" |
| 3689 | : "=r"(c->dst.val), "=q"(zf) |
| 3690 | : "r"(c->src.val)); |
| 3691 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
| 3692 | if (zf) { |
| 3693 | ctxt->eflags |= X86_EFLAGS_ZF; |
| 3694 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3695 | } |
| 3696 | break; |
| 3697 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3698 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3699 | c->dst.bytes = c->op_bytes; |
| 3700 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 3701 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3702 | break; |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3703 | case 0xc0 ... 0xc1: /* xadd */ |
| 3704 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
| 3705 | /* Write back the register source. */ |
| 3706 | c->src.val = c->dst.orig_val; |
| 3707 | write_register_operand(&c->src); |
| 3708 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3709 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3710 | c->dst.bytes = c->op_bytes; |
| 3711 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 3712 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3713 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3714 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3715 | rc = emulate_grp9(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3716 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3717 | default: |
| 3718 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3719 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 3720 | |
| 3721 | if (rc != X86EMUL_CONTINUE) |
| 3722 | goto done; |
| 3723 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3724 | goto writeback; |
| 3725 | |
| 3726 | cannot_emulate: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3727 | return -1; |
| 3728 | } |