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