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