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 | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 29 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 30 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | /* |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
| 89 | #define DstImmUByte (OpImmUByte << DstShift) |
| 90 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 91 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 92 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 94 | #define SrcShift 6 |
| 95 | #define SrcNone (OpNone << SrcShift) |
| 96 | #define SrcReg (OpReg << SrcShift) |
| 97 | #define SrcMem (OpMem << SrcShift) |
| 98 | #define SrcMem16 (OpMem16 << SrcShift) |
| 99 | #define SrcMem32 (OpMem32 << SrcShift) |
| 100 | #define SrcImm (OpImm << SrcShift) |
| 101 | #define SrcImmByte (OpImmByte << SrcShift) |
| 102 | #define SrcOne (OpOne << SrcShift) |
| 103 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 104 | #define SrcImmU (OpImmU << SrcShift) |
| 105 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 106 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 107 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 108 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 109 | #define SrcAcc (OpAcc << SrcShift) |
| 110 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 111 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 112 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 113 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 114 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 115 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 116 | #define BitOp (1<<11) |
| 117 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 118 | #define String (1<<13) /* String instruction (rep capable) */ |
| 119 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 120 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 121 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 122 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 123 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 124 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 125 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 126 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 127 | /* Generic ModRM decode. */ |
| 128 | #define ModRM (1<<19) |
| 129 | /* Destination is only written; never read. */ |
| 130 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 131 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 132 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 133 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 134 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 135 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 136 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 137 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 138 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 139 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 140 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 141 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 142 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 143 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 144 | #define Src2None (OpNone << Src2Shift) |
Avi Kivity | ab2c5ce | 2013-02-09 11:31:46 +0200 | [diff] [blame] | 145 | #define Src2Mem (OpMem << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 146 | #define Src2CL (OpCL << Src2Shift) |
| 147 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 148 | #define Src2One (OpOne << Src2Shift) |
| 149 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 150 | #define Src2ES (OpES << Src2Shift) |
| 151 | #define Src2CS (OpCS << Src2Shift) |
| 152 | #define Src2SS (OpSS << Src2Shift) |
| 153 | #define Src2DS (OpDS << Src2Shift) |
| 154 | #define Src2FS (OpFS << Src2Shift) |
| 155 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 156 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 157 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 158 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 159 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 160 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 161 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 162 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 163 | #define SrcWrite ((u64)1 << 46) /* Write back src operand */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 164 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 165 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 166 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 167 | #define X2(x...) x, x |
| 168 | #define X3(x...) X2(x), x |
| 169 | #define X4(x...) X2(x), X2(x) |
| 170 | #define X5(x...) X4(x), x |
| 171 | #define X6(x...) X4(x), X2(x) |
| 172 | #define X7(x...) X4(x), X3(x) |
| 173 | #define X8(x...) X4(x), X4(x) |
| 174 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 175 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 176 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 177 | #define FASTOP_SIZE 8 |
| 178 | |
| 179 | /* |
| 180 | * fastop functions have a special calling convention: |
| 181 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 182 | * dst: rax (in/out) |
| 183 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 184 | * src2: rcx (in) |
| 185 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 186 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 187 | * |
| 188 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 189 | * different operand sizes can be reached by calculation, rather than a jump |
| 190 | * table (which would be bigger than the code). |
| 191 | * |
| 192 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 193 | * so they can't be called from C directly. |
| 194 | */ |
| 195 | |
| 196 | struct fastop; |
| 197 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 198 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 199 | u64 flags : 56; |
| 200 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 201 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 202 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 203 | const struct opcode *group; |
| 204 | const struct group_dual *gdual; |
| 205 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 206 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 207 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 208 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 209 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | struct group_dual { |
| 213 | struct opcode mod012[8]; |
| 214 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 215 | }; |
| 216 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 217 | struct gprefix { |
| 218 | struct opcode pfx_no; |
| 219 | struct opcode pfx_66; |
| 220 | struct opcode pfx_f2; |
| 221 | struct opcode pfx_f3; |
| 222 | }; |
| 223 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 224 | struct escape { |
| 225 | struct opcode op[8]; |
| 226 | struct opcode high[64]; |
| 227 | }; |
| 228 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 229 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 230 | #define EFLG_ID (1<<21) |
| 231 | #define EFLG_VIP (1<<20) |
| 232 | #define EFLG_VIF (1<<19) |
| 233 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 234 | #define EFLG_VM (1<<17) |
| 235 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 236 | #define EFLG_IOPL (3<<12) |
| 237 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 238 | #define EFLG_OF (1<<11) |
| 239 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 240 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 241 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 242 | #define EFLG_SF (1<<7) |
| 243 | #define EFLG_ZF (1<<6) |
| 244 | #define EFLG_AF (1<<4) |
| 245 | #define EFLG_PF (1<<2) |
| 246 | #define EFLG_CF (1<<0) |
| 247 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 248 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 249 | #define EFLG_RESERVED_ONE_MASK 2 |
| 250 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 251 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 252 | { |
| 253 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 254 | ctxt->regs_valid |= 1 << nr; |
| 255 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 256 | } |
| 257 | return ctxt->_regs[nr]; |
| 258 | } |
| 259 | |
| 260 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 261 | { |
| 262 | ctxt->regs_valid |= 1 << nr; |
| 263 | ctxt->regs_dirty |= 1 << nr; |
| 264 | return &ctxt->_regs[nr]; |
| 265 | } |
| 266 | |
| 267 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 268 | { |
| 269 | reg_read(ctxt, nr); |
| 270 | return reg_write(ctxt, nr); |
| 271 | } |
| 272 | |
| 273 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 274 | { |
| 275 | unsigned reg; |
| 276 | |
| 277 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 278 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 279 | } |
| 280 | |
| 281 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 282 | { |
| 283 | ctxt->regs_dirty = 0; |
| 284 | ctxt->regs_valid = 0; |
| 285 | } |
| 286 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 287 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 288 | * These EFLAGS bits are restored from saved value during emulation, and |
| 289 | * any changes are written back to the saved value after emulation. |
| 290 | */ |
| 291 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 292 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 293 | #ifdef CONFIG_X86_64 |
| 294 | #define ON64(x) x |
| 295 | #else |
| 296 | #define ON64(x) |
| 297 | #endif |
| 298 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 299 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 300 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 301 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 302 | #define FOP_RET "ret \n\t" |
| 303 | |
| 304 | #define FOP_START(op) \ |
| 305 | extern void em_##op(struct fastop *fake); \ |
| 306 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 307 | ".global em_" #op " \n\t" \ |
| 308 | FOP_ALIGN \ |
| 309 | "em_" #op ": \n\t" |
| 310 | |
| 311 | #define FOP_END \ |
| 312 | ".popsection") |
| 313 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 314 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 315 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 316 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 317 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 318 | |
| 319 | #define FOP1EEX(op, dst) \ |
| 320 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 321 | |
| 322 | #define FASTOP1(op) \ |
| 323 | FOP_START(op) \ |
| 324 | FOP1E(op##b, al) \ |
| 325 | FOP1E(op##w, ax) \ |
| 326 | FOP1E(op##l, eax) \ |
| 327 | ON64(FOP1E(op##q, rax)) \ |
| 328 | FOP_END |
| 329 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 330 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 331 | #define FASTOP1SRC2(op, name) \ |
| 332 | FOP_START(name) \ |
| 333 | FOP1E(op, cl) \ |
| 334 | FOP1E(op, cx) \ |
| 335 | FOP1E(op, ecx) \ |
| 336 | ON64(FOP1E(op, rcx)) \ |
| 337 | FOP_END |
| 338 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 339 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 340 | #define FASTOP1SRC2EX(op, name) \ |
| 341 | FOP_START(name) \ |
| 342 | FOP1EEX(op, cl) \ |
| 343 | FOP1EEX(op, cx) \ |
| 344 | FOP1EEX(op, ecx) \ |
| 345 | ON64(FOP1EEX(op, rcx)) \ |
| 346 | FOP_END |
| 347 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 348 | #define FOP2E(op, dst, src) \ |
| 349 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 350 | |
| 351 | #define FASTOP2(op) \ |
| 352 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 353 | FOP2E(op##b, al, dl) \ |
| 354 | FOP2E(op##w, ax, dx) \ |
| 355 | FOP2E(op##l, eax, edx) \ |
| 356 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 357 | FOP_END |
| 358 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 359 | /* 2 operand, word only */ |
| 360 | #define FASTOP2W(op) \ |
| 361 | FOP_START(op) \ |
| 362 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 363 | FOP2E(op##w, ax, dx) \ |
| 364 | FOP2E(op##l, eax, edx) \ |
| 365 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 366 | FOP_END |
| 367 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 368 | /* 2 operand, src is CL */ |
| 369 | #define FASTOP2CL(op) \ |
| 370 | FOP_START(op) \ |
| 371 | FOP2E(op##b, al, cl) \ |
| 372 | FOP2E(op##w, ax, cl) \ |
| 373 | FOP2E(op##l, eax, cl) \ |
| 374 | ON64(FOP2E(op##q, rax, cl)) \ |
| 375 | FOP_END |
| 376 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 377 | #define FOP3E(op, dst, src, src2) \ |
| 378 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 379 | |
| 380 | /* 3-operand, word-only, src2=cl */ |
| 381 | #define FASTOP3WCL(op) \ |
| 382 | FOP_START(op) \ |
| 383 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 384 | FOP3E(op##w, ax, dx, cl) \ |
| 385 | FOP3E(op##l, eax, edx, cl) \ |
| 386 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 387 | FOP_END |
| 388 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 389 | /* Special case for SETcc - 1 instruction per cc */ |
| 390 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 391 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 392 | asm(".global kvm_fastop_exception \n" |
| 393 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 394 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 395 | FOP_START(setcc) |
| 396 | FOP_SETCC(seto) |
| 397 | FOP_SETCC(setno) |
| 398 | FOP_SETCC(setc) |
| 399 | FOP_SETCC(setnc) |
| 400 | FOP_SETCC(setz) |
| 401 | FOP_SETCC(setnz) |
| 402 | FOP_SETCC(setbe) |
| 403 | FOP_SETCC(setnbe) |
| 404 | FOP_SETCC(sets) |
| 405 | FOP_SETCC(setns) |
| 406 | FOP_SETCC(setp) |
| 407 | FOP_SETCC(setnp) |
| 408 | FOP_SETCC(setl) |
| 409 | FOP_SETCC(setnl) |
| 410 | FOP_SETCC(setle) |
| 411 | FOP_SETCC(setnle) |
| 412 | FOP_END; |
| 413 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 414 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 415 | FOP_END; |
| 416 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 417 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 418 | enum x86_intercept intercept, |
| 419 | enum x86_intercept_stage stage) |
| 420 | { |
| 421 | struct x86_instruction_info info = { |
| 422 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 423 | .rep_prefix = ctxt->rep_prefix, |
| 424 | .modrm_mod = ctxt->modrm_mod, |
| 425 | .modrm_reg = ctxt->modrm_reg, |
| 426 | .modrm_rm = ctxt->modrm_rm, |
| 427 | .src_val = ctxt->src.val64, |
| 428 | .src_bytes = ctxt->src.bytes, |
| 429 | .dst_bytes = ctxt->dst.bytes, |
| 430 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 431 | .next_rip = ctxt->eip, |
| 432 | }; |
| 433 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 434 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 437 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 438 | { |
| 439 | *dest = (*dest & ~mask) | (src & mask); |
| 440 | } |
| 441 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 442 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 443 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 444 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 447 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 448 | { |
| 449 | u16 sel; |
| 450 | struct desc_struct ss; |
| 451 | |
| 452 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 453 | return ~0UL; |
| 454 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 455 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 456 | } |
| 457 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 458 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 459 | { |
| 460 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 461 | } |
| 462 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 463 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 464 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 465 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 466 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 467 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 468 | return reg; |
| 469 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 470 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 474 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 475 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 476 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 479 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 480 | { |
| 481 | assign_masked(reg, *reg + inc, mask); |
| 482 | } |
| 483 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 484 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 485 | 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] | 486 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 487 | ulong mask; |
| 488 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 489 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 490 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 491 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 492 | mask = ad_mask(ctxt); |
| 493 | masked_increment(reg, mask, inc); |
| 494 | } |
| 495 | |
| 496 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 497 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 498 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 499 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 500 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 501 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 502 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 503 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 504 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 505 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 506 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 507 | { |
| 508 | u32 limit = get_desc_limit(desc); |
| 509 | |
| 510 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 511 | } |
| 512 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 513 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 514 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 515 | ctxt->has_seg_override = true; |
| 516 | ctxt->seg_override = seg; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 517 | } |
| 518 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 519 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 520 | { |
| 521 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 522 | return 0; |
| 523 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 524 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 525 | } |
| 526 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 527 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 528 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 529 | if (!ctxt->has_seg_override) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 530 | return 0; |
| 531 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 532 | return ctxt->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 533 | } |
| 534 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 535 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 536 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 537 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 538 | ctxt->exception.vector = vec; |
| 539 | ctxt->exception.error_code = error; |
| 540 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 541 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 542 | } |
| 543 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 544 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 545 | { |
| 546 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 547 | } |
| 548 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 549 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 550 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 551 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 552 | } |
| 553 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 554 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 555 | { |
| 556 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 557 | } |
| 558 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 559 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 560 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 561 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 562 | } |
| 563 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 564 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 565 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 566 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 567 | } |
| 568 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 569 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 570 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 571 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 572 | } |
| 573 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 574 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 575 | { |
| 576 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 577 | } |
| 578 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 579 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 580 | { |
| 581 | u16 selector; |
| 582 | struct desc_struct desc; |
| 583 | |
| 584 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 585 | return selector; |
| 586 | } |
| 587 | |
| 588 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 589 | unsigned seg) |
| 590 | { |
| 591 | u16 dummy; |
| 592 | u32 base3; |
| 593 | struct desc_struct desc; |
| 594 | |
| 595 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 596 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 597 | } |
| 598 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 599 | /* |
| 600 | * x86 defines three classes of vector instructions: explicitly |
| 601 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 602 | * depending on whether they're AVX encoded or not. |
| 603 | * |
| 604 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 605 | * subject to the same check. |
| 606 | */ |
| 607 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 608 | { |
| 609 | if (likely(size < 16)) |
| 610 | return false; |
| 611 | |
| 612 | if (ctxt->d & Aligned) |
| 613 | return true; |
| 614 | else if (ctxt->d & Unaligned) |
| 615 | return false; |
| 616 | else if (ctxt->d & Avx) |
| 617 | return false; |
| 618 | else |
| 619 | return true; |
| 620 | } |
| 621 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 622 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 623 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 624 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 625 | ulong *linear) |
| 626 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 627 | struct desc_struct desc; |
| 628 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 629 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 630 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 631 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 632 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 633 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 634 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 635 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 636 | case X86EMUL_MODE_PROT64: |
| 637 | if (((signed long)la << 16) >> 16 != la) |
| 638 | return emulate_gp(ctxt, 0); |
| 639 | break; |
| 640 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 641 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 642 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 643 | if (!usable) |
| 644 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 645 | /* code segment in protected mode or read-only data segment */ |
| 646 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 647 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 648 | goto bad; |
| 649 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 650 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 651 | goto bad; |
| 652 | lim = desc_limit_scaled(&desc); |
| 653 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 654 | /* expand-up segment */ |
| 655 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 656 | goto bad; |
| 657 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 658 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 659 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 660 | goto bad; |
| 661 | lim = desc.d ? 0xffffffff : 0xffff; |
| 662 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 663 | goto bad; |
| 664 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 665 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 666 | if (!(desc.type & 8)) { |
| 667 | /* data segment */ |
| 668 | if (cpl > desc.dpl) |
| 669 | goto bad; |
| 670 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 671 | /* nonconforming code segment */ |
| 672 | if (cpl != desc.dpl) |
| 673 | goto bad; |
| 674 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 675 | /* conforming code segment */ |
| 676 | if (cpl < desc.dpl) |
| 677 | goto bad; |
| 678 | } |
| 679 | break; |
| 680 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 681 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 682 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 683 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 684 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 685 | *linear = la; |
| 686 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 687 | bad: |
| 688 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 689 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 690 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 691 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 692 | } |
| 693 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 694 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 695 | struct segmented_address addr, |
| 696 | unsigned size, bool write, |
| 697 | ulong *linear) |
| 698 | { |
| 699 | return __linearize(ctxt, addr, size, write, false, linear); |
| 700 | } |
| 701 | |
| 702 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 703 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 704 | struct segmented_address addr, |
| 705 | void *data, |
| 706 | unsigned size) |
| 707 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 708 | int rc; |
| 709 | ulong linear; |
| 710 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 711 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 712 | if (rc != X86EMUL_CONTINUE) |
| 713 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 714 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 717 | /* |
| 718 | * Fetch the next byte of the instruction being emulated which is pointed to |
| 719 | * by ctxt->_eip, then increment ctxt->_eip. |
| 720 | * |
| 721 | * Also prefetch the remaining bytes of the instruction without crossing page |
| 722 | * boundary if they are not in fetch_cache yet. |
| 723 | */ |
| 724 | 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] | 725 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 726 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 727 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 728 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 729 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 730 | if (ctxt->_eip == fc->end) { |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 731 | unsigned long linear; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 732 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 733 | .ea = ctxt->_eip }; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 734 | cur_size = fc->end - fc->start; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 735 | size = min(15UL - cur_size, |
| 736 | PAGE_SIZE - offset_in_page(ctxt->_eip)); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 737 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 738 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 739 | return rc; |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 740 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 741 | size, &ctxt->exception); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 742 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 743 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 744 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 745 | } |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 746 | *dest = fc->data[ctxt->_eip - fc->start]; |
| 747 | ctxt->_eip++; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 748 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 752 | void *dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 753 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 754 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 755 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 756 | /* x86 instructions are limited to 15 bytes. */ |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 757 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 758 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 759 | while (size--) { |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 760 | rc = do_insn_fetch_byte(ctxt, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 761 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 762 | return rc; |
| 763 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 764 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 767 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 768 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 769 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 770 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 771 | if (rc != X86EMUL_CONTINUE) \ |
| 772 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 773 | (_type)_x; \ |
| 774 | }) |
| 775 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 776 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 777 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 778 | if (rc != X86EMUL_CONTINUE) \ |
| 779 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 780 | }) |
| 781 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 782 | /* |
| 783 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 784 | * pointer into the block that addresses the relevant register. |
| 785 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 786 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 787 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 788 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 789 | { |
| 790 | void *p; |
| 791 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 792 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 793 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 794 | else |
| 795 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 796 | return p; |
| 797 | } |
| 798 | |
| 799 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 800 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 801 | u16 *size, unsigned long *address, int op_bytes) |
| 802 | { |
| 803 | int rc; |
| 804 | |
| 805 | if (op_bytes == 2) |
| 806 | op_bytes = 3; |
| 807 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 808 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 809 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 810 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 811 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 812 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 813 | return rc; |
| 814 | } |
| 815 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 816 | FASTOP2(add); |
| 817 | FASTOP2(or); |
| 818 | FASTOP2(adc); |
| 819 | FASTOP2(sbb); |
| 820 | FASTOP2(and); |
| 821 | FASTOP2(sub); |
| 822 | FASTOP2(xor); |
| 823 | FASTOP2(cmp); |
| 824 | FASTOP2(test); |
| 825 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 826 | FASTOP1SRC2(mul, mul_ex); |
| 827 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 828 | FASTOP1SRC2EX(div, div_ex); |
| 829 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 830 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 831 | FASTOP3WCL(shld); |
| 832 | FASTOP3WCL(shrd); |
| 833 | |
| 834 | FASTOP2W(imul); |
| 835 | |
| 836 | FASTOP1(not); |
| 837 | FASTOP1(neg); |
| 838 | FASTOP1(inc); |
| 839 | FASTOP1(dec); |
| 840 | |
| 841 | FASTOP2CL(rol); |
| 842 | FASTOP2CL(ror); |
| 843 | FASTOP2CL(rcl); |
| 844 | FASTOP2CL(rcr); |
| 845 | FASTOP2CL(shl); |
| 846 | FASTOP2CL(shr); |
| 847 | FASTOP2CL(sar); |
| 848 | |
| 849 | FASTOP2W(bsf); |
| 850 | FASTOP2W(bsr); |
| 851 | FASTOP2W(bt); |
| 852 | FASTOP2W(bts); |
| 853 | FASTOP2W(btr); |
| 854 | FASTOP2W(btc); |
| 855 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 856 | FASTOP2(xadd); |
| 857 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 858 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 859 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 860 | u8 rc; |
| 861 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 862 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 863 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 864 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 865 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 866 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 867 | } |
| 868 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 869 | static void fetch_register_operand(struct operand *op) |
| 870 | { |
| 871 | switch (op->bytes) { |
| 872 | case 1: |
| 873 | op->val = *(u8 *)op->addr.reg; |
| 874 | break; |
| 875 | case 2: |
| 876 | op->val = *(u16 *)op->addr.reg; |
| 877 | break; |
| 878 | case 4: |
| 879 | op->val = *(u32 *)op->addr.reg; |
| 880 | break; |
| 881 | case 8: |
| 882 | op->val = *(u64 *)op->addr.reg; |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 887 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 888 | { |
| 889 | ctxt->ops->get_fpu(ctxt); |
| 890 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 891 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 892 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 893 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 894 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 895 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 896 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 897 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 898 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 899 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 900 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 901 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 902 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 903 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 904 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 905 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 906 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 907 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 908 | #endif |
| 909 | default: BUG(); |
| 910 | } |
| 911 | ctxt->ops->put_fpu(ctxt); |
| 912 | } |
| 913 | |
| 914 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 915 | int reg) |
| 916 | { |
| 917 | ctxt->ops->get_fpu(ctxt); |
| 918 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 919 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 920 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 921 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 922 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 923 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 924 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 925 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 926 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 927 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 928 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 929 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 930 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 931 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 932 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 933 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 934 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 935 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 936 | #endif |
| 937 | default: BUG(); |
| 938 | } |
| 939 | ctxt->ops->put_fpu(ctxt); |
| 940 | } |
| 941 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 942 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 943 | { |
| 944 | ctxt->ops->get_fpu(ctxt); |
| 945 | switch (reg) { |
| 946 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 947 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 948 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 949 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 950 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 951 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 952 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 953 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 954 | default: BUG(); |
| 955 | } |
| 956 | ctxt->ops->put_fpu(ctxt); |
| 957 | } |
| 958 | |
| 959 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 960 | { |
| 961 | ctxt->ops->get_fpu(ctxt); |
| 962 | switch (reg) { |
| 963 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 964 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 965 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 966 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 967 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 968 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 969 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 970 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 971 | default: BUG(); |
| 972 | } |
| 973 | ctxt->ops->put_fpu(ctxt); |
| 974 | } |
| 975 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 976 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 977 | { |
| 978 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 979 | return emulate_nm(ctxt); |
| 980 | |
| 981 | ctxt->ops->get_fpu(ctxt); |
| 982 | asm volatile("fninit"); |
| 983 | ctxt->ops->put_fpu(ctxt); |
| 984 | return X86EMUL_CONTINUE; |
| 985 | } |
| 986 | |
| 987 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 988 | { |
| 989 | u16 fcw; |
| 990 | |
| 991 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 992 | return emulate_nm(ctxt); |
| 993 | |
| 994 | ctxt->ops->get_fpu(ctxt); |
| 995 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 996 | ctxt->ops->put_fpu(ctxt); |
| 997 | |
| 998 | /* force 2 byte destination */ |
| 999 | ctxt->dst.bytes = 2; |
| 1000 | ctxt->dst.val = fcw; |
| 1001 | |
| 1002 | return X86EMUL_CONTINUE; |
| 1003 | } |
| 1004 | |
| 1005 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1006 | { |
| 1007 | u16 fsw; |
| 1008 | |
| 1009 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1010 | return emulate_nm(ctxt); |
| 1011 | |
| 1012 | ctxt->ops->get_fpu(ctxt); |
| 1013 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1014 | ctxt->ops->put_fpu(ctxt); |
| 1015 | |
| 1016 | /* force 2 byte destination */ |
| 1017 | ctxt->dst.bytes = 2; |
| 1018 | ctxt->dst.val = fsw; |
| 1019 | |
| 1020 | return X86EMUL_CONTINUE; |
| 1021 | } |
| 1022 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1023 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1024 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1025 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1026 | unsigned reg = ctxt->modrm_reg; |
| 1027 | int highbyte_regs = ctxt->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1028 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1029 | if (!(ctxt->d & ModRM)) |
| 1030 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1031 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1032 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1033 | op->type = OP_XMM; |
| 1034 | op->bytes = 16; |
| 1035 | op->addr.xmm = reg; |
| 1036 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1037 | return; |
| 1038 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1039 | if (ctxt->d & Mmx) { |
| 1040 | reg &= 7; |
| 1041 | op->type = OP_MM; |
| 1042 | op->bytes = 8; |
| 1043 | op->addr.mm = reg; |
| 1044 | return; |
| 1045 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1046 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1047 | op->type = OP_REG; |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1048 | if (ctxt->d & ByteOp) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1049 | op->addr.reg = decode_register(ctxt, reg, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1050 | op->bytes = 1; |
| 1051 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1052 | op->addr.reg = decode_register(ctxt, reg, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1053 | op->bytes = ctxt->op_bytes; |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1054 | } |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1055 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1056 | op->orig_val = op->val; |
| 1057 | } |
| 1058 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1059 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1060 | { |
| 1061 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1062 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1063 | } |
| 1064 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1065 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1066 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1067 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1068 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1069 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1070 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1071 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1072 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1073 | if (ctxt->rex_prefix) { |
| 1074 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ |
| 1075 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ |
| 1076 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1079 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
| 1080 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
| 1081 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); |
| 1082 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1083 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1084 | if (ctxt->modrm_mod == 3) { |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1085 | int highbyte_regs = ctxt->rex_prefix == 0; |
| 1086 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1087 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1088 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1089 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
| 1090 | highbyte_regs && (ctxt->d & ByteOp)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1091 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1092 | op->type = OP_XMM; |
| 1093 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1094 | op->addr.xmm = ctxt->modrm_rm; |
| 1095 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1096 | return rc; |
| 1097 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1098 | if (ctxt->d & Mmx) { |
| 1099 | op->type = OP_MM; |
| 1100 | op->bytes = 8; |
| 1101 | op->addr.xmm = ctxt->modrm_rm & 7; |
| 1102 | return rc; |
| 1103 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1104 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1105 | return rc; |
| 1106 | } |
| 1107 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1108 | op->type = OP_MEM; |
| 1109 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1110 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1111 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1112 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1113 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1114 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1117 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1118 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1119 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1120 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1121 | break; |
| 1122 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1123 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1124 | break; |
| 1125 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1126 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1129 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1130 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1131 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1132 | break; |
| 1133 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1134 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1135 | break; |
| 1136 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1137 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1138 | break; |
| 1139 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1140 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1141 | break; |
| 1142 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1143 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1144 | break; |
| 1145 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1146 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1147 | break; |
| 1148 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1149 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1150 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1151 | break; |
| 1152 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1153 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1154 | break; |
| 1155 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1156 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1157 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1158 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1159 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1160 | } else { |
| 1161 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1162 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1163 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1164 | index_reg |= (sib >> 3) & 7; |
| 1165 | base_reg |= sib & 7; |
| 1166 | scale = sib >> 6; |
| 1167 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1168 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1169 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1170 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1171 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1172 | adjust_modrm_seg(ctxt, base_reg); |
| 1173 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1174 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1175 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1176 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1177 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1178 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1179 | } else { |
| 1180 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1181 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1182 | adjust_modrm_seg(ctxt, base_reg); |
| 1183 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1184 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1185 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1186 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1187 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1188 | break; |
| 1189 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1190 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1191 | break; |
| 1192 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1193 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1194 | break; |
| 1195 | } |
| 1196 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1197 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1198 | done: |
| 1199 | return rc; |
| 1200 | } |
| 1201 | |
| 1202 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1203 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1204 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1205 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1206 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1207 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1208 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1209 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1210 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1211 | break; |
| 1212 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1213 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1214 | break; |
| 1215 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1216 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1217 | break; |
| 1218 | } |
| 1219 | done: |
| 1220 | return rc; |
| 1221 | } |
| 1222 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1223 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1224 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1225 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1226 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1227 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
| 1228 | mask = ~(ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1229 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1230 | if (ctxt->src.bytes == 2) |
| 1231 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1232 | else if (ctxt->src.bytes == 4) |
| 1233 | sv = (s32)ctxt->src.val & (s32)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1234 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1235 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1236 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1237 | |
| 1238 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1239 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1242 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1243 | unsigned long addr, void *dest, unsigned size) |
| 1244 | { |
| 1245 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1246 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1247 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1248 | if (mc->pos < mc->end) |
| 1249 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1250 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1251 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1252 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1253 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1254 | &ctxt->exception); |
| 1255 | if (rc != X86EMUL_CONTINUE) |
| 1256 | return rc; |
| 1257 | |
| 1258 | mc->end += size; |
| 1259 | |
| 1260 | read_cached: |
| 1261 | memcpy(dest, mc->data + mc->pos, size); |
| 1262 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1263 | return X86EMUL_CONTINUE; |
| 1264 | } |
| 1265 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1266 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1267 | struct segmented_address addr, |
| 1268 | void *data, |
| 1269 | unsigned size) |
| 1270 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1271 | int rc; |
| 1272 | ulong linear; |
| 1273 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1274 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1275 | if (rc != X86EMUL_CONTINUE) |
| 1276 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1277 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1281 | struct segmented_address addr, |
| 1282 | const void *data, |
| 1283 | unsigned size) |
| 1284 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1285 | int rc; |
| 1286 | ulong linear; |
| 1287 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1288 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1289 | if (rc != X86EMUL_CONTINUE) |
| 1290 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1291 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1292 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1296 | struct segmented_address addr, |
| 1297 | const void *orig_data, const void *data, |
| 1298 | unsigned size) |
| 1299 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1300 | int rc; |
| 1301 | ulong linear; |
| 1302 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1303 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1304 | if (rc != X86EMUL_CONTINUE) |
| 1305 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1306 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1307 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1310 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1311 | unsigned int size, unsigned short port, |
| 1312 | void *dest) |
| 1313 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1314 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1315 | |
| 1316 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1317 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1318 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1319 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1320 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1321 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1322 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1323 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1324 | count); |
| 1325 | if (n == 0) |
| 1326 | n = 1; |
| 1327 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1328 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | rc->end = n * size; |
| 1331 | } |
| 1332 | |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1333 | if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) { |
| 1334 | ctxt->dst.data = rc->data + rc->pos; |
| 1335 | ctxt->dst.type = OP_MEM_STR; |
| 1336 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1337 | rc->pos = rc->end; |
| 1338 | } else { |
| 1339 | memcpy(dest, rc->data + rc->pos, size); |
| 1340 | rc->pos += size; |
| 1341 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1342 | return 1; |
| 1343 | } |
| 1344 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1345 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1346 | u16 index, struct desc_struct *desc) |
| 1347 | { |
| 1348 | struct desc_ptr dt; |
| 1349 | ulong addr; |
| 1350 | |
| 1351 | ctxt->ops->get_idt(ctxt, &dt); |
| 1352 | |
| 1353 | if (dt.size < index * 8 + 7) |
| 1354 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1355 | |
| 1356 | addr = dt.address + index * 8; |
| 1357 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1358 | &ctxt->exception); |
| 1359 | } |
| 1360 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1361 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1362 | u16 selector, struct desc_ptr *dt) |
| 1363 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1364 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1365 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1366 | if (selector & 1 << 2) { |
| 1367 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1368 | u16 sel; |
| 1369 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1370 | memset (dt, 0, sizeof *dt); |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1371 | if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1372 | return; |
| 1373 | |
| 1374 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1375 | dt->address = get_desc_base(&desc); |
| 1376 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1377 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* allowed just for 8 bytes segments */ |
| 1381 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1382 | u16 selector, struct desc_struct *desc, |
| 1383 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1384 | { |
| 1385 | struct desc_ptr dt; |
| 1386 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1387 | ulong addr; |
| 1388 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1389 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1390 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1391 | if (dt.size < index * 8 + 7) |
| 1392 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1393 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1394 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1395 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1396 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* allowed just for 8 bytes segments */ |
| 1400 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1401 | u16 selector, struct desc_struct *desc) |
| 1402 | { |
| 1403 | struct desc_ptr dt; |
| 1404 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1405 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1406 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1407 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1408 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1409 | if (dt.size < index * 8 + 7) |
| 1410 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1411 | |
| 1412 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1413 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1414 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1417 | /* Does not support long mode */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1418 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1419 | u16 selector, int seg) |
| 1420 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1421 | struct desc_struct seg_desc, old_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1422 | u8 dpl, rpl, cpl; |
| 1423 | unsigned err_vec = GP_VECTOR; |
| 1424 | u32 err_code = 0; |
| 1425 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1426 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1427 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1428 | u16 dummy; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1429 | |
| 1430 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1431 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1432 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1433 | /* set real mode segment descriptor (keep limit etc. for |
| 1434 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1435 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1436 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1437 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1438 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1439 | /* VM86 needs a clean new segment descriptor */ |
| 1440 | set_desc_base(&seg_desc, selector << 4); |
| 1441 | set_desc_limit(&seg_desc, 0xffff); |
| 1442 | seg_desc.type = 3; |
| 1443 | seg_desc.p = 1; |
| 1444 | seg_desc.s = 1; |
| 1445 | seg_desc.dpl = 3; |
| 1446 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1449 | rpl = selector & 3; |
| 1450 | cpl = ctxt->ops->cpl(ctxt); |
| 1451 | |
| 1452 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1453 | if ((seg == VCPU_SREG_CS |
| 1454 | || (seg == VCPU_SREG_SS |
| 1455 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1456 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1457 | && null_selector) |
| 1458 | goto exception; |
| 1459 | |
| 1460 | /* TR should be in GDT only */ |
| 1461 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1462 | goto exception; |
| 1463 | |
| 1464 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1465 | goto load; |
| 1466 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1467 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1468 | if (ret != X86EMUL_CONTINUE) |
| 1469 | return ret; |
| 1470 | |
| 1471 | err_code = selector & 0xfffc; |
| 1472 | err_vec = GP_VECTOR; |
| 1473 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1474 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1475 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1476 | goto exception; |
| 1477 | |
| 1478 | if (!seg_desc.p) { |
| 1479 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1480 | goto exception; |
| 1481 | } |
| 1482 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1483 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1484 | |
| 1485 | switch (seg) { |
| 1486 | case VCPU_SREG_SS: |
| 1487 | /* |
| 1488 | * segment is not a writable data segment or segment |
| 1489 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1490 | */ |
| 1491 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1492 | goto exception; |
| 1493 | break; |
| 1494 | case VCPU_SREG_CS: |
| 1495 | if (!(seg_desc.type & 8)) |
| 1496 | goto exception; |
| 1497 | |
| 1498 | if (seg_desc.type & 4) { |
| 1499 | /* conforming */ |
| 1500 | if (dpl > cpl) |
| 1501 | goto exception; |
| 1502 | } else { |
| 1503 | /* nonconforming */ |
| 1504 | if (rpl > cpl || dpl != cpl) |
| 1505 | goto exception; |
| 1506 | } |
| 1507 | /* CS(RPL) <- CPL */ |
| 1508 | selector = (selector & 0xfffc) | cpl; |
| 1509 | break; |
| 1510 | case VCPU_SREG_TR: |
| 1511 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1512 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1513 | old_desc = seg_desc; |
| 1514 | seg_desc.type |= 2; /* busy */ |
| 1515 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1516 | sizeof(seg_desc), &ctxt->exception); |
| 1517 | if (ret != X86EMUL_CONTINUE) |
| 1518 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1519 | break; |
| 1520 | case VCPU_SREG_LDTR: |
| 1521 | if (seg_desc.s || seg_desc.type != 2) |
| 1522 | goto exception; |
| 1523 | break; |
| 1524 | default: /* DS, ES, FS, or GS */ |
| 1525 | /* |
| 1526 | * segment is not a data or readable code segment or |
| 1527 | * ((segment is a data or nonconforming code segment) |
| 1528 | * and (both RPL and CPL > DPL)) |
| 1529 | */ |
| 1530 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1531 | (((seg_desc.type & 0xc) != 0xc) && |
| 1532 | (rpl > dpl && cpl > dpl))) |
| 1533 | goto exception; |
| 1534 | break; |
| 1535 | } |
| 1536 | |
| 1537 | if (seg_desc.s) { |
| 1538 | /* mark segment as accessed */ |
| 1539 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1540 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1541 | if (ret != X86EMUL_CONTINUE) |
| 1542 | return ret; |
| 1543 | } |
| 1544 | load: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1545 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1546 | return X86EMUL_CONTINUE; |
| 1547 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1548 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1549 | return X86EMUL_PROPAGATE_FAULT; |
| 1550 | } |
| 1551 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1552 | static void write_register_operand(struct operand *op) |
| 1553 | { |
| 1554 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1555 | switch (op->bytes) { |
| 1556 | case 1: |
| 1557 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1558 | break; |
| 1559 | case 2: |
| 1560 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1561 | break; |
| 1562 | case 4: |
| 1563 | *op->addr.reg = (u32)op->val; |
| 1564 | break; /* 64b: zero-extend */ |
| 1565 | case 8: |
| 1566 | *op->addr.reg = op->val; |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1571 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1572 | { |
| 1573 | int rc; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1574 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1575 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1576 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1577 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1578 | break; |
| 1579 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1580 | if (ctxt->lock_prefix) |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1581 | rc = segmented_cmpxchg(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1582 | op->addr.mem, |
| 1583 | &op->orig_val, |
| 1584 | &op->val, |
| 1585 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1586 | else |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1587 | rc = segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1588 | op->addr.mem, |
| 1589 | &op->val, |
| 1590 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1591 | if (rc != X86EMUL_CONTINUE) |
| 1592 | return rc; |
| 1593 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1594 | case OP_MEM_STR: |
| 1595 | rc = segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1596 | op->addr.mem, |
| 1597 | op->data, |
| 1598 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1599 | if (rc != X86EMUL_CONTINUE) |
| 1600 | return rc; |
| 1601 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1602 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1603 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1604 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1605 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1606 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1607 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1608 | case OP_NONE: |
| 1609 | /* no writeback */ |
| 1610 | break; |
| 1611 | default: |
| 1612 | break; |
| 1613 | } |
| 1614 | return X86EMUL_CONTINUE; |
| 1615 | } |
| 1616 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1617 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1618 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1619 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1620 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1621 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1622 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1623 | addr.seg = VCPU_SREG_SS; |
| 1624 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1625 | return segmented_write(ctxt, addr, data, bytes); |
| 1626 | } |
| 1627 | |
| 1628 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1629 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1630 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1631 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1632 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1633 | } |
| 1634 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1635 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1636 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1637 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1638 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1639 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1640 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1641 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1642 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1643 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1644 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1645 | return rc; |
| 1646 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1647 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1648 | return rc; |
| 1649 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1650 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1651 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1652 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1653 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1654 | } |
| 1655 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1656 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1657 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1658 | { |
| 1659 | int rc; |
| 1660 | unsigned long val, change_mask; |
| 1661 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1662 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1663 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1664 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1665 | if (rc != X86EMUL_CONTINUE) |
| 1666 | return rc; |
| 1667 | |
| 1668 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1669 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1670 | |
| 1671 | switch(ctxt->mode) { |
| 1672 | case X86EMUL_MODE_PROT64: |
| 1673 | case X86EMUL_MODE_PROT32: |
| 1674 | case X86EMUL_MODE_PROT16: |
| 1675 | if (cpl == 0) |
| 1676 | change_mask |= EFLG_IOPL; |
| 1677 | if (cpl <= iopl) |
| 1678 | change_mask |= EFLG_IF; |
| 1679 | break; |
| 1680 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1681 | if (iopl < 3) |
| 1682 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1683 | change_mask |= EFLG_IF; |
| 1684 | break; |
| 1685 | default: /* real mode */ |
| 1686 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1687 | break; |
| 1688 | } |
| 1689 | |
| 1690 | *(unsigned long *)dest = |
| 1691 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1692 | |
| 1693 | return rc; |
| 1694 | } |
| 1695 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1696 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1697 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1698 | ctxt->dst.type = OP_REG; |
| 1699 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1700 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1701 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1702 | } |
| 1703 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1704 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1705 | { |
| 1706 | int rc; |
| 1707 | unsigned frame_size = ctxt->src.val; |
| 1708 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1709 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1710 | |
| 1711 | if (nesting_level) |
| 1712 | return X86EMUL_UNHANDLEABLE; |
| 1713 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1714 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1715 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1716 | if (rc != X86EMUL_CONTINUE) |
| 1717 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1718 | 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] | 1719 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1720 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1721 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1722 | stack_mask(ctxt)); |
| 1723 | return X86EMUL_CONTINUE; |
| 1724 | } |
| 1725 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1726 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1727 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1728 | 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] | 1729 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1730 | 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] | 1731 | } |
| 1732 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1733 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1734 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1735 | int seg = ctxt->src2.val; |
| 1736 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1737 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1738 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1739 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1740 | } |
| 1741 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1742 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1743 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1744 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1745 | unsigned long selector; |
| 1746 | int rc; |
| 1747 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1748 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1749 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1750 | return rc; |
| 1751 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1752 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1753 | return rc; |
| 1754 | } |
| 1755 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1756 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1757 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1758 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1759 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1760 | int reg = VCPU_REGS_RAX; |
| 1761 | |
| 1762 | while (reg <= VCPU_REGS_RDI) { |
| 1763 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1764 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1765 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1766 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1767 | if (rc != X86EMUL_CONTINUE) |
| 1768 | return rc; |
| 1769 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1770 | ++reg; |
| 1771 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1772 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1773 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1776 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1777 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1778 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1779 | return em_push(ctxt); |
| 1780 | } |
| 1781 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1782 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1783 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1784 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1785 | int reg = VCPU_REGS_RDI; |
| 1786 | |
| 1787 | while (reg >= VCPU_REGS_RAX) { |
| 1788 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1789 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1790 | --reg; |
| 1791 | } |
| 1792 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1793 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1794 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1795 | break; |
| 1796 | --reg; |
| 1797 | } |
| 1798 | return rc; |
| 1799 | } |
| 1800 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1801 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1802 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1803 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1804 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1805 | struct desc_ptr dt; |
| 1806 | gva_t cs_addr; |
| 1807 | gva_t eip_addr; |
| 1808 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1809 | |
| 1810 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1811 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1812 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1813 | if (rc != X86EMUL_CONTINUE) |
| 1814 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1815 | |
| 1816 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1817 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1818 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1819 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1820 | if (rc != X86EMUL_CONTINUE) |
| 1821 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1822 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1823 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1824 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1825 | if (rc != X86EMUL_CONTINUE) |
| 1826 | return rc; |
| 1827 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1828 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1829 | |
| 1830 | eip_addr = dt.address + (irq << 2); |
| 1831 | cs_addr = dt.address + (irq << 2) + 2; |
| 1832 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1833 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1834 | if (rc != X86EMUL_CONTINUE) |
| 1835 | return rc; |
| 1836 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1837 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1838 | if (rc != X86EMUL_CONTINUE) |
| 1839 | return rc; |
| 1840 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1841 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1842 | if (rc != X86EMUL_CONTINUE) |
| 1843 | return rc; |
| 1844 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1845 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1846 | |
| 1847 | return rc; |
| 1848 | } |
| 1849 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1850 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1851 | { |
| 1852 | int rc; |
| 1853 | |
| 1854 | invalidate_registers(ctxt); |
| 1855 | rc = __emulate_int_real(ctxt, irq); |
| 1856 | if (rc == X86EMUL_CONTINUE) |
| 1857 | writeback_registers(ctxt); |
| 1858 | return rc; |
| 1859 | } |
| 1860 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1861 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1862 | { |
| 1863 | switch(ctxt->mode) { |
| 1864 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1865 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1866 | case X86EMUL_MODE_VM86: |
| 1867 | case X86EMUL_MODE_PROT16: |
| 1868 | case X86EMUL_MODE_PROT32: |
| 1869 | case X86EMUL_MODE_PROT64: |
| 1870 | default: |
| 1871 | /* Protected mode interrupts unimplemented yet */ |
| 1872 | return X86EMUL_UNHANDLEABLE; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1876 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1877 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1878 | int rc = X86EMUL_CONTINUE; |
| 1879 | unsigned long temp_eip = 0; |
| 1880 | unsigned long temp_eflags = 0; |
| 1881 | unsigned long cs = 0; |
| 1882 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1883 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1884 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1885 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1886 | |
| 1887 | /* TODO: Add stack limit check */ |
| 1888 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1889 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1890 | |
| 1891 | if (rc != X86EMUL_CONTINUE) |
| 1892 | return rc; |
| 1893 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1894 | if (temp_eip & ~0xffff) |
| 1895 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1896 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1897 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1898 | |
| 1899 | if (rc != X86EMUL_CONTINUE) |
| 1900 | return rc; |
| 1901 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1902 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1903 | |
| 1904 | if (rc != X86EMUL_CONTINUE) |
| 1905 | return rc; |
| 1906 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1907 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1908 | |
| 1909 | if (rc != X86EMUL_CONTINUE) |
| 1910 | return rc; |
| 1911 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1912 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1913 | |
| 1914 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1915 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1916 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1917 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1918 | ctxt->eflags &= ~0xffff; |
| 1919 | ctxt->eflags |= temp_eflags; |
| 1920 | } |
| 1921 | |
| 1922 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1923 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1924 | |
| 1925 | return rc; |
| 1926 | } |
| 1927 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 1928 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1929 | { |
| 1930 | switch(ctxt->mode) { |
| 1931 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1932 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1933 | case X86EMUL_MODE_VM86: |
| 1934 | case X86EMUL_MODE_PROT16: |
| 1935 | case X86EMUL_MODE_PROT32: |
| 1936 | case X86EMUL_MODE_PROT64: |
| 1937 | default: |
| 1938 | /* iret from protected mode unimplemented yet */ |
| 1939 | return X86EMUL_UNHANDLEABLE; |
| 1940 | } |
| 1941 | } |
| 1942 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1943 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 1944 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1945 | int rc; |
| 1946 | unsigned short sel; |
| 1947 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1948 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1949 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1950 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1951 | if (rc != X86EMUL_CONTINUE) |
| 1952 | return rc; |
| 1953 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1954 | ctxt->_eip = 0; |
| 1955 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1956 | return X86EMUL_CONTINUE; |
| 1957 | } |
| 1958 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1959 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1960 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1961 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1962 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1963 | switch (ctxt->modrm_reg) { |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1964 | case 2: /* call near abs */ { |
| 1965 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1966 | old_eip = ctxt->_eip; |
| 1967 | ctxt->_eip = ctxt->src.val; |
| 1968 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1969 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1970 | break; |
| 1971 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1972 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1973 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1974 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1975 | case 5: /* jmp far */ |
| 1976 | rc = em_jmp_far(ctxt); |
| 1977 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1978 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1979 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1980 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1981 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1982 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 1985 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1986 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1987 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1988 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1989 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 1990 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 1991 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 1992 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1993 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1994 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1995 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 1996 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1997 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1998 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1999 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2000 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2003 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2004 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2005 | ctxt->dst.type = OP_REG; |
| 2006 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2007 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2008 | return em_pop(ctxt); |
| 2009 | } |
| 2010 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2011 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2012 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2013 | int rc; |
| 2014 | unsigned long cs; |
| 2015 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2016 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2017 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2018 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2019 | if (ctxt->op_bytes == 4) |
| 2020 | ctxt->_eip = (u32)ctxt->_eip; |
| 2021 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2022 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2023 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2024 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2025 | return rc; |
| 2026 | } |
| 2027 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2028 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2029 | { |
| 2030 | int rc; |
| 2031 | |
| 2032 | rc = em_ret_far(ctxt); |
| 2033 | if (rc != X86EMUL_CONTINUE) |
| 2034 | return rc; |
| 2035 | rsp_increment(ctxt, ctxt->src.val); |
| 2036 | return X86EMUL_CONTINUE; |
| 2037 | } |
| 2038 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2039 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2040 | { |
| 2041 | /* Save real source value, then compare EAX against destination. */ |
| 2042 | ctxt->src.orig_val = ctxt->src.val; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2043 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX); |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2044 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2045 | |
| 2046 | if (ctxt->eflags & EFLG_ZF) { |
| 2047 | /* Success: write back to memory. */ |
| 2048 | ctxt->dst.val = ctxt->src.orig_val; |
| 2049 | } else { |
| 2050 | /* Failure: write the value we saw to EAX. */ |
| 2051 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2052 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2053 | } |
| 2054 | return X86EMUL_CONTINUE; |
| 2055 | } |
| 2056 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2057 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2058 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2059 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2060 | unsigned short sel; |
| 2061 | int rc; |
| 2062 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2063 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2064 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2065 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2066 | if (rc != X86EMUL_CONTINUE) |
| 2067 | return rc; |
| 2068 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2069 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2070 | return rc; |
| 2071 | } |
| 2072 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2073 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2074 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2075 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2076 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2077 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2078 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2079 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2080 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2081 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2082 | cs->s = 1; |
| 2083 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2084 | cs->p = 1; |
| 2085 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2086 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2087 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2088 | set_desc_base(ss, 0); /* flat segment */ |
| 2089 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2090 | ss->g = 1; /* 4kb granularity */ |
| 2091 | ss->s = 1; |
| 2092 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2093 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2094 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2095 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2096 | ss->l = 0; |
| 2097 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2100 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2101 | { |
| 2102 | u32 eax, ebx, ecx, edx; |
| 2103 | |
| 2104 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2105 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2106 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2107 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2108 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2109 | } |
| 2110 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2111 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2112 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2113 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2114 | u32 eax, ebx, ecx, edx; |
| 2115 | |
| 2116 | /* |
| 2117 | * syscall should always be enabled in longmode - so only become |
| 2118 | * vendor specific (cpuid) if other modes are active... |
| 2119 | */ |
| 2120 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2121 | return true; |
| 2122 | |
| 2123 | eax = 0x00000000; |
| 2124 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2125 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2126 | /* |
| 2127 | * Intel ("GenuineIntel") |
| 2128 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2129 | * longmode. Also an 64bit guest with a |
| 2130 | * 32bit compat-app running will #UD !! While this |
| 2131 | * behaviour can be fixed (by emulating) into AMD |
| 2132 | * response - CPUs of AMD can't behave like Intel. |
| 2133 | */ |
| 2134 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2135 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2136 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2137 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2138 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2139 | /* AMD ("AuthenticAMD") */ |
| 2140 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2141 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2142 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2143 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2144 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2145 | /* AMD ("AMDisbetter!") */ |
| 2146 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2147 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2148 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2149 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2150 | |
| 2151 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2152 | return false; |
| 2153 | } |
| 2154 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2155 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2156 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2157 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2158 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2159 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2160 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2161 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2162 | |
| 2163 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2164 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2165 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2166 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2167 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2168 | if (!(em_syscall_is_enabled(ctxt))) |
| 2169 | return emulate_ud(ctxt); |
| 2170 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2171 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2172 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2173 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2174 | if (!(efer & EFER_SCE)) |
| 2175 | return emulate_ud(ctxt); |
| 2176 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2177 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2178 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2179 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2180 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2181 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2182 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2183 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2184 | cs.l = 1; |
| 2185 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2186 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2187 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2188 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2189 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2190 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2191 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2192 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2193 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2194 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2195 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2196 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2197 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2198 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2199 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2200 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2201 | #endif |
| 2202 | } else { |
| 2203 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2204 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2205 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2206 | |
| 2207 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2208 | } |
| 2209 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2210 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2211 | } |
| 2212 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2213 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2214 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2215 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2216 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2217 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2218 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2219 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2220 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2221 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2222 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2223 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2224 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2225 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2226 | /* |
| 2227 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2228 | * mode). |
| 2229 | */ |
| 2230 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2231 | && !vendor_intel(ctxt)) |
| 2232 | return emulate_ud(ctxt); |
| 2233 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2234 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2235 | * Therefore, we inject an #UD. |
| 2236 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2237 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2238 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2239 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2240 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2241 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2242 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2243 | switch (ctxt->mode) { |
| 2244 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2245 | if ((msr_data & 0xfffc) == 0x0) |
| 2246 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2247 | break; |
| 2248 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2249 | if (msr_data == 0x0) |
| 2250 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2251 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2252 | default: |
| 2253 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2257 | cs_sel = (u16)msr_data; |
| 2258 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2259 | ss_sel = cs_sel + 8; |
| 2260 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2261 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2262 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2263 | cs.l = 1; |
| 2264 | } |
| 2265 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2266 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2267 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2268 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2269 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2270 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2271 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2272 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2273 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2274 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2275 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2278 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2279 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2280 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2281 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2282 | u64 msr_data; |
| 2283 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2284 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2285 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2286 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2287 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2288 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2289 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2290 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2291 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2292 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2293 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2294 | usermode = X86EMUL_MODE_PROT64; |
| 2295 | else |
| 2296 | usermode = X86EMUL_MODE_PROT32; |
| 2297 | |
| 2298 | cs.dpl = 3; |
| 2299 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2300 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2301 | switch (usermode) { |
| 2302 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2303 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2304 | if ((msr_data & 0xfffc) == 0x0) |
| 2305 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2306 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2307 | break; |
| 2308 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2309 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2310 | if (msr_data == 0x0) |
| 2311 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2312 | ss_sel = cs_sel + 8; |
| 2313 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2314 | cs.l = 1; |
| 2315 | break; |
| 2316 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2317 | cs_sel |= SELECTOR_RPL_MASK; |
| 2318 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2319 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2320 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2321 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2322 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2323 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2324 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2325 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2326 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2327 | } |
| 2328 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2329 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2330 | { |
| 2331 | int iopl; |
| 2332 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2333 | return false; |
| 2334 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2335 | return true; |
| 2336 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2337 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2341 | u16 port, u16 len) |
| 2342 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2343 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2344 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2345 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2346 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2347 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2348 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2349 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2350 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2351 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2352 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2353 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2354 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2355 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2356 | base = get_desc_base(&tr_seg); |
| 2357 | #ifdef CONFIG_X86_64 |
| 2358 | base |= ((u64)base3) << 32; |
| 2359 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2360 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2361 | if (r != X86EMUL_CONTINUE) |
| 2362 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2363 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2364 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2365 | 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] | 2366 | if (r != X86EMUL_CONTINUE) |
| 2367 | return false; |
| 2368 | if ((perm >> bit_idx) & mask) |
| 2369 | return false; |
| 2370 | return true; |
| 2371 | } |
| 2372 | |
| 2373 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2374 | u16 port, u16 len) |
| 2375 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2376 | if (ctxt->perm_ok) |
| 2377 | return true; |
| 2378 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2379 | if (emulator_bad_iopl(ctxt)) |
| 2380 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2381 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2382 | |
| 2383 | ctxt->perm_ok = true; |
| 2384 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2385 | return true; |
| 2386 | } |
| 2387 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2388 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2389 | struct tss_segment_16 *tss) |
| 2390 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2391 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2392 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2393 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2394 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2395 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2396 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2397 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2398 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2399 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2400 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2401 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2402 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2403 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2404 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2405 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2406 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2410 | struct tss_segment_16 *tss) |
| 2411 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2412 | int ret; |
| 2413 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2414 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2415 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2416 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2417 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2418 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2419 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2420 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2421 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2422 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2423 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2424 | |
| 2425 | /* |
| 2426 | * SDM says that segment selectors are loaded before segment |
| 2427 | * descriptors |
| 2428 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2429 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2430 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2431 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2432 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2433 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2434 | |
| 2435 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2436 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2437 | * it is handled in a context of new task |
| 2438 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2439 | ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2440 | if (ret != X86EMUL_CONTINUE) |
| 2441 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2442 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2443 | if (ret != X86EMUL_CONTINUE) |
| 2444 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2445 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2446 | if (ret != X86EMUL_CONTINUE) |
| 2447 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2448 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2449 | if (ret != X86EMUL_CONTINUE) |
| 2450 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2451 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2452 | if (ret != X86EMUL_CONTINUE) |
| 2453 | return ret; |
| 2454 | |
| 2455 | return X86EMUL_CONTINUE; |
| 2456 | } |
| 2457 | |
| 2458 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2459 | u16 tss_selector, u16 old_tss_sel, |
| 2460 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2461 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2462 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2463 | struct tss_segment_16 tss_seg; |
| 2464 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2465 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2466 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2467 | 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] | 2468 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2469 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2470 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2471 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2472 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2473 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2474 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2475 | 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] | 2476 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2477 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2478 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2479 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2480 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2481 | 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] | 2482 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2483 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2484 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2485 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2486 | |
| 2487 | if (old_tss_sel != 0xffff) { |
| 2488 | tss_seg.prev_task_link = old_tss_sel; |
| 2489 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2490 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2491 | &tss_seg.prev_task_link, |
| 2492 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2493 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2494 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2495 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2496 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2499 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2503 | struct tss_segment_32 *tss) |
| 2504 | { |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2505 | tss->cr3 = ctxt->ops->get_cr(ctxt, 3); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2506 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2507 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2508 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2509 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2510 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2511 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2512 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2513 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2514 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2515 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2516 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2517 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2518 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2519 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2520 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2521 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2522 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
| 2523 | tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2527 | struct tss_segment_32 *tss) |
| 2528 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2529 | int ret; |
| 2530 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2531 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2532 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2533 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2534 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2535 | |
| 2536 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2537 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2538 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2539 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2540 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2541 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2542 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2543 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2544 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2545 | |
| 2546 | /* |
| 2547 | * SDM says that segment selectors are loaded before segment |
| 2548 | * descriptors |
| 2549 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2550 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2551 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2552 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2553 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2554 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2555 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2556 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2557 | |
| 2558 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2559 | * If we're switching between Protected Mode and VM86, we need to make |
| 2560 | * sure to update the mode before loading the segment descriptors so |
| 2561 | * that the selectors are interpreted correctly. |
| 2562 | * |
| 2563 | * Need to get rflags to the vcpu struct immediately because it |
| 2564 | * influences the CPL which is checked at least when loading the segment |
| 2565 | * descriptors and when pushing an error code to the new kernel stack. |
| 2566 | * |
| 2567 | * TODO Introduce a separate ctxt->ops->set_cpl callback |
| 2568 | */ |
| 2569 | if (ctxt->eflags & X86_EFLAGS_VM) |
| 2570 | ctxt->mode = X86EMUL_MODE_VM86; |
| 2571 | else |
| 2572 | ctxt->mode = X86EMUL_MODE_PROT32; |
| 2573 | |
| 2574 | ctxt->ops->set_rflags(ctxt, ctxt->eflags); |
| 2575 | |
| 2576 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2577 | * Now load segment descriptors. If fault happenes at this stage |
| 2578 | * it is handled in a context of new task |
| 2579 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2580 | ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2581 | if (ret != X86EMUL_CONTINUE) |
| 2582 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2583 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2584 | if (ret != X86EMUL_CONTINUE) |
| 2585 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2586 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2587 | if (ret != X86EMUL_CONTINUE) |
| 2588 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2589 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2590 | if (ret != X86EMUL_CONTINUE) |
| 2591 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2592 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2593 | if (ret != X86EMUL_CONTINUE) |
| 2594 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2595 | ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2596 | if (ret != X86EMUL_CONTINUE) |
| 2597 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2598 | ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2599 | if (ret != X86EMUL_CONTINUE) |
| 2600 | return ret; |
| 2601 | |
| 2602 | return X86EMUL_CONTINUE; |
| 2603 | } |
| 2604 | |
| 2605 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2606 | u16 tss_selector, u16 old_tss_sel, |
| 2607 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2608 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2609 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2610 | struct tss_segment_32 tss_seg; |
| 2611 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2612 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2613 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2614 | 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] | 2615 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2616 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2617 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2618 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2619 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2620 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2621 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2622 | 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] | 2623 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2624 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2625 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2626 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2627 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2628 | 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] | 2629 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2630 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2632 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2633 | |
| 2634 | if (old_tss_sel != 0xffff) { |
| 2635 | tss_seg.prev_task_link = old_tss_sel; |
| 2636 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2637 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2638 | &tss_seg.prev_task_link, |
| 2639 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2640 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2641 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2642 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2643 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2646 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2650 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2651 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2652 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2653 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2654 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2655 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2656 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2657 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2658 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2659 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2660 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2661 | |
| 2662 | /* FIXME: old_tss_base == ~0 ? */ |
| 2663 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2664 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2665 | if (ret != X86EMUL_CONTINUE) |
| 2666 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2667 | 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] | 2668 | if (ret != X86EMUL_CONTINUE) |
| 2669 | return ret; |
| 2670 | |
| 2671 | /* FIXME: check that next_tss_desc is tss */ |
| 2672 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2673 | /* |
| 2674 | * Check privileges. The three cases are task switch caused by... |
| 2675 | * |
| 2676 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2677 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2678 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2679 | */ |
| 2680 | if (reason == TASK_SWITCH_GATE) { |
| 2681 | if (idt_index != -1) { |
| 2682 | /* Software interrupts */ |
| 2683 | struct desc_struct task_gate_desc; |
| 2684 | int dpl; |
| 2685 | |
| 2686 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2687 | &task_gate_desc); |
| 2688 | if (ret != X86EMUL_CONTINUE) |
| 2689 | return ret; |
| 2690 | |
| 2691 | dpl = task_gate_desc.dpl; |
| 2692 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2693 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2694 | } |
| 2695 | } else if (reason != TASK_SWITCH_IRET) { |
| 2696 | int dpl = next_tss_desc.dpl; |
| 2697 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2698 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2699 | } |
| 2700 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2701 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2702 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2703 | if (!next_tss_desc.p || |
| 2704 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2705 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2706 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2707 | return X86EMUL_PROPAGATE_FAULT; |
| 2708 | } |
| 2709 | |
| 2710 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2711 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2712 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | if (reason == TASK_SWITCH_IRET) |
| 2716 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2717 | |
| 2718 | /* 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] | 2719 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2720 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2721 | old_tss_sel = 0xffff; |
| 2722 | |
| 2723 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2724 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2725 | old_tss_base, &next_tss_desc); |
| 2726 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2727 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2728 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2729 | if (ret != X86EMUL_CONTINUE) |
| 2730 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2731 | |
| 2732 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2733 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2734 | |
| 2735 | if (reason != TASK_SWITCH_IRET) { |
| 2736 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2737 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2740 | 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] | 2741 | 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] | 2742 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2743 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2744 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2745 | ctxt->lock_prefix = 0; |
| 2746 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2747 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2750 | return ret; |
| 2751 | } |
| 2752 | |
| 2753 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2754 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2755 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2756 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2757 | int rc; |
| 2758 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2759 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2760 | ctxt->_eip = ctxt->eip; |
| 2761 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2762 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2763 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2764 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2765 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2766 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2767 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2768 | writeback_registers(ctxt); |
| 2769 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2770 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2771 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2774 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2775 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2776 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2777 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2778 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2779 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2780 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2781 | } |
| 2782 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2783 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2784 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2785 | u8 al, old_al; |
| 2786 | bool af, cf, old_cf; |
| 2787 | |
| 2788 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2789 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2790 | |
| 2791 | old_al = al; |
| 2792 | old_cf = cf; |
| 2793 | cf = false; |
| 2794 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2795 | if ((al & 0x0f) > 9 || af) { |
| 2796 | al -= 6; |
| 2797 | cf = old_cf | (al >= 250); |
| 2798 | af = true; |
| 2799 | } else { |
| 2800 | af = false; |
| 2801 | } |
| 2802 | if (old_al > 0x99 || old_cf) { |
| 2803 | al -= 0x60; |
| 2804 | cf = true; |
| 2805 | } |
| 2806 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2807 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2808 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2809 | ctxt->src.type = OP_IMM; |
| 2810 | ctxt->src.val = 0; |
| 2811 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2812 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2813 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2814 | if (cf) |
| 2815 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2816 | if (af) |
| 2817 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2818 | return X86EMUL_CONTINUE; |
| 2819 | } |
| 2820 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2821 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2822 | { |
| 2823 | u8 al, ah; |
| 2824 | |
| 2825 | if (ctxt->src.val == 0) |
| 2826 | return emulate_de(ctxt); |
| 2827 | |
| 2828 | al = ctxt->dst.val & 0xff; |
| 2829 | ah = al / ctxt->src.val; |
| 2830 | al %= ctxt->src.val; |
| 2831 | |
| 2832 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 2833 | |
| 2834 | /* Set PF, ZF, SF */ |
| 2835 | ctxt->src.type = OP_IMM; |
| 2836 | ctxt->src.val = 0; |
| 2837 | ctxt->src.bytes = 1; |
| 2838 | fastop(ctxt, em_or); |
| 2839 | |
| 2840 | return X86EMUL_CONTINUE; |
| 2841 | } |
| 2842 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2843 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2844 | { |
| 2845 | u8 al = ctxt->dst.val & 0xff; |
| 2846 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2847 | |
| 2848 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2849 | |
| 2850 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2851 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 2852 | /* Set PF, ZF, SF */ |
| 2853 | ctxt->src.type = OP_IMM; |
| 2854 | ctxt->src.val = 0; |
| 2855 | ctxt->src.bytes = 1; |
| 2856 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2857 | |
| 2858 | return X86EMUL_CONTINUE; |
| 2859 | } |
| 2860 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 2861 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 2862 | { |
| 2863 | long rel = ctxt->src.val; |
| 2864 | |
| 2865 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 2866 | jmp_rel(ctxt, rel); |
| 2867 | return em_push(ctxt); |
| 2868 | } |
| 2869 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2870 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2871 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2872 | u16 sel, old_cs; |
| 2873 | ulong old_eip; |
| 2874 | int rc; |
| 2875 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2876 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2877 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2878 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2879 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2880 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2881 | return X86EMUL_CONTINUE; |
| 2882 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2883 | ctxt->_eip = 0; |
| 2884 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2885 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2886 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2887 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2888 | if (rc != X86EMUL_CONTINUE) |
| 2889 | return rc; |
| 2890 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2891 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2892 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2893 | } |
| 2894 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2895 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2896 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2897 | int rc; |
| 2898 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2899 | ctxt->dst.type = OP_REG; |
| 2900 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2901 | ctxt->dst.bytes = ctxt->op_bytes; |
| 2902 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2903 | if (rc != X86EMUL_CONTINUE) |
| 2904 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 2905 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2906 | return X86EMUL_CONTINUE; |
| 2907 | } |
| 2908 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2909 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 2910 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2911 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2912 | ctxt->src.val = ctxt->dst.val; |
| 2913 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2914 | |
| 2915 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2916 | ctxt->dst.val = ctxt->src.orig_val; |
| 2917 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2918 | return X86EMUL_CONTINUE; |
| 2919 | } |
| 2920 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2921 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2922 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2923 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 2924 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2925 | } |
| 2926 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2927 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2928 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2929 | ctxt->dst.type = OP_REG; |
| 2930 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2931 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2932 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2933 | |
| 2934 | return X86EMUL_CONTINUE; |
| 2935 | } |
| 2936 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2937 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2938 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2939 | u64 tsc = 0; |
| 2940 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2941 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2942 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 2943 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2944 | return X86EMUL_CONTINUE; |
| 2945 | } |
| 2946 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2947 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 2948 | { |
| 2949 | u64 pmc; |
| 2950 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2951 | 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] | 2952 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2953 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 2954 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2955 | return X86EMUL_CONTINUE; |
| 2956 | } |
| 2957 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2958 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 2959 | { |
Stefan Hajnoczi | 49597d8 | 2012-04-09 18:40:00 +0300 | [diff] [blame] | 2960 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2961 | return X86EMUL_CONTINUE; |
| 2962 | } |
| 2963 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 2964 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 2965 | { |
| 2966 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 2967 | return emulate_gp(ctxt, 0); |
| 2968 | |
| 2969 | /* Disable writeback. */ |
| 2970 | ctxt->dst.type = OP_NONE; |
| 2971 | return X86EMUL_CONTINUE; |
| 2972 | } |
| 2973 | |
| 2974 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 2975 | { |
| 2976 | unsigned long val; |
| 2977 | |
| 2978 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2979 | val = ctxt->src.val & ~0ULL; |
| 2980 | else |
| 2981 | val = ctxt->src.val & ~0U; |
| 2982 | |
| 2983 | /* #UD condition is already handled. */ |
| 2984 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 2985 | return emulate_gp(ctxt, 0); |
| 2986 | |
| 2987 | /* Disable writeback. */ |
| 2988 | ctxt->dst.type = OP_NONE; |
| 2989 | return X86EMUL_CONTINUE; |
| 2990 | } |
| 2991 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 2992 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 2993 | { |
| 2994 | u64 msr_data; |
| 2995 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2996 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 2997 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 2998 | 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] | 2999 | return emulate_gp(ctxt, 0); |
| 3000 | |
| 3001 | return X86EMUL_CONTINUE; |
| 3002 | } |
| 3003 | |
| 3004 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3005 | { |
| 3006 | u64 msr_data; |
| 3007 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3008 | 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] | 3009 | return emulate_gp(ctxt, 0); |
| 3010 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3011 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3012 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3013 | return X86EMUL_CONTINUE; |
| 3014 | } |
| 3015 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3016 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3017 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3018 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3019 | return emulate_ud(ctxt); |
| 3020 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3021 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3022 | return X86EMUL_CONTINUE; |
| 3023 | } |
| 3024 | |
| 3025 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3026 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3027 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3028 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3029 | 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] | 3030 | return emulate_ud(ctxt); |
| 3031 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3032 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3033 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3034 | |
| 3035 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3036 | ctxt->dst.type = OP_NONE; |
| 3037 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3038 | } |
| 3039 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3040 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3041 | { |
| 3042 | u16 sel = ctxt->src.val; |
| 3043 | |
| 3044 | /* Disable writeback. */ |
| 3045 | ctxt->dst.type = OP_NONE; |
| 3046 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3047 | } |
| 3048 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3049 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3050 | { |
| 3051 | u16 sel = ctxt->src.val; |
| 3052 | |
| 3053 | /* Disable writeback. */ |
| 3054 | ctxt->dst.type = OP_NONE; |
| 3055 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3056 | } |
| 3057 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3058 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3059 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3060 | int rc; |
| 3061 | ulong linear; |
| 3062 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3063 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3064 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3065 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3066 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3067 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3068 | return X86EMUL_CONTINUE; |
| 3069 | } |
| 3070 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3071 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3072 | { |
| 3073 | ulong cr0; |
| 3074 | |
| 3075 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3076 | cr0 &= ~X86_CR0_TS; |
| 3077 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3078 | return X86EMUL_CONTINUE; |
| 3079 | } |
| 3080 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3081 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3082 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3083 | int rc; |
| 3084 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3085 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3086 | return X86EMUL_UNHANDLEABLE; |
| 3087 | |
| 3088 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3089 | if (rc != X86EMUL_CONTINUE) |
| 3090 | return rc; |
| 3091 | |
| 3092 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3093 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3094 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3095 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3096 | return X86EMUL_CONTINUE; |
| 3097 | } |
| 3098 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3099 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3100 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3101 | struct desc_ptr *ptr)) |
| 3102 | { |
| 3103 | struct desc_ptr desc_ptr; |
| 3104 | |
| 3105 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3106 | ctxt->op_bytes = 8; |
| 3107 | get(ctxt, &desc_ptr); |
| 3108 | if (ctxt->op_bytes == 2) { |
| 3109 | ctxt->op_bytes = 4; |
| 3110 | desc_ptr.address &= 0x00ffffff; |
| 3111 | } |
| 3112 | /* Disable writeback. */ |
| 3113 | ctxt->dst.type = OP_NONE; |
| 3114 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3115 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3116 | } |
| 3117 | |
| 3118 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3119 | { |
| 3120 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3121 | } |
| 3122 | |
| 3123 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3124 | { |
| 3125 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3126 | } |
| 3127 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3128 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3129 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3130 | struct desc_ptr desc_ptr; |
| 3131 | int rc; |
| 3132 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3133 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3134 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3135 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3136 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3137 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3138 | if (rc != X86EMUL_CONTINUE) |
| 3139 | return rc; |
| 3140 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3141 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3142 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3143 | return X86EMUL_CONTINUE; |
| 3144 | } |
| 3145 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3146 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3147 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3148 | int rc; |
| 3149 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3150 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3151 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3152 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3153 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3154 | return rc; |
| 3155 | } |
| 3156 | |
| 3157 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3158 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3159 | struct desc_ptr desc_ptr; |
| 3160 | int rc; |
| 3161 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3162 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3163 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3164 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3165 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3166 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3167 | if (rc != X86EMUL_CONTINUE) |
| 3168 | return rc; |
| 3169 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3170 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3171 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3172 | return X86EMUL_CONTINUE; |
| 3173 | } |
| 3174 | |
| 3175 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3176 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3177 | ctxt->dst.bytes = 2; |
| 3178 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3179 | return X86EMUL_CONTINUE; |
| 3180 | } |
| 3181 | |
| 3182 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3183 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3184 | 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] | 3185 | | (ctxt->src.val & 0x0f)); |
| 3186 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3187 | return X86EMUL_CONTINUE; |
| 3188 | } |
| 3189 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3190 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3191 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3192 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3193 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3194 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3195 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3196 | |
| 3197 | return X86EMUL_CONTINUE; |
| 3198 | } |
| 3199 | |
| 3200 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3201 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3202 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3203 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3204 | |
| 3205 | return X86EMUL_CONTINUE; |
| 3206 | } |
| 3207 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3208 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3209 | { |
| 3210 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3211 | &ctxt->dst.val)) |
| 3212 | return X86EMUL_IO_NEEDED; |
| 3213 | |
| 3214 | return X86EMUL_CONTINUE; |
| 3215 | } |
| 3216 | |
| 3217 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3218 | { |
| 3219 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3220 | &ctxt->src.val, 1); |
| 3221 | /* Disable writeback. */ |
| 3222 | ctxt->dst.type = OP_NONE; |
| 3223 | return X86EMUL_CONTINUE; |
| 3224 | } |
| 3225 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3226 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3227 | { |
| 3228 | if (emulator_bad_iopl(ctxt)) |
| 3229 | return emulate_gp(ctxt, 0); |
| 3230 | |
| 3231 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3232 | return X86EMUL_CONTINUE; |
| 3233 | } |
| 3234 | |
| 3235 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3236 | { |
| 3237 | if (emulator_bad_iopl(ctxt)) |
| 3238 | return emulate_gp(ctxt, 0); |
| 3239 | |
| 3240 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3241 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3242 | return X86EMUL_CONTINUE; |
| 3243 | } |
| 3244 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3245 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3246 | { |
| 3247 | u32 eax, ebx, ecx, edx; |
| 3248 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3249 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3250 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3251 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3252 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3253 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3254 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3255 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3256 | return X86EMUL_CONTINUE; |
| 3257 | } |
| 3258 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3259 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3260 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3261 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3262 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3263 | return X86EMUL_CONTINUE; |
| 3264 | } |
| 3265 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3266 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3267 | { |
| 3268 | switch (ctxt->op_bytes) { |
| 3269 | #ifdef CONFIG_X86_64 |
| 3270 | case 8: |
| 3271 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3272 | break; |
| 3273 | #endif |
| 3274 | default: |
| 3275 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3276 | break; |
| 3277 | } |
| 3278 | return X86EMUL_CONTINUE; |
| 3279 | } |
| 3280 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3281 | static bool valid_cr(int nr) |
| 3282 | { |
| 3283 | switch (nr) { |
| 3284 | case 0: |
| 3285 | case 2 ... 4: |
| 3286 | case 8: |
| 3287 | return true; |
| 3288 | default: |
| 3289 | return false; |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3294 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3295 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3296 | return emulate_ud(ctxt); |
| 3297 | |
| 3298 | return X86EMUL_CONTINUE; |
| 3299 | } |
| 3300 | |
| 3301 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3302 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3303 | u64 new_val = ctxt->src.val64; |
| 3304 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3305 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3306 | |
| 3307 | static u64 cr_reserved_bits[] = { |
| 3308 | 0xffffffff00000000ULL, |
| 3309 | 0, 0, 0, /* CR3 checked later */ |
| 3310 | CR4_RESERVED_BITS, |
| 3311 | 0, 0, 0, |
| 3312 | CR8_RESERVED_BITS, |
| 3313 | }; |
| 3314 | |
| 3315 | if (!valid_cr(cr)) |
| 3316 | return emulate_ud(ctxt); |
| 3317 | |
| 3318 | if (new_val & cr_reserved_bits[cr]) |
| 3319 | return emulate_gp(ctxt, 0); |
| 3320 | |
| 3321 | switch (cr) { |
| 3322 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3323 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3324 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3325 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3326 | return emulate_gp(ctxt, 0); |
| 3327 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3328 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3329 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3330 | |
| 3331 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3332 | !(cr4 & X86_CR4_PAE)) |
| 3333 | return emulate_gp(ctxt, 0); |
| 3334 | |
| 3335 | break; |
| 3336 | } |
| 3337 | case 3: { |
| 3338 | u64 rsvd = 0; |
| 3339 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3340 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3341 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3342 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3343 | else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3344 | rsvd = CR3_PAE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3345 | else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3346 | rsvd = CR3_NONPAE_RESERVED_BITS; |
| 3347 | |
| 3348 | if (new_val & rsvd) |
| 3349 | return emulate_gp(ctxt, 0); |
| 3350 | |
| 3351 | break; |
| 3352 | } |
| 3353 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3354 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3355 | |
| 3356 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3357 | return emulate_gp(ctxt, 0); |
| 3358 | |
| 3359 | break; |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | return X86EMUL_CONTINUE; |
| 3364 | } |
| 3365 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3366 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3367 | { |
| 3368 | unsigned long dr7; |
| 3369 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3370 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3371 | |
| 3372 | /* Check if DR7.Global_Enable is set */ |
| 3373 | return dr7 & (1 << 13); |
| 3374 | } |
| 3375 | |
| 3376 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3377 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3378 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3379 | u64 cr4; |
| 3380 | |
| 3381 | if (dr > 7) |
| 3382 | return emulate_ud(ctxt); |
| 3383 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3384 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3385 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3386 | return emulate_ud(ctxt); |
| 3387 | |
| 3388 | if (check_dr7_gd(ctxt)) |
| 3389 | return emulate_db(ctxt); |
| 3390 | |
| 3391 | return X86EMUL_CONTINUE; |
| 3392 | } |
| 3393 | |
| 3394 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3395 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3396 | u64 new_val = ctxt->src.val64; |
| 3397 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3398 | |
| 3399 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3400 | return emulate_gp(ctxt, 0); |
| 3401 | |
| 3402 | return check_dr_read(ctxt); |
| 3403 | } |
| 3404 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3405 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3406 | { |
| 3407 | u64 efer; |
| 3408 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3409 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3410 | |
| 3411 | if (!(efer & EFER_SVME)) |
| 3412 | return emulate_ud(ctxt); |
| 3413 | |
| 3414 | return X86EMUL_CONTINUE; |
| 3415 | } |
| 3416 | |
| 3417 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3418 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3419 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3420 | |
| 3421 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3422 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3423 | return emulate_gp(ctxt, 0); |
| 3424 | |
| 3425 | return check_svme(ctxt); |
| 3426 | } |
| 3427 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3428 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3429 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3430 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3431 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3432 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3433 | return emulate_ud(ctxt); |
| 3434 | |
| 3435 | return X86EMUL_CONTINUE; |
| 3436 | } |
| 3437 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3438 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3439 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3440 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3441 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3442 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3443 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3444 | (rcx > 3)) |
| 3445 | return emulate_gp(ctxt, 0); |
| 3446 | |
| 3447 | return X86EMUL_CONTINUE; |
| 3448 | } |
| 3449 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3450 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3451 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3452 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3453 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3454 | return emulate_gp(ctxt, 0); |
| 3455 | |
| 3456 | return X86EMUL_CONTINUE; |
| 3457 | } |
| 3458 | |
| 3459 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3460 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3461 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3462 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3463 | return emulate_gp(ctxt, 0); |
| 3464 | |
| 3465 | return X86EMUL_CONTINUE; |
| 3466 | } |
| 3467 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3468 | #define D(_y) { .flags = (_y) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3469 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3470 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
| 3471 | .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3472 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3473 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3474 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3475 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3476 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3477 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3478 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3479 | #define II(_f, _e, _i) \ |
| 3480 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3481 | #define IIP(_f, _e, _i, _p) \ |
| 3482 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ |
| 3483 | .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3484 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3485 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3486 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3487 | #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] | 3488 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3489 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3490 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3491 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3492 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3493 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3494 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3495 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3496 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3497 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3498 | DI(SrcNone | Priv, monitor), |
| 3499 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3500 | N, N, N, N, N, N, |
| 3501 | }; |
| 3502 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3503 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3504 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
| 3505 | II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall), |
| 3506 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3507 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3508 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3509 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3510 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3511 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3512 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3513 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3514 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3515 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3516 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3517 | N, N, N, N, N, N, |
| 3518 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3519 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3520 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3521 | F(Lock, em_add), |
| 3522 | F(Lock | PageTable, em_or), |
| 3523 | F(Lock, em_adc), |
| 3524 | F(Lock, em_sbb), |
| 3525 | F(Lock | PageTable, em_and), |
| 3526 | F(Lock, em_sub), |
| 3527 | F(Lock, em_xor), |
| 3528 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3529 | }; |
| 3530 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3531 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3532 | 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] | 3533 | }; |
| 3534 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3535 | static const struct opcode group2[] = { |
| 3536 | F(DstMem | ModRM, em_rol), |
| 3537 | F(DstMem | ModRM, em_ror), |
| 3538 | F(DstMem | ModRM, em_rcl), |
| 3539 | F(DstMem | ModRM, em_rcr), |
| 3540 | F(DstMem | ModRM, em_shl), |
| 3541 | F(DstMem | ModRM, em_shr), |
| 3542 | F(DstMem | ModRM, em_shl), |
| 3543 | F(DstMem | ModRM, em_sar), |
| 3544 | }; |
| 3545 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3546 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3547 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3548 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3549 | F(DstMem | SrcNone | Lock, em_not), |
| 3550 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3551 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3552 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3553 | F(DstXacc | Src2Mem, em_div_ex), |
| 3554 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3555 | }; |
| 3556 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3557 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3558 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3559 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3560 | N, N, N, N, N, N, |
| 3561 | }; |
| 3562 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3563 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3564 | F(DstMem | SrcNone | Lock, em_inc), |
| 3565 | F(DstMem | SrcNone | Lock, em_dec), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3566 | I(SrcMem | Stack, em_grp45), |
| 3567 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3568 | I(SrcMem | Stack, em_grp45), |
| 3569 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
Gleb Natapov | 188424b | 2013-04-11 12:32:14 +0300 | [diff] [blame] | 3570 | I(SrcMem | Stack, em_grp45), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3571 | }; |
| 3572 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3573 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3574 | DI(Prot, sldt), |
| 3575 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3576 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3577 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3578 | N, N, N, N, |
| 3579 | }; |
| 3580 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3581 | static const struct group_dual group7 = { { |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3582 | II(Mov | DstMem | Priv, em_sgdt, sgdt), |
| 3583 | II(Mov | DstMem | Priv, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3584 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3585 | II(SrcMem | Priv, em_lidt, lidt), |
| 3586 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3587 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3588 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3589 | }, { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3590 | I(SrcNone | Priv | VendorSpecific, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3591 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3592 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3593 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3594 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3595 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3596 | } }; |
| 3597 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3598 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3599 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3600 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3601 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3602 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3603 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3604 | }; |
| 3605 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3606 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3607 | 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] | 3608 | }, { |
| 3609 | N, N, N, N, N, N, N, N, |
| 3610 | } }; |
| 3611 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3612 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3613 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3614 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3615 | }; |
| 3616 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3617 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3618 | 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] | 3619 | }; |
| 3620 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3621 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3622 | I(0, em_mov), N, N, N, |
| 3623 | }; |
| 3624 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3625 | static const struct escape escape_d9 = { { |
| 3626 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3627 | }, { |
| 3628 | /* 0xC0 - 0xC7 */ |
| 3629 | N, N, N, N, N, N, N, N, |
| 3630 | /* 0xC8 - 0xCF */ |
| 3631 | N, N, N, N, N, N, N, N, |
| 3632 | /* 0xD0 - 0xC7 */ |
| 3633 | N, N, N, N, N, N, N, N, |
| 3634 | /* 0xD8 - 0xDF */ |
| 3635 | N, N, N, N, N, N, N, N, |
| 3636 | /* 0xE0 - 0xE7 */ |
| 3637 | N, N, N, N, N, N, N, N, |
| 3638 | /* 0xE8 - 0xEF */ |
| 3639 | N, N, N, N, N, N, N, N, |
| 3640 | /* 0xF0 - 0xF7 */ |
| 3641 | N, N, N, N, N, N, N, N, |
| 3642 | /* 0xF8 - 0xFF */ |
| 3643 | N, N, N, N, N, N, N, N, |
| 3644 | } }; |
| 3645 | |
| 3646 | static const struct escape escape_db = { { |
| 3647 | N, N, N, N, N, N, N, N, |
| 3648 | }, { |
| 3649 | /* 0xC0 - 0xC7 */ |
| 3650 | N, N, N, N, N, N, N, N, |
| 3651 | /* 0xC8 - 0xCF */ |
| 3652 | N, N, N, N, N, N, N, N, |
| 3653 | /* 0xD0 - 0xC7 */ |
| 3654 | N, N, N, N, N, N, N, N, |
| 3655 | /* 0xD8 - 0xDF */ |
| 3656 | N, N, N, N, N, N, N, N, |
| 3657 | /* 0xE0 - 0xE7 */ |
| 3658 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3659 | /* 0xE8 - 0xEF */ |
| 3660 | N, N, N, N, N, N, N, N, |
| 3661 | /* 0xF0 - 0xF7 */ |
| 3662 | N, N, N, N, N, N, N, N, |
| 3663 | /* 0xF8 - 0xFF */ |
| 3664 | N, N, N, N, N, N, N, N, |
| 3665 | } }; |
| 3666 | |
| 3667 | static const struct escape escape_dd = { { |
| 3668 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3669 | }, { |
| 3670 | /* 0xC0 - 0xC7 */ |
| 3671 | N, N, N, N, N, N, N, N, |
| 3672 | /* 0xC8 - 0xCF */ |
| 3673 | N, N, N, N, N, N, N, N, |
| 3674 | /* 0xD0 - 0xC7 */ |
| 3675 | N, N, N, N, N, N, N, N, |
| 3676 | /* 0xD8 - 0xDF */ |
| 3677 | N, N, N, N, N, N, N, N, |
| 3678 | /* 0xE0 - 0xE7 */ |
| 3679 | N, N, N, N, N, N, N, N, |
| 3680 | /* 0xE8 - 0xEF */ |
| 3681 | N, N, N, N, N, N, N, N, |
| 3682 | /* 0xF0 - 0xF7 */ |
| 3683 | N, N, N, N, N, N, N, N, |
| 3684 | /* 0xF8 - 0xFF */ |
| 3685 | N, N, N, N, N, N, N, N, |
| 3686 | } }; |
| 3687 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3688 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3689 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3690 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3691 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3692 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3693 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3694 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3695 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3696 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3697 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3698 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3699 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3700 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3701 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3702 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3703 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3704 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3705 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3706 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3707 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3708 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3709 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3710 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3711 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3712 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3713 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3714 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3715 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3716 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3717 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3718 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3719 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3720 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3721 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3722 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3723 | N, N, N, N, |
| 3724 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3725 | I(SrcImm | Mov | Stack, em_push), |
| 3726 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3727 | I(SrcImmByte | Mov | Stack, em_push), |
| 3728 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3729 | 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] | 3730 | 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] | 3731 | /* 0x70 - 0x7F */ |
| 3732 | X16(D(SrcImmByte)), |
| 3733 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3734 | G(ByteOp | DstMem | SrcImm, group1), |
| 3735 | G(DstMem | SrcImm, group1), |
| 3736 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3737 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3738 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3739 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3740 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3741 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3742 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3743 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3744 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3745 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3746 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3747 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3748 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3749 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3750 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3751 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3752 | II(ImplicitOps | Stack, em_pushf, pushf), |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3753 | II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3754 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3755 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3756 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3757 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3758 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3759 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3760 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3761 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3762 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3763 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3764 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3765 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3766 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3767 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3768 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3769 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3770 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3771 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3772 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3773 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3774 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3775 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3776 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 3777 | I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm), |
| 3778 | I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3779 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3780 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3781 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3782 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 3783 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3784 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 3785 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 3786 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 3787 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3788 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3789 | N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3790 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3791 | X3(I(SrcImmByte, em_loop)), |
| 3792 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3793 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3794 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3795 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3796 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3797 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3798 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3799 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3800 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3801 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3802 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3803 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3804 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3805 | D(ImplicitOps), D(ImplicitOps), |
| 3806 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3807 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3808 | }; |
| 3809 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3810 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3811 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3812 | G(0, group6), GD(0, &group7), N, N, |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3813 | N, I(ImplicitOps | VendorSpecific, em_syscall), |
| 3814 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3815 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3816 | N, D(ImplicitOps | ModRM), N, N, |
| 3817 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 3818 | N, N, N, N, N, N, N, N, |
| 3819 | D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3820 | /* 0x20 - 0x2F */ |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3821 | DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3822 | DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read), |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3823 | IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write), |
| 3824 | 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] | 3825 | N, N, N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3826 | N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
| 3827 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3828 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3829 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3830 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3831 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3832 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3833 | I(ImplicitOps | VendorSpecific, em_sysenter), |
| 3834 | I(ImplicitOps | Priv | VendorSpecific, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 3835 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3836 | N, N, N, N, N, N, N, N, |
| 3837 | /* 0x40 - 0x4F */ |
| 3838 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 3839 | /* 0x50 - 0x5F */ |
| 3840 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3841 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3842 | N, N, N, N, |
| 3843 | N, N, N, N, |
| 3844 | N, N, N, N, |
| 3845 | 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] | 3846 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3847 | N, N, N, N, |
| 3848 | N, N, N, N, |
| 3849 | N, N, N, N, |
| 3850 | 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] | 3851 | /* 0x80 - 0x8F */ |
| 3852 | X16(D(SrcImm)), |
| 3853 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3854 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3855 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3856 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3857 | II(ImplicitOps, em_cpuid, cpuid), |
| 3858 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3859 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 3860 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3861 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3862 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3863 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3864 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3865 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 3866 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3867 | D(ModRM), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3868 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 3869 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3870 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3871 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3872 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 3873 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3874 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3875 | /* 0xB8 - 0xBF */ |
| 3876 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3877 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3878 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 3879 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3880 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3881 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 3882 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3883 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3884 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3885 | /* 0xC8 - 0xCF */ |
| 3886 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3887 | /* 0xD0 - 0xDF */ |
| 3888 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3889 | /* 0xE0 - 0xEF */ |
| 3890 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3891 | /* 0xF0 - 0xFF */ |
| 3892 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 3893 | }; |
| 3894 | |
| 3895 | #undef D |
| 3896 | #undef N |
| 3897 | #undef G |
| 3898 | #undef GD |
| 3899 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3900 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3901 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3902 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3903 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3904 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3905 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3906 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3907 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3908 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3909 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3910 | { |
| 3911 | unsigned size; |
| 3912 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3913 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3914 | if (size == 8) |
| 3915 | size = 4; |
| 3916 | return size; |
| 3917 | } |
| 3918 | |
| 3919 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 3920 | unsigned size, bool sign_extension) |
| 3921 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3922 | int rc = X86EMUL_CONTINUE; |
| 3923 | |
| 3924 | op->type = OP_IMM; |
| 3925 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3926 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3927 | /* NB. Immediates are sign-extended as necessary. */ |
| 3928 | switch (op->bytes) { |
| 3929 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3930 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3931 | break; |
| 3932 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3933 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3934 | break; |
| 3935 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 3936 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3937 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3938 | case 8: |
| 3939 | op->val = insn_fetch(s64, ctxt); |
| 3940 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 3941 | } |
| 3942 | if (!sign_extension) { |
| 3943 | switch (op->bytes) { |
| 3944 | case 1: |
| 3945 | op->val &= 0xff; |
| 3946 | break; |
| 3947 | case 2: |
| 3948 | op->val &= 0xffff; |
| 3949 | break; |
| 3950 | case 4: |
| 3951 | op->val &= 0xffffffff; |
| 3952 | break; |
| 3953 | } |
| 3954 | } |
| 3955 | done: |
| 3956 | return rc; |
| 3957 | } |
| 3958 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3959 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 3960 | unsigned d) |
| 3961 | { |
| 3962 | int rc = X86EMUL_CONTINUE; |
| 3963 | |
| 3964 | switch (d) { |
| 3965 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3966 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3967 | break; |
| 3968 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 3969 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3970 | break; |
| 3971 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 3972 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 3973 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3974 | *op = ctxt->memop; |
| 3975 | ctxt->memopp = op; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 3976 | if ((ctxt->d & BitOp) && op == &ctxt->dst) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3977 | fetch_bit_operand(ctxt); |
| 3978 | op->orig_val = op->val; |
| 3979 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 3980 | case OpMem64: |
| 3981 | ctxt->memop.bytes = 8; |
| 3982 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3983 | case OpAcc: |
| 3984 | op->type = OP_REG; |
| 3985 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3986 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 3987 | fetch_register_operand(op); |
| 3988 | op->orig_val = op->val; |
| 3989 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 3990 | case OpAccLo: |
| 3991 | op->type = OP_REG; |
| 3992 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 3993 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 3994 | fetch_register_operand(op); |
| 3995 | op->orig_val = op->val; |
| 3996 | break; |
| 3997 | case OpAccHi: |
| 3998 | if (ctxt->d & ByteOp) { |
| 3999 | op->type = OP_NONE; |
| 4000 | break; |
| 4001 | } |
| 4002 | op->type = OP_REG; |
| 4003 | op->bytes = ctxt->op_bytes; |
| 4004 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4005 | fetch_register_operand(op); |
| 4006 | op->orig_val = op->val; |
| 4007 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4008 | case OpDI: |
| 4009 | op->type = OP_MEM; |
| 4010 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4011 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4012 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4013 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4014 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4015 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4016 | break; |
| 4017 | case OpDX: |
| 4018 | op->type = OP_REG; |
| 4019 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4020 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4021 | fetch_register_operand(op); |
| 4022 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4023 | case OpCL: |
| 4024 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4025 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4026 | break; |
| 4027 | case OpImmByte: |
| 4028 | rc = decode_imm(ctxt, op, 1, true); |
| 4029 | break; |
| 4030 | case OpOne: |
| 4031 | op->bytes = 1; |
| 4032 | op->val = 1; |
| 4033 | break; |
| 4034 | case OpImm: |
| 4035 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4036 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4037 | case OpImm64: |
| 4038 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4039 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4040 | case OpMem8: |
| 4041 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4042 | if (ctxt->memop.type == OP_REG) { |
| 4043 | ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1); |
| 4044 | fetch_register_operand(&ctxt->memop); |
| 4045 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4046 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4047 | case OpMem16: |
| 4048 | ctxt->memop.bytes = 2; |
| 4049 | goto mem_common; |
| 4050 | case OpMem32: |
| 4051 | ctxt->memop.bytes = 4; |
| 4052 | goto mem_common; |
| 4053 | case OpImmU16: |
| 4054 | rc = decode_imm(ctxt, op, 2, false); |
| 4055 | break; |
| 4056 | case OpImmU: |
| 4057 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4058 | break; |
| 4059 | case OpSI: |
| 4060 | op->type = OP_MEM; |
| 4061 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4062 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4063 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4064 | op->addr.mem.seg = seg_override(ctxt); |
| 4065 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4066 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4067 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4068 | case OpXLat: |
| 4069 | op->type = OP_MEM; |
| 4070 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4071 | op->addr.mem.ea = |
| 4072 | register_address(ctxt, |
| 4073 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4074 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
| 4075 | op->addr.mem.seg = seg_override(ctxt); |
| 4076 | op->val = 0; |
| 4077 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4078 | case OpImmFAddr: |
| 4079 | op->type = OP_IMM; |
| 4080 | op->addr.mem.ea = ctxt->_eip; |
| 4081 | op->bytes = ctxt->op_bytes + 2; |
| 4082 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4083 | break; |
| 4084 | case OpMemFAddr: |
| 4085 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4086 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4087 | case OpES: |
| 4088 | op->val = VCPU_SREG_ES; |
| 4089 | break; |
| 4090 | case OpCS: |
| 4091 | op->val = VCPU_SREG_CS; |
| 4092 | break; |
| 4093 | case OpSS: |
| 4094 | op->val = VCPU_SREG_SS; |
| 4095 | break; |
| 4096 | case OpDS: |
| 4097 | op->val = VCPU_SREG_DS; |
| 4098 | break; |
| 4099 | case OpFS: |
| 4100 | op->val = VCPU_SREG_FS; |
| 4101 | break; |
| 4102 | case OpGS: |
| 4103 | op->val = VCPU_SREG_GS; |
| 4104 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4105 | case OpImplicit: |
| 4106 | /* Special instructions do their own operand decoding. */ |
| 4107 | default: |
| 4108 | op->type = OP_NONE; /* Disable writeback. */ |
| 4109 | break; |
| 4110 | } |
| 4111 | |
| 4112 | done: |
| 4113 | return rc; |
| 4114 | } |
| 4115 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4116 | 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] | 4117 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4118 | int rc = X86EMUL_CONTINUE; |
| 4119 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4120 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4121 | bool op_prefix = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4122 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4123 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4124 | ctxt->memop.type = OP_NONE; |
| 4125 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4126 | ctxt->_eip = ctxt->eip; |
| 4127 | ctxt->fetch.start = ctxt->_eip; |
| 4128 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4129 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4130 | memcpy(ctxt->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4131 | |
| 4132 | switch (mode) { |
| 4133 | case X86EMUL_MODE_REAL: |
| 4134 | case X86EMUL_MODE_VM86: |
| 4135 | case X86EMUL_MODE_PROT16: |
| 4136 | def_op_bytes = def_ad_bytes = 2; |
| 4137 | break; |
| 4138 | case X86EMUL_MODE_PROT32: |
| 4139 | def_op_bytes = def_ad_bytes = 4; |
| 4140 | break; |
| 4141 | #ifdef CONFIG_X86_64 |
| 4142 | case X86EMUL_MODE_PROT64: |
| 4143 | def_op_bytes = 4; |
| 4144 | def_ad_bytes = 8; |
| 4145 | break; |
| 4146 | #endif |
| 4147 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4148 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4149 | } |
| 4150 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4151 | ctxt->op_bytes = def_op_bytes; |
| 4152 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4153 | |
| 4154 | /* Legacy prefixes. */ |
| 4155 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4156 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4157 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4158 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4159 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4160 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4161 | break; |
| 4162 | case 0x67: /* address-size override */ |
| 4163 | if (mode == X86EMUL_MODE_PROT64) |
| 4164 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4165 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4166 | else |
| 4167 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4168 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4169 | break; |
| 4170 | case 0x26: /* ES override */ |
| 4171 | case 0x2e: /* CS override */ |
| 4172 | case 0x36: /* SS override */ |
| 4173 | case 0x3e: /* DS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4174 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4175 | break; |
| 4176 | case 0x64: /* FS override */ |
| 4177 | case 0x65: /* GS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4178 | set_seg_override(ctxt, ctxt->b & 7); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4179 | break; |
| 4180 | case 0x40 ... 0x4f: /* REX */ |
| 4181 | if (mode != X86EMUL_MODE_PROT64) |
| 4182 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4183 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4184 | continue; |
| 4185 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4186 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4187 | break; |
| 4188 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4189 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4190 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4191 | break; |
| 4192 | default: |
| 4193 | goto done_prefixes; |
| 4194 | } |
| 4195 | |
| 4196 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4197 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4198 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | done_prefixes: |
| 4202 | |
| 4203 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4204 | if (ctxt->rex_prefix & 8) |
| 4205 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4206 | |
| 4207 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4208 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4209 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4210 | if (ctxt->b == 0x0f) { |
| 4211 | ctxt->twobyte = 1; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4212 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4213 | opcode = twobyte_table[ctxt->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4214 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4215 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4216 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4217 | if (ctxt->d & ModRM) |
| 4218 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4219 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4220 | while (ctxt->d & GroupMask) { |
| 4221 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4222 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4223 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4224 | opcode = opcode.u.group[goffset]; |
| 4225 | break; |
| 4226 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4227 | goffset = (ctxt->modrm >> 3) & 7; |
| 4228 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4229 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4230 | else |
| 4231 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4232 | break; |
| 4233 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4234 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4235 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4236 | break; |
| 4237 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4238 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4239 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4240 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4241 | switch (simd_prefix) { |
| 4242 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4243 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4244 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4245 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4246 | } |
| 4247 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4248 | case Escape: |
| 4249 | if (ctxt->modrm > 0xbf) |
| 4250 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4251 | else |
| 4252 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4253 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4254 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4255 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4256 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4257 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4258 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4259 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4260 | } |
| 4261 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4262 | ctxt->execute = opcode.u.execute; |
| 4263 | ctxt->check_perm = opcode.check_perm; |
| 4264 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4265 | |
| 4266 | /* Unrecognised? */ |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4267 | if (ctxt->d == 0 || (ctxt->d & NotImpl)) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4268 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4269 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4270 | if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4271 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4272 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4273 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
| 4274 | ctxt->op_bytes = 8; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4275 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4276 | if (ctxt->d & Op3264) { |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4277 | if (mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4278 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4279 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4280 | ctxt->op_bytes = 4; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4281 | } |
| 4282 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4283 | if (ctxt->d & Sse) |
| 4284 | ctxt->op_bytes = 16; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4285 | else if (ctxt->d & Mmx) |
| 4286 | ctxt->op_bytes = 8; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4287 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4288 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4289 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4290 | rc = decode_modrm(ctxt, &ctxt->memop); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4291 | if (!ctxt->has_seg_override) |
| 4292 | set_seg_override(ctxt, ctxt->modrm_seg); |
| 4293 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4294 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4295 | if (rc != X86EMUL_CONTINUE) |
| 4296 | goto done; |
| 4297 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4298 | if (!ctxt->has_seg_override) |
| 4299 | set_seg_override(ctxt, VCPU_SREG_DS); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4300 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4301 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4302 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4303 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
| 4304 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4305 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4306 | /* |
| 4307 | * Decode and fetch the source operand: register, memory |
| 4308 | * or immediate. |
| 4309 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4310 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4311 | if (rc != X86EMUL_CONTINUE) |
| 4312 | goto done; |
| 4313 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4314 | /* |
| 4315 | * Decode and fetch the second source operand: register, memory |
| 4316 | * or immediate. |
| 4317 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4318 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4319 | if (rc != X86EMUL_CONTINUE) |
| 4320 | goto done; |
| 4321 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4322 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4323 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4324 | |
| 4325 | done: |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4326 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
| 4327 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4328 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4329 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4330 | } |
| 4331 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4332 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4333 | { |
| 4334 | return ctxt->d & PageTable; |
| 4335 | } |
| 4336 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4337 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4338 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4339 | /* The second termination condition only applies for REPE |
| 4340 | * and REPNE. Test if the repeat string operation prefix is |
| 4341 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4342 | * corresponding termination condition according to: |
| 4343 | * - if REPE/REPZ and ZF = 0 then done |
| 4344 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4345 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4346 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4347 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4348 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4349 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4350 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4351 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4352 | return true; |
| 4353 | |
| 4354 | return false; |
| 4355 | } |
| 4356 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4357 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4358 | { |
| 4359 | bool fault = false; |
| 4360 | |
| 4361 | ctxt->ops->get_fpu(ctxt); |
| 4362 | asm volatile("1: fwait \n\t" |
| 4363 | "2: \n\t" |
| 4364 | ".pushsection .fixup,\"ax\" \n\t" |
| 4365 | "3: \n\t" |
| 4366 | "movb $1, %[fault] \n\t" |
| 4367 | "jmp 2b \n\t" |
| 4368 | ".popsection \n\t" |
| 4369 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4370 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4371 | ctxt->ops->put_fpu(ctxt); |
| 4372 | |
| 4373 | if (unlikely(fault)) |
| 4374 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4375 | |
| 4376 | return X86EMUL_CONTINUE; |
| 4377 | } |
| 4378 | |
| 4379 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4380 | struct operand *op) |
| 4381 | { |
| 4382 | if (op->type == OP_MM) |
| 4383 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4384 | } |
| 4385 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4386 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4387 | { |
| 4388 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4389 | if (!(ctxt->d & ByteOp)) |
| 4390 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4391 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4392 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4393 | [fastop]"+S"(fop) |
| 4394 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4395 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4396 | if (!fop) /* exception is returned in fop variable */ |
| 4397 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4398 | return X86EMUL_CONTINUE; |
| 4399 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4400 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4401 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4402 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4403 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4404 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4405 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4406 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4407 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4408 | |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4409 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4410 | (ctxt->d & Undefined)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4411 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 4412 | goto done; |
| 4413 | } |
| 4414 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4415 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4416 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4417 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4418 | goto done; |
| 4419 | } |
| 4420 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4421 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4422 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4423 | goto done; |
| 4424 | } |
| 4425 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4426 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4427 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4428 | rc = emulate_ud(ctxt); |
| 4429 | goto done; |
| 4430 | } |
| 4431 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4432 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4433 | rc = emulate_nm(ctxt); |
| 4434 | goto done; |
| 4435 | } |
| 4436 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4437 | if (ctxt->d & Mmx) { |
| 4438 | rc = flush_pending_x87_faults(ctxt); |
| 4439 | if (rc != X86EMUL_CONTINUE) |
| 4440 | goto done; |
| 4441 | /* |
| 4442 | * Now that we know the fpu is exception safe, we can fetch |
| 4443 | * operands from it. |
| 4444 | */ |
| 4445 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4446 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4447 | if (!(ctxt->d & Mov)) |
| 4448 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4449 | } |
| 4450 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4451 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4452 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4453 | X86_ICPT_PRE_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4454 | if (rc != X86EMUL_CONTINUE) |
| 4455 | goto done; |
| 4456 | } |
| 4457 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4458 | /* Privileged instruction can be executed only in CPL=0 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4459 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4460 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4461 | goto done; |
| 4462 | } |
| 4463 | |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4464 | /* Instruction can only be executed in protected mode */ |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 4465 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4466 | rc = emulate_ud(ctxt); |
| 4467 | goto done; |
| 4468 | } |
| 4469 | |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4470 | /* Do instruction specific permission checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4471 | if (ctxt->check_perm) { |
| 4472 | rc = ctxt->check_perm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4473 | if (rc != X86EMUL_CONTINUE) |
| 4474 | goto done; |
| 4475 | } |
| 4476 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4477 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4478 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4479 | X86_ICPT_POST_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4480 | if (rc != X86EMUL_CONTINUE) |
| 4481 | goto done; |
| 4482 | } |
| 4483 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4484 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4485 | /* All REP prefixes have the same first termination condition */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4486 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4487 | ctxt->eip = ctxt->_eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4488 | goto done; |
| 4489 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4490 | } |
| 4491 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4492 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4493 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4494 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4495 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4496 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4497 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4498 | } |
| 4499 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4500 | if (ctxt->src2.type == OP_MEM) { |
| 4501 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4502 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4503 | if (rc != X86EMUL_CONTINUE) |
| 4504 | goto done; |
| 4505 | } |
| 4506 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4507 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4508 | goto special_insn; |
| 4509 | |
| 4510 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4511 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4512 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4513 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4514 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4515 | if (rc != X86EMUL_CONTINUE) |
| 4516 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4517 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4518 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4519 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4520 | special_insn: |
| 4521 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4522 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4523 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4524 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4525 | if (rc != X86EMUL_CONTINUE) |
| 4526 | goto done; |
| 4527 | } |
| 4528 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4529 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4530 | if (ctxt->d & Fastop) { |
| 4531 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4532 | rc = fastop(ctxt, fop); |
| 4533 | if (rc != X86EMUL_CONTINUE) |
| 4534 | goto done; |
| 4535 | goto writeback; |
| 4536 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4537 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4538 | if (rc != X86EMUL_CONTINUE) |
| 4539 | goto done; |
| 4540 | goto writeback; |
| 4541 | } |
| 4542 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4543 | if (ctxt->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4544 | goto twobyte_insn; |
| 4545 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4546 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4547 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4548 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4549 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4550 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4551 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4552 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4553 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4554 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4555 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4556 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4557 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4558 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4559 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4560 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 4561 | break; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4562 | rc = em_xchg(ctxt); |
| 4563 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4564 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4565 | switch (ctxt->op_bytes) { |
| 4566 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4567 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4568 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4569 | } |
| 4570 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4571 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4572 | rc = emulate_int(ctxt, 3); |
| 4573 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4574 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4575 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4576 | break; |
| 4577 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4578 | if (ctxt->eflags & EFLG_OF) |
| 4579 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4580 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4581 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4582 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4583 | jmp_rel(ctxt, ctxt->src.val); |
| 4584 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4585 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4586 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4587 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4588 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4589 | case 0xf5: /* cmc */ |
| 4590 | /* complement carry flag from eflags reg */ |
| 4591 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4592 | break; |
| 4593 | case 0xf8: /* clc */ |
| 4594 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4595 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4596 | case 0xf9: /* stc */ |
| 4597 | ctxt->eflags |= EFLG_CF; |
| 4598 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4599 | case 0xfc: /* cld */ |
| 4600 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4601 | break; |
| 4602 | case 0xfd: /* std */ |
| 4603 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4604 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4605 | default: |
| 4606 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4607 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4608 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4609 | if (rc != X86EMUL_CONTINUE) |
| 4610 | goto done; |
| 4611 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4612 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 4613 | if (!(ctxt->d & NoWrite)) { |
| 4614 | rc = writeback(ctxt, &ctxt->dst); |
| 4615 | if (rc != X86EMUL_CONTINUE) |
| 4616 | goto done; |
| 4617 | } |
| 4618 | if (ctxt->d & SrcWrite) { |
| 4619 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 4620 | rc = writeback(ctxt, &ctxt->src); |
| 4621 | if (rc != X86EMUL_CONTINUE) |
| 4622 | goto done; |
| 4623 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4624 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4625 | /* |
| 4626 | * restore dst type in case the decoding will be reused |
| 4627 | * (happens for string instruction ) |
| 4628 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4629 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4630 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4631 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4632 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4633 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4634 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4635 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4636 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4637 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4638 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4639 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4640 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4641 | count = ctxt->src.count; |
| 4642 | else |
| 4643 | count = ctxt->dst.count; |
| 4644 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4645 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4646 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4647 | if (!string_insn_completed(ctxt)) { |
| 4648 | /* |
| 4649 | * Re-enter guest when pio read ahead buffer is empty |
| 4650 | * or, if it is not used, after each 1024 iteration. |
| 4651 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4652 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4653 | (r->end == 0 || r->end != r->pos)) { |
| 4654 | /* |
| 4655 | * Reset read cache. Usually happens before |
| 4656 | * decode, but since instruction is restarted |
| 4657 | * we have to do it here. |
| 4658 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4659 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4660 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4661 | return EMULATION_RESTART; |
| 4662 | } |
| 4663 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4664 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4665 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4666 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4667 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4668 | |
| 4669 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4670 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4671 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4672 | if (rc == X86EMUL_INTERCEPTED) |
| 4673 | return EMULATION_INTERCEPTED; |
| 4674 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4675 | if (rc == X86EMUL_CONTINUE) |
| 4676 | writeback_registers(ctxt); |
| 4677 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4678 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4679 | |
| 4680 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4681 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4682 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4683 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4684 | break; |
| 4685 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4686 | case 0x0d: /* GrpP (prefetch) */ |
| 4687 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4688 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4689 | break; |
| 4690 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4691 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4692 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4693 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4694 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4695 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4696 | case 0x40 ... 0x4f: /* cmov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4697 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
| 4698 | if (!test_cc(ctxt->b, ctxt->eflags)) |
| 4699 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4700 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4701 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4702 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4703 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4704 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4705 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4706 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4707 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4708 | case 0xae: /* clflush */ |
| 4709 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4710 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4711 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4712 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4713 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4714 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4715 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4716 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4717 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4718 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4719 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4720 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4721 | ctxt->dst.bytes = ctxt->op_bytes; |
| 4722 | ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val : |
| 4723 | (u64) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4724 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4725 | default: |
| 4726 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4727 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4728 | |
| 4729 | if (rc != X86EMUL_CONTINUE) |
| 4730 | goto done; |
| 4731 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4732 | goto writeback; |
| 4733 | |
| 4734 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4735 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4736 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4737 | |
| 4738 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4739 | { |
| 4740 | invalidate_registers(ctxt); |
| 4741 | } |
| 4742 | |
| 4743 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4744 | { |
| 4745 | writeback_registers(ctxt); |
| 4746 | } |