Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 28 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 29 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 30 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | /* |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 32 | * Operand types |
| 33 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 34 | #define OpNone 0ull |
| 35 | #define OpImplicit 1ull /* No generic decode */ |
| 36 | #define OpReg 2ull /* Register */ |
| 37 | #define OpMem 3ull /* Memory */ |
| 38 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 39 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 40 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 41 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 42 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 43 | #define OpCL 9ull /* CL register (for shifts) */ |
| 44 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 45 | #define OpOne 11ull /* Implied 1 */ |
| 46 | #define OpImm 12ull /* Sign extended immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 47 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 48 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 49 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 50 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 51 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 52 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 53 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 54 | #define OpES 20ull /* ES */ |
| 55 | #define OpCS 21ull /* CS */ |
| 56 | #define OpSS 22ull /* SS */ |
| 57 | #define OpDS 23ull /* DS */ |
| 58 | #define OpFS 24ull /* FS */ |
| 59 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 60 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 61 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 62 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 63 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 64 | |
| 65 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 66 | * Opcode effective-address decode tables. |
| 67 | * Note that we only emulate instructions that have at least one memory |
| 68 | * operand (excluding implicit stack references). We assume that stack |
| 69 | * references and instruction fetches will never occur in special memory |
| 70 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 71 | * not be handled. |
| 72 | */ |
| 73 | |
| 74 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 75 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 76 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 77 | #define DstShift 1 |
| 78 | #define ImplicitOps (OpImplicit << DstShift) |
| 79 | #define DstReg (OpReg << DstShift) |
| 80 | #define DstMem (OpMem << DstShift) |
| 81 | #define DstAcc (OpAcc << DstShift) |
| 82 | #define DstDI (OpDI << DstShift) |
| 83 | #define DstMem64 (OpMem64 << DstShift) |
| 84 | #define DstImmUByte (OpImmUByte << DstShift) |
| 85 | #define DstDX (OpDX << DstShift) |
| 86 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 88 | #define SrcShift 6 |
| 89 | #define SrcNone (OpNone << SrcShift) |
| 90 | #define SrcReg (OpReg << SrcShift) |
| 91 | #define SrcMem (OpMem << SrcShift) |
| 92 | #define SrcMem16 (OpMem16 << SrcShift) |
| 93 | #define SrcMem32 (OpMem32 << SrcShift) |
| 94 | #define SrcImm (OpImm << SrcShift) |
| 95 | #define SrcImmByte (OpImmByte << SrcShift) |
| 96 | #define SrcOne (OpOne << SrcShift) |
| 97 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 98 | #define SrcImmU (OpImmU << SrcShift) |
| 99 | #define SrcSI (OpSI << SrcShift) |
| 100 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 101 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 102 | #define SrcAcc (OpAcc << SrcShift) |
| 103 | #define SrcImmU16 (OpImmU16 << SrcShift) |
| 104 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 105 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 106 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 107 | #define BitOp (1<<11) |
| 108 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 109 | #define String (1<<13) /* String instruction (rep capable) */ |
| 110 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 111 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 112 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 113 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 114 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 115 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
| 116 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 117 | /* Generic ModRM decode. */ |
| 118 | #define ModRM (1<<19) |
| 119 | /* Destination is only written; never read. */ |
| 120 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 121 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 122 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 123 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 124 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 125 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 126 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 127 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 128 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 129 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 130 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 131 | /* Source 2 operand type */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 132 | #define Src2Shift (30) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 133 | #define Src2None (OpNone << Src2Shift) |
| 134 | #define Src2CL (OpCL << Src2Shift) |
| 135 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 136 | #define Src2One (OpOne << Src2Shift) |
| 137 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 138 | #define Src2ES (OpES << Src2Shift) |
| 139 | #define Src2CS (OpCS << Src2Shift) |
| 140 | #define Src2SS (OpSS << Src2Shift) |
| 141 | #define Src2DS (OpDS << Src2Shift) |
| 142 | #define Src2FS (OpFS << Src2Shift) |
| 143 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 144 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 145 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 146 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 147 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 148 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 149 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 150 | #define X2(x...) x, x |
| 151 | #define X3(x...) X2(x), x |
| 152 | #define X4(x...) X2(x), X2(x) |
| 153 | #define X5(x...) X4(x), x |
| 154 | #define X6(x...) X4(x), X2(x) |
| 155 | #define X7(x...) X4(x), X3(x) |
| 156 | #define X8(x...) X4(x), X4(x) |
| 157 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 158 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 159 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 160 | u64 flags : 56; |
| 161 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 162 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 163 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 164 | const struct opcode *group; |
| 165 | const struct group_dual *gdual; |
| 166 | const struct gprefix *gprefix; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 167 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 168 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | struct group_dual { |
| 172 | struct opcode mod012[8]; |
| 173 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 174 | }; |
| 175 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 176 | struct gprefix { |
| 177 | struct opcode pfx_no; |
| 178 | struct opcode pfx_66; |
| 179 | struct opcode pfx_f2; |
| 180 | struct opcode pfx_f3; |
| 181 | }; |
| 182 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 183 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 184 | #define EFLG_ID (1<<21) |
| 185 | #define EFLG_VIP (1<<20) |
| 186 | #define EFLG_VIF (1<<19) |
| 187 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 188 | #define EFLG_VM (1<<17) |
| 189 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 190 | #define EFLG_IOPL (3<<12) |
| 191 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 192 | #define EFLG_OF (1<<11) |
| 193 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 194 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 195 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 196 | #define EFLG_SF (1<<7) |
| 197 | #define EFLG_ZF (1<<6) |
| 198 | #define EFLG_AF (1<<4) |
| 199 | #define EFLG_PF (1<<2) |
| 200 | #define EFLG_CF (1<<0) |
| 201 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 202 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 203 | #define EFLG_RESERVED_ONE_MASK 2 |
| 204 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 205 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 206 | { |
| 207 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 208 | ctxt->regs_valid |= 1 << nr; |
| 209 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 210 | } |
| 211 | return ctxt->_regs[nr]; |
| 212 | } |
| 213 | |
| 214 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 215 | { |
| 216 | ctxt->regs_valid |= 1 << nr; |
| 217 | ctxt->regs_dirty |= 1 << nr; |
| 218 | return &ctxt->_regs[nr]; |
| 219 | } |
| 220 | |
| 221 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 222 | { |
| 223 | reg_read(ctxt, nr); |
| 224 | return reg_write(ctxt, nr); |
| 225 | } |
| 226 | |
| 227 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 228 | { |
| 229 | unsigned reg; |
| 230 | |
| 231 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 232 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 233 | } |
| 234 | |
| 235 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 236 | { |
| 237 | ctxt->regs_dirty = 0; |
| 238 | ctxt->regs_valid = 0; |
| 239 | } |
| 240 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 241 | /* |
| 242 | * Instruction emulation: |
| 243 | * Most instructions are emulated directly via a fragment of inline assembly |
| 244 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 245 | * any modified flags. |
| 246 | */ |
| 247 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 248 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 249 | #define _LO32 "k" /* force 32-bit operand */ |
| 250 | #define _STK "%%rsp" /* stack pointer */ |
| 251 | #elif defined(__i386__) |
| 252 | #define _LO32 "" /* force 32-bit operand */ |
| 253 | #define _STK "%%esp" /* stack pointer */ |
| 254 | #endif |
| 255 | |
| 256 | /* |
| 257 | * These EFLAGS bits are restored from saved value during emulation, and |
| 258 | * any changes are written back to the saved value after emulation. |
| 259 | */ |
| 260 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 261 | |
| 262 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 263 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 264 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 265 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 266 | "push %"_tmp"; " \ |
| 267 | "push %"_tmp"; " \ |
| 268 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 269 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 270 | "pushf; " \ |
| 271 | "notl %"_LO32 _tmp"; " \ |
| 272 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 273 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 274 | "pop %"_tmp"; " \ |
| 275 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 276 | "popf; " \ |
| 277 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 278 | |
| 279 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 280 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 281 | /* _sav |= EFLAGS & _msk; */ \ |
| 282 | "pushf; " \ |
| 283 | "pop %"_tmp"; " \ |
| 284 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 285 | "orl %"_LO32 _tmp",%"_sav"; " |
| 286 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 287 | #ifdef CONFIG_X86_64 |
| 288 | #define ON64(x) x |
| 289 | #else |
| 290 | #define ON64(x) |
| 291 | #endif |
| 292 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 293 | #define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 294 | do { \ |
| 295 | __asm__ __volatile__ ( \ |
| 296 | _PRE_EFLAGS("0", "4", "2") \ |
| 297 | _op _suffix " %"_x"3,%1; " \ |
| 298 | _POST_EFLAGS("0", "4", "2") \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 299 | : "=m" ((ctxt)->eflags), \ |
| 300 | "+q" (*(_dsttype*)&(ctxt)->dst.val), \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 301 | "=&r" (_tmp) \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 302 | : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 303 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 304 | |
| 305 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | /* Raw emulation: instruction has two explicit operands. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 307 | #define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 308 | do { \ |
| 309 | unsigned long _tmp; \ |
| 310 | \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 311 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 312 | case 2: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 313 | ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 314 | break; \ |
| 315 | case 4: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 316 | ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 317 | break; \ |
| 318 | case 8: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 319 | ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 320 | break; \ |
| 321 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 322 | } while (0) |
| 323 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 324 | #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] | 325 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 326 | unsigned long _tmp; \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 327 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 328 | case 1: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 329 | ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 330 | break; \ |
| 331 | default: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 332 | __emulate_2op_nobyte(ctxt, _op, \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 333 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 334 | break; \ |
| 335 | } \ |
| 336 | } while (0) |
| 337 | |
| 338 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 339 | #define emulate_2op_SrcB(ctxt, _op) \ |
| 340 | __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 341 | |
| 342 | /* Source operand is byte, word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 343 | #define emulate_2op_SrcV(ctxt, _op) \ |
| 344 | __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 345 | |
| 346 | /* Source operand is word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 347 | #define emulate_2op_SrcV_nobyte(ctxt, _op) \ |
| 348 | __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 349 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 350 | /* Instruction has three operands and one operand is stored in ECX register */ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 351 | #define __emulate_2op_cl(ctxt, _op, _suffix, _type) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 352 | do { \ |
| 353 | unsigned long _tmp; \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 354 | _type _clv = (ctxt)->src2.val; \ |
| 355 | _type _srcv = (ctxt)->src.val; \ |
| 356 | _type _dstv = (ctxt)->dst.val; \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 357 | \ |
| 358 | __asm__ __volatile__ ( \ |
| 359 | _PRE_EFLAGS("0", "5", "2") \ |
| 360 | _op _suffix " %4,%1 \n" \ |
| 361 | _POST_EFLAGS("0", "5", "2") \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 362 | : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 363 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 364 | ); \ |
| 365 | \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 366 | (ctxt)->src2.val = (unsigned long) _clv; \ |
| 367 | (ctxt)->src2.val = (unsigned long) _srcv; \ |
| 368 | (ctxt)->dst.val = (unsigned long) _dstv; \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 369 | } while (0) |
| 370 | |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 371 | #define emulate_2op_cl(ctxt, _op) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 372 | do { \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 373 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 374 | case 2: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 375 | __emulate_2op_cl(ctxt, _op, "w", u16); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 376 | break; \ |
| 377 | case 4: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 378 | __emulate_2op_cl(ctxt, _op, "l", u32); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 379 | break; \ |
| 380 | case 8: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 381 | ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 382 | break; \ |
| 383 | } \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 384 | } while (0) |
| 385 | |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 386 | #define __emulate_1op(ctxt, _op, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 387 | do { \ |
| 388 | unsigned long _tmp; \ |
| 389 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 390 | __asm__ __volatile__ ( \ |
| 391 | _PRE_EFLAGS("0", "3", "2") \ |
| 392 | _op _suffix " %1; " \ |
| 393 | _POST_EFLAGS("0", "3", "2") \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 394 | : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 395 | "=&r" (_tmp) \ |
| 396 | : "i" (EFLAGS_MASK)); \ |
| 397 | } while (0) |
| 398 | |
| 399 | /* Instruction has only one explicit operand (no source operand). */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 400 | #define emulate_1op(ctxt, _op) \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 401 | do { \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 402 | switch ((ctxt)->dst.bytes) { \ |
| 403 | case 1: __emulate_1op(ctxt, _op, "b"); break; \ |
| 404 | case 2: __emulate_1op(ctxt, _op, "w"); break; \ |
| 405 | case 4: __emulate_1op(ctxt, _op, "l"); break; \ |
| 406 | case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 407 | } \ |
| 408 | } while (0) |
| 409 | |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 410 | #define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 411 | do { \ |
| 412 | unsigned long _tmp; \ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 413 | ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \ |
| 414 | ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 415 | \ |
| 416 | __asm__ __volatile__ ( \ |
| 417 | _PRE_EFLAGS("0", "5", "1") \ |
| 418 | "1: \n\t" \ |
| 419 | _op _suffix " %6; " \ |
| 420 | "2: \n\t" \ |
| 421 | _POST_EFLAGS("0", "5", "1") \ |
| 422 | ".pushsection .fixup,\"ax\" \n\t" \ |
| 423 | "3: movb $1, %4 \n\t" \ |
| 424 | "jmp 2b \n\t" \ |
| 425 | ".popsection \n\t" \ |
| 426 | _ASM_EXTABLE(1b, 3b) \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 427 | : "=m" ((ctxt)->eflags), "=&r" (_tmp), \ |
| 428 | "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \ |
| 429 | : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \ |
| 430 | "a" (*rax), "d" (*rdx)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 431 | } while (0) |
| 432 | |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 433 | /* 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] | 434 | #define emulate_1op_rax_rdx(ctxt, _op, _ex) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 435 | do { \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 436 | switch((ctxt)->src.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 437 | case 1: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 438 | __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 439 | break; \ |
| 440 | case 2: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 441 | __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 442 | break; \ |
| 443 | case 4: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 444 | __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 445 | break; \ |
| 446 | case 8: ON64( \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 447 | __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 448 | break; \ |
| 449 | } \ |
| 450 | } while (0) |
| 451 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 452 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 453 | enum x86_intercept intercept, |
| 454 | enum x86_intercept_stage stage) |
| 455 | { |
| 456 | struct x86_instruction_info info = { |
| 457 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 458 | .rep_prefix = ctxt->rep_prefix, |
| 459 | .modrm_mod = ctxt->modrm_mod, |
| 460 | .modrm_reg = ctxt->modrm_reg, |
| 461 | .modrm_rm = ctxt->modrm_rm, |
| 462 | .src_val = ctxt->src.val64, |
| 463 | .src_bytes = ctxt->src.bytes, |
| 464 | .dst_bytes = ctxt->dst.bytes, |
| 465 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 466 | .next_rip = ctxt->eip, |
| 467 | }; |
| 468 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 469 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 472 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 473 | { |
| 474 | *dest = (*dest & ~mask) | (src & mask); |
| 475 | } |
| 476 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 477 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 478 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 479 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 482 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 483 | { |
| 484 | u16 sel; |
| 485 | struct desc_struct ss; |
| 486 | |
| 487 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 488 | return ~0UL; |
| 489 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 490 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 491 | } |
| 492 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 493 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 494 | { |
| 495 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 496 | } |
| 497 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 498 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 499 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 500 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 501 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 502 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 503 | return reg; |
| 504 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 505 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 509 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 510 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 511 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 514 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 515 | { |
| 516 | assign_masked(reg, *reg + inc, mask); |
| 517 | } |
| 518 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 519 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 520 | 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] | 521 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 522 | ulong mask; |
| 523 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 524 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 525 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 526 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 527 | mask = ad_mask(ctxt); |
| 528 | masked_increment(reg, mask, inc); |
| 529 | } |
| 530 | |
| 531 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 532 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 533 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 534 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 535 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 536 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 537 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 538 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 539 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 540 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 541 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 542 | { |
| 543 | u32 limit = get_desc_limit(desc); |
| 544 | |
| 545 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 546 | } |
| 547 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 548 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 549 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 550 | ctxt->has_seg_override = true; |
| 551 | ctxt->seg_override = seg; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 552 | } |
| 553 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 554 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 555 | { |
| 556 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 557 | return 0; |
| 558 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 559 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 560 | } |
| 561 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 562 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 563 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 564 | if (!ctxt->has_seg_override) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 565 | return 0; |
| 566 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 567 | return ctxt->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 568 | } |
| 569 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 570 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 571 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 572 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 573 | ctxt->exception.vector = vec; |
| 574 | ctxt->exception.error_code = error; |
| 575 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 576 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 577 | } |
| 578 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 579 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 580 | { |
| 581 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 582 | } |
| 583 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 584 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 585 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 586 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 587 | } |
| 588 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 589 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 590 | { |
| 591 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 592 | } |
| 593 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 594 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 595 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 596 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 597 | } |
| 598 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 599 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 600 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 601 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 602 | } |
| 603 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 604 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 605 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 606 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 607 | } |
| 608 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 609 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 610 | { |
| 611 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 612 | } |
| 613 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 614 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 615 | { |
| 616 | u16 selector; |
| 617 | struct desc_struct desc; |
| 618 | |
| 619 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 620 | return selector; |
| 621 | } |
| 622 | |
| 623 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 624 | unsigned seg) |
| 625 | { |
| 626 | u16 dummy; |
| 627 | u32 base3; |
| 628 | struct desc_struct desc; |
| 629 | |
| 630 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 631 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 632 | } |
| 633 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 634 | /* |
| 635 | * x86 defines three classes of vector instructions: explicitly |
| 636 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 637 | * depending on whether they're AVX encoded or not. |
| 638 | * |
| 639 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 640 | * subject to the same check. |
| 641 | */ |
| 642 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 643 | { |
| 644 | if (likely(size < 16)) |
| 645 | return false; |
| 646 | |
| 647 | if (ctxt->d & Aligned) |
| 648 | return true; |
| 649 | else if (ctxt->d & Unaligned) |
| 650 | return false; |
| 651 | else if (ctxt->d & Avx) |
| 652 | return false; |
| 653 | else |
| 654 | return true; |
| 655 | } |
| 656 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 657 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 658 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 659 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 660 | ulong *linear) |
| 661 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 662 | struct desc_struct desc; |
| 663 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 664 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 665 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 666 | u16 sel; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 667 | unsigned cpl, rpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 668 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 669 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 670 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 671 | case X86EMUL_MODE_PROT64: |
| 672 | if (((signed long)la << 16) >> 16 != la) |
| 673 | return emulate_gp(ctxt, 0); |
| 674 | break; |
| 675 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 676 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 677 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 678 | if (!usable) |
| 679 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame^] | 680 | /* code segment in protected mode or read-only data segment */ |
| 681 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 682 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 683 | goto bad; |
| 684 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 685 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 686 | goto bad; |
| 687 | lim = desc_limit_scaled(&desc); |
| 688 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 689 | /* expand-up segment */ |
| 690 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 691 | goto bad; |
| 692 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 693 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 694 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 695 | goto bad; |
| 696 | lim = desc.d ? 0xffffffff : 0xffff; |
| 697 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 698 | goto bad; |
| 699 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 700 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | a562518 | 2012-08-21 17:07:05 +0300 | [diff] [blame] | 701 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 702 | rpl = 0; |
| 703 | else |
| 704 | rpl = sel & 3; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 705 | cpl = max(cpl, rpl); |
| 706 | if (!(desc.type & 8)) { |
| 707 | /* data segment */ |
| 708 | if (cpl > desc.dpl) |
| 709 | goto bad; |
| 710 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 711 | /* nonconforming code segment */ |
| 712 | if (cpl != desc.dpl) |
| 713 | goto bad; |
| 714 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 715 | /* conforming code segment */ |
| 716 | if (cpl < desc.dpl) |
| 717 | goto bad; |
| 718 | } |
| 719 | break; |
| 720 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 721 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 722 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 723 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 724 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 725 | *linear = la; |
| 726 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 727 | bad: |
| 728 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 729 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 730 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 731 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 732 | } |
| 733 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 734 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 735 | struct segmented_address addr, |
| 736 | unsigned size, bool write, |
| 737 | ulong *linear) |
| 738 | { |
| 739 | return __linearize(ctxt, addr, size, write, false, linear); |
| 740 | } |
| 741 | |
| 742 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 743 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 744 | struct segmented_address addr, |
| 745 | void *data, |
| 746 | unsigned size) |
| 747 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 748 | int rc; |
| 749 | ulong linear; |
| 750 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 751 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 752 | if (rc != X86EMUL_CONTINUE) |
| 753 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 754 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 757 | /* |
| 758 | * Fetch the next byte of the instruction being emulated which is pointed to |
| 759 | * by ctxt->_eip, then increment ctxt->_eip. |
| 760 | * |
| 761 | * Also prefetch the remaining bytes of the instruction without crossing page |
| 762 | * boundary if they are not in fetch_cache yet. |
| 763 | */ |
| 764 | 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] | 765 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 766 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 767 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 768 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 769 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 770 | if (ctxt->_eip == fc->end) { |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 771 | unsigned long linear; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 772 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 773 | .ea = ctxt->_eip }; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 774 | cur_size = fc->end - fc->start; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 775 | size = min(15UL - cur_size, |
| 776 | PAGE_SIZE - offset_in_page(ctxt->_eip)); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 777 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 778 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 779 | return rc; |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 780 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 781 | size, &ctxt->exception); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 782 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 783 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 784 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 785 | } |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 786 | *dest = fc->data[ctxt->_eip - fc->start]; |
| 787 | ctxt->_eip++; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 788 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 792 | void *dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 793 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 794 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 795 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 796 | /* x86 instructions are limited to 15 bytes. */ |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 797 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 798 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 799 | while (size--) { |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 800 | rc = do_insn_fetch_byte(ctxt, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 801 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 802 | return rc; |
| 803 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 804 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 805 | } |
| 806 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 807 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 808 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 809 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 810 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 811 | if (rc != X86EMUL_CONTINUE) \ |
| 812 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 813 | (_type)_x; \ |
| 814 | }) |
| 815 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 816 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 817 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 818 | if (rc != X86EMUL_CONTINUE) \ |
| 819 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 820 | }) |
| 821 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 822 | /* |
| 823 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 824 | * pointer into the block that addresses the relevant register. |
| 825 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 826 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 827 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 828 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 829 | { |
| 830 | void *p; |
| 831 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 832 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 833 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 834 | else |
| 835 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 836 | return p; |
| 837 | } |
| 838 | |
| 839 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 840 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 841 | u16 *size, unsigned long *address, int op_bytes) |
| 842 | { |
| 843 | int rc; |
| 844 | |
| 845 | if (op_bytes == 2) |
| 846 | op_bytes = 3; |
| 847 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 848 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 849 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 850 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 851 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 852 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 853 | return rc; |
| 854 | } |
| 855 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 856 | static int test_cc(unsigned int condition, unsigned int flags) |
| 857 | { |
| 858 | int rc = 0; |
| 859 | |
| 860 | switch ((condition & 15) >> 1) { |
| 861 | case 0: /* o */ |
| 862 | rc |= (flags & EFLG_OF); |
| 863 | break; |
| 864 | case 1: /* b/c/nae */ |
| 865 | rc |= (flags & EFLG_CF); |
| 866 | break; |
| 867 | case 2: /* z/e */ |
| 868 | rc |= (flags & EFLG_ZF); |
| 869 | break; |
| 870 | case 3: /* be/na */ |
| 871 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 872 | break; |
| 873 | case 4: /* s */ |
| 874 | rc |= (flags & EFLG_SF); |
| 875 | break; |
| 876 | case 5: /* p/pe */ |
| 877 | rc |= (flags & EFLG_PF); |
| 878 | break; |
| 879 | case 7: /* le/ng */ |
| 880 | rc |= (flags & EFLG_ZF); |
| 881 | /* fall through */ |
| 882 | case 6: /* l/nge */ |
| 883 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 884 | break; |
| 885 | } |
| 886 | |
| 887 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 888 | return (!!rc ^ (condition & 1)); |
| 889 | } |
| 890 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 891 | static void fetch_register_operand(struct operand *op) |
| 892 | { |
| 893 | switch (op->bytes) { |
| 894 | case 1: |
| 895 | op->val = *(u8 *)op->addr.reg; |
| 896 | break; |
| 897 | case 2: |
| 898 | op->val = *(u16 *)op->addr.reg; |
| 899 | break; |
| 900 | case 4: |
| 901 | op->val = *(u32 *)op->addr.reg; |
| 902 | break; |
| 903 | case 8: |
| 904 | op->val = *(u64 *)op->addr.reg; |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 909 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 910 | { |
| 911 | ctxt->ops->get_fpu(ctxt); |
| 912 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 913 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 914 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 915 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 916 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 917 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 918 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 919 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 920 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 921 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 922 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 923 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 924 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 925 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 926 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 927 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 928 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 929 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 930 | #endif |
| 931 | default: BUG(); |
| 932 | } |
| 933 | ctxt->ops->put_fpu(ctxt); |
| 934 | } |
| 935 | |
| 936 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 937 | int reg) |
| 938 | { |
| 939 | ctxt->ops->get_fpu(ctxt); |
| 940 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 941 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 942 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 943 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 944 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 945 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 946 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 947 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 948 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 949 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 950 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 951 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 952 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 953 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 954 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 955 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 956 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 957 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 958 | #endif |
| 959 | default: BUG(); |
| 960 | } |
| 961 | ctxt->ops->put_fpu(ctxt); |
| 962 | } |
| 963 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 964 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 965 | { |
| 966 | ctxt->ops->get_fpu(ctxt); |
| 967 | switch (reg) { |
| 968 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 969 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 970 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 971 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 972 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 973 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 974 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 975 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 976 | default: BUG(); |
| 977 | } |
| 978 | ctxt->ops->put_fpu(ctxt); |
| 979 | } |
| 980 | |
| 981 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 982 | { |
| 983 | ctxt->ops->get_fpu(ctxt); |
| 984 | switch (reg) { |
| 985 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 986 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 987 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 988 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 989 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 990 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 991 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 992 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 993 | default: BUG(); |
| 994 | } |
| 995 | ctxt->ops->put_fpu(ctxt); |
| 996 | } |
| 997 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 998 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 999 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1000 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1001 | unsigned reg = ctxt->modrm_reg; |
| 1002 | int highbyte_regs = ctxt->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1003 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1004 | if (!(ctxt->d & ModRM)) |
| 1005 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1006 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1007 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1008 | op->type = OP_XMM; |
| 1009 | op->bytes = 16; |
| 1010 | op->addr.xmm = reg; |
| 1011 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1012 | return; |
| 1013 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1014 | if (ctxt->d & Mmx) { |
| 1015 | reg &= 7; |
| 1016 | op->type = OP_MM; |
| 1017 | op->bytes = 8; |
| 1018 | op->addr.mm = reg; |
| 1019 | return; |
| 1020 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1021 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1022 | op->type = OP_REG; |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1023 | if (ctxt->d & ByteOp) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1024 | op->addr.reg = decode_register(ctxt, reg, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1025 | op->bytes = 1; |
| 1026 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1027 | op->addr.reg = decode_register(ctxt, reg, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1028 | op->bytes = ctxt->op_bytes; |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1029 | } |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1030 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1031 | op->orig_val = op->val; |
| 1032 | } |
| 1033 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1034 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1035 | { |
| 1036 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1037 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1038 | } |
| 1039 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1040 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1041 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1042 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1043 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1044 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1045 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1046 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1047 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1048 | if (ctxt->rex_prefix) { |
| 1049 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ |
| 1050 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ |
| 1051 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1054 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
| 1055 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
| 1056 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); |
| 1057 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1058 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1059 | if (ctxt->modrm_mod == 3) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1060 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1061 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1062 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1063 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1064 | op->type = OP_XMM; |
| 1065 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1066 | op->addr.xmm = ctxt->modrm_rm; |
| 1067 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1068 | return rc; |
| 1069 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1070 | if (ctxt->d & Mmx) { |
| 1071 | op->type = OP_MM; |
| 1072 | op->bytes = 8; |
| 1073 | op->addr.xmm = ctxt->modrm_rm & 7; |
| 1074 | return rc; |
| 1075 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1076 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1077 | return rc; |
| 1078 | } |
| 1079 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1080 | op->type = OP_MEM; |
| 1081 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1082 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1083 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1084 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1085 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1086 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1087 | |
| 1088 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1089 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1090 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1091 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1092 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1095 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1096 | break; |
| 1097 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1098 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1099 | break; |
| 1100 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1101 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1102 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1103 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1104 | break; |
| 1105 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1106 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1107 | break; |
| 1108 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1109 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1110 | break; |
| 1111 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1112 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1113 | break; |
| 1114 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1115 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1118 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1119 | break; |
| 1120 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1121 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1122 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1123 | break; |
| 1124 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1125 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1128 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1129 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1130 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1131 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1132 | } else { |
| 1133 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1134 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1135 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1136 | index_reg |= (sib >> 3) & 7; |
| 1137 | base_reg |= sib & 7; |
| 1138 | scale = sib >> 6; |
| 1139 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1140 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1141 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1142 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1143 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1144 | adjust_modrm_seg(ctxt, base_reg); |
| 1145 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1146 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1147 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1148 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1149 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1150 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1151 | } else { |
| 1152 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1153 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1154 | adjust_modrm_seg(ctxt, base_reg); |
| 1155 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1156 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1157 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1158 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1159 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1160 | break; |
| 1161 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1162 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1163 | break; |
| 1164 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1165 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1166 | break; |
| 1167 | } |
| 1168 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1169 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1170 | done: |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
| 1174 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1175 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1176 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1177 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1178 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1179 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1180 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1181 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1182 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1183 | break; |
| 1184 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1185 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1188 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | done: |
| 1192 | return rc; |
| 1193 | } |
| 1194 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1195 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1196 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1197 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1198 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1199 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
| 1200 | mask = ~(ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1201 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1202 | if (ctxt->src.bytes == 2) |
| 1203 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1204 | else if (ctxt->src.bytes == 4) |
| 1205 | sv = (s32)ctxt->src.val & (s32)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1206 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1207 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1208 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1209 | |
| 1210 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1211 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1214 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1215 | unsigned long addr, void *dest, unsigned size) |
| 1216 | { |
| 1217 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1218 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1219 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1220 | if (mc->pos < mc->end) |
| 1221 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1222 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1223 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1224 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1225 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1226 | &ctxt->exception); |
| 1227 | if (rc != X86EMUL_CONTINUE) |
| 1228 | return rc; |
| 1229 | |
| 1230 | mc->end += size; |
| 1231 | |
| 1232 | read_cached: |
| 1233 | memcpy(dest, mc->data + mc->pos, size); |
| 1234 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1235 | return X86EMUL_CONTINUE; |
| 1236 | } |
| 1237 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1238 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1239 | struct segmented_address addr, |
| 1240 | void *data, |
| 1241 | unsigned size) |
| 1242 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1243 | int rc; |
| 1244 | ulong linear; |
| 1245 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1246 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1247 | if (rc != X86EMUL_CONTINUE) |
| 1248 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1249 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1253 | struct segmented_address addr, |
| 1254 | const void *data, |
| 1255 | unsigned size) |
| 1256 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1257 | int rc; |
| 1258 | ulong linear; |
| 1259 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1260 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1261 | if (rc != X86EMUL_CONTINUE) |
| 1262 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1263 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1264 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1268 | struct segmented_address addr, |
| 1269 | const void *orig_data, const void *data, |
| 1270 | unsigned size) |
| 1271 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1272 | int rc; |
| 1273 | ulong linear; |
| 1274 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1275 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1276 | if (rc != X86EMUL_CONTINUE) |
| 1277 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1278 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1279 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1282 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1283 | unsigned int size, unsigned short port, |
| 1284 | void *dest) |
| 1285 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1286 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1287 | |
| 1288 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1289 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1290 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1291 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1292 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1293 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1294 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1295 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1296 | count); |
| 1297 | if (n == 0) |
| 1298 | n = 1; |
| 1299 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1300 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1301 | return 0; |
| 1302 | rc->end = n * size; |
| 1303 | } |
| 1304 | |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1305 | if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) { |
| 1306 | ctxt->dst.data = rc->data + rc->pos; |
| 1307 | ctxt->dst.type = OP_MEM_STR; |
| 1308 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1309 | rc->pos = rc->end; |
| 1310 | } else { |
| 1311 | memcpy(dest, rc->data + rc->pos, size); |
| 1312 | rc->pos += size; |
| 1313 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1314 | return 1; |
| 1315 | } |
| 1316 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1317 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1318 | u16 index, struct desc_struct *desc) |
| 1319 | { |
| 1320 | struct desc_ptr dt; |
| 1321 | ulong addr; |
| 1322 | |
| 1323 | ctxt->ops->get_idt(ctxt, &dt); |
| 1324 | |
| 1325 | if (dt.size < index * 8 + 7) |
| 1326 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1327 | |
| 1328 | addr = dt.address + index * 8; |
| 1329 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1330 | &ctxt->exception); |
| 1331 | } |
| 1332 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1333 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1334 | u16 selector, struct desc_ptr *dt) |
| 1335 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1336 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1337 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1338 | if (selector & 1 << 2) { |
| 1339 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1340 | u16 sel; |
| 1341 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1342 | memset (dt, 0, sizeof *dt); |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1343 | if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1344 | return; |
| 1345 | |
| 1346 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1347 | dt->address = get_desc_base(&desc); |
| 1348 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1349 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* allowed just for 8 bytes segments */ |
| 1353 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1354 | u16 selector, struct desc_struct *desc, |
| 1355 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1356 | { |
| 1357 | struct desc_ptr dt; |
| 1358 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1359 | ulong addr; |
| 1360 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1361 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1362 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1363 | if (dt.size < index * 8 + 7) |
| 1364 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1365 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1366 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1367 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1368 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* allowed just for 8 bytes segments */ |
| 1372 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1373 | u16 selector, struct desc_struct *desc) |
| 1374 | { |
| 1375 | struct desc_ptr dt; |
| 1376 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1377 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1378 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1379 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1380 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1381 | if (dt.size < index * 8 + 7) |
| 1382 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1383 | |
| 1384 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1385 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1386 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1389 | /* Does not support long mode */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1390 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1391 | u16 selector, int seg) |
| 1392 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1393 | struct desc_struct seg_desc, old_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1394 | u8 dpl, rpl, cpl; |
| 1395 | unsigned err_vec = GP_VECTOR; |
| 1396 | u32 err_code = 0; |
| 1397 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1398 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1399 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1400 | u16 dummy; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1401 | |
| 1402 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1403 | |
| 1404 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1405 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1406 | /* set real mode segment descriptor */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1407 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1408 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1409 | goto load; |
| 1410 | } |
| 1411 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1412 | rpl = selector & 3; |
| 1413 | cpl = ctxt->ops->cpl(ctxt); |
| 1414 | |
| 1415 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1416 | if ((seg == VCPU_SREG_CS |
| 1417 | || (seg == VCPU_SREG_SS |
| 1418 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1419 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1420 | && null_selector) |
| 1421 | goto exception; |
| 1422 | |
| 1423 | /* TR should be in GDT only */ |
| 1424 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1425 | goto exception; |
| 1426 | |
| 1427 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1428 | goto load; |
| 1429 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1430 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1431 | if (ret != X86EMUL_CONTINUE) |
| 1432 | return ret; |
| 1433 | |
| 1434 | err_code = selector & 0xfffc; |
| 1435 | err_vec = GP_VECTOR; |
| 1436 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1437 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1438 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1439 | goto exception; |
| 1440 | |
| 1441 | if (!seg_desc.p) { |
| 1442 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1443 | goto exception; |
| 1444 | } |
| 1445 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1446 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1447 | |
| 1448 | switch (seg) { |
| 1449 | case VCPU_SREG_SS: |
| 1450 | /* |
| 1451 | * segment is not a writable data segment or segment |
| 1452 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1453 | */ |
| 1454 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1455 | goto exception; |
| 1456 | break; |
| 1457 | case VCPU_SREG_CS: |
| 1458 | if (!(seg_desc.type & 8)) |
| 1459 | goto exception; |
| 1460 | |
| 1461 | if (seg_desc.type & 4) { |
| 1462 | /* conforming */ |
| 1463 | if (dpl > cpl) |
| 1464 | goto exception; |
| 1465 | } else { |
| 1466 | /* nonconforming */ |
| 1467 | if (rpl > cpl || dpl != cpl) |
| 1468 | goto exception; |
| 1469 | } |
| 1470 | /* CS(RPL) <- CPL */ |
| 1471 | selector = (selector & 0xfffc) | cpl; |
| 1472 | break; |
| 1473 | case VCPU_SREG_TR: |
| 1474 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1475 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1476 | old_desc = seg_desc; |
| 1477 | seg_desc.type |= 2; /* busy */ |
| 1478 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1479 | sizeof(seg_desc), &ctxt->exception); |
| 1480 | if (ret != X86EMUL_CONTINUE) |
| 1481 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1482 | break; |
| 1483 | case VCPU_SREG_LDTR: |
| 1484 | if (seg_desc.s || seg_desc.type != 2) |
| 1485 | goto exception; |
| 1486 | break; |
| 1487 | default: /* DS, ES, FS, or GS */ |
| 1488 | /* |
| 1489 | * segment is not a data or readable code segment or |
| 1490 | * ((segment is a data or nonconforming code segment) |
| 1491 | * and (both RPL and CPL > DPL)) |
| 1492 | */ |
| 1493 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1494 | (((seg_desc.type & 0xc) != 0xc) && |
| 1495 | (rpl > dpl && cpl > dpl))) |
| 1496 | goto exception; |
| 1497 | break; |
| 1498 | } |
| 1499 | |
| 1500 | if (seg_desc.s) { |
| 1501 | /* mark segment as accessed */ |
| 1502 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1503 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1504 | if (ret != X86EMUL_CONTINUE) |
| 1505 | return ret; |
| 1506 | } |
| 1507 | load: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1508 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1509 | return X86EMUL_CONTINUE; |
| 1510 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1511 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1512 | return X86EMUL_PROPAGATE_FAULT; |
| 1513 | } |
| 1514 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1515 | static void write_register_operand(struct operand *op) |
| 1516 | { |
| 1517 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1518 | switch (op->bytes) { |
| 1519 | case 1: |
| 1520 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1521 | break; |
| 1522 | case 2: |
| 1523 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1524 | break; |
| 1525 | case 4: |
| 1526 | *op->addr.reg = (u32)op->val; |
| 1527 | break; /* 64b: zero-extend */ |
| 1528 | case 8: |
| 1529 | *op->addr.reg = op->val; |
| 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 1534 | static int writeback(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1535 | { |
| 1536 | int rc; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1537 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1538 | switch (ctxt->dst.type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1539 | case OP_REG: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1540 | write_register_operand(&ctxt->dst); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1541 | break; |
| 1542 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1543 | if (ctxt->lock_prefix) |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1544 | rc = segmented_cmpxchg(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1545 | ctxt->dst.addr.mem, |
| 1546 | &ctxt->dst.orig_val, |
| 1547 | &ctxt->dst.val, |
| 1548 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1549 | else |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1550 | rc = segmented_write(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1551 | ctxt->dst.addr.mem, |
| 1552 | &ctxt->dst.val, |
| 1553 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1554 | if (rc != X86EMUL_CONTINUE) |
| 1555 | return rc; |
| 1556 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1557 | case OP_MEM_STR: |
| 1558 | rc = segmented_write(ctxt, |
| 1559 | ctxt->dst.addr.mem, |
| 1560 | ctxt->dst.data, |
| 1561 | ctxt->dst.bytes * ctxt->dst.count); |
| 1562 | if (rc != X86EMUL_CONTINUE) |
| 1563 | return rc; |
| 1564 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1565 | case OP_XMM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1566 | write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1567 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1568 | case OP_MM: |
| 1569 | write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm); |
| 1570 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1571 | case OP_NONE: |
| 1572 | /* no writeback */ |
| 1573 | break; |
| 1574 | default: |
| 1575 | break; |
| 1576 | } |
| 1577 | return X86EMUL_CONTINUE; |
| 1578 | } |
| 1579 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1580 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1581 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1582 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1583 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1584 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1585 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1586 | addr.seg = VCPU_SREG_SS; |
| 1587 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1588 | return segmented_write(ctxt, addr, data, bytes); |
| 1589 | } |
| 1590 | |
| 1591 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1592 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1593 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1594 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1595 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1596 | } |
| 1597 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1598 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1599 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1600 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1601 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1602 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1603 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1604 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1605 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1606 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1607 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1608 | return rc; |
| 1609 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1610 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1611 | return rc; |
| 1612 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1613 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1614 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1615 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1616 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1617 | } |
| 1618 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1619 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1620 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1621 | { |
| 1622 | int rc; |
| 1623 | unsigned long val, change_mask; |
| 1624 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1625 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1626 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1627 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1628 | if (rc != X86EMUL_CONTINUE) |
| 1629 | return rc; |
| 1630 | |
| 1631 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1632 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1633 | |
| 1634 | switch(ctxt->mode) { |
| 1635 | case X86EMUL_MODE_PROT64: |
| 1636 | case X86EMUL_MODE_PROT32: |
| 1637 | case X86EMUL_MODE_PROT16: |
| 1638 | if (cpl == 0) |
| 1639 | change_mask |= EFLG_IOPL; |
| 1640 | if (cpl <= iopl) |
| 1641 | change_mask |= EFLG_IF; |
| 1642 | break; |
| 1643 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1644 | if (iopl < 3) |
| 1645 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1646 | change_mask |= EFLG_IF; |
| 1647 | break; |
| 1648 | default: /* real mode */ |
| 1649 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1650 | break; |
| 1651 | } |
| 1652 | |
| 1653 | *(unsigned long *)dest = |
| 1654 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1655 | |
| 1656 | return rc; |
| 1657 | } |
| 1658 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1659 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1660 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1661 | ctxt->dst.type = OP_REG; |
| 1662 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1663 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1664 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1665 | } |
| 1666 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1667 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1668 | { |
| 1669 | int rc; |
| 1670 | unsigned frame_size = ctxt->src.val; |
| 1671 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1672 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1673 | |
| 1674 | if (nesting_level) |
| 1675 | return X86EMUL_UNHANDLEABLE; |
| 1676 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1677 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1678 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1679 | if (rc != X86EMUL_CONTINUE) |
| 1680 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1681 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP), |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1682 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1683 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1684 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1685 | stack_mask(ctxt)); |
| 1686 | return X86EMUL_CONTINUE; |
| 1687 | } |
| 1688 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1689 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1690 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1691 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP), |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1692 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1693 | return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes); |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1694 | } |
| 1695 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1696 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1697 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1698 | int seg = ctxt->src2.val; |
| 1699 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1700 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1701 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1702 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1703 | } |
| 1704 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1705 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1706 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1707 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1708 | unsigned long selector; |
| 1709 | int rc; |
| 1710 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1711 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1712 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1713 | return rc; |
| 1714 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1715 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1716 | return rc; |
| 1717 | } |
| 1718 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1719 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1720 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1721 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1722 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1723 | int reg = VCPU_REGS_RAX; |
| 1724 | |
| 1725 | while (reg <= VCPU_REGS_RDI) { |
| 1726 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1727 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1728 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1729 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1730 | if (rc != X86EMUL_CONTINUE) |
| 1731 | return rc; |
| 1732 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1733 | ++reg; |
| 1734 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1735 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1736 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1739 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1740 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1741 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1742 | return em_push(ctxt); |
| 1743 | } |
| 1744 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1745 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1746 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1747 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1748 | int reg = VCPU_REGS_RDI; |
| 1749 | |
| 1750 | while (reg >= VCPU_REGS_RAX) { |
| 1751 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1752 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1753 | --reg; |
| 1754 | } |
| 1755 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1756 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1757 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1758 | break; |
| 1759 | --reg; |
| 1760 | } |
| 1761 | return rc; |
| 1762 | } |
| 1763 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1764 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1765 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1766 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1767 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1768 | struct desc_ptr dt; |
| 1769 | gva_t cs_addr; |
| 1770 | gva_t eip_addr; |
| 1771 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1772 | |
| 1773 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1774 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1775 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1776 | if (rc != X86EMUL_CONTINUE) |
| 1777 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1778 | |
| 1779 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1780 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1781 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1782 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1783 | if (rc != X86EMUL_CONTINUE) |
| 1784 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1785 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1786 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1787 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1788 | if (rc != X86EMUL_CONTINUE) |
| 1789 | return rc; |
| 1790 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1791 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1792 | |
| 1793 | eip_addr = dt.address + (irq << 2); |
| 1794 | cs_addr = dt.address + (irq << 2) + 2; |
| 1795 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1796 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1797 | if (rc != X86EMUL_CONTINUE) |
| 1798 | return rc; |
| 1799 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1800 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1801 | if (rc != X86EMUL_CONTINUE) |
| 1802 | return rc; |
| 1803 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1804 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1805 | if (rc != X86EMUL_CONTINUE) |
| 1806 | return rc; |
| 1807 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1808 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1809 | |
| 1810 | return rc; |
| 1811 | } |
| 1812 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1813 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1814 | { |
| 1815 | int rc; |
| 1816 | |
| 1817 | invalidate_registers(ctxt); |
| 1818 | rc = __emulate_int_real(ctxt, irq); |
| 1819 | if (rc == X86EMUL_CONTINUE) |
| 1820 | writeback_registers(ctxt); |
| 1821 | return rc; |
| 1822 | } |
| 1823 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1824 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1825 | { |
| 1826 | switch(ctxt->mode) { |
| 1827 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1828 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1829 | case X86EMUL_MODE_VM86: |
| 1830 | case X86EMUL_MODE_PROT16: |
| 1831 | case X86EMUL_MODE_PROT32: |
| 1832 | case X86EMUL_MODE_PROT64: |
| 1833 | default: |
| 1834 | /* Protected mode interrupts unimplemented yet */ |
| 1835 | return X86EMUL_UNHANDLEABLE; |
| 1836 | } |
| 1837 | } |
| 1838 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1839 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1840 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1841 | int rc = X86EMUL_CONTINUE; |
| 1842 | unsigned long temp_eip = 0; |
| 1843 | unsigned long temp_eflags = 0; |
| 1844 | unsigned long cs = 0; |
| 1845 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1846 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1847 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1848 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1849 | |
| 1850 | /* TODO: Add stack limit check */ |
| 1851 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1852 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1853 | |
| 1854 | if (rc != X86EMUL_CONTINUE) |
| 1855 | return rc; |
| 1856 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1857 | if (temp_eip & ~0xffff) |
| 1858 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1859 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1860 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1861 | |
| 1862 | if (rc != X86EMUL_CONTINUE) |
| 1863 | return rc; |
| 1864 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1865 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1866 | |
| 1867 | if (rc != X86EMUL_CONTINUE) |
| 1868 | return rc; |
| 1869 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1870 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1871 | |
| 1872 | if (rc != X86EMUL_CONTINUE) |
| 1873 | return rc; |
| 1874 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1875 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1876 | |
| 1877 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1878 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1879 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1880 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1881 | ctxt->eflags &= ~0xffff; |
| 1882 | ctxt->eflags |= temp_eflags; |
| 1883 | } |
| 1884 | |
| 1885 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1886 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1887 | |
| 1888 | return rc; |
| 1889 | } |
| 1890 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 1891 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1892 | { |
| 1893 | switch(ctxt->mode) { |
| 1894 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1895 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1896 | case X86EMUL_MODE_VM86: |
| 1897 | case X86EMUL_MODE_PROT16: |
| 1898 | case X86EMUL_MODE_PROT32: |
| 1899 | case X86EMUL_MODE_PROT64: |
| 1900 | default: |
| 1901 | /* iret from protected mode unimplemented yet */ |
| 1902 | return X86EMUL_UNHANDLEABLE; |
| 1903 | } |
| 1904 | } |
| 1905 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1906 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 1907 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1908 | int rc; |
| 1909 | unsigned short sel; |
| 1910 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1911 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1912 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1913 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1914 | if (rc != X86EMUL_CONTINUE) |
| 1915 | return rc; |
| 1916 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1917 | ctxt->_eip = 0; |
| 1918 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1919 | return X86EMUL_CONTINUE; |
| 1920 | } |
| 1921 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1922 | static int em_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1923 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1924 | switch (ctxt->modrm_reg) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1925 | case 0: /* rol */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1926 | emulate_2op_SrcB(ctxt, "rol"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1927 | break; |
| 1928 | case 1: /* ror */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1929 | emulate_2op_SrcB(ctxt, "ror"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1930 | break; |
| 1931 | case 2: /* rcl */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1932 | emulate_2op_SrcB(ctxt, "rcl"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1933 | break; |
| 1934 | case 3: /* rcr */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1935 | emulate_2op_SrcB(ctxt, "rcr"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1936 | break; |
| 1937 | case 4: /* sal/shl */ |
| 1938 | case 6: /* sal/shl */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1939 | emulate_2op_SrcB(ctxt, "sal"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1940 | break; |
| 1941 | case 5: /* shr */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1942 | emulate_2op_SrcB(ctxt, "shr"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1943 | break; |
| 1944 | case 7: /* sar */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 1945 | emulate_2op_SrcB(ctxt, "sar"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1946 | break; |
| 1947 | } |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1948 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1949 | } |
| 1950 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 1951 | static int em_not(struct x86_emulate_ctxt *ctxt) |
| 1952 | { |
| 1953 | ctxt->dst.val = ~ctxt->dst.val; |
| 1954 | return X86EMUL_CONTINUE; |
| 1955 | } |
| 1956 | |
| 1957 | static int em_neg(struct x86_emulate_ctxt *ctxt) |
| 1958 | { |
| 1959 | emulate_1op(ctxt, "neg"); |
| 1960 | return X86EMUL_CONTINUE; |
| 1961 | } |
| 1962 | |
| 1963 | static int em_mul_ex(struct x86_emulate_ctxt *ctxt) |
| 1964 | { |
| 1965 | u8 ex = 0; |
| 1966 | |
| 1967 | emulate_1op_rax_rdx(ctxt, "mul", ex); |
| 1968 | return X86EMUL_CONTINUE; |
| 1969 | } |
| 1970 | |
| 1971 | static int em_imul_ex(struct x86_emulate_ctxt *ctxt) |
| 1972 | { |
| 1973 | u8 ex = 0; |
| 1974 | |
| 1975 | emulate_1op_rax_rdx(ctxt, "imul", ex); |
| 1976 | return X86EMUL_CONTINUE; |
| 1977 | } |
| 1978 | |
| 1979 | static int em_div_ex(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1980 | { |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1981 | u8 de = 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1982 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 1983 | emulate_1op_rax_rdx(ctxt, "div", de); |
| 1984 | if (de) |
| 1985 | return emulate_de(ctxt); |
| 1986 | return X86EMUL_CONTINUE; |
| 1987 | } |
| 1988 | |
| 1989 | static int em_idiv_ex(struct x86_emulate_ctxt *ctxt) |
| 1990 | { |
| 1991 | u8 de = 0; |
| 1992 | |
| 1993 | emulate_1op_rax_rdx(ctxt, "idiv", de); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 1994 | if (de) |
| 1995 | return emulate_de(ctxt); |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 1996 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1999 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2000 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2001 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2002 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2003 | switch (ctxt->modrm_reg) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2004 | case 0: /* inc */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 2005 | emulate_1op(ctxt, "inc"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2006 | break; |
| 2007 | case 1: /* dec */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 2008 | emulate_1op(ctxt, "dec"); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2009 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2010 | case 2: /* call near abs */ { |
| 2011 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2012 | old_eip = ctxt->_eip; |
| 2013 | ctxt->_eip = ctxt->src.val; |
| 2014 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2015 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2016 | break; |
| 2017 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2018 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2019 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2020 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2021 | case 5: /* jmp far */ |
| 2022 | rc = em_jmp_far(ctxt); |
| 2023 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2024 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2025 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2026 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2027 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2028 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2031 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2032 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2033 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2034 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2035 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2036 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2037 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2038 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2039 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2040 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2041 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2042 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2043 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2044 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2045 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2046 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2047 | } |
| 2048 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2049 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2050 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2051 | ctxt->dst.type = OP_REG; |
| 2052 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2053 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2054 | return em_pop(ctxt); |
| 2055 | } |
| 2056 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2057 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2058 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2059 | int rc; |
| 2060 | unsigned long cs; |
| 2061 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2062 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2063 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2064 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2065 | if (ctxt->op_bytes == 4) |
| 2066 | ctxt->_eip = (u32)ctxt->_eip; |
| 2067 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2068 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2069 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2070 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2071 | return rc; |
| 2072 | } |
| 2073 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2074 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2075 | { |
| 2076 | /* Save real source value, then compare EAX against destination. */ |
| 2077 | ctxt->src.orig_val = ctxt->src.val; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2078 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2079 | emulate_2op_SrcV(ctxt, "cmp"); |
| 2080 | |
| 2081 | if (ctxt->eflags & EFLG_ZF) { |
| 2082 | /* Success: write back to memory. */ |
| 2083 | ctxt->dst.val = ctxt->src.orig_val; |
| 2084 | } else { |
| 2085 | /* Failure: write the value we saw to EAX. */ |
| 2086 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2087 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2088 | } |
| 2089 | return X86EMUL_CONTINUE; |
| 2090 | } |
| 2091 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2092 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2093 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2094 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2095 | unsigned short sel; |
| 2096 | int rc; |
| 2097 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2098 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2099 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2100 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2101 | if (rc != X86EMUL_CONTINUE) |
| 2102 | return rc; |
| 2103 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2104 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2105 | return rc; |
| 2106 | } |
| 2107 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2108 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2109 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2110 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2111 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2112 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2113 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2114 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2115 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2116 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2117 | cs->s = 1; |
| 2118 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2119 | cs->p = 1; |
| 2120 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2121 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2122 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2123 | set_desc_base(ss, 0); /* flat segment */ |
| 2124 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2125 | ss->g = 1; /* 4kb granularity */ |
| 2126 | ss->s = 1; |
| 2127 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2128 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2129 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2130 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2131 | ss->l = 0; |
| 2132 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2133 | } |
| 2134 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2135 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2136 | { |
| 2137 | u32 eax, ebx, ecx, edx; |
| 2138 | |
| 2139 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2140 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2141 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2142 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2143 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2144 | } |
| 2145 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2146 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2147 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2148 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2149 | u32 eax, ebx, ecx, edx; |
| 2150 | |
| 2151 | /* |
| 2152 | * syscall should always be enabled in longmode - so only become |
| 2153 | * vendor specific (cpuid) if other modes are active... |
| 2154 | */ |
| 2155 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2156 | return true; |
| 2157 | |
| 2158 | eax = 0x00000000; |
| 2159 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2160 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2161 | /* |
| 2162 | * Intel ("GenuineIntel") |
| 2163 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2164 | * longmode. Also an 64bit guest with a |
| 2165 | * 32bit compat-app running will #UD !! While this |
| 2166 | * behaviour can be fixed (by emulating) into AMD |
| 2167 | * response - CPUs of AMD can't behave like Intel. |
| 2168 | */ |
| 2169 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2170 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2171 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2172 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2173 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2174 | /* AMD ("AuthenticAMD") */ |
| 2175 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2176 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2177 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2178 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2179 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2180 | /* AMD ("AMDisbetter!") */ |
| 2181 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2182 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2183 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2184 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2185 | |
| 2186 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2187 | return false; |
| 2188 | } |
| 2189 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2190 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2191 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2192 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2193 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2194 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2195 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2196 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2197 | |
| 2198 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2199 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2200 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2201 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2202 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2203 | if (!(em_syscall_is_enabled(ctxt))) |
| 2204 | return emulate_ud(ctxt); |
| 2205 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2206 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2207 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2208 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2209 | if (!(efer & EFER_SCE)) |
| 2210 | return emulate_ud(ctxt); |
| 2211 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2212 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2213 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2214 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2215 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2216 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2217 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2218 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2219 | cs.l = 1; |
| 2220 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2221 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2222 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2223 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2224 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2225 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2226 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2227 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2228 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2229 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2230 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2231 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2232 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2233 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2234 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2235 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2236 | #endif |
| 2237 | } else { |
| 2238 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2239 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2240 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2241 | |
| 2242 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2243 | } |
| 2244 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2245 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2246 | } |
| 2247 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2248 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2249 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2250 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2251 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2252 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2253 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2254 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2255 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2256 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2257 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2258 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2259 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2260 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2261 | /* |
| 2262 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2263 | * mode). |
| 2264 | */ |
| 2265 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2266 | && !vendor_intel(ctxt)) |
| 2267 | return emulate_ud(ctxt); |
| 2268 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2269 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2270 | * Therefore, we inject an #UD. |
| 2271 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2272 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2273 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2274 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2275 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2276 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2277 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2278 | switch (ctxt->mode) { |
| 2279 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2280 | if ((msr_data & 0xfffc) == 0x0) |
| 2281 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2282 | break; |
| 2283 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2284 | if (msr_data == 0x0) |
| 2285 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2286 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2287 | default: |
| 2288 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2292 | cs_sel = (u16)msr_data; |
| 2293 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2294 | ss_sel = cs_sel + 8; |
| 2295 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2296 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2297 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2298 | cs.l = 1; |
| 2299 | } |
| 2300 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2301 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2302 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2303 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2304 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2305 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2306 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2307 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2308 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2309 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2310 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2313 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2314 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2315 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2316 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2317 | u64 msr_data; |
| 2318 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2319 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2320 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2321 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2322 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2323 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2324 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2325 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2326 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2327 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2328 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2329 | usermode = X86EMUL_MODE_PROT64; |
| 2330 | else |
| 2331 | usermode = X86EMUL_MODE_PROT32; |
| 2332 | |
| 2333 | cs.dpl = 3; |
| 2334 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2335 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2336 | switch (usermode) { |
| 2337 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2338 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2339 | if ((msr_data & 0xfffc) == 0x0) |
| 2340 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2341 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2342 | break; |
| 2343 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2344 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2345 | if (msr_data == 0x0) |
| 2346 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2347 | ss_sel = cs_sel + 8; |
| 2348 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2349 | cs.l = 1; |
| 2350 | break; |
| 2351 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2352 | cs_sel |= SELECTOR_RPL_MASK; |
| 2353 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2354 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2355 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2356 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2357 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2358 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2359 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2360 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2361 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2364 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2365 | { |
| 2366 | int iopl; |
| 2367 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2368 | return false; |
| 2369 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2370 | return true; |
| 2371 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2372 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2376 | u16 port, u16 len) |
| 2377 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2378 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2379 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2380 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2381 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2382 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2383 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2384 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2385 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2386 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2387 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2388 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2389 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2390 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2391 | base = get_desc_base(&tr_seg); |
| 2392 | #ifdef CONFIG_X86_64 |
| 2393 | base |= ((u64)base3) << 32; |
| 2394 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2395 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2396 | if (r != X86EMUL_CONTINUE) |
| 2397 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2398 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2399 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2400 | 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] | 2401 | if (r != X86EMUL_CONTINUE) |
| 2402 | return false; |
| 2403 | if ((perm >> bit_idx) & mask) |
| 2404 | return false; |
| 2405 | return true; |
| 2406 | } |
| 2407 | |
| 2408 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2409 | u16 port, u16 len) |
| 2410 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2411 | if (ctxt->perm_ok) |
| 2412 | return true; |
| 2413 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2414 | if (emulator_bad_iopl(ctxt)) |
| 2415 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2416 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2417 | |
| 2418 | ctxt->perm_ok = true; |
| 2419 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2420 | return true; |
| 2421 | } |
| 2422 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2423 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2424 | struct tss_segment_16 *tss) |
| 2425 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2426 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2427 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2428 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2429 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2430 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2431 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2432 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2433 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2434 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2435 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2436 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2437 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2438 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2439 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2440 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2441 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2445 | struct tss_segment_16 *tss) |
| 2446 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2447 | int ret; |
| 2448 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2449 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2450 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2451 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2452 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2453 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2454 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2455 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2456 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2457 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2458 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2459 | |
| 2460 | /* |
| 2461 | * SDM says that segment selectors are loaded before segment |
| 2462 | * descriptors |
| 2463 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2464 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2465 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2466 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2467 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2468 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2469 | |
| 2470 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2471 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2472 | * it is handled in a context of new task |
| 2473 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2474 | ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2475 | if (ret != X86EMUL_CONTINUE) |
| 2476 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2477 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2478 | if (ret != X86EMUL_CONTINUE) |
| 2479 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2480 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2481 | if (ret != X86EMUL_CONTINUE) |
| 2482 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2483 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2484 | if (ret != X86EMUL_CONTINUE) |
| 2485 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2486 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2487 | if (ret != X86EMUL_CONTINUE) |
| 2488 | return ret; |
| 2489 | |
| 2490 | return X86EMUL_CONTINUE; |
| 2491 | } |
| 2492 | |
| 2493 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2494 | u16 tss_selector, u16 old_tss_sel, |
| 2495 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2496 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2497 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2498 | struct tss_segment_16 tss_seg; |
| 2499 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2500 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2501 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2502 | 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] | 2503 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2504 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2505 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2506 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2507 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2508 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2509 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2510 | 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] | 2511 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2512 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2513 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2514 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2515 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2516 | 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] | 2517 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2518 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2519 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2520 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2521 | |
| 2522 | if (old_tss_sel != 0xffff) { |
| 2523 | tss_seg.prev_task_link = old_tss_sel; |
| 2524 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2525 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2526 | &tss_seg.prev_task_link, |
| 2527 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2528 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2529 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2530 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2531 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2534 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2538 | struct tss_segment_32 *tss) |
| 2539 | { |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2540 | tss->cr3 = ctxt->ops->get_cr(ctxt, 3); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2541 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2542 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2543 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2544 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2545 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2546 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2547 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2548 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2549 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2550 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2551 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2552 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2553 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2554 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2555 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2556 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2557 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
| 2558 | tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2562 | struct tss_segment_32 *tss) |
| 2563 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2564 | int ret; |
| 2565 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2566 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2567 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2568 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2569 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2570 | |
| 2571 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2572 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2573 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2574 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2575 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2576 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2577 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2578 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2579 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2580 | |
| 2581 | /* |
| 2582 | * SDM says that segment selectors are loaded before segment |
| 2583 | * descriptors |
| 2584 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2585 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2586 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2587 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2588 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2589 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2590 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2591 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2592 | |
| 2593 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2594 | * If we're switching between Protected Mode and VM86, we need to make |
| 2595 | * sure to update the mode before loading the segment descriptors so |
| 2596 | * that the selectors are interpreted correctly. |
| 2597 | * |
| 2598 | * Need to get rflags to the vcpu struct immediately because it |
| 2599 | * influences the CPL which is checked at least when loading the segment |
| 2600 | * descriptors and when pushing an error code to the new kernel stack. |
| 2601 | * |
| 2602 | * TODO Introduce a separate ctxt->ops->set_cpl callback |
| 2603 | */ |
| 2604 | if (ctxt->eflags & X86_EFLAGS_VM) |
| 2605 | ctxt->mode = X86EMUL_MODE_VM86; |
| 2606 | else |
| 2607 | ctxt->mode = X86EMUL_MODE_PROT32; |
| 2608 | |
| 2609 | ctxt->ops->set_rflags(ctxt, ctxt->eflags); |
| 2610 | |
| 2611 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2612 | * Now load segment descriptors. If fault happenes at this stage |
| 2613 | * it is handled in a context of new task |
| 2614 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2615 | ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2616 | if (ret != X86EMUL_CONTINUE) |
| 2617 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2618 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2619 | if (ret != X86EMUL_CONTINUE) |
| 2620 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2621 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2622 | if (ret != X86EMUL_CONTINUE) |
| 2623 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2624 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2625 | if (ret != X86EMUL_CONTINUE) |
| 2626 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2627 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2628 | if (ret != X86EMUL_CONTINUE) |
| 2629 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2630 | ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | if (ret != X86EMUL_CONTINUE) |
| 2632 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2633 | ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2634 | if (ret != X86EMUL_CONTINUE) |
| 2635 | return ret; |
| 2636 | |
| 2637 | return X86EMUL_CONTINUE; |
| 2638 | } |
| 2639 | |
| 2640 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2641 | u16 tss_selector, u16 old_tss_sel, |
| 2642 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2643 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2644 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2645 | struct tss_segment_32 tss_seg; |
| 2646 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2647 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2648 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2649 | 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] | 2650 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2651 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2652 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2653 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2654 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2655 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2656 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2657 | 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] | 2658 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2659 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2660 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2661 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2662 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2663 | 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] | 2664 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2665 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2667 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2668 | |
| 2669 | if (old_tss_sel != 0xffff) { |
| 2670 | tss_seg.prev_task_link = old_tss_sel; |
| 2671 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2672 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2673 | &tss_seg.prev_task_link, |
| 2674 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2675 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2676 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2677 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2678 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2679 | } |
| 2680 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2681 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2685 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2686 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2687 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2688 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2689 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2690 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2691 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2692 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2693 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2694 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2695 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2696 | |
| 2697 | /* FIXME: old_tss_base == ~0 ? */ |
| 2698 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2699 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2700 | if (ret != X86EMUL_CONTINUE) |
| 2701 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2702 | ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2703 | if (ret != X86EMUL_CONTINUE) |
| 2704 | return ret; |
| 2705 | |
| 2706 | /* FIXME: check that next_tss_desc is tss */ |
| 2707 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2708 | /* |
| 2709 | * Check privileges. The three cases are task switch caused by... |
| 2710 | * |
| 2711 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2712 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2713 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2714 | */ |
| 2715 | if (reason == TASK_SWITCH_GATE) { |
| 2716 | if (idt_index != -1) { |
| 2717 | /* Software interrupts */ |
| 2718 | struct desc_struct task_gate_desc; |
| 2719 | int dpl; |
| 2720 | |
| 2721 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2722 | &task_gate_desc); |
| 2723 | if (ret != X86EMUL_CONTINUE) |
| 2724 | return ret; |
| 2725 | |
| 2726 | dpl = task_gate_desc.dpl; |
| 2727 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2728 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2729 | } |
| 2730 | } else if (reason != TASK_SWITCH_IRET) { |
| 2731 | int dpl = next_tss_desc.dpl; |
| 2732 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2733 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2736 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2737 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2738 | if (!next_tss_desc.p || |
| 2739 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2740 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2741 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2742 | return X86EMUL_PROPAGATE_FAULT; |
| 2743 | } |
| 2744 | |
| 2745 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2746 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2747 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | if (reason == TASK_SWITCH_IRET) |
| 2751 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2752 | |
| 2753 | /* set back link to prev task only if NT bit is set in eflags |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2754 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2755 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2756 | old_tss_sel = 0xffff; |
| 2757 | |
| 2758 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2759 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2760 | old_tss_base, &next_tss_desc); |
| 2761 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2762 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2763 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2764 | if (ret != X86EMUL_CONTINUE) |
| 2765 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2766 | |
| 2767 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2768 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2769 | |
| 2770 | if (reason != TASK_SWITCH_IRET) { |
| 2771 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2772 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2773 | } |
| 2774 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2775 | 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] | 2776 | 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] | 2777 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2778 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2779 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2780 | ctxt->lock_prefix = 0; |
| 2781 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2782 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2785 | return ret; |
| 2786 | } |
| 2787 | |
| 2788 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2789 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2790 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2791 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2792 | int rc; |
| 2793 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2794 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2795 | ctxt->_eip = ctxt->eip; |
| 2796 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2797 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2798 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2799 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2800 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2801 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2802 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2803 | writeback_registers(ctxt); |
| 2804 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2805 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2806 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2809 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2810 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2811 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2812 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2813 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2814 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2815 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2816 | } |
| 2817 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2818 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2819 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2820 | u8 al, old_al; |
| 2821 | bool af, cf, old_cf; |
| 2822 | |
| 2823 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2824 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2825 | |
| 2826 | old_al = al; |
| 2827 | old_cf = cf; |
| 2828 | cf = false; |
| 2829 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2830 | if ((al & 0x0f) > 9 || af) { |
| 2831 | al -= 6; |
| 2832 | cf = old_cf | (al >= 250); |
| 2833 | af = true; |
| 2834 | } else { |
| 2835 | af = false; |
| 2836 | } |
| 2837 | if (old_al > 0x99 || old_cf) { |
| 2838 | al -= 0x60; |
| 2839 | cf = true; |
| 2840 | } |
| 2841 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2842 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2843 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2844 | ctxt->src.type = OP_IMM; |
| 2845 | ctxt->src.val = 0; |
| 2846 | ctxt->src.bytes = 1; |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2847 | emulate_2op_SrcV(ctxt, "or"); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2848 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2849 | if (cf) |
| 2850 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2851 | if (af) |
| 2852 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2853 | return X86EMUL_CONTINUE; |
| 2854 | } |
| 2855 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 2856 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 2857 | { |
| 2858 | long rel = ctxt->src.val; |
| 2859 | |
| 2860 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 2861 | jmp_rel(ctxt, rel); |
| 2862 | return em_push(ctxt); |
| 2863 | } |
| 2864 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2865 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2866 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2867 | u16 sel, old_cs; |
| 2868 | ulong old_eip; |
| 2869 | int rc; |
| 2870 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2871 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2872 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2873 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2874 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2875 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2876 | return X86EMUL_CONTINUE; |
| 2877 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2878 | ctxt->_eip = 0; |
| 2879 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2880 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2881 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2882 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2883 | if (rc != X86EMUL_CONTINUE) |
| 2884 | return rc; |
| 2885 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2886 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2887 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2888 | } |
| 2889 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2890 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2891 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2892 | int rc; |
| 2893 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2894 | ctxt->dst.type = OP_REG; |
| 2895 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2896 | ctxt->dst.bytes = ctxt->op_bytes; |
| 2897 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2898 | if (rc != X86EMUL_CONTINUE) |
| 2899 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 2900 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2901 | return X86EMUL_CONTINUE; |
| 2902 | } |
| 2903 | |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2904 | static int em_add(struct x86_emulate_ctxt *ctxt) |
| 2905 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2906 | emulate_2op_SrcV(ctxt, "add"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2907 | return X86EMUL_CONTINUE; |
| 2908 | } |
| 2909 | |
| 2910 | static int em_or(struct x86_emulate_ctxt *ctxt) |
| 2911 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2912 | emulate_2op_SrcV(ctxt, "or"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2913 | return X86EMUL_CONTINUE; |
| 2914 | } |
| 2915 | |
| 2916 | static int em_adc(struct x86_emulate_ctxt *ctxt) |
| 2917 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2918 | emulate_2op_SrcV(ctxt, "adc"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2919 | return X86EMUL_CONTINUE; |
| 2920 | } |
| 2921 | |
| 2922 | static int em_sbb(struct x86_emulate_ctxt *ctxt) |
| 2923 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2924 | emulate_2op_SrcV(ctxt, "sbb"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2925 | return X86EMUL_CONTINUE; |
| 2926 | } |
| 2927 | |
| 2928 | static int em_and(struct x86_emulate_ctxt *ctxt) |
| 2929 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2930 | emulate_2op_SrcV(ctxt, "and"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2931 | return X86EMUL_CONTINUE; |
| 2932 | } |
| 2933 | |
| 2934 | static int em_sub(struct x86_emulate_ctxt *ctxt) |
| 2935 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2936 | emulate_2op_SrcV(ctxt, "sub"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2937 | return X86EMUL_CONTINUE; |
| 2938 | } |
| 2939 | |
| 2940 | static int em_xor(struct x86_emulate_ctxt *ctxt) |
| 2941 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2942 | emulate_2op_SrcV(ctxt, "xor"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2943 | return X86EMUL_CONTINUE; |
| 2944 | } |
| 2945 | |
| 2946 | static int em_cmp(struct x86_emulate_ctxt *ctxt) |
| 2947 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2948 | emulate_2op_SrcV(ctxt, "cmp"); |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2949 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2950 | ctxt->dst.type = OP_NONE; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 2951 | return X86EMUL_CONTINUE; |
| 2952 | } |
| 2953 | |
Takuya Yoshikawa | 9f21ca5 | 2011-05-29 21:57:53 +0900 | [diff] [blame] | 2954 | static int em_test(struct x86_emulate_ctxt *ctxt) |
| 2955 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2956 | emulate_2op_SrcV(ctxt, "test"); |
Avi Kivity | caa8a16 | 2011-09-11 11:23:02 +0300 | [diff] [blame] | 2957 | /* Disable writeback. */ |
| 2958 | ctxt->dst.type = OP_NONE; |
Takuya Yoshikawa | 9f21ca5 | 2011-05-29 21:57:53 +0900 | [diff] [blame] | 2959 | return X86EMUL_CONTINUE; |
| 2960 | } |
| 2961 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2962 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 2963 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2964 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2965 | ctxt->src.val = ctxt->dst.val; |
| 2966 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2967 | |
| 2968 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2969 | ctxt->dst.val = ctxt->src.orig_val; |
| 2970 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2971 | return X86EMUL_CONTINUE; |
| 2972 | } |
| 2973 | |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2974 | static int em_imul(struct x86_emulate_ctxt *ctxt) |
| 2975 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2976 | emulate_2op_SrcV_nobyte(ctxt, "imul"); |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2977 | return X86EMUL_CONTINUE; |
| 2978 | } |
| 2979 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2980 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2981 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2982 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 2983 | return em_imul(ctxt); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2984 | } |
| 2985 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2986 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2987 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2988 | ctxt->dst.type = OP_REG; |
| 2989 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2990 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2991 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2992 | |
| 2993 | return X86EMUL_CONTINUE; |
| 2994 | } |
| 2995 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2996 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2997 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2998 | u64 tsc = 0; |
| 2999 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3000 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3001 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3002 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3003 | return X86EMUL_CONTINUE; |
| 3004 | } |
| 3005 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3006 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3007 | { |
| 3008 | u64 pmc; |
| 3009 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3010 | if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc)) |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3011 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3012 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3013 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3014 | return X86EMUL_CONTINUE; |
| 3015 | } |
| 3016 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3017 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3018 | { |
Stefan Hajnoczi | 49597d8 | 2012-04-09 18:40:00 +0300 | [diff] [blame] | 3019 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3020 | return X86EMUL_CONTINUE; |
| 3021 | } |
| 3022 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3023 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3024 | { |
| 3025 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3026 | return emulate_gp(ctxt, 0); |
| 3027 | |
| 3028 | /* Disable writeback. */ |
| 3029 | ctxt->dst.type = OP_NONE; |
| 3030 | return X86EMUL_CONTINUE; |
| 3031 | } |
| 3032 | |
| 3033 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3034 | { |
| 3035 | unsigned long val; |
| 3036 | |
| 3037 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3038 | val = ctxt->src.val & ~0ULL; |
| 3039 | else |
| 3040 | val = ctxt->src.val & ~0U; |
| 3041 | |
| 3042 | /* #UD condition is already handled. */ |
| 3043 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3044 | return emulate_gp(ctxt, 0); |
| 3045 | |
| 3046 | /* Disable writeback. */ |
| 3047 | ctxt->dst.type = OP_NONE; |
| 3048 | return X86EMUL_CONTINUE; |
| 3049 | } |
| 3050 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3051 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3052 | { |
| 3053 | u64 msr_data; |
| 3054 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3055 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3056 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3057 | if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data)) |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3058 | return emulate_gp(ctxt, 0); |
| 3059 | |
| 3060 | return X86EMUL_CONTINUE; |
| 3061 | } |
| 3062 | |
| 3063 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3064 | { |
| 3065 | u64 msr_data; |
| 3066 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3067 | if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data)) |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3068 | return emulate_gp(ctxt, 0); |
| 3069 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3070 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3071 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3072 | return X86EMUL_CONTINUE; |
| 3073 | } |
| 3074 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3075 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3076 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3077 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3078 | return emulate_ud(ctxt); |
| 3079 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3080 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3081 | return X86EMUL_CONTINUE; |
| 3082 | } |
| 3083 | |
| 3084 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3085 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3086 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3087 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3088 | 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] | 3089 | return emulate_ud(ctxt); |
| 3090 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3091 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3092 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3093 | |
| 3094 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3095 | ctxt->dst.type = OP_NONE; |
| 3096 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3097 | } |
| 3098 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3099 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3100 | { |
| 3101 | u16 sel = ctxt->src.val; |
| 3102 | |
| 3103 | /* Disable writeback. */ |
| 3104 | ctxt->dst.type = OP_NONE; |
| 3105 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3106 | } |
| 3107 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3108 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3109 | { |
| 3110 | u16 sel = ctxt->src.val; |
| 3111 | |
| 3112 | /* Disable writeback. */ |
| 3113 | ctxt->dst.type = OP_NONE; |
| 3114 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3115 | } |
| 3116 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3117 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3118 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3119 | int rc; |
| 3120 | ulong linear; |
| 3121 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3122 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3123 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3124 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3125 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3126 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3127 | return X86EMUL_CONTINUE; |
| 3128 | } |
| 3129 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3130 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3131 | { |
| 3132 | ulong cr0; |
| 3133 | |
| 3134 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3135 | cr0 &= ~X86_CR0_TS; |
| 3136 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3137 | return X86EMUL_CONTINUE; |
| 3138 | } |
| 3139 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3140 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3141 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3142 | int rc; |
| 3143 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3144 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3145 | return X86EMUL_UNHANDLEABLE; |
| 3146 | |
| 3147 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3148 | if (rc != X86EMUL_CONTINUE) |
| 3149 | return rc; |
| 3150 | |
| 3151 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3152 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3153 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3154 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3155 | return X86EMUL_CONTINUE; |
| 3156 | } |
| 3157 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3158 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3159 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3160 | struct desc_ptr *ptr)) |
| 3161 | { |
| 3162 | struct desc_ptr desc_ptr; |
| 3163 | |
| 3164 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3165 | ctxt->op_bytes = 8; |
| 3166 | get(ctxt, &desc_ptr); |
| 3167 | if (ctxt->op_bytes == 2) { |
| 3168 | ctxt->op_bytes = 4; |
| 3169 | desc_ptr.address &= 0x00ffffff; |
| 3170 | } |
| 3171 | /* Disable writeback. */ |
| 3172 | ctxt->dst.type = OP_NONE; |
| 3173 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3174 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3175 | } |
| 3176 | |
| 3177 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3178 | { |
| 3179 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3180 | } |
| 3181 | |
| 3182 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3183 | { |
| 3184 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3185 | } |
| 3186 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3187 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3188 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3189 | struct desc_ptr desc_ptr; |
| 3190 | int rc; |
| 3191 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3192 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3193 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3194 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3195 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3196 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3197 | if (rc != X86EMUL_CONTINUE) |
| 3198 | return rc; |
| 3199 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3200 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3201 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3202 | return X86EMUL_CONTINUE; |
| 3203 | } |
| 3204 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3205 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3206 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3207 | int rc; |
| 3208 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3209 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3210 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3211 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3212 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3213 | return rc; |
| 3214 | } |
| 3215 | |
| 3216 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3217 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3218 | struct desc_ptr desc_ptr; |
| 3219 | int rc; |
| 3220 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3221 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3222 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3223 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3224 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3225 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3226 | if (rc != X86EMUL_CONTINUE) |
| 3227 | return rc; |
| 3228 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3229 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3230 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3231 | return X86EMUL_CONTINUE; |
| 3232 | } |
| 3233 | |
| 3234 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3235 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3236 | ctxt->dst.bytes = 2; |
| 3237 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3238 | return X86EMUL_CONTINUE; |
| 3239 | } |
| 3240 | |
| 3241 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3242 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3243 | 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] | 3244 | | (ctxt->src.val & 0x0f)); |
| 3245 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3246 | return X86EMUL_CONTINUE; |
| 3247 | } |
| 3248 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3249 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3250 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3251 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3252 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3253 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3254 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3255 | |
| 3256 | return X86EMUL_CONTINUE; |
| 3257 | } |
| 3258 | |
| 3259 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3260 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3261 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3262 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3263 | |
| 3264 | return X86EMUL_CONTINUE; |
| 3265 | } |
| 3266 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3267 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3268 | { |
| 3269 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3270 | &ctxt->dst.val)) |
| 3271 | return X86EMUL_IO_NEEDED; |
| 3272 | |
| 3273 | return X86EMUL_CONTINUE; |
| 3274 | } |
| 3275 | |
| 3276 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3277 | { |
| 3278 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3279 | &ctxt->src.val, 1); |
| 3280 | /* Disable writeback. */ |
| 3281 | ctxt->dst.type = OP_NONE; |
| 3282 | return X86EMUL_CONTINUE; |
| 3283 | } |
| 3284 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3285 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3286 | { |
| 3287 | if (emulator_bad_iopl(ctxt)) |
| 3288 | return emulate_gp(ctxt, 0); |
| 3289 | |
| 3290 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3291 | return X86EMUL_CONTINUE; |
| 3292 | } |
| 3293 | |
| 3294 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3295 | { |
| 3296 | if (emulator_bad_iopl(ctxt)) |
| 3297 | return emulate_gp(ctxt, 0); |
| 3298 | |
| 3299 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3300 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3301 | return X86EMUL_CONTINUE; |
| 3302 | } |
| 3303 | |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3304 | static int em_bt(struct x86_emulate_ctxt *ctxt) |
| 3305 | { |
| 3306 | /* Disable writeback. */ |
| 3307 | ctxt->dst.type = OP_NONE; |
| 3308 | /* only subword offset */ |
| 3309 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
| 3310 | |
| 3311 | emulate_2op_SrcV_nobyte(ctxt, "bt"); |
| 3312 | return X86EMUL_CONTINUE; |
| 3313 | } |
| 3314 | |
| 3315 | static int em_bts(struct x86_emulate_ctxt *ctxt) |
| 3316 | { |
| 3317 | emulate_2op_SrcV_nobyte(ctxt, "bts"); |
| 3318 | return X86EMUL_CONTINUE; |
| 3319 | } |
| 3320 | |
| 3321 | static int em_btr(struct x86_emulate_ctxt *ctxt) |
| 3322 | { |
| 3323 | emulate_2op_SrcV_nobyte(ctxt, "btr"); |
| 3324 | return X86EMUL_CONTINUE; |
| 3325 | } |
| 3326 | |
| 3327 | static int em_btc(struct x86_emulate_ctxt *ctxt) |
| 3328 | { |
| 3329 | emulate_2op_SrcV_nobyte(ctxt, "btc"); |
| 3330 | return X86EMUL_CONTINUE; |
| 3331 | } |
| 3332 | |
Takuya Yoshikawa | ff22739 | 2011-11-22 15:21:33 +0900 | [diff] [blame] | 3333 | static int em_bsf(struct x86_emulate_ctxt *ctxt) |
| 3334 | { |
Joerg Roedel | d54e423 | 2012-05-07 12:12:25 +0200 | [diff] [blame] | 3335 | emulate_2op_SrcV_nobyte(ctxt, "bsf"); |
Takuya Yoshikawa | ff22739 | 2011-11-22 15:21:33 +0900 | [diff] [blame] | 3336 | return X86EMUL_CONTINUE; |
| 3337 | } |
| 3338 | |
| 3339 | static int em_bsr(struct x86_emulate_ctxt *ctxt) |
| 3340 | { |
Joerg Roedel | d54e423 | 2012-05-07 12:12:25 +0200 | [diff] [blame] | 3341 | emulate_2op_SrcV_nobyte(ctxt, "bsr"); |
Takuya Yoshikawa | ff22739 | 2011-11-22 15:21:33 +0900 | [diff] [blame] | 3342 | return X86EMUL_CONTINUE; |
| 3343 | } |
| 3344 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3345 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3346 | { |
| 3347 | u32 eax, ebx, ecx, edx; |
| 3348 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3349 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3350 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3351 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3352 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3353 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3354 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3355 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3356 | return X86EMUL_CONTINUE; |
| 3357 | } |
| 3358 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3359 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3360 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3361 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3362 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3363 | return X86EMUL_CONTINUE; |
| 3364 | } |
| 3365 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3366 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3367 | { |
| 3368 | switch (ctxt->op_bytes) { |
| 3369 | #ifdef CONFIG_X86_64 |
| 3370 | case 8: |
| 3371 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3372 | break; |
| 3373 | #endif |
| 3374 | default: |
| 3375 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3376 | break; |
| 3377 | } |
| 3378 | return X86EMUL_CONTINUE; |
| 3379 | } |
| 3380 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3381 | static bool valid_cr(int nr) |
| 3382 | { |
| 3383 | switch (nr) { |
| 3384 | case 0: |
| 3385 | case 2 ... 4: |
| 3386 | case 8: |
| 3387 | return true; |
| 3388 | default: |
| 3389 | return false; |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3394 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3395 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3396 | return emulate_ud(ctxt); |
| 3397 | |
| 3398 | return X86EMUL_CONTINUE; |
| 3399 | } |
| 3400 | |
| 3401 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3402 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3403 | u64 new_val = ctxt->src.val64; |
| 3404 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3405 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3406 | |
| 3407 | static u64 cr_reserved_bits[] = { |
| 3408 | 0xffffffff00000000ULL, |
| 3409 | 0, 0, 0, /* CR3 checked later */ |
| 3410 | CR4_RESERVED_BITS, |
| 3411 | 0, 0, 0, |
| 3412 | CR8_RESERVED_BITS, |
| 3413 | }; |
| 3414 | |
| 3415 | if (!valid_cr(cr)) |
| 3416 | return emulate_ud(ctxt); |
| 3417 | |
| 3418 | if (new_val & cr_reserved_bits[cr]) |
| 3419 | return emulate_gp(ctxt, 0); |
| 3420 | |
| 3421 | switch (cr) { |
| 3422 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3423 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3424 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3425 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3426 | return emulate_gp(ctxt, 0); |
| 3427 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3428 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3429 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3430 | |
| 3431 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3432 | !(cr4 & X86_CR4_PAE)) |
| 3433 | return emulate_gp(ctxt, 0); |
| 3434 | |
| 3435 | break; |
| 3436 | } |
| 3437 | case 3: { |
| 3438 | u64 rsvd = 0; |
| 3439 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3440 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3441 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3442 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3443 | else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3444 | rsvd = CR3_PAE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3445 | else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3446 | rsvd = CR3_NONPAE_RESERVED_BITS; |
| 3447 | |
| 3448 | if (new_val & rsvd) |
| 3449 | return emulate_gp(ctxt, 0); |
| 3450 | |
| 3451 | break; |
| 3452 | } |
| 3453 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3454 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3455 | |
| 3456 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3457 | return emulate_gp(ctxt, 0); |
| 3458 | |
| 3459 | break; |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | return X86EMUL_CONTINUE; |
| 3464 | } |
| 3465 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3466 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3467 | { |
| 3468 | unsigned long dr7; |
| 3469 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3470 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3471 | |
| 3472 | /* Check if DR7.Global_Enable is set */ |
| 3473 | return dr7 & (1 << 13); |
| 3474 | } |
| 3475 | |
| 3476 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3477 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3478 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3479 | u64 cr4; |
| 3480 | |
| 3481 | if (dr > 7) |
| 3482 | return emulate_ud(ctxt); |
| 3483 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3484 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3485 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3486 | return emulate_ud(ctxt); |
| 3487 | |
| 3488 | if (check_dr7_gd(ctxt)) |
| 3489 | return emulate_db(ctxt); |
| 3490 | |
| 3491 | return X86EMUL_CONTINUE; |
| 3492 | } |
| 3493 | |
| 3494 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3495 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3496 | u64 new_val = ctxt->src.val64; |
| 3497 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3498 | |
| 3499 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3500 | return emulate_gp(ctxt, 0); |
| 3501 | |
| 3502 | return check_dr_read(ctxt); |
| 3503 | } |
| 3504 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3505 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3506 | { |
| 3507 | u64 efer; |
| 3508 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3509 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3510 | |
| 3511 | if (!(efer & EFER_SVME)) |
| 3512 | return emulate_ud(ctxt); |
| 3513 | |
| 3514 | return X86EMUL_CONTINUE; |
| 3515 | } |
| 3516 | |
| 3517 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3518 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3519 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3520 | |
| 3521 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3522 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3523 | return emulate_gp(ctxt, 0); |
| 3524 | |
| 3525 | return check_svme(ctxt); |
| 3526 | } |
| 3527 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3528 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3529 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3530 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3531 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3532 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3533 | return emulate_ud(ctxt); |
| 3534 | |
| 3535 | return X86EMUL_CONTINUE; |
| 3536 | } |
| 3537 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3538 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3539 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3540 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3541 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3542 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3543 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3544 | (rcx > 3)) |
| 3545 | return emulate_gp(ctxt, 0); |
| 3546 | |
| 3547 | return X86EMUL_CONTINUE; |
| 3548 | } |
| 3549 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3550 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3551 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3552 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3553 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3554 | return emulate_gp(ctxt, 0); |
| 3555 | |
| 3556 | return X86EMUL_CONTINUE; |
| 3557 | } |
| 3558 | |
| 3559 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3560 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3561 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3562 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3563 | return emulate_gp(ctxt, 0); |
| 3564 | |
| 3565 | return X86EMUL_CONTINUE; |
| 3566 | } |
| 3567 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3568 | #define D(_y) { .flags = (_y) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3569 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3570 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
| 3571 | .check_perm = (_p) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3572 | #define N D(0) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3573 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3574 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3575 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3576 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3577 | #define II(_f, _e, _i) \ |
| 3578 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3579 | #define IIP(_f, _e, _i, _p) \ |
| 3580 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ |
| 3581 | .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3582 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3583 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3584 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3585 | #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] | 3586 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3587 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3588 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3589 | |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3590 | #define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3591 | I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3592 | I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3593 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3594 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3595 | DI(SrcNone | Priv, monitor), |
| 3596 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3597 | N, N, N, N, N, N, |
| 3598 | }; |
| 3599 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3600 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3601 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
| 3602 | II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall), |
| 3603 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3604 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3605 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3606 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3607 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3608 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3609 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3610 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3611 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3612 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3613 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3614 | N, N, N, N, N, N, |
| 3615 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3616 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3617 | static const struct opcode group1[] = { |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3618 | I(Lock, em_add), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3619 | I(Lock | PageTable, em_or), |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3620 | I(Lock, em_adc), |
| 3621 | I(Lock, em_sbb), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3622 | I(Lock | PageTable, em_and), |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3623 | I(Lock, em_sub), |
| 3624 | I(Lock, em_xor), |
| 3625 | I(0, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3626 | }; |
| 3627 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3628 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3629 | I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3630 | }; |
| 3631 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3632 | static const struct opcode group3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3633 | I(DstMem | SrcImm, em_test), |
| 3634 | I(DstMem | SrcImm, em_test), |
| 3635 | I(DstMem | SrcNone | Lock, em_not), |
| 3636 | I(DstMem | SrcNone | Lock, em_neg), |
| 3637 | I(SrcMem, em_mul_ex), |
| 3638 | I(SrcMem, em_imul_ex), |
| 3639 | I(SrcMem, em_div_ex), |
| 3640 | I(SrcMem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3641 | }; |
| 3642 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3643 | static const struct opcode group4[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3644 | I(ByteOp | DstMem | SrcNone | Lock, em_grp45), |
| 3645 | I(ByteOp | DstMem | SrcNone | Lock, em_grp45), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3646 | N, N, N, N, N, N, |
| 3647 | }; |
| 3648 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3649 | static const struct opcode group5[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3650 | I(DstMem | SrcNone | Lock, em_grp45), |
| 3651 | I(DstMem | SrcNone | Lock, em_grp45), |
| 3652 | I(SrcMem | Stack, em_grp45), |
| 3653 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3654 | I(SrcMem | Stack, em_grp45), |
| 3655 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
| 3656 | I(SrcMem | Stack, em_grp45), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3657 | }; |
| 3658 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3659 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3660 | DI(Prot, sldt), |
| 3661 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3662 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3663 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3664 | N, N, N, N, |
| 3665 | }; |
| 3666 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3667 | static const struct group_dual group7 = { { |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3668 | II(Mov | DstMem | Priv, em_sgdt, sgdt), |
| 3669 | II(Mov | DstMem | Priv, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3670 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3671 | II(SrcMem | Priv, em_lidt, lidt), |
| 3672 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3673 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3674 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3675 | }, { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3676 | I(SrcNone | Priv | VendorSpecific, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3677 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3678 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3679 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3680 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3681 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3682 | } }; |
| 3683 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3684 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3685 | N, N, N, N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3686 | I(DstMem | SrcImmByte, em_bt), |
| 3687 | I(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3688 | I(DstMem | SrcImmByte | Lock, em_btr), |
| 3689 | I(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3690 | }; |
| 3691 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3692 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3693 | N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3694 | }, { |
| 3695 | N, N, N, N, N, N, N, N, |
| 3696 | } }; |
| 3697 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3698 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3699 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3700 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3701 | }; |
| 3702 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3703 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3704 | I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov), |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3705 | }; |
| 3706 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3707 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3708 | I(0, em_mov), N, N, N, |
| 3709 | }; |
| 3710 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3711 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3712 | /* 0x00 - 0x07 */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3713 | I6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3714 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3715 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3716 | /* 0x08 - 0x0F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3717 | I6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3718 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3719 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3720 | /* 0x10 - 0x17 */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3721 | I6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3722 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3723 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3724 | /* 0x18 - 0x1F */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3725 | I6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3726 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3727 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3728 | /* 0x20 - 0x27 */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3729 | I6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3730 | /* 0x28 - 0x2F */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3731 | I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3732 | /* 0x30 - 0x37 */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3733 | I6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3734 | /* 0x38 - 0x3F */ |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3735 | I6ALU(0, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3736 | /* 0x40 - 0x4F */ |
| 3737 | X16(D(DstReg)), |
| 3738 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3739 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3740 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3741 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3742 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3743 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3744 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3745 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3746 | N, N, N, N, |
| 3747 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3748 | I(SrcImm | Mov | Stack, em_push), |
| 3749 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3750 | I(SrcImmByte | Mov | Stack, em_push), |
| 3751 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3752 | I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */ |
Takuya Yoshikawa | 2b5e97e | 2011-11-23 12:27:39 +0900 | [diff] [blame] | 3753 | I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */ |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3754 | /* 0x70 - 0x7F */ |
| 3755 | X16(D(SrcImmByte)), |
| 3756 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3757 | G(ByteOp | DstMem | SrcImm, group1), |
| 3758 | G(DstMem | SrcImm, group1), |
| 3759 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3760 | G(DstMem | SrcImmByte, group1), |
Takuya Yoshikawa | 9f21ca5 | 2011-05-29 21:57:53 +0900 | [diff] [blame] | 3761 | I2bv(DstMem | SrcReg | ModRM, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3762 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3763 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3764 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3765 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3766 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3767 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3768 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3769 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3770 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3771 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3772 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3773 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3774 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3775 | II(ImplicitOps | Stack, em_pushf, pushf), |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3776 | II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3777 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3778 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3779 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3780 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3781 | I2bv(SrcSI | DstDI | String, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3782 | /* 0xA8 - 0xAF */ |
Takuya Yoshikawa | 9f21ca5 | 2011-05-29 21:57:53 +0900 | [diff] [blame] | 3783 | I2bv(DstAcc | SrcImm, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3784 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3785 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3786 | I2bv(SrcAcc | DstDI | String, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3787 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3788 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3789 | /* 0xB8 - 0xBF */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3790 | X8(I(DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3791 | /* 0xC0 - 0xC7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 3792 | D2bv(DstMem | SrcImmByte | ModRM), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3793 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3794 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3795 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3796 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3797 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3798 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3799 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
| 3800 | N, I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3801 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3802 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3803 | /* 0xD0 - 0xD7 */ |
Avi Kivity | d2c6c7a | 2010-08-26 11:56:11 +0300 | [diff] [blame] | 3804 | D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3805 | N, N, N, N, |
| 3806 | /* 0xD8 - 0xDF */ |
| 3807 | N, N, N, N, N, N, N, N, |
| 3808 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3809 | X3(I(SrcImmByte, em_loop)), |
| 3810 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3811 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3812 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3813 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3814 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3815 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3816 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3817 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3818 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3819 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3820 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3821 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3822 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3823 | D(ImplicitOps), D(ImplicitOps), |
| 3824 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3825 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3826 | }; |
| 3827 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3828 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3829 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3830 | G(0, group6), GD(0, &group7), N, N, |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3831 | N, I(ImplicitOps | VendorSpecific, em_syscall), |
| 3832 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3833 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3834 | N, D(ImplicitOps | ModRM), N, N, |
| 3835 | /* 0x10 - 0x1F */ |
| 3836 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, |
| 3837 | /* 0x20 - 0x2F */ |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3838 | DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3839 | DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read), |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3840 | IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write), |
| 3841 | 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] | 3842 | N, N, N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3843 | N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
| 3844 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3845 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3846 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3847 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3848 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3849 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3850 | I(ImplicitOps | VendorSpecific, em_sysenter), |
| 3851 | I(ImplicitOps | Priv | VendorSpecific, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 3852 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3853 | N, N, N, N, N, N, N, N, |
| 3854 | /* 0x40 - 0x4F */ |
| 3855 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 3856 | /* 0x50 - 0x5F */ |
| 3857 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3858 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3859 | N, N, N, N, |
| 3860 | N, N, N, N, |
| 3861 | N, N, N, N, |
| 3862 | 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] | 3863 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3864 | N, N, N, N, |
| 3865 | N, N, N, N, |
| 3866 | N, N, N, N, |
| 3867 | 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] | 3868 | /* 0x80 - 0x8F */ |
| 3869 | X16(D(SrcImm)), |
| 3870 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3871 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3872 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3873 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3874 | II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3875 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 3876 | D(DstMem | SrcReg | Src2CL | ModRM), N, N, |
| 3877 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3878 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3879 | DI(ImplicitOps, rsm), |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3880 | I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3881 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 3882 | D(DstMem | SrcReg | Src2CL | ModRM), |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 3883 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3884 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 3885 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3886 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3887 | I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3888 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 3889 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3890 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3891 | /* 0xB8 - 0xBF */ |
| 3892 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3893 | G(BitOp, group8), |
| 3894 | I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
Takuya Yoshikawa | ff22739 | 2011-11-22 15:21:33 +0900 | [diff] [blame] | 3895 | I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3896 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3897 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 3898 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3899 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3900 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3901 | /* 0xC8 - 0xCF */ |
| 3902 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3903 | /* 0xD0 - 0xDF */ |
| 3904 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3905 | /* 0xE0 - 0xEF */ |
| 3906 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3907 | /* 0xF0 - 0xFF */ |
| 3908 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 3909 | }; |
| 3910 | |
| 3911 | #undef D |
| 3912 | #undef N |
| 3913 | #undef G |
| 3914 | #undef GD |
| 3915 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3916 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3917 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3918 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3919 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3920 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3921 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3922 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3923 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3924 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3925 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3926 | { |
| 3927 | unsigned size; |
| 3928 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3929 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3930 | if (size == 8) |
| 3931 | size = 4; |
| 3932 | return size; |
| 3933 | } |
| 3934 | |
| 3935 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 3936 | unsigned size, bool sign_extension) |
| 3937 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3938 | int rc = X86EMUL_CONTINUE; |
| 3939 | |
| 3940 | op->type = OP_IMM; |
| 3941 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3942 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3943 | /* NB. Immediates are sign-extended as necessary. */ |
| 3944 | switch (op->bytes) { |
| 3945 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3946 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3947 | break; |
| 3948 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3949 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3950 | break; |
| 3951 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3952 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3953 | break; |
| 3954 | } |
| 3955 | if (!sign_extension) { |
| 3956 | switch (op->bytes) { |
| 3957 | case 1: |
| 3958 | op->val &= 0xff; |
| 3959 | break; |
| 3960 | case 2: |
| 3961 | op->val &= 0xffff; |
| 3962 | break; |
| 3963 | case 4: |
| 3964 | op->val &= 0xffffffff; |
| 3965 | break; |
| 3966 | } |
| 3967 | } |
| 3968 | done: |
| 3969 | return rc; |
| 3970 | } |
| 3971 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3972 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 3973 | unsigned d) |
| 3974 | { |
| 3975 | int rc = X86EMUL_CONTINUE; |
| 3976 | |
| 3977 | switch (d) { |
| 3978 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3979 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3980 | break; |
| 3981 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 3982 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3983 | break; |
| 3984 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 3985 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 3986 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3987 | *op = ctxt->memop; |
| 3988 | ctxt->memopp = op; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 3989 | if ((ctxt->d & BitOp) && op == &ctxt->dst) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3990 | fetch_bit_operand(ctxt); |
| 3991 | op->orig_val = op->val; |
| 3992 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 3993 | case OpMem64: |
| 3994 | ctxt->memop.bytes = 8; |
| 3995 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3996 | case OpAcc: |
| 3997 | op->type = OP_REG; |
| 3998 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3999 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4000 | fetch_register_operand(op); |
| 4001 | op->orig_val = op->val; |
| 4002 | break; |
| 4003 | case OpDI: |
| 4004 | op->type = OP_MEM; |
| 4005 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4006 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4007 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4008 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4009 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4010 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4011 | break; |
| 4012 | case OpDX: |
| 4013 | op->type = OP_REG; |
| 4014 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4015 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4016 | fetch_register_operand(op); |
| 4017 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4018 | case OpCL: |
| 4019 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4020 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4021 | break; |
| 4022 | case OpImmByte: |
| 4023 | rc = decode_imm(ctxt, op, 1, true); |
| 4024 | break; |
| 4025 | case OpOne: |
| 4026 | op->bytes = 1; |
| 4027 | op->val = 1; |
| 4028 | break; |
| 4029 | case OpImm: |
| 4030 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4031 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4032 | case OpMem8: |
| 4033 | ctxt->memop.bytes = 1; |
| 4034 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4035 | case OpMem16: |
| 4036 | ctxt->memop.bytes = 2; |
| 4037 | goto mem_common; |
| 4038 | case OpMem32: |
| 4039 | ctxt->memop.bytes = 4; |
| 4040 | goto mem_common; |
| 4041 | case OpImmU16: |
| 4042 | rc = decode_imm(ctxt, op, 2, false); |
| 4043 | break; |
| 4044 | case OpImmU: |
| 4045 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4046 | break; |
| 4047 | case OpSI: |
| 4048 | op->type = OP_MEM; |
| 4049 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4050 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4051 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4052 | op->addr.mem.seg = seg_override(ctxt); |
| 4053 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4054 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4055 | break; |
| 4056 | case OpImmFAddr: |
| 4057 | op->type = OP_IMM; |
| 4058 | op->addr.mem.ea = ctxt->_eip; |
| 4059 | op->bytes = ctxt->op_bytes + 2; |
| 4060 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4061 | break; |
| 4062 | case OpMemFAddr: |
| 4063 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4064 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4065 | case OpES: |
| 4066 | op->val = VCPU_SREG_ES; |
| 4067 | break; |
| 4068 | case OpCS: |
| 4069 | op->val = VCPU_SREG_CS; |
| 4070 | break; |
| 4071 | case OpSS: |
| 4072 | op->val = VCPU_SREG_SS; |
| 4073 | break; |
| 4074 | case OpDS: |
| 4075 | op->val = VCPU_SREG_DS; |
| 4076 | break; |
| 4077 | case OpFS: |
| 4078 | op->val = VCPU_SREG_FS; |
| 4079 | break; |
| 4080 | case OpGS: |
| 4081 | op->val = VCPU_SREG_GS; |
| 4082 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4083 | case OpImplicit: |
| 4084 | /* Special instructions do their own operand decoding. */ |
| 4085 | default: |
| 4086 | op->type = OP_NONE; /* Disable writeback. */ |
| 4087 | break; |
| 4088 | } |
| 4089 | |
| 4090 | done: |
| 4091 | return rc; |
| 4092 | } |
| 4093 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4094 | 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] | 4095 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4096 | int rc = X86EMUL_CONTINUE; |
| 4097 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4098 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4099 | bool op_prefix = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4100 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4101 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4102 | ctxt->memop.type = OP_NONE; |
| 4103 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4104 | ctxt->_eip = ctxt->eip; |
| 4105 | ctxt->fetch.start = ctxt->_eip; |
| 4106 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4107 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4108 | memcpy(ctxt->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4109 | |
| 4110 | switch (mode) { |
| 4111 | case X86EMUL_MODE_REAL: |
| 4112 | case X86EMUL_MODE_VM86: |
| 4113 | case X86EMUL_MODE_PROT16: |
| 4114 | def_op_bytes = def_ad_bytes = 2; |
| 4115 | break; |
| 4116 | case X86EMUL_MODE_PROT32: |
| 4117 | def_op_bytes = def_ad_bytes = 4; |
| 4118 | break; |
| 4119 | #ifdef CONFIG_X86_64 |
| 4120 | case X86EMUL_MODE_PROT64: |
| 4121 | def_op_bytes = 4; |
| 4122 | def_ad_bytes = 8; |
| 4123 | break; |
| 4124 | #endif |
| 4125 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4126 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4127 | } |
| 4128 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4129 | ctxt->op_bytes = def_op_bytes; |
| 4130 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4131 | |
| 4132 | /* Legacy prefixes. */ |
| 4133 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4134 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4135 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4136 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4137 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4138 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4139 | break; |
| 4140 | case 0x67: /* address-size override */ |
| 4141 | if (mode == X86EMUL_MODE_PROT64) |
| 4142 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4143 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4144 | else |
| 4145 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4146 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4147 | break; |
| 4148 | case 0x26: /* ES override */ |
| 4149 | case 0x2e: /* CS override */ |
| 4150 | case 0x36: /* SS override */ |
| 4151 | case 0x3e: /* DS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4152 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4153 | break; |
| 4154 | case 0x64: /* FS override */ |
| 4155 | case 0x65: /* GS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4156 | set_seg_override(ctxt, ctxt->b & 7); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4157 | break; |
| 4158 | case 0x40 ... 0x4f: /* REX */ |
| 4159 | if (mode != X86EMUL_MODE_PROT64) |
| 4160 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4161 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4162 | continue; |
| 4163 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4164 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4165 | break; |
| 4166 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4167 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4168 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4169 | break; |
| 4170 | default: |
| 4171 | goto done_prefixes; |
| 4172 | } |
| 4173 | |
| 4174 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4175 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4176 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | done_prefixes: |
| 4180 | |
| 4181 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4182 | if (ctxt->rex_prefix & 8) |
| 4183 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4184 | |
| 4185 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4186 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4187 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4188 | if (ctxt->b == 0x0f) { |
| 4189 | ctxt->twobyte = 1; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4190 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4191 | opcode = twobyte_table[ctxt->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4192 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4193 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4194 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4195 | if (ctxt->d & ModRM) |
| 4196 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4197 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4198 | while (ctxt->d & GroupMask) { |
| 4199 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4200 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4201 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4202 | opcode = opcode.u.group[goffset]; |
| 4203 | break; |
| 4204 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4205 | goffset = (ctxt->modrm >> 3) & 7; |
| 4206 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4207 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4208 | else |
| 4209 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4210 | break; |
| 4211 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4212 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4213 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4214 | break; |
| 4215 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4216 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4217 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4218 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4219 | switch (simd_prefix) { |
| 4220 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4221 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4222 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4223 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4224 | } |
| 4225 | break; |
| 4226 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4227 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4228 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4229 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4230 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4231 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4232 | } |
| 4233 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4234 | ctxt->execute = opcode.u.execute; |
| 4235 | ctxt->check_perm = opcode.check_perm; |
| 4236 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4237 | |
| 4238 | /* Unrecognised? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4239 | if (ctxt->d == 0 || (ctxt->d & Undefined)) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4240 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4241 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4242 | if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4243 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4244 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4245 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
| 4246 | ctxt->op_bytes = 8; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4247 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4248 | if (ctxt->d & Op3264) { |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4249 | if (mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4250 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4251 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4252 | ctxt->op_bytes = 4; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4253 | } |
| 4254 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4255 | if (ctxt->d & Sse) |
| 4256 | ctxt->op_bytes = 16; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4257 | else if (ctxt->d & Mmx) |
| 4258 | ctxt->op_bytes = 8; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4259 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4260 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4261 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4262 | rc = decode_modrm(ctxt, &ctxt->memop); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4263 | if (!ctxt->has_seg_override) |
| 4264 | set_seg_override(ctxt, ctxt->modrm_seg); |
| 4265 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4266 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4267 | if (rc != X86EMUL_CONTINUE) |
| 4268 | goto done; |
| 4269 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4270 | if (!ctxt->has_seg_override) |
| 4271 | set_seg_override(ctxt, VCPU_SREG_DS); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4272 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4273 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4274 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4275 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
| 4276 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4277 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4278 | /* |
| 4279 | * Decode and fetch the source operand: register, memory |
| 4280 | * or immediate. |
| 4281 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4282 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4283 | if (rc != X86EMUL_CONTINUE) |
| 4284 | goto done; |
| 4285 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4286 | /* |
| 4287 | * Decode and fetch the second source operand: register, memory |
| 4288 | * or immediate. |
| 4289 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4290 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4291 | if (rc != X86EMUL_CONTINUE) |
| 4292 | goto done; |
| 4293 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4294 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4295 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4296 | |
| 4297 | done: |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4298 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
| 4299 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4300 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4301 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4302 | } |
| 4303 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4304 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4305 | { |
| 4306 | return ctxt->d & PageTable; |
| 4307 | } |
| 4308 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4309 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4310 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4311 | /* The second termination condition only applies for REPE |
| 4312 | * and REPNE. Test if the repeat string operation prefix is |
| 4313 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4314 | * corresponding termination condition according to: |
| 4315 | * - if REPE/REPZ and ZF = 0 then done |
| 4316 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4317 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4318 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4319 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4320 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4321 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4322 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4323 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4324 | return true; |
| 4325 | |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4329 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4330 | { |
| 4331 | bool fault = false; |
| 4332 | |
| 4333 | ctxt->ops->get_fpu(ctxt); |
| 4334 | asm volatile("1: fwait \n\t" |
| 4335 | "2: \n\t" |
| 4336 | ".pushsection .fixup,\"ax\" \n\t" |
| 4337 | "3: \n\t" |
| 4338 | "movb $1, %[fault] \n\t" |
| 4339 | "jmp 2b \n\t" |
| 4340 | ".popsection \n\t" |
| 4341 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4342 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4343 | ctxt->ops->put_fpu(ctxt); |
| 4344 | |
| 4345 | if (unlikely(fault)) |
| 4346 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4347 | |
| 4348 | return X86EMUL_CONTINUE; |
| 4349 | } |
| 4350 | |
| 4351 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4352 | struct operand *op) |
| 4353 | { |
| 4354 | if (op->type == OP_MM) |
| 4355 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4356 | } |
| 4357 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4358 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4359 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4360 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4361 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4362 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4363 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4364 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4365 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4366 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4367 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4368 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 4369 | goto done; |
| 4370 | } |
| 4371 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4372 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4373 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4374 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4375 | goto done; |
| 4376 | } |
| 4377 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4378 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4379 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4380 | goto done; |
| 4381 | } |
| 4382 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4383 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4384 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4385 | rc = emulate_ud(ctxt); |
| 4386 | goto done; |
| 4387 | } |
| 4388 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4389 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4390 | rc = emulate_nm(ctxt); |
| 4391 | goto done; |
| 4392 | } |
| 4393 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4394 | if (ctxt->d & Mmx) { |
| 4395 | rc = flush_pending_x87_faults(ctxt); |
| 4396 | if (rc != X86EMUL_CONTINUE) |
| 4397 | goto done; |
| 4398 | /* |
| 4399 | * Now that we know the fpu is exception safe, we can fetch |
| 4400 | * operands from it. |
| 4401 | */ |
| 4402 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4403 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4404 | if (!(ctxt->d & Mov)) |
| 4405 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4406 | } |
| 4407 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4408 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4409 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4410 | X86_ICPT_PRE_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4411 | if (rc != X86EMUL_CONTINUE) |
| 4412 | goto done; |
| 4413 | } |
| 4414 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4415 | /* Privileged instruction can be executed only in CPL=0 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4416 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4417 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4418 | goto done; |
| 4419 | } |
| 4420 | |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4421 | /* Instruction can only be executed in protected mode */ |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 4422 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4423 | rc = emulate_ud(ctxt); |
| 4424 | goto done; |
| 4425 | } |
| 4426 | |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4427 | /* Do instruction specific permission checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4428 | if (ctxt->check_perm) { |
| 4429 | rc = ctxt->check_perm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4430 | if (rc != X86EMUL_CONTINUE) |
| 4431 | goto done; |
| 4432 | } |
| 4433 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4434 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4435 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4436 | X86_ICPT_POST_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4437 | if (rc != X86EMUL_CONTINUE) |
| 4438 | goto done; |
| 4439 | } |
| 4440 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4441 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4442 | /* All REP prefixes have the same first termination condition */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4443 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4444 | ctxt->eip = ctxt->_eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4445 | goto done; |
| 4446 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4447 | } |
| 4448 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4449 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4450 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4451 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4452 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4453 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4454 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4455 | } |
| 4456 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4457 | if (ctxt->src2.type == OP_MEM) { |
| 4458 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4459 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4460 | if (rc != X86EMUL_CONTINUE) |
| 4461 | goto done; |
| 4462 | } |
| 4463 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4464 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4465 | goto special_insn; |
| 4466 | |
| 4467 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4468 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4469 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4470 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4471 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4472 | if (rc != X86EMUL_CONTINUE) |
| 4473 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4474 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4475 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4476 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4477 | special_insn: |
| 4478 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4479 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4480 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4481 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4482 | if (rc != X86EMUL_CONTINUE) |
| 4483 | goto done; |
| 4484 | } |
| 4485 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4486 | if (ctxt->execute) { |
| 4487 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4488 | if (rc != X86EMUL_CONTINUE) |
| 4489 | goto done; |
| 4490 | goto writeback; |
| 4491 | } |
| 4492 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4493 | if (ctxt->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4494 | goto twobyte_insn; |
| 4495 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4496 | switch (ctxt->b) { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 4497 | case 0x40 ... 0x47: /* inc r16/r32 */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 4498 | emulate_1op(ctxt, "inc"); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 4499 | break; |
| 4500 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 4501 | emulate_1op(ctxt, "dec"); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 4502 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4503 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4504 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4505 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4506 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4507 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4508 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4509 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4510 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4511 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4512 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4513 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4514 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4515 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4516 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 4517 | break; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4518 | rc = em_xchg(ctxt); |
| 4519 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4520 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4521 | switch (ctxt->op_bytes) { |
| 4522 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4523 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4524 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4525 | } |
| 4526 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4527 | case 0xc0 ... 0xc1: |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 4528 | rc = em_grp2(ctxt); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4529 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4530 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4531 | rc = emulate_int(ctxt, 3); |
| 4532 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4533 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4534 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4535 | break; |
| 4536 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4537 | if (ctxt->eflags & EFLG_OF) |
| 4538 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4539 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4540 | case 0xd0 ... 0xd1: /* Grp2 */ |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 4541 | rc = em_grp2(ctxt); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4542 | break; |
| 4543 | case 0xd2 ... 0xd3: /* Grp2 */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4544 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX); |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 4545 | rc = em_grp2(ctxt); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4546 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4547 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4548 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4549 | jmp_rel(ctxt, ctxt->src.val); |
| 4550 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4551 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4552 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4553 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4554 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4555 | case 0xf5: /* cmc */ |
| 4556 | /* complement carry flag from eflags reg */ |
| 4557 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4558 | break; |
| 4559 | case 0xf8: /* clc */ |
| 4560 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4561 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4562 | case 0xf9: /* stc */ |
| 4563 | ctxt->eflags |= EFLG_CF; |
| 4564 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4565 | case 0xfc: /* cld */ |
| 4566 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4567 | break; |
| 4568 | case 0xfd: /* std */ |
| 4569 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4570 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4571 | default: |
| 4572 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4573 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4574 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4575 | if (rc != X86EMUL_CONTINUE) |
| 4576 | goto done; |
| 4577 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4578 | writeback: |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 4579 | rc = writeback(ctxt); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4580 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4581 | goto done; |
| 4582 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4583 | /* |
| 4584 | * restore dst type in case the decoding will be reused |
| 4585 | * (happens for string instruction ) |
| 4586 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4587 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4588 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4589 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4590 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4591 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4592 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4593 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4594 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4595 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4596 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4597 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4598 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4599 | count = ctxt->src.count; |
| 4600 | else |
| 4601 | count = ctxt->dst.count; |
| 4602 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4603 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4604 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4605 | if (!string_insn_completed(ctxt)) { |
| 4606 | /* |
| 4607 | * Re-enter guest when pio read ahead buffer is empty |
| 4608 | * or, if it is not used, after each 1024 iteration. |
| 4609 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4610 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4611 | (r->end == 0 || r->end != r->pos)) { |
| 4612 | /* |
| 4613 | * Reset read cache. Usually happens before |
| 4614 | * decode, but since instruction is restarted |
| 4615 | * we have to do it here. |
| 4616 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4617 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4618 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4619 | return EMULATION_RESTART; |
| 4620 | } |
| 4621 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4622 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4623 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4624 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4625 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4626 | |
| 4627 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4628 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4629 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4630 | if (rc == X86EMUL_INTERCEPTED) |
| 4631 | return EMULATION_INTERCEPTED; |
| 4632 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4633 | if (rc == X86EMUL_CONTINUE) |
| 4634 | writeback_registers(ctxt); |
| 4635 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4636 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4637 | |
| 4638 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4639 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4640 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4641 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4642 | break; |
| 4643 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4644 | case 0x0d: /* GrpP (prefetch) */ |
| 4645 | case 0x18: /* Grp16 (prefetch/nop) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4646 | break; |
| 4647 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4648 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4649 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4650 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4651 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4652 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4653 | case 0x40 ... 0x4f: /* cmov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4654 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
| 4655 | if (!test_cc(ctxt->b, ctxt->eflags)) |
| 4656 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4657 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4658 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4659 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4660 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4661 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4662 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4663 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4664 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 4665 | case 0xa4: /* shld imm8, r, r/m */ |
| 4666 | case 0xa5: /* shld cl, r, r/m */ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 4667 | emulate_2op_cl(ctxt, "shld"); |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 4668 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 4669 | case 0xac: /* shrd imm8, r, r/m */ |
| 4670 | case 0xad: /* shrd cl, r, r/m */ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 4671 | emulate_2op_cl(ctxt, "shrd"); |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 4672 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4673 | case 0xae: /* clflush */ |
| 4674 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4675 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4676 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4677 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4678 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4679 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4680 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4681 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4682 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4683 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4684 | break; |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4685 | case 0xc0 ... 0xc1: /* xadd */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 4686 | emulate_2op_SrcV(ctxt, "add"); |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4687 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4688 | ctxt->src.val = ctxt->dst.orig_val; |
| 4689 | write_register_operand(&ctxt->src); |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4690 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4691 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4692 | ctxt->dst.bytes = ctxt->op_bytes; |
| 4693 | ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val : |
| 4694 | (u64) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4695 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4696 | default: |
| 4697 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4698 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4699 | |
| 4700 | if (rc != X86EMUL_CONTINUE) |
| 4701 | goto done; |
| 4702 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4703 | goto writeback; |
| 4704 | |
| 4705 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4706 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4707 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4708 | |
| 4709 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4710 | { |
| 4711 | invalidate_registers(ctxt); |
| 4712 | } |
| 4713 | |
| 4714 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4715 | { |
| 4716 | writeback_registers(ctxt); |
| 4717 | } |