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