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