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