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