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