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