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