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) |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 89 | #define DstMem16 (OpMem16 << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 90 | #define DstImmUByte (OpImmUByte << DstShift) |
| 91 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 92 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 93 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 94 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 95 | #define SrcShift 6 |
| 96 | #define SrcNone (OpNone << SrcShift) |
| 97 | #define SrcReg (OpReg << SrcShift) |
| 98 | #define SrcMem (OpMem << SrcShift) |
| 99 | #define SrcMem16 (OpMem16 << SrcShift) |
| 100 | #define SrcMem32 (OpMem32 << SrcShift) |
| 101 | #define SrcImm (OpImm << SrcShift) |
| 102 | #define SrcImmByte (OpImmByte << SrcShift) |
| 103 | #define SrcOne (OpOne << SrcShift) |
| 104 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 105 | #define SrcImmU (OpImmU << SrcShift) |
| 106 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 107 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 108 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 109 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 110 | #define SrcAcc (OpAcc << SrcShift) |
| 111 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 112 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 113 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 114 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 115 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 116 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 117 | #define BitOp (1<<11) |
| 118 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 119 | #define String (1<<13) /* String instruction (rep capable) */ |
| 120 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 121 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 122 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 123 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 124 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 125 | #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] | 126 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 127 | #define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */ |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 128 | #define ModeDual (7<<15) /* Different instruction for 32/64 bit */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 129 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 130 | /* Generic ModRM decode. */ |
| 131 | #define ModRM (1<<19) |
| 132 | /* Destination is only written; never read. */ |
| 133 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 134 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 135 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 136 | #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 137 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 138 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 139 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 140 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 141 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 142 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 143 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 144 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 145 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 146 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 147 | #define Src2None (OpNone << Src2Shift) |
Avi Kivity | ab2c5ce | 2013-02-09 11:31:46 +0200 | [diff] [blame] | 148 | #define Src2Mem (OpMem << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 149 | #define Src2CL (OpCL << Src2Shift) |
| 150 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 151 | #define Src2One (OpOne << Src2Shift) |
| 152 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 153 | #define Src2ES (OpES << Src2Shift) |
| 154 | #define Src2CS (OpCS << Src2Shift) |
| 155 | #define Src2SS (OpSS << Src2Shift) |
| 156 | #define Src2DS (OpDS << Src2Shift) |
| 157 | #define Src2FS (OpFS << Src2Shift) |
| 158 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 159 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 160 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 161 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 162 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 163 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 164 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 165 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 166 | #define SrcWrite ((u64)1 << 46) /* Write back src operand */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 167 | #define NoMod ((u64)1 << 47) /* Mod field is ignored */ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 168 | #define Intercept ((u64)1 << 48) /* Has valid intercept field */ |
| 169 | #define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */ |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 170 | #define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 171 | #define NearBranch ((u64)1 << 52) /* Near branches */ |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 172 | #define No16 ((u64)1 << 53) /* No 16 bit operand */ |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 173 | #define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 175 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 176 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 177 | #define X2(x...) x, x |
| 178 | #define X3(x...) X2(x), x |
| 179 | #define X4(x...) X2(x), X2(x) |
| 180 | #define X5(x...) X4(x), x |
| 181 | #define X6(x...) X4(x), X2(x) |
| 182 | #define X7(x...) X4(x), X3(x) |
| 183 | #define X8(x...) X4(x), X4(x) |
| 184 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 185 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 186 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 187 | #define FASTOP_SIZE 8 |
| 188 | |
| 189 | /* |
| 190 | * fastop functions have a special calling convention: |
| 191 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 192 | * dst: rax (in/out) |
| 193 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 194 | * src2: rcx (in) |
| 195 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 196 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 197 | * |
| 198 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 199 | * different operand sizes can be reached by calculation, rather than a jump |
| 200 | * table (which would be bigger than the code). |
| 201 | * |
| 202 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 203 | * so they can't be called from C directly. |
| 204 | */ |
| 205 | |
| 206 | struct fastop; |
| 207 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 208 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 209 | u64 flags : 56; |
| 210 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 211 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 212 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 213 | const struct opcode *group; |
| 214 | const struct group_dual *gdual; |
| 215 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 216 | const struct escape *esc; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 217 | const struct instr_dual *idual; |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 218 | const struct mode_dual *mdual; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 219 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 220 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 221 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | struct group_dual { |
| 225 | struct opcode mod012[8]; |
| 226 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 227 | }; |
| 228 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 229 | struct gprefix { |
| 230 | struct opcode pfx_no; |
| 231 | struct opcode pfx_66; |
| 232 | struct opcode pfx_f2; |
| 233 | struct opcode pfx_f3; |
| 234 | }; |
| 235 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 236 | struct escape { |
| 237 | struct opcode op[8]; |
| 238 | struct opcode high[64]; |
| 239 | }; |
| 240 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 241 | struct instr_dual { |
| 242 | struct opcode mod012; |
| 243 | struct opcode mod3; |
| 244 | }; |
| 245 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 246 | struct mode_dual { |
| 247 | struct opcode mode32; |
| 248 | struct opcode mode64; |
| 249 | }; |
| 250 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 251 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 252 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 253 | enum x86_transfer_type { |
| 254 | X86_TRANSFER_NONE, |
| 255 | X86_TRANSFER_CALL_JMP, |
| 256 | X86_TRANSFER_RET, |
| 257 | X86_TRANSFER_TASK_SWITCH, |
| 258 | }; |
| 259 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 260 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 261 | { |
| 262 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 263 | ctxt->regs_valid |= 1 << nr; |
| 264 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 265 | } |
| 266 | return ctxt->_regs[nr]; |
| 267 | } |
| 268 | |
| 269 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 270 | { |
| 271 | ctxt->regs_valid |= 1 << nr; |
| 272 | ctxt->regs_dirty |= 1 << nr; |
| 273 | return &ctxt->_regs[nr]; |
| 274 | } |
| 275 | |
| 276 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 277 | { |
| 278 | reg_read(ctxt, nr); |
| 279 | return reg_write(ctxt, nr); |
| 280 | } |
| 281 | |
| 282 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 283 | { |
| 284 | unsigned reg; |
| 285 | |
| 286 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 287 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 288 | } |
| 289 | |
| 290 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 291 | { |
| 292 | ctxt->regs_dirty = 0; |
| 293 | ctxt->regs_valid = 0; |
| 294 | } |
| 295 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 296 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 297 | * These EFLAGS bits are restored from saved value during emulation, and |
| 298 | * any changes are written back to the saved value after emulation. |
| 299 | */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 300 | #define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\ |
| 301 | X86_EFLAGS_PF|X86_EFLAGS_CF) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 302 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 303 | #ifdef CONFIG_X86_64 |
| 304 | #define ON64(x) x |
| 305 | #else |
| 306 | #define ON64(x) |
| 307 | #endif |
| 308 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 309 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 310 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 311 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 312 | #define FOP_RET "ret \n\t" |
| 313 | |
| 314 | #define FOP_START(op) \ |
| 315 | extern void em_##op(struct fastop *fake); \ |
| 316 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 317 | ".global em_" #op " \n\t" \ |
| 318 | FOP_ALIGN \ |
| 319 | "em_" #op ": \n\t" |
| 320 | |
| 321 | #define FOP_END \ |
| 322 | ".popsection") |
| 323 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 324 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 325 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 326 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 327 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 328 | |
| 329 | #define FOP1EEX(op, dst) \ |
| 330 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 331 | |
| 332 | #define FASTOP1(op) \ |
| 333 | FOP_START(op) \ |
| 334 | FOP1E(op##b, al) \ |
| 335 | FOP1E(op##w, ax) \ |
| 336 | FOP1E(op##l, eax) \ |
| 337 | ON64(FOP1E(op##q, rax)) \ |
| 338 | FOP_END |
| 339 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 340 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 341 | #define FASTOP1SRC2(op, name) \ |
| 342 | FOP_START(name) \ |
| 343 | FOP1E(op, cl) \ |
| 344 | FOP1E(op, cx) \ |
| 345 | FOP1E(op, ecx) \ |
| 346 | ON64(FOP1E(op, rcx)) \ |
| 347 | FOP_END |
| 348 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 349 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 350 | #define FASTOP1SRC2EX(op, name) \ |
| 351 | FOP_START(name) \ |
| 352 | FOP1EEX(op, cl) \ |
| 353 | FOP1EEX(op, cx) \ |
| 354 | FOP1EEX(op, ecx) \ |
| 355 | ON64(FOP1EEX(op, rcx)) \ |
| 356 | FOP_END |
| 357 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 358 | #define FOP2E(op, dst, src) \ |
| 359 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 360 | |
| 361 | #define FASTOP2(op) \ |
| 362 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 363 | FOP2E(op##b, al, dl) \ |
| 364 | FOP2E(op##w, ax, dx) \ |
| 365 | FOP2E(op##l, eax, edx) \ |
| 366 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 367 | FOP_END |
| 368 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 369 | /* 2 operand, word only */ |
| 370 | #define FASTOP2W(op) \ |
| 371 | FOP_START(op) \ |
| 372 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 373 | FOP2E(op##w, ax, dx) \ |
| 374 | FOP2E(op##l, eax, edx) \ |
| 375 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 376 | FOP_END |
| 377 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 378 | /* 2 operand, src is CL */ |
| 379 | #define FASTOP2CL(op) \ |
| 380 | FOP_START(op) \ |
| 381 | FOP2E(op##b, al, cl) \ |
| 382 | FOP2E(op##w, ax, cl) \ |
| 383 | FOP2E(op##l, eax, cl) \ |
| 384 | ON64(FOP2E(op##q, rax, cl)) \ |
| 385 | FOP_END |
| 386 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 387 | /* 2 operand, src and dest are reversed */ |
| 388 | #define FASTOP2R(op, name) \ |
| 389 | FOP_START(name) \ |
| 390 | FOP2E(op##b, dl, al) \ |
| 391 | FOP2E(op##w, dx, ax) \ |
| 392 | FOP2E(op##l, edx, eax) \ |
| 393 | ON64(FOP2E(op##q, rdx, rax)) \ |
| 394 | FOP_END |
| 395 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 396 | #define FOP3E(op, dst, src, src2) \ |
| 397 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 398 | |
| 399 | /* 3-operand, word-only, src2=cl */ |
| 400 | #define FASTOP3WCL(op) \ |
| 401 | FOP_START(op) \ |
| 402 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 403 | FOP3E(op##w, ax, dx, cl) \ |
| 404 | FOP3E(op##l, eax, edx, cl) \ |
| 405 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 406 | FOP_END |
| 407 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 408 | /* Special case for SETcc - 1 instruction per cc */ |
| 409 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 410 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 411 | asm(".global kvm_fastop_exception \n" |
| 412 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 413 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 414 | FOP_START(setcc) |
| 415 | FOP_SETCC(seto) |
| 416 | FOP_SETCC(setno) |
| 417 | FOP_SETCC(setc) |
| 418 | FOP_SETCC(setnc) |
| 419 | FOP_SETCC(setz) |
| 420 | FOP_SETCC(setnz) |
| 421 | FOP_SETCC(setbe) |
| 422 | FOP_SETCC(setnbe) |
| 423 | FOP_SETCC(sets) |
| 424 | FOP_SETCC(setns) |
| 425 | FOP_SETCC(setp) |
| 426 | FOP_SETCC(setnp) |
| 427 | FOP_SETCC(setl) |
| 428 | FOP_SETCC(setnl) |
| 429 | FOP_SETCC(setle) |
| 430 | FOP_SETCC(setnle) |
| 431 | FOP_END; |
| 432 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 433 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 434 | FOP_END; |
| 435 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 436 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 437 | enum x86_intercept intercept, |
| 438 | enum x86_intercept_stage stage) |
| 439 | { |
| 440 | struct x86_instruction_info info = { |
| 441 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 442 | .rep_prefix = ctxt->rep_prefix, |
| 443 | .modrm_mod = ctxt->modrm_mod, |
| 444 | .modrm_reg = ctxt->modrm_reg, |
| 445 | .modrm_rm = ctxt->modrm_rm, |
| 446 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 447 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 448 | .src_bytes = ctxt->src.bytes, |
| 449 | .dst_bytes = ctxt->dst.bytes, |
| 450 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 451 | .next_rip = ctxt->eip, |
| 452 | }; |
| 453 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 454 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 457 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 458 | { |
| 459 | *dest = (*dest & ~mask) | (src & mask); |
| 460 | } |
| 461 | |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 462 | static void assign_register(unsigned long *reg, u64 val, int bytes) |
| 463 | { |
| 464 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 465 | switch (bytes) { |
| 466 | case 1: |
| 467 | *(u8 *)reg = (u8)val; |
| 468 | break; |
| 469 | case 2: |
| 470 | *(u16 *)reg = (u16)val; |
| 471 | break; |
| 472 | case 4: |
| 473 | *reg = (u32)val; |
| 474 | break; /* 64b: zero-extend */ |
| 475 | case 8: |
| 476 | *reg = val; |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 481 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 482 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 483 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 486 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 487 | { |
| 488 | u16 sel; |
| 489 | struct desc_struct ss; |
| 490 | |
| 491 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 492 | return ~0UL; |
| 493 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 494 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 495 | } |
| 496 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 497 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 498 | { |
| 499 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 500 | } |
| 501 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 502 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 503 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 504 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 505 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 506 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 507 | return reg; |
| 508 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 509 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static inline unsigned long |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 513 | register_address(struct x86_emulate_ctxt *ctxt, int reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 514 | { |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 515 | return address_mask(ctxt, reg_read(ctxt, reg)); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 518 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 519 | { |
| 520 | assign_masked(reg, *reg + inc, mask); |
| 521 | } |
| 522 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 523 | static inline void |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 524 | register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 525 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 526 | ulong mask; |
| 527 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 528 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 529 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 530 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 531 | mask = ad_mask(ctxt); |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 532 | masked_increment(reg_rmw(ctxt, reg), mask, inc); |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 536 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 537 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 538 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 539 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 540 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 541 | { |
| 542 | u32 limit = get_desc_limit(desc); |
| 543 | |
| 544 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 545 | } |
| 546 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 547 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 548 | { |
| 549 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 550 | return 0; |
| 551 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 552 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 555 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 556 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 557 | { |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 558 | WARN_ON(vec > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 559 | ctxt->exception.vector = vec; |
| 560 | ctxt->exception.error_code = error; |
| 561 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 562 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 563 | } |
| 564 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 565 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 566 | { |
| 567 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 568 | } |
| 569 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 570 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 571 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 572 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 573 | } |
| 574 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 575 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 576 | { |
| 577 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 578 | } |
| 579 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 580 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 581 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 582 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 583 | } |
| 584 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 585 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 586 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 587 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 588 | } |
| 589 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 590 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 591 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 592 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 593 | } |
| 594 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 595 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 596 | { |
| 597 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 598 | } |
| 599 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 600 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 601 | { |
| 602 | u16 selector; |
| 603 | struct desc_struct desc; |
| 604 | |
| 605 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 606 | return selector; |
| 607 | } |
| 608 | |
| 609 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 610 | unsigned seg) |
| 611 | { |
| 612 | u16 dummy; |
| 613 | u32 base3; |
| 614 | struct desc_struct desc; |
| 615 | |
| 616 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 617 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 618 | } |
| 619 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 620 | /* |
| 621 | * x86 defines three classes of vector instructions: explicitly |
| 622 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 623 | * depending on whether they're AVX encoded or not. |
| 624 | * |
| 625 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 626 | * subject to the same check. |
| 627 | */ |
| 628 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 629 | { |
| 630 | if (likely(size < 16)) |
| 631 | return false; |
| 632 | |
| 633 | if (ctxt->d & Aligned) |
| 634 | return true; |
| 635 | else if (ctxt->d & Unaligned) |
| 636 | return false; |
| 637 | else if (ctxt->d & Avx) |
| 638 | return false; |
| 639 | else |
| 640 | return true; |
| 641 | } |
| 642 | |
Paolo Bonzini | d09155d | 2014-10-27 14:54:44 +0100 | [diff] [blame] | 643 | static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, |
| 644 | struct segmented_address addr, |
| 645 | unsigned *max_size, unsigned size, |
| 646 | bool write, bool fetch, |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 647 | enum x86emul_mode mode, ulong *linear) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 648 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 649 | struct desc_struct desc; |
| 650 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 651 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 652 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 653 | u16 sel; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 654 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 655 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 656 | *max_size = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 657 | switch (mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 658 | case X86EMUL_MODE_PROT64: |
Nadav Amit | 4be4de7 | 2014-09-18 22:39:40 +0300 | [diff] [blame] | 659 | if (is_noncanonical_address(la)) |
Nadav Amit | abc7d8a | 2014-11-19 17:43:12 +0200 | [diff] [blame] | 660 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 661 | |
| 662 | *max_size = min_t(u64, ~0u, (1ull << 48) - la); |
| 663 | if (size > *max_size) |
| 664 | goto bad; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 665 | break; |
| 666 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 667 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 668 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 669 | if (!usable) |
| 670 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 671 | /* code segment in protected mode or read-only data segment */ |
| 672 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 673 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 674 | goto bad; |
| 675 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 676 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 677 | goto bad; |
| 678 | lim = desc_limit_scaled(&desc); |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 679 | if (!(desc.type & 8) && (desc.type & 4)) { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 680 | /* expand-down segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 681 | if (addr.ea <= lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 682 | goto bad; |
| 683 | lim = desc.d ? 0xffffffff : 0xffff; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 684 | } |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 685 | if (addr.ea > lim) |
| 686 | goto bad; |
Nadav Amit | bac15531 | 2015-01-26 09:32:26 +0200 | [diff] [blame] | 687 | if (lim == 0xffffffff) |
| 688 | *max_size = ~0u; |
| 689 | else { |
| 690 | *max_size = (u64)lim + 1 - addr.ea; |
| 691 | if (size > *max_size) |
| 692 | goto bad; |
| 693 | } |
Nadav Amit | 31ff648 | 2014-11-19 17:43:13 +0200 | [diff] [blame] | 694 | la &= (u32)-1; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 695 | break; |
| 696 | } |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 697 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 698 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 699 | *linear = la; |
| 700 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 701 | bad: |
| 702 | if (addr.seg == VCPU_SREG_SS) |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 703 | return emulate_ss(ctxt, 0); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 704 | else |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 705 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 706 | } |
| 707 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 708 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 709 | struct segmented_address addr, |
| 710 | unsigned size, bool write, |
| 711 | ulong *linear) |
| 712 | { |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 713 | unsigned max_size; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 714 | return __linearize(ctxt, addr, &max_size, size, write, false, |
| 715 | ctxt->mode, linear); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 718 | static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 719 | enum x86emul_mode mode) |
| 720 | { |
| 721 | ulong linear; |
| 722 | int rc; |
| 723 | unsigned max_size; |
| 724 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 725 | .ea = dst }; |
| 726 | |
| 727 | if (ctxt->op_bytes != sizeof(unsigned long)) |
| 728 | addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1); |
| 729 | rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear); |
| 730 | if (rc == X86EMUL_CONTINUE) |
| 731 | ctxt->_eip = addr.ea; |
| 732 | return rc; |
| 733 | } |
| 734 | |
| 735 | static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst) |
| 736 | { |
| 737 | return assign_eip(ctxt, dst, ctxt->mode); |
| 738 | } |
| 739 | |
| 740 | static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 741 | const struct desc_struct *cs_desc) |
| 742 | { |
| 743 | enum x86emul_mode mode = ctxt->mode; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 744 | int rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 745 | |
| 746 | #ifdef CONFIG_X86_64 |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 747 | if (ctxt->mode >= X86EMUL_MODE_PROT16) { |
| 748 | if (cs_desc->l) { |
| 749 | u64 efer = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 750 | |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 751 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 752 | if (efer & EFER_LMA) |
| 753 | mode = X86EMUL_MODE_PROT64; |
| 754 | } else |
| 755 | mode = X86EMUL_MODE_PROT32; /* temporary value */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 756 | } |
| 757 | #endif |
| 758 | if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32) |
| 759 | mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 760 | rc = assign_eip(ctxt, dst, mode); |
| 761 | if (rc == X86EMUL_CONTINUE) |
| 762 | ctxt->mode = mode; |
| 763 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
| 767 | { |
| 768 | return assign_eip_near(ctxt, ctxt->_eip + rel); |
| 769 | } |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 770 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 771 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 772 | struct segmented_address addr, |
| 773 | void *data, |
| 774 | unsigned size) |
| 775 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 776 | int rc; |
| 777 | ulong linear; |
| 778 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 779 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 780 | if (rc != X86EMUL_CONTINUE) |
| 781 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 782 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 785 | /* |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 786 | * Prefetch the remaining bytes of the instruction without crossing page |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 787 | * boundary if they are not in fetch_cache yet. |
| 788 | */ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 789 | static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 790 | { |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 791 | int rc; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 792 | unsigned size, max_size; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 793 | unsigned long linear; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 794 | int cur_size = ctxt->fetch.end - ctxt->fetch.data; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 795 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 796 | .ea = ctxt->eip + cur_size }; |
| 797 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 798 | /* |
| 799 | * We do not know exactly how many bytes will be needed, and |
| 800 | * __linearize is expensive, so fetch as much as possible. We |
| 801 | * just have to avoid going beyond the 15 byte limit, the end |
| 802 | * of the segment, or the end of the page. |
| 803 | * |
| 804 | * __linearize is called with size 0 so that it does not do any |
| 805 | * boundary check itself. Instead, we use max_size to check |
| 806 | * against op_size. |
| 807 | */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 808 | rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode, |
| 809 | &linear); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 810 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 811 | return rc; |
| 812 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 813 | size = min_t(unsigned, 15UL ^ cur_size, max_size); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 814 | size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear)); |
Paolo Bonzini | 5cfc7e0 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * One instruction can only straddle two pages, |
| 818 | * and one has been loaded at the beginning of |
| 819 | * x86_decode_insn. So, if not enough bytes |
| 820 | * still, we must have hit the 15-byte boundary. |
| 821 | */ |
| 822 | if (unlikely(size < op_size)) |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 823 | return emulate_gp(ctxt, 0); |
| 824 | |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 825 | rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 826 | size, &ctxt->exception); |
| 827 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 828 | return rc; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 829 | ctxt->fetch.end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 830 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 833 | static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, |
| 834 | unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 835 | { |
Nadav Amit | 08da44a | 2014-10-03 01:10:04 +0300 | [diff] [blame] | 836 | unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr; |
| 837 | |
| 838 | if (unlikely(done_size < size)) |
| 839 | return __do_insn_fetch_bytes(ctxt, size - done_size); |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 840 | else |
| 841 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 844 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 845 | #define insn_fetch(_type, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 846 | ({ _type _x; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 847 | \ |
| 848 | rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 849 | if (rc != X86EMUL_CONTINUE) \ |
| 850 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 851 | ctxt->_eip += sizeof(_type); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 852 | _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \ |
| 853 | ctxt->fetch.ptr += sizeof(_type); \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 854 | _x; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 855 | }) |
| 856 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 857 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 858 | ({ \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 859 | rc = do_insn_fetch_bytes(_ctxt, _size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 860 | if (rc != X86EMUL_CONTINUE) \ |
| 861 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 862 | ctxt->_eip += (_size); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 863 | memcpy(_arr, ctxt->fetch.ptr, _size); \ |
| 864 | ctxt->fetch.ptr += (_size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 865 | }) |
| 866 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 867 | /* |
| 868 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 869 | * pointer into the block that addresses the relevant register. |
| 870 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 871 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 872 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 873 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 874 | { |
| 875 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 876 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 877 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 878 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 879 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 880 | else |
| 881 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 882 | return p; |
| 883 | } |
| 884 | |
| 885 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 886 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 887 | u16 *size, unsigned long *address, int op_bytes) |
| 888 | { |
| 889 | int rc; |
| 890 | |
| 891 | if (op_bytes == 2) |
| 892 | op_bytes = 3; |
| 893 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 894 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 895 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 896 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 897 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 898 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 899 | return rc; |
| 900 | } |
| 901 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 902 | FASTOP2(add); |
| 903 | FASTOP2(or); |
| 904 | FASTOP2(adc); |
| 905 | FASTOP2(sbb); |
| 906 | FASTOP2(and); |
| 907 | FASTOP2(sub); |
| 908 | FASTOP2(xor); |
| 909 | FASTOP2(cmp); |
| 910 | FASTOP2(test); |
| 911 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 912 | FASTOP1SRC2(mul, mul_ex); |
| 913 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 914 | FASTOP1SRC2EX(div, div_ex); |
| 915 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 916 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 917 | FASTOP3WCL(shld); |
| 918 | FASTOP3WCL(shrd); |
| 919 | |
| 920 | FASTOP2W(imul); |
| 921 | |
| 922 | FASTOP1(not); |
| 923 | FASTOP1(neg); |
| 924 | FASTOP1(inc); |
| 925 | FASTOP1(dec); |
| 926 | |
| 927 | FASTOP2CL(rol); |
| 928 | FASTOP2CL(ror); |
| 929 | FASTOP2CL(rcl); |
| 930 | FASTOP2CL(rcr); |
| 931 | FASTOP2CL(shl); |
| 932 | FASTOP2CL(shr); |
| 933 | FASTOP2CL(sar); |
| 934 | |
| 935 | FASTOP2W(bsf); |
| 936 | FASTOP2W(bsr); |
| 937 | FASTOP2W(bt); |
| 938 | FASTOP2W(bts); |
| 939 | FASTOP2W(btr); |
| 940 | FASTOP2W(btc); |
| 941 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 942 | FASTOP2(xadd); |
| 943 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 944 | FASTOP2R(cmp, cmp_r); |
| 945 | |
Nadav Amit | 900efe2 | 2015-03-30 15:39:21 +0300 | [diff] [blame] | 946 | static int em_bsf_c(struct x86_emulate_ctxt *ctxt) |
| 947 | { |
| 948 | /* If src is zero, do not writeback, but update flags */ |
| 949 | if (ctxt->src.val == 0) |
| 950 | ctxt->dst.type = OP_NONE; |
| 951 | return fastop(ctxt, em_bsf); |
| 952 | } |
| 953 | |
| 954 | static int em_bsr_c(struct x86_emulate_ctxt *ctxt) |
| 955 | { |
| 956 | /* If src is zero, do not writeback, but update flags */ |
| 957 | if (ctxt->src.val == 0) |
| 958 | ctxt->dst.type = OP_NONE; |
| 959 | return fastop(ctxt, em_bsr); |
| 960 | } |
| 961 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 962 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 963 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 964 | u8 rc; |
| 965 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 966 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 967 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 968 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 969 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 970 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 971 | } |
| 972 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 973 | static void fetch_register_operand(struct operand *op) |
| 974 | { |
| 975 | switch (op->bytes) { |
| 976 | case 1: |
| 977 | op->val = *(u8 *)op->addr.reg; |
| 978 | break; |
| 979 | case 2: |
| 980 | op->val = *(u16 *)op->addr.reg; |
| 981 | break; |
| 982 | case 4: |
| 983 | op->val = *(u32 *)op->addr.reg; |
| 984 | break; |
| 985 | case 8: |
| 986 | op->val = *(u64 *)op->addr.reg; |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 991 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 992 | { |
| 993 | ctxt->ops->get_fpu(ctxt); |
| 994 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 995 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 996 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 997 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 998 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 999 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 1000 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 1001 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 1002 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1003 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1004 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 1005 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 1006 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 1007 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 1008 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 1009 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 1010 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 1011 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1012 | #endif |
| 1013 | default: BUG(); |
| 1014 | } |
| 1015 | ctxt->ops->put_fpu(ctxt); |
| 1016 | } |
| 1017 | |
| 1018 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 1019 | int reg) |
| 1020 | { |
| 1021 | ctxt->ops->get_fpu(ctxt); |
| 1022 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1023 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 1024 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 1025 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 1026 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 1027 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 1028 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 1029 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 1030 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1031 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1032 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 1033 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1034 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1035 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1036 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1037 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1038 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1039 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1040 | #endif |
| 1041 | default: BUG(); |
| 1042 | } |
| 1043 | ctxt->ops->put_fpu(ctxt); |
| 1044 | } |
| 1045 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1046 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1047 | { |
| 1048 | ctxt->ops->get_fpu(ctxt); |
| 1049 | switch (reg) { |
| 1050 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1051 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1052 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1053 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1054 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1055 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1056 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1057 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1058 | default: BUG(); |
| 1059 | } |
| 1060 | ctxt->ops->put_fpu(ctxt); |
| 1061 | } |
| 1062 | |
| 1063 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1064 | { |
| 1065 | ctxt->ops->get_fpu(ctxt); |
| 1066 | switch (reg) { |
| 1067 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1068 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1069 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1070 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1071 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1072 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1073 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1074 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1075 | default: BUG(); |
| 1076 | } |
| 1077 | ctxt->ops->put_fpu(ctxt); |
| 1078 | } |
| 1079 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1080 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1081 | { |
| 1082 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1083 | return emulate_nm(ctxt); |
| 1084 | |
| 1085 | ctxt->ops->get_fpu(ctxt); |
| 1086 | asm volatile("fninit"); |
| 1087 | ctxt->ops->put_fpu(ctxt); |
| 1088 | return X86EMUL_CONTINUE; |
| 1089 | } |
| 1090 | |
| 1091 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1092 | { |
| 1093 | u16 fcw; |
| 1094 | |
| 1095 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1096 | return emulate_nm(ctxt); |
| 1097 | |
| 1098 | ctxt->ops->get_fpu(ctxt); |
| 1099 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1100 | ctxt->ops->put_fpu(ctxt); |
| 1101 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1102 | ctxt->dst.val = fcw; |
| 1103 | |
| 1104 | return X86EMUL_CONTINUE; |
| 1105 | } |
| 1106 | |
| 1107 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1108 | { |
| 1109 | u16 fsw; |
| 1110 | |
| 1111 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1112 | return emulate_nm(ctxt); |
| 1113 | |
| 1114 | ctxt->ops->get_fpu(ctxt); |
| 1115 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1116 | ctxt->ops->put_fpu(ctxt); |
| 1117 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1118 | ctxt->dst.val = fsw; |
| 1119 | |
| 1120 | return X86EMUL_CONTINUE; |
| 1121 | } |
| 1122 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1123 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1124 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1125 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1126 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1127 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1128 | if (!(ctxt->d & ModRM)) |
| 1129 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1130 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1131 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1132 | op->type = OP_XMM; |
| 1133 | op->bytes = 16; |
| 1134 | op->addr.xmm = reg; |
| 1135 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1136 | return; |
| 1137 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1138 | if (ctxt->d & Mmx) { |
| 1139 | reg &= 7; |
| 1140 | op->type = OP_MM; |
| 1141 | op->bytes = 8; |
| 1142 | op->addr.mm = reg; |
| 1143 | return; |
| 1144 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1145 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1146 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1147 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1148 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1149 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1150 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1151 | op->orig_val = op->val; |
| 1152 | } |
| 1153 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1154 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1155 | { |
| 1156 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1157 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1158 | } |
| 1159 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1160 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1161 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1162 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1163 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1164 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1165 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1166 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1167 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1168 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1169 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1170 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1171 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1172 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1173 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1174 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1175 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1176 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1177 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1178 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1179 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1180 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1181 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1182 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1183 | op->type = OP_XMM; |
| 1184 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1185 | op->addr.xmm = ctxt->modrm_rm; |
| 1186 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1187 | return rc; |
| 1188 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1189 | if (ctxt->d & Mmx) { |
| 1190 | op->type = OP_MM; |
| 1191 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1192 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1193 | return rc; |
| 1194 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1195 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1196 | return rc; |
| 1197 | } |
| 1198 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1199 | op->type = OP_MEM; |
| 1200 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1201 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1202 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1203 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1204 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1205 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1206 | |
| 1207 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1208 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1209 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1210 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1211 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1212 | break; |
| 1213 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1214 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1215 | break; |
| 1216 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1217 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1218 | break; |
| 1219 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1220 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1221 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1222 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1223 | break; |
| 1224 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1225 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1226 | break; |
| 1227 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1228 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1229 | break; |
| 1230 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1231 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1232 | break; |
| 1233 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1234 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1235 | break; |
| 1236 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1237 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1238 | break; |
| 1239 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1240 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1241 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1242 | break; |
| 1243 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1244 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1245 | break; |
| 1246 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1247 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1248 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1249 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1250 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1251 | } else { |
| 1252 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1253 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1254 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1255 | index_reg |= (sib >> 3) & 7; |
| 1256 | base_reg |= sib & 7; |
| 1257 | scale = sib >> 6; |
| 1258 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1259 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1260 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1261 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1262 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1263 | adjust_modrm_seg(ctxt, base_reg); |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 1264 | /* Increment ESP on POP [ESP] */ |
| 1265 | if ((ctxt->d & IncSP) && |
| 1266 | base_reg == VCPU_REGS_RSP) |
| 1267 | modrm_ea += ctxt->op_bytes; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1268 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1269 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1270 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1271 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Nadav Amit | 5b38ab8 | 2014-11-02 11:54:41 +0200 | [diff] [blame] | 1272 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1273 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1274 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1275 | } else { |
| 1276 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1277 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1278 | adjust_modrm_seg(ctxt, base_reg); |
| 1279 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1280 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1281 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1282 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1283 | break; |
| 1284 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1285 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1289 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1290 | if (ctxt->ad_bytes != 8) |
| 1291 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1292 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1293 | done: |
| 1294 | return rc; |
| 1295 | } |
| 1296 | |
| 1297 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1298 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1299 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1300 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1301 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1302 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1303 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1304 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1305 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1306 | break; |
| 1307 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1308 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1309 | break; |
| 1310 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1311 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1312 | break; |
| 1313 | } |
| 1314 | done: |
| 1315 | return rc; |
| 1316 | } |
| 1317 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1318 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1319 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1320 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1321 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1322 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1323 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1324 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1325 | if (ctxt->src.bytes == 2) |
| 1326 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1327 | else if (ctxt->src.bytes == 4) |
| 1328 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1329 | else |
| 1330 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1331 | |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 1332 | ctxt->dst.addr.mem.ea = address_mask(ctxt, |
| 1333 | ctxt->dst.addr.mem.ea + (sv >> 3)); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1334 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1335 | |
| 1336 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1337 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1340 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1341 | unsigned long addr, void *dest, unsigned size) |
| 1342 | { |
| 1343 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1344 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1345 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1346 | if (mc->pos < mc->end) |
| 1347 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1348 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1349 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1350 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1351 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1352 | &ctxt->exception); |
| 1353 | if (rc != X86EMUL_CONTINUE) |
| 1354 | return rc; |
| 1355 | |
| 1356 | mc->end += size; |
| 1357 | |
| 1358 | read_cached: |
| 1359 | memcpy(dest, mc->data + mc->pos, size); |
| 1360 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1361 | return X86EMUL_CONTINUE; |
| 1362 | } |
| 1363 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1364 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1365 | struct segmented_address addr, |
| 1366 | void *data, |
| 1367 | unsigned size) |
| 1368 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1369 | int rc; |
| 1370 | ulong linear; |
| 1371 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1372 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1373 | if (rc != X86EMUL_CONTINUE) |
| 1374 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1375 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1379 | struct segmented_address addr, |
| 1380 | const void *data, |
| 1381 | unsigned size) |
| 1382 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1383 | int rc; |
| 1384 | ulong linear; |
| 1385 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1386 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1387 | if (rc != X86EMUL_CONTINUE) |
| 1388 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1389 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1390 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1394 | struct segmented_address addr, |
| 1395 | const void *orig_data, const void *data, |
| 1396 | unsigned size) |
| 1397 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1398 | int rc; |
| 1399 | ulong linear; |
| 1400 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1401 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1402 | if (rc != X86EMUL_CONTINUE) |
| 1403 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1404 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1405 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1408 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1409 | unsigned int size, unsigned short port, |
| 1410 | void *dest) |
| 1411 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1412 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1413 | |
| 1414 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1415 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1416 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1417 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1418 | in_page = (ctxt->eflags & X86_EFLAGS_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1419 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1420 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Mark Rustad | b55a814 | 2014-07-25 06:27:05 -0700 | [diff] [blame] | 1421 | n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1422 | if (n == 0) |
| 1423 | n = 1; |
| 1424 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1425 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1426 | return 0; |
| 1427 | rc->end = n * size; |
| 1428 | } |
| 1429 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1430 | if (ctxt->rep_prefix && (ctxt->d & String) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1431 | !(ctxt->eflags & X86_EFLAGS_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1432 | ctxt->dst.data = rc->data + rc->pos; |
| 1433 | ctxt->dst.type = OP_MEM_STR; |
| 1434 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1435 | rc->pos = rc->end; |
| 1436 | } else { |
| 1437 | memcpy(dest, rc->data + rc->pos, size); |
| 1438 | rc->pos += size; |
| 1439 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1440 | return 1; |
| 1441 | } |
| 1442 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1443 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1444 | u16 index, struct desc_struct *desc) |
| 1445 | { |
| 1446 | struct desc_ptr dt; |
| 1447 | ulong addr; |
| 1448 | |
| 1449 | ctxt->ops->get_idt(ctxt, &dt); |
| 1450 | |
| 1451 | if (dt.size < index * 8 + 7) |
| 1452 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1453 | |
| 1454 | addr = dt.address + index * 8; |
| 1455 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1456 | &ctxt->exception); |
| 1457 | } |
| 1458 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1459 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1460 | u16 selector, struct desc_ptr *dt) |
| 1461 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1462 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1463 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1464 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1465 | if (selector & 1 << 2) { |
| 1466 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1467 | u16 sel; |
| 1468 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1469 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1470 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1471 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1472 | return; |
| 1473 | |
| 1474 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1475 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1476 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1477 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1480 | static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt, |
| 1481 | u16 selector, ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1482 | { |
| 1483 | struct desc_ptr dt; |
| 1484 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1485 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1486 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1487 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1488 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1489 | if (dt.size < index * 8 + 7) |
| 1490 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1491 | |
| 1492 | addr = dt.address + index * 8; |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1493 | |
| 1494 | #ifdef CONFIG_X86_64 |
| 1495 | if (addr >> 32 != 0) { |
| 1496 | u64 efer = 0; |
| 1497 | |
| 1498 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1499 | if (!(efer & EFER_LMA)) |
| 1500 | addr &= (u32)-1; |
| 1501 | } |
| 1502 | #endif |
| 1503 | |
| 1504 | *desc_addr_p = addr; |
| 1505 | return X86EMUL_CONTINUE; |
| 1506 | } |
| 1507 | |
| 1508 | /* allowed just for 8 bytes segments */ |
| 1509 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1510 | u16 selector, struct desc_struct *desc, |
| 1511 | ulong *desc_addr_p) |
| 1512 | { |
| 1513 | int rc; |
| 1514 | |
| 1515 | rc = get_descriptor_ptr(ctxt, selector, desc_addr_p); |
| 1516 | if (rc != X86EMUL_CONTINUE) |
| 1517 | return rc; |
| 1518 | |
| 1519 | return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc), |
| 1520 | &ctxt->exception); |
| 1521 | } |
| 1522 | |
| 1523 | /* allowed just for 8 bytes segments */ |
| 1524 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1525 | u16 selector, struct desc_struct *desc) |
| 1526 | { |
| 1527 | int rc; |
| 1528 | ulong addr; |
| 1529 | |
| 1530 | rc = get_descriptor_ptr(ctxt, selector, &addr); |
| 1531 | if (rc != X86EMUL_CONTINUE) |
| 1532 | return rc; |
| 1533 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1534 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1535 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1538 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1539 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1540 | u16 selector, int seg, u8 cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1541 | enum x86_transfer_type transfer, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1542 | struct desc_struct *desc) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1543 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1544 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1545 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1546 | unsigned err_vec = GP_VECTOR; |
| 1547 | u32 err_code = 0; |
| 1548 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1549 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1550 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1551 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1552 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1553 | |
| 1554 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1555 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1556 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1557 | /* set real mode segment descriptor (keep limit etc. for |
| 1558 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1559 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1560 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1561 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1562 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1563 | /* VM86 needs a clean new segment descriptor */ |
| 1564 | set_desc_base(&seg_desc, selector << 4); |
| 1565 | set_desc_limit(&seg_desc, 0xffff); |
| 1566 | seg_desc.type = 3; |
| 1567 | seg_desc.p = 1; |
| 1568 | seg_desc.s = 1; |
| 1569 | seg_desc.dpl = 3; |
| 1570 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1571 | } |
| 1572 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1573 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1574 | |
| 1575 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1576 | if ((seg == VCPU_SREG_CS |
| 1577 | || (seg == VCPU_SREG_SS |
| 1578 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1579 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1580 | && null_selector) |
| 1581 | goto exception; |
| 1582 | |
| 1583 | /* TR should be in GDT only */ |
| 1584 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1585 | goto exception; |
| 1586 | |
| 1587 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1588 | goto load; |
| 1589 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1590 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1591 | if (ret != X86EMUL_CONTINUE) |
| 1592 | return ret; |
| 1593 | |
| 1594 | err_code = selector & 0xfffc; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1595 | err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR : |
| 1596 | GP_VECTOR; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1597 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1598 | /* can't load system descriptor into segment selector */ |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1599 | if (seg <= VCPU_SREG_GS && !seg_desc.s) { |
| 1600 | if (transfer == X86_TRANSFER_CALL_JMP) |
| 1601 | return X86EMUL_UNHANDLEABLE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1602 | goto exception; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1603 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1604 | |
| 1605 | if (!seg_desc.p) { |
| 1606 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1607 | goto exception; |
| 1608 | } |
| 1609 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1610 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1611 | |
| 1612 | switch (seg) { |
| 1613 | case VCPU_SREG_SS: |
| 1614 | /* |
| 1615 | * segment is not a writable data segment or segment |
| 1616 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1617 | */ |
| 1618 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1619 | goto exception; |
| 1620 | break; |
| 1621 | case VCPU_SREG_CS: |
| 1622 | if (!(seg_desc.type & 8)) |
| 1623 | goto exception; |
| 1624 | |
| 1625 | if (seg_desc.type & 4) { |
| 1626 | /* conforming */ |
| 1627 | if (dpl > cpl) |
| 1628 | goto exception; |
| 1629 | } else { |
| 1630 | /* nonconforming */ |
| 1631 | if (rpl > cpl || dpl != cpl) |
| 1632 | goto exception; |
| 1633 | } |
Nadav Amit | 040c8dc | 2014-09-18 22:39:43 +0300 | [diff] [blame] | 1634 | /* in long-mode d/b must be clear if l is set */ |
| 1635 | if (seg_desc.d && seg_desc.l) { |
| 1636 | u64 efer = 0; |
| 1637 | |
| 1638 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1639 | if (efer & EFER_LMA) |
| 1640 | goto exception; |
| 1641 | } |
| 1642 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1643 | /* CS(RPL) <- CPL */ |
| 1644 | selector = (selector & 0xfffc) | cpl; |
| 1645 | break; |
| 1646 | case VCPU_SREG_TR: |
| 1647 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1648 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1649 | old_desc = seg_desc; |
| 1650 | seg_desc.type |= 2; /* busy */ |
| 1651 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1652 | sizeof(seg_desc), &ctxt->exception); |
| 1653 | if (ret != X86EMUL_CONTINUE) |
| 1654 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1655 | break; |
| 1656 | case VCPU_SREG_LDTR: |
| 1657 | if (seg_desc.s || seg_desc.type != 2) |
| 1658 | goto exception; |
| 1659 | break; |
| 1660 | default: /* DS, ES, FS, or GS */ |
| 1661 | /* |
| 1662 | * segment is not a data or readable code segment or |
| 1663 | * ((segment is a data or nonconforming code segment) |
| 1664 | * and (both RPL and CPL > DPL)) |
| 1665 | */ |
| 1666 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1667 | (((seg_desc.type & 0xc) != 0xc) && |
| 1668 | (rpl > dpl && cpl > dpl))) |
| 1669 | goto exception; |
| 1670 | break; |
| 1671 | } |
| 1672 | |
| 1673 | if (seg_desc.s) { |
| 1674 | /* mark segment as accessed */ |
Nadav Amit | e2cefa7 | 2014-12-25 02:52:22 +0200 | [diff] [blame] | 1675 | if (!(seg_desc.type & 1)) { |
| 1676 | seg_desc.type |= 1; |
| 1677 | ret = write_segment_descriptor(ctxt, selector, |
| 1678 | &seg_desc); |
| 1679 | if (ret != X86EMUL_CONTINUE) |
| 1680 | return ret; |
| 1681 | } |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1682 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1683 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1684 | sizeof(base3), &ctxt->exception); |
| 1685 | if (ret != X86EMUL_CONTINUE) |
| 1686 | return ret; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 1687 | if (is_noncanonical_address(get_desc_base(&seg_desc) | |
| 1688 | ((u64)base3 << 32))) |
| 1689 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1690 | } |
| 1691 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1692 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1693 | if (desc) |
| 1694 | *desc = seg_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1695 | return X86EMUL_CONTINUE; |
| 1696 | exception: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 1697 | return emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1698 | } |
| 1699 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1700 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1701 | u16 selector, int seg) |
| 1702 | { |
| 1703 | u8 cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1704 | return __load_segment_descriptor(ctxt, selector, seg, cpl, |
| 1705 | X86_TRANSFER_NONE, NULL); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1708 | static void write_register_operand(struct operand *op) |
| 1709 | { |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 1710 | return assign_register(op->addr.reg, op->val, op->bytes); |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1711 | } |
| 1712 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1713 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1714 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1715 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1716 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1717 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1718 | break; |
| 1719 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1720 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1721 | return segmented_cmpxchg(ctxt, |
| 1722 | op->addr.mem, |
| 1723 | &op->orig_val, |
| 1724 | &op->val, |
| 1725 | op->bytes); |
| 1726 | else |
| 1727 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1728 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1729 | &op->val, |
| 1730 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1731 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1732 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1733 | return segmented_write(ctxt, |
| 1734 | op->addr.mem, |
| 1735 | op->data, |
| 1736 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1737 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1738 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1739 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1740 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1741 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1742 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1743 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1744 | case OP_NONE: |
| 1745 | /* no writeback */ |
| 1746 | break; |
| 1747 | default: |
| 1748 | break; |
| 1749 | } |
| 1750 | return X86EMUL_CONTINUE; |
| 1751 | } |
| 1752 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1753 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1754 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1755 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1756 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1757 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1758 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1759 | addr.seg = VCPU_SREG_SS; |
| 1760 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1761 | return segmented_write(ctxt, addr, data, bytes); |
| 1762 | } |
| 1763 | |
| 1764 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1765 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1766 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1767 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1768 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1769 | } |
| 1770 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1771 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1772 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1773 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1774 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1775 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1776 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1777 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1778 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1779 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1780 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1781 | return rc; |
| 1782 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1783 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1784 | return rc; |
| 1785 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1786 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1787 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1788 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1789 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1790 | } |
| 1791 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1792 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1793 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1794 | { |
| 1795 | int rc; |
| 1796 | unsigned long val, change_mask; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1797 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1798 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1799 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1800 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1801 | if (rc != X86EMUL_CONTINUE) |
| 1802 | return rc; |
| 1803 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1804 | change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 1805 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF | |
| 1806 | X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT | |
| 1807 | X86_EFLAGS_AC | X86_EFLAGS_ID; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1808 | |
| 1809 | switch(ctxt->mode) { |
| 1810 | case X86EMUL_MODE_PROT64: |
| 1811 | case X86EMUL_MODE_PROT32: |
| 1812 | case X86EMUL_MODE_PROT16: |
| 1813 | if (cpl == 0) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1814 | change_mask |= X86_EFLAGS_IOPL; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1815 | if (cpl <= iopl) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1816 | change_mask |= X86_EFLAGS_IF; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1817 | break; |
| 1818 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1819 | if (iopl < 3) |
| 1820 | return emulate_gp(ctxt, 0); |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1821 | change_mask |= X86_EFLAGS_IF; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1822 | break; |
| 1823 | default: /* real mode */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1824 | change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1825 | break; |
| 1826 | } |
| 1827 | |
| 1828 | *(unsigned long *)dest = |
| 1829 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1830 | |
| 1831 | return rc; |
| 1832 | } |
| 1833 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1834 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1835 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1836 | ctxt->dst.type = OP_REG; |
| 1837 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1838 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1839 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1840 | } |
| 1841 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1842 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1843 | { |
| 1844 | int rc; |
| 1845 | unsigned frame_size = ctxt->src.val; |
| 1846 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1847 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1848 | |
| 1849 | if (nesting_level) |
| 1850 | return X86EMUL_UNHANDLEABLE; |
| 1851 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1852 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1853 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1854 | if (rc != X86EMUL_CONTINUE) |
| 1855 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1856 | 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] | 1857 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1858 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1859 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1860 | stack_mask(ctxt)); |
| 1861 | return X86EMUL_CONTINUE; |
| 1862 | } |
| 1863 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1864 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1865 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1866 | 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] | 1867 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1868 | 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] | 1869 | } |
| 1870 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1871 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1872 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1873 | int seg = ctxt->src2.val; |
| 1874 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1875 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Nadav Amit | 0fcc207 | 2014-11-02 11:54:51 +0200 | [diff] [blame] | 1876 | if (ctxt->op_bytes == 4) { |
| 1877 | rsp_increment(ctxt, -2); |
| 1878 | ctxt->op_bytes = 2; |
| 1879 | } |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1880 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1881 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1882 | } |
| 1883 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1884 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1885 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1886 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1887 | unsigned long selector; |
| 1888 | int rc; |
| 1889 | |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1890 | rc = emulate_pop(ctxt, &selector, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1891 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1892 | return rc; |
| 1893 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1894 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1895 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1896 | if (ctxt->op_bytes > 2) |
| 1897 | rsp_increment(ctxt, ctxt->op_bytes - 2); |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1898 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1899 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1900 | return rc; |
| 1901 | } |
| 1902 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1903 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1904 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1905 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1906 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1907 | int reg = VCPU_REGS_RAX; |
| 1908 | |
| 1909 | while (reg <= VCPU_REGS_RDI) { |
| 1910 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1911 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1912 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1913 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1914 | if (rc != X86EMUL_CONTINUE) |
| 1915 | return rc; |
| 1916 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1917 | ++reg; |
| 1918 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1919 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1920 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1923 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1924 | { |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1925 | ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1926 | return em_push(ctxt); |
| 1927 | } |
| 1928 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1929 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1930 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1931 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1932 | int reg = VCPU_REGS_RDI; |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 1933 | u32 val; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1934 | |
| 1935 | while (reg >= VCPU_REGS_RAX) { |
| 1936 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1937 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1938 | --reg; |
| 1939 | } |
| 1940 | |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 1941 | rc = emulate_pop(ctxt, &val, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1942 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1943 | break; |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 1944 | assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1945 | --reg; |
| 1946 | } |
| 1947 | return rc; |
| 1948 | } |
| 1949 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1950 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1951 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1952 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1953 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1954 | struct desc_ptr dt; |
| 1955 | gva_t cs_addr; |
| 1956 | gva_t eip_addr; |
| 1957 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1958 | |
| 1959 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1960 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1961 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1962 | if (rc != X86EMUL_CONTINUE) |
| 1963 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1964 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1965 | ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1966 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1967 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1968 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1969 | if (rc != X86EMUL_CONTINUE) |
| 1970 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1971 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1972 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1973 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1974 | if (rc != X86EMUL_CONTINUE) |
| 1975 | return rc; |
| 1976 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1977 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1978 | |
| 1979 | eip_addr = dt.address + (irq << 2); |
| 1980 | cs_addr = dt.address + (irq << 2) + 2; |
| 1981 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1982 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1983 | if (rc != X86EMUL_CONTINUE) |
| 1984 | return rc; |
| 1985 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1986 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1987 | if (rc != X86EMUL_CONTINUE) |
| 1988 | return rc; |
| 1989 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1990 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1991 | if (rc != X86EMUL_CONTINUE) |
| 1992 | return rc; |
| 1993 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1994 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1995 | |
| 1996 | return rc; |
| 1997 | } |
| 1998 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1999 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 2000 | { |
| 2001 | int rc; |
| 2002 | |
| 2003 | invalidate_registers(ctxt); |
| 2004 | rc = __emulate_int_real(ctxt, irq); |
| 2005 | if (rc == X86EMUL_CONTINUE) |
| 2006 | writeback_registers(ctxt); |
| 2007 | return rc; |
| 2008 | } |
| 2009 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2010 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2011 | { |
| 2012 | switch(ctxt->mode) { |
| 2013 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2014 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2015 | case X86EMUL_MODE_VM86: |
| 2016 | case X86EMUL_MODE_PROT16: |
| 2017 | case X86EMUL_MODE_PROT32: |
| 2018 | case X86EMUL_MODE_PROT64: |
| 2019 | default: |
| 2020 | /* Protected mode interrupts unimplemented yet */ |
| 2021 | return X86EMUL_UNHANDLEABLE; |
| 2022 | } |
| 2023 | } |
| 2024 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2025 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2026 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2027 | int rc = X86EMUL_CONTINUE; |
| 2028 | unsigned long temp_eip = 0; |
| 2029 | unsigned long temp_eflags = 0; |
| 2030 | unsigned long cs = 0; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2031 | unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 2032 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF | |
| 2033 | X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF | |
| 2034 | X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF | |
| 2035 | X86_EFLAGS_AC | X86_EFLAGS_ID | |
| 2036 | X86_EFLAGS_FIXED_BIT; |
| 2037 | unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF | |
| 2038 | X86_EFLAGS_VIP; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2039 | |
| 2040 | /* TODO: Add stack limit check */ |
| 2041 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2042 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2043 | |
| 2044 | if (rc != X86EMUL_CONTINUE) |
| 2045 | return rc; |
| 2046 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2047 | if (temp_eip & ~0xffff) |
| 2048 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2049 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2050 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2051 | |
| 2052 | if (rc != X86EMUL_CONTINUE) |
| 2053 | return rc; |
| 2054 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2055 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2056 | |
| 2057 | if (rc != X86EMUL_CONTINUE) |
| 2058 | return rc; |
| 2059 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2060 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2061 | |
| 2062 | if (rc != X86EMUL_CONTINUE) |
| 2063 | return rc; |
| 2064 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2065 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2066 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2067 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2068 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2069 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2070 | ctxt->eflags &= ~0xffff; |
| 2071 | ctxt->eflags |= temp_eflags; |
| 2072 | } |
| 2073 | |
| 2074 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2075 | ctxt->eflags |= X86_EFLAGS_FIXED_BIT; |
Nadav Amit | 801806d | 2015-01-26 09:32:23 +0200 | [diff] [blame] | 2076 | ctxt->ops->set_nmi_mask(ctxt, false); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2077 | |
| 2078 | return rc; |
| 2079 | } |
| 2080 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2081 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2082 | { |
| 2083 | switch(ctxt->mode) { |
| 2084 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2085 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2086 | case X86EMUL_MODE_VM86: |
| 2087 | case X86EMUL_MODE_PROT16: |
| 2088 | case X86EMUL_MODE_PROT32: |
| 2089 | case X86EMUL_MODE_PROT64: |
| 2090 | default: |
| 2091 | /* iret from protected mode unimplemented yet */ |
| 2092 | return X86EMUL_UNHANDLEABLE; |
| 2093 | } |
| 2094 | } |
| 2095 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2096 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2097 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2098 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2099 | unsigned short sel, old_sel; |
| 2100 | struct desc_struct old_desc, new_desc; |
| 2101 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 2102 | u8 cpl = ctxt->ops->cpl(ctxt); |
| 2103 | |
| 2104 | /* Assignment of RIP may only fail in 64-bit mode */ |
| 2105 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2106 | ops->get_segment(ctxt, &old_sel, &old_desc, NULL, |
| 2107 | VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2108 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2109 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2110 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2111 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 2112 | X86_TRANSFER_CALL_JMP, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2113 | &new_desc); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2114 | if (rc != X86EMUL_CONTINUE) |
| 2115 | return rc; |
| 2116 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2117 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2118 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2119 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2120 | /* assigning eip failed; restore the old cs */ |
| 2121 | ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS); |
| 2122 | return rc; |
| 2123 | } |
| 2124 | return rc; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2125 | } |
| 2126 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2127 | static int em_jmp_abs(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2128 | { |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2129 | return assign_eip_near(ctxt, ctxt->src.val); |
| 2130 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2131 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2132 | static int em_call_near_abs(struct x86_emulate_ctxt *ctxt) |
| 2133 | { |
| 2134 | int rc; |
| 2135 | long int old_eip; |
| 2136 | |
| 2137 | old_eip = ctxt->_eip; |
| 2138 | rc = assign_eip_near(ctxt, ctxt->src.val); |
| 2139 | if (rc != X86EMUL_CONTINUE) |
| 2140 | return rc; |
| 2141 | ctxt->src.val = old_eip; |
| 2142 | rc = em_push(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2143 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2144 | } |
| 2145 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2146 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2147 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2148 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2149 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2150 | if (ctxt->dst.bytes == 16) |
| 2151 | return X86EMUL_UNHANDLEABLE; |
| 2152 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2153 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2154 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2155 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2156 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2157 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2158 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2159 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2160 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2161 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2162 | ctxt->eflags |= X86_EFLAGS_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2163 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2164 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2167 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2168 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2169 | int rc; |
| 2170 | unsigned long eip; |
| 2171 | |
| 2172 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 2173 | if (rc != X86EMUL_CONTINUE) |
| 2174 | return rc; |
| 2175 | |
| 2176 | return assign_eip_near(ctxt, eip); |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2177 | } |
| 2178 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2179 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2180 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2181 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2182 | unsigned long eip, cs; |
| 2183 | u16 old_cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2184 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2185 | struct desc_struct old_desc, new_desc; |
| 2186 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2187 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2188 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2189 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, |
| 2190 | VCPU_SREG_CS); |
| 2191 | |
| 2192 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2193 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2194 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2195 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2196 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2197 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2198 | /* Outer-privilege level return is not implemented */ |
| 2199 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2200 | return X86EMUL_UNHANDLEABLE; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2201 | rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, |
| 2202 | X86_TRANSFER_RET, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2203 | &new_desc); |
| 2204 | if (rc != X86EMUL_CONTINUE) |
| 2205 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2206 | rc = assign_eip_far(ctxt, eip, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2207 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2208 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2209 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 2210 | } |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2211 | return rc; |
| 2212 | } |
| 2213 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2214 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2215 | { |
| 2216 | int rc; |
| 2217 | |
| 2218 | rc = em_ret_far(ctxt); |
| 2219 | if (rc != X86EMUL_CONTINUE) |
| 2220 | return rc; |
| 2221 | rsp_increment(ctxt, ctxt->src.val); |
| 2222 | return X86EMUL_CONTINUE; |
| 2223 | } |
| 2224 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2225 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2226 | { |
| 2227 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2228 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2229 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2230 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2231 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2232 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2233 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2234 | if (ctxt->eflags & X86_EFLAGS_ZF) { |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2235 | /* Success: write back to memory; no update of EAX */ |
| 2236 | ctxt->src.type = OP_NONE; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2237 | ctxt->dst.val = ctxt->src.orig_val; |
| 2238 | } else { |
| 2239 | /* Failure: write the value we saw to EAX. */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2240 | ctxt->src.type = OP_REG; |
| 2241 | ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 2242 | ctxt->src.val = ctxt->dst.orig_val; |
| 2243 | /* Create write-cycle to dest by writing the same value */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2244 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2245 | } |
| 2246 | return X86EMUL_CONTINUE; |
| 2247 | } |
| 2248 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2249 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2250 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2251 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2252 | unsigned short sel; |
| 2253 | int rc; |
| 2254 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2255 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2256 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2257 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2258 | if (rc != X86EMUL_CONTINUE) |
| 2259 | return rc; |
| 2260 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2261 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2262 | return rc; |
| 2263 | } |
| 2264 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2265 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2266 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2267 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2268 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2269 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2270 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2271 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2272 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2273 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2274 | cs->s = 1; |
| 2275 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2276 | cs->p = 1; |
| 2277 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2278 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2279 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2280 | set_desc_base(ss, 0); /* flat segment */ |
| 2281 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2282 | ss->g = 1; /* 4kb granularity */ |
| 2283 | ss->s = 1; |
| 2284 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2285 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2286 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2287 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2288 | ss->l = 0; |
| 2289 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2290 | } |
| 2291 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2292 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2293 | { |
| 2294 | u32 eax, ebx, ecx, edx; |
| 2295 | |
| 2296 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2297 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2298 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2299 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2300 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2301 | } |
| 2302 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2303 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2304 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2305 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2306 | u32 eax, ebx, ecx, edx; |
| 2307 | |
| 2308 | /* |
| 2309 | * syscall should always be enabled in longmode - so only become |
| 2310 | * vendor specific (cpuid) if other modes are active... |
| 2311 | */ |
| 2312 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2313 | return true; |
| 2314 | |
| 2315 | eax = 0x00000000; |
| 2316 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2317 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2318 | /* |
| 2319 | * Intel ("GenuineIntel") |
| 2320 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2321 | * longmode. Also an 64bit guest with a |
| 2322 | * 32bit compat-app running will #UD !! While this |
| 2323 | * behaviour can be fixed (by emulating) into AMD |
| 2324 | * response - CPUs of AMD can't behave like Intel. |
| 2325 | */ |
| 2326 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2327 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2328 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2329 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2330 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2331 | /* AMD ("AuthenticAMD") */ |
| 2332 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2333 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2334 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2335 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2336 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2337 | /* AMD ("AMDisbetter!") */ |
| 2338 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2339 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2340 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2341 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2342 | |
| 2343 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2344 | return false; |
| 2345 | } |
| 2346 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2347 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2348 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2349 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2350 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2351 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2352 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2353 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2354 | |
| 2355 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2356 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2357 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2358 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2359 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2360 | if (!(em_syscall_is_enabled(ctxt))) |
| 2361 | return emulate_ud(ctxt); |
| 2362 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2363 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2364 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2365 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2366 | if (!(efer & EFER_SCE)) |
| 2367 | return emulate_ud(ctxt); |
| 2368 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2369 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2370 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2371 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2372 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2373 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2374 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2375 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2376 | cs.l = 1; |
| 2377 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2378 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2379 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2380 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2381 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2382 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2383 | #ifdef CONFIG_X86_64 |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2384 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2385 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2386 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2387 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2388 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2389 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2390 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2391 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2392 | ctxt->eflags &= ~msr_data; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2393 | ctxt->eflags |= X86_EFLAGS_FIXED_BIT; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2394 | #endif |
| 2395 | } else { |
| 2396 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2397 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2398 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2399 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2400 | ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2403 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2406 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2407 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2408 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2409 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2410 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2411 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2412 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2413 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2414 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2415 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2416 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2417 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2418 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2419 | /* |
| 2420 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2421 | * mode). |
| 2422 | */ |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2423 | if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA) |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2424 | && !vendor_intel(ctxt)) |
| 2425 | return emulate_ud(ctxt); |
| 2426 | |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2427 | /* sysenter/sysexit have not been tested in 64bit mode. */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2428 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2429 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2430 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2431 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2432 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2433 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2434 | if ((msr_data & 0xfffc) == 0x0) |
| 2435 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2436 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2437 | ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2438 | cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2439 | ss_sel = cs_sel + 8; |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2440 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2441 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2442 | cs.l = 1; |
| 2443 | } |
| 2444 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2445 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2446 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2447 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2448 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2449 | ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2450 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2451 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2452 | *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data : |
| 2453 | (u32)msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2454 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2455 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2456 | } |
| 2457 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2458 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2459 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2460 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2461 | struct desc_struct cs, ss; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2462 | u64 msr_data, rcx, rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2463 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2464 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2465 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2466 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2467 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2468 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2469 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2470 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2471 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2472 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2473 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2474 | usermode = X86EMUL_MODE_PROT64; |
| 2475 | else |
| 2476 | usermode = X86EMUL_MODE_PROT32; |
| 2477 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2478 | rcx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2479 | rdx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2480 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2481 | cs.dpl = 3; |
| 2482 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2483 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2484 | switch (usermode) { |
| 2485 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2486 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2487 | if ((msr_data & 0xfffc) == 0x0) |
| 2488 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2489 | ss_sel = (u16)(msr_data + 24); |
Nadav Amit | bf0b682 | 2014-09-18 22:39:45 +0300 | [diff] [blame] | 2490 | rcx = (u32)rcx; |
| 2491 | rdx = (u32)rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2492 | break; |
| 2493 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2494 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2495 | if (msr_data == 0x0) |
| 2496 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2497 | ss_sel = cs_sel + 8; |
| 2498 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2499 | cs.l = 1; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2500 | if (is_noncanonical_address(rcx) || |
| 2501 | is_noncanonical_address(rdx)) |
| 2502 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2503 | break; |
| 2504 | } |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2505 | cs_sel |= SEGMENT_RPL_MASK; |
| 2506 | ss_sel |= SEGMENT_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2507 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2508 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2509 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2510 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2511 | ctxt->_eip = rdx; |
| 2512 | *reg_write(ctxt, VCPU_REGS_RSP) = rcx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2513 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2514 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2517 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2518 | { |
| 2519 | int iopl; |
| 2520 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2521 | return false; |
| 2522 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2523 | return true; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2524 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2525 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2529 | u16 port, u16 len) |
| 2530 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2531 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2532 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2533 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2534 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2535 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2536 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2537 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2538 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2539 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2540 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2541 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2542 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2543 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2544 | base = get_desc_base(&tr_seg); |
| 2545 | #ifdef CONFIG_X86_64 |
| 2546 | base |= ((u64)base3) << 32; |
| 2547 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2548 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2549 | if (r != X86EMUL_CONTINUE) |
| 2550 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2551 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2552 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2553 | 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] | 2554 | if (r != X86EMUL_CONTINUE) |
| 2555 | return false; |
| 2556 | if ((perm >> bit_idx) & mask) |
| 2557 | return false; |
| 2558 | return true; |
| 2559 | } |
| 2560 | |
| 2561 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2562 | u16 port, u16 len) |
| 2563 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2564 | if (ctxt->perm_ok) |
| 2565 | return true; |
| 2566 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2567 | if (emulator_bad_iopl(ctxt)) |
| 2568 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2569 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2570 | |
| 2571 | ctxt->perm_ok = true; |
| 2572 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2573 | return true; |
| 2574 | } |
| 2575 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2576 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2577 | struct tss_segment_16 *tss) |
| 2578 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2579 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2580 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2581 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2582 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2583 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2584 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2585 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2586 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2587 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2588 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2589 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2590 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2591 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2592 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2593 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2594 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | struct tss_segment_16 *tss) |
| 2599 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2600 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2601 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2602 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2603 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2604 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2605 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2606 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2607 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2608 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2609 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2610 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2611 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2612 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2613 | |
| 2614 | /* |
| 2615 | * SDM says that segment selectors are loaded before segment |
| 2616 | * descriptors |
| 2617 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2618 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2619 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2620 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2621 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2622 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2623 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2624 | cpl = tss->cs & 3; |
| 2625 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2626 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2627 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2628 | * it is handled in a context of new task |
| 2629 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2630 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2631 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2632 | if (ret != X86EMUL_CONTINUE) |
| 2633 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2634 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2635 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2636 | if (ret != X86EMUL_CONTINUE) |
| 2637 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2638 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2639 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | if (ret != X86EMUL_CONTINUE) |
| 2641 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2642 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2643 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2644 | if (ret != X86EMUL_CONTINUE) |
| 2645 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2646 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2647 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2648 | if (ret != X86EMUL_CONTINUE) |
| 2649 | return ret; |
| 2650 | |
| 2651 | return X86EMUL_CONTINUE; |
| 2652 | } |
| 2653 | |
| 2654 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2655 | u16 tss_selector, u16 old_tss_sel, |
| 2656 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2657 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2658 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2659 | struct tss_segment_16 tss_seg; |
| 2660 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2661 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2662 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2663 | ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2664 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2665 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2667 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2668 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2669 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2670 | 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] | 2671 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2672 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2673 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2674 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2675 | 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] | 2676 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2677 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2678 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2679 | |
| 2680 | if (old_tss_sel != 0xffff) { |
| 2681 | tss_seg.prev_task_link = old_tss_sel; |
| 2682 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2683 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2684 | &tss_seg.prev_task_link, |
| 2685 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2686 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2687 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2688 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2689 | } |
| 2690 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2691 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2695 | struct tss_segment_32 *tss) |
| 2696 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2697 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2698 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2699 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2700 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2701 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2702 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2703 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2704 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2705 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2706 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2707 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2708 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2709 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2710 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2711 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2712 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2713 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2714 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2718 | struct tss_segment_32 *tss) |
| 2719 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2720 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2721 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2722 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2723 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2724 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2725 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2726 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2727 | |
| 2728 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2729 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2730 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2731 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2732 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2733 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2734 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2735 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2736 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2737 | |
| 2738 | /* |
| 2739 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2740 | * descriptors. This is important because CPL checks will |
| 2741 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2742 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2743 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2744 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2745 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2746 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2747 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2748 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2749 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2750 | |
| 2751 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2752 | * If we're switching between Protected Mode and VM86, we need to make |
| 2753 | * sure to update the mode before loading the segment descriptors so |
| 2754 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2755 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2756 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2757 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2758 | cpl = 3; |
| 2759 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2760 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2761 | cpl = tss->cs & 3; |
| 2762 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2763 | |
| 2764 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2765 | * Now load segment descriptors. If fault happenes at this stage |
| 2766 | * it is handled in a context of new task |
| 2767 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2768 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2769 | cpl, X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2770 | if (ret != X86EMUL_CONTINUE) |
| 2771 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2772 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2773 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2774 | if (ret != X86EMUL_CONTINUE) |
| 2775 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2776 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2777 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2778 | if (ret != X86EMUL_CONTINUE) |
| 2779 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2780 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2781 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2782 | if (ret != X86EMUL_CONTINUE) |
| 2783 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2784 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2785 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2786 | if (ret != X86EMUL_CONTINUE) |
| 2787 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2788 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2789 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2790 | if (ret != X86EMUL_CONTINUE) |
| 2791 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2792 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2793 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2794 | |
Eugene Korenevsky | 2f729b1 | 2015-03-29 01:27:17 +0300 | [diff] [blame^] | 2795 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2799 | u16 tss_selector, u16 old_tss_sel, |
| 2800 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2801 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2802 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2803 | struct tss_segment_32 tss_seg; |
| 2804 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2805 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2806 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2807 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2808 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2809 | 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] | 2810 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2811 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2812 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2813 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2814 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2815 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2816 | /* Only GP registers and segment selectors are saved */ |
| 2817 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2818 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2819 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2820 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2821 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2822 | 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] | 2823 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2824 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2825 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2826 | |
| 2827 | if (old_tss_sel != 0xffff) { |
| 2828 | tss_seg.prev_task_link = old_tss_sel; |
| 2829 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2830 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2831 | &tss_seg.prev_task_link, |
| 2832 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2833 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2834 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2835 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2836 | } |
| 2837 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2838 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2842 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2843 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2844 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2845 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2846 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2847 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2848 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2849 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2850 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2851 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2852 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2853 | |
| 2854 | /* FIXME: old_tss_base == ~0 ? */ |
| 2855 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2856 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2857 | if (ret != X86EMUL_CONTINUE) |
| 2858 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2859 | 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] | 2860 | if (ret != X86EMUL_CONTINUE) |
| 2861 | return ret; |
| 2862 | |
| 2863 | /* FIXME: check that next_tss_desc is tss */ |
| 2864 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2865 | /* |
| 2866 | * Check privileges. The three cases are task switch caused by... |
| 2867 | * |
| 2868 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2869 | * 2. Exception/IRQ/iret: No check is performed |
Nadav Amit | 2c2ca2d | 2014-11-02 11:54:57 +0200 | [diff] [blame] | 2870 | * 3. jmp/call to TSS/task-gate: No check is performed since the |
| 2871 | * hardware checks it before exiting. |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2872 | */ |
| 2873 | if (reason == TASK_SWITCH_GATE) { |
| 2874 | if (idt_index != -1) { |
| 2875 | /* Software interrupts */ |
| 2876 | struct desc_struct task_gate_desc; |
| 2877 | int dpl; |
| 2878 | |
| 2879 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2880 | &task_gate_desc); |
| 2881 | if (ret != X86EMUL_CONTINUE) |
| 2882 | return ret; |
| 2883 | |
| 2884 | dpl = task_gate_desc.dpl; |
| 2885 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2886 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2887 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2888 | } |
| 2889 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2890 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2891 | if (!next_tss_desc.p || |
| 2892 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2893 | desc_limit < 0x2b)) { |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 2894 | return emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2898 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2899 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | if (reason == TASK_SWITCH_IRET) |
| 2903 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2904 | |
| 2905 | /* 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] | 2906 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2907 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2908 | old_tss_sel = 0xffff; |
| 2909 | |
| 2910 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2911 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2912 | old_tss_base, &next_tss_desc); |
| 2913 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2914 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2915 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2916 | if (ret != X86EMUL_CONTINUE) |
| 2917 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2918 | |
| 2919 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2920 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2921 | |
| 2922 | if (reason != TASK_SWITCH_IRET) { |
| 2923 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2924 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2927 | 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] | 2928 | 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] | 2929 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2930 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2931 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2932 | ctxt->lock_prefix = 0; |
| 2933 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2934 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2935 | } |
| 2936 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2937 | return ret; |
| 2938 | } |
| 2939 | |
| 2940 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2941 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2942 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2943 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2944 | int rc; |
| 2945 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2946 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2947 | ctxt->_eip = ctxt->eip; |
| 2948 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2949 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2950 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2951 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2952 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2953 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2954 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2955 | writeback_registers(ctxt); |
| 2956 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2957 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2958 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2961 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2962 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2963 | { |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2964 | int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2965 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 2966 | register_address_increment(ctxt, reg, df * op->bytes); |
| 2967 | op->addr.mem.ea = register_address(ctxt, reg); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2968 | } |
| 2969 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2970 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2971 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2972 | u8 al, old_al; |
| 2973 | bool af, cf, old_cf; |
| 2974 | |
| 2975 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2976 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2977 | |
| 2978 | old_al = al; |
| 2979 | old_cf = cf; |
| 2980 | cf = false; |
| 2981 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2982 | if ((al & 0x0f) > 9 || af) { |
| 2983 | al -= 6; |
| 2984 | cf = old_cf | (al >= 250); |
| 2985 | af = true; |
| 2986 | } else { |
| 2987 | af = false; |
| 2988 | } |
| 2989 | if (old_al > 0x99 || old_cf) { |
| 2990 | al -= 0x60; |
| 2991 | cf = true; |
| 2992 | } |
| 2993 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2994 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2995 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2996 | ctxt->src.type = OP_IMM; |
| 2997 | ctxt->src.val = 0; |
| 2998 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2999 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3000 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 3001 | if (cf) |
| 3002 | ctxt->eflags |= X86_EFLAGS_CF; |
| 3003 | if (af) |
| 3004 | ctxt->eflags |= X86_EFLAGS_AF; |
| 3005 | return X86EMUL_CONTINUE; |
| 3006 | } |
| 3007 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3008 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 3009 | { |
| 3010 | u8 al, ah; |
| 3011 | |
| 3012 | if (ctxt->src.val == 0) |
| 3013 | return emulate_de(ctxt); |
| 3014 | |
| 3015 | al = ctxt->dst.val & 0xff; |
| 3016 | ah = al / ctxt->src.val; |
| 3017 | al %= ctxt->src.val; |
| 3018 | |
| 3019 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 3020 | |
| 3021 | /* Set PF, ZF, SF */ |
| 3022 | ctxt->src.type = OP_IMM; |
| 3023 | ctxt->src.val = 0; |
| 3024 | ctxt->src.bytes = 1; |
| 3025 | fastop(ctxt, em_or); |
| 3026 | |
| 3027 | return X86EMUL_CONTINUE; |
| 3028 | } |
| 3029 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3030 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 3031 | { |
| 3032 | u8 al = ctxt->dst.val & 0xff; |
| 3033 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 3034 | |
| 3035 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 3036 | |
| 3037 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 3038 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 3039 | /* Set PF, ZF, SF */ |
| 3040 | ctxt->src.type = OP_IMM; |
| 3041 | ctxt->src.val = 0; |
| 3042 | ctxt->src.bytes = 1; |
| 3043 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3044 | |
| 3045 | return X86EMUL_CONTINUE; |
| 3046 | } |
| 3047 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3048 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3049 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3050 | int rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3051 | long rel = ctxt->src.val; |
| 3052 | |
| 3053 | ctxt->src.val = (unsigned long)ctxt->_eip; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3054 | rc = jmp_rel(ctxt, rel); |
| 3055 | if (rc != X86EMUL_CONTINUE) |
| 3056 | return rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3057 | return em_push(ctxt); |
| 3058 | } |
| 3059 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3060 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3061 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3062 | u16 sel, old_cs; |
| 3063 | ulong old_eip; |
| 3064 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3065 | struct desc_struct old_desc, new_desc; |
| 3066 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 3067 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3068 | enum x86emul_mode prev_mode = ctxt->mode; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3069 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3070 | old_eip = ctxt->_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3071 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3072 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3073 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3074 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 3075 | X86_TRANSFER_CALL_JMP, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3076 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | 80976db | 2014-12-25 02:52:20 +0200 | [diff] [blame] | 3077 | return rc; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3078 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 3079 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3080 | if (rc != X86EMUL_CONTINUE) |
| 3081 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3082 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3083 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3084 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3085 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3086 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3087 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3088 | ctxt->src.val = old_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3089 | rc = em_push(ctxt); |
| 3090 | /* If we failed, we tainted the memory, but the very least we should |
| 3091 | restore cs */ |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3092 | if (rc != X86EMUL_CONTINUE) { |
| 3093 | pr_warn_once("faulting far call emulation tainted memory\n"); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3094 | goto fail; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3095 | } |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3096 | return rc; |
| 3097 | fail: |
| 3098 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3099 | ctxt->mode = prev_mode; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3100 | return rc; |
| 3101 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3102 | } |
| 3103 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3104 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3105 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3106 | int rc; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3107 | unsigned long eip; |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3108 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3109 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 3110 | if (rc != X86EMUL_CONTINUE) |
| 3111 | return rc; |
| 3112 | rc = assign_eip_near(ctxt, eip); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3113 | if (rc != X86EMUL_CONTINUE) |
| 3114 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3115 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3116 | return X86EMUL_CONTINUE; |
| 3117 | } |
| 3118 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3119 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3120 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3121 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3122 | ctxt->src.val = ctxt->dst.val; |
| 3123 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3124 | |
| 3125 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3126 | ctxt->dst.val = ctxt->src.orig_val; |
| 3127 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3128 | return X86EMUL_CONTINUE; |
| 3129 | } |
| 3130 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3131 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3132 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3133 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3134 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3135 | } |
| 3136 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3137 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3138 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3139 | ctxt->dst.type = OP_REG; |
| 3140 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3141 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3142 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3143 | |
| 3144 | return X86EMUL_CONTINUE; |
| 3145 | } |
| 3146 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3147 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3148 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3149 | u64 tsc = 0; |
| 3150 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3151 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3152 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3153 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3154 | return X86EMUL_CONTINUE; |
| 3155 | } |
| 3156 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3157 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3158 | { |
| 3159 | u64 pmc; |
| 3160 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3161 | 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] | 3162 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3163 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3164 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3165 | return X86EMUL_CONTINUE; |
| 3166 | } |
| 3167 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3168 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3169 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 3170 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3171 | return X86EMUL_CONTINUE; |
| 3172 | } |
| 3173 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3174 | #define FFL(x) bit(X86_FEATURE_##x) |
| 3175 | |
| 3176 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 3177 | { |
| 3178 | u32 ebx, ecx, edx, eax = 1; |
| 3179 | u16 tmp; |
| 3180 | |
| 3181 | /* |
| 3182 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 3183 | */ |
| 3184 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3185 | if (!(ecx & FFL(MOVBE))) |
| 3186 | return emulate_ud(ctxt); |
| 3187 | |
| 3188 | switch (ctxt->op_bytes) { |
| 3189 | case 2: |
| 3190 | /* |
| 3191 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3192 | * the upper word of the destination register remains unchanged |
| 3193 | * ..." |
| 3194 | * |
| 3195 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3196 | * rules so we have to do the operation almost per hand. |
| 3197 | */ |
| 3198 | tmp = (u16)ctxt->src.val; |
| 3199 | ctxt->dst.val &= ~0xffffUL; |
| 3200 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3201 | break; |
| 3202 | case 4: |
| 3203 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3204 | break; |
| 3205 | case 8: |
| 3206 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3207 | break; |
| 3208 | default: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3209 | BUG(); |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3210 | } |
| 3211 | return X86EMUL_CONTINUE; |
| 3212 | } |
| 3213 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3214 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3215 | { |
| 3216 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3217 | return emulate_gp(ctxt, 0); |
| 3218 | |
| 3219 | /* Disable writeback. */ |
| 3220 | ctxt->dst.type = OP_NONE; |
| 3221 | return X86EMUL_CONTINUE; |
| 3222 | } |
| 3223 | |
| 3224 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3225 | { |
| 3226 | unsigned long val; |
| 3227 | |
| 3228 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3229 | val = ctxt->src.val & ~0ULL; |
| 3230 | else |
| 3231 | val = ctxt->src.val & ~0U; |
| 3232 | |
| 3233 | /* #UD condition is already handled. */ |
| 3234 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3235 | return emulate_gp(ctxt, 0); |
| 3236 | |
| 3237 | /* Disable writeback. */ |
| 3238 | ctxt->dst.type = OP_NONE; |
| 3239 | return X86EMUL_CONTINUE; |
| 3240 | } |
| 3241 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3242 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3243 | { |
| 3244 | u64 msr_data; |
| 3245 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3246 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3247 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3248 | 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] | 3249 | return emulate_gp(ctxt, 0); |
| 3250 | |
| 3251 | return X86EMUL_CONTINUE; |
| 3252 | } |
| 3253 | |
| 3254 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3255 | { |
| 3256 | u64 msr_data; |
| 3257 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3258 | 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] | 3259 | return emulate_gp(ctxt, 0); |
| 3260 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3261 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3262 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3263 | return X86EMUL_CONTINUE; |
| 3264 | } |
| 3265 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3266 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3267 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3268 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3269 | return emulate_ud(ctxt); |
| 3270 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3271 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Nadav Amit | b5bbf10 | 2014-11-02 11:54:46 +0200 | [diff] [blame] | 3272 | if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM) |
| 3273 | ctxt->dst.bytes = 2; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3274 | return X86EMUL_CONTINUE; |
| 3275 | } |
| 3276 | |
| 3277 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3278 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3279 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3280 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3281 | 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] | 3282 | return emulate_ud(ctxt); |
| 3283 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3284 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3285 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3286 | |
| 3287 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3288 | ctxt->dst.type = OP_NONE; |
| 3289 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3290 | } |
| 3291 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3292 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3293 | { |
| 3294 | u16 sel = ctxt->src.val; |
| 3295 | |
| 3296 | /* Disable writeback. */ |
| 3297 | ctxt->dst.type = OP_NONE; |
| 3298 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3299 | } |
| 3300 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3301 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3302 | { |
| 3303 | u16 sel = ctxt->src.val; |
| 3304 | |
| 3305 | /* Disable writeback. */ |
| 3306 | ctxt->dst.type = OP_NONE; |
| 3307 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3308 | } |
| 3309 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3310 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3311 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3312 | int rc; |
| 3313 | ulong linear; |
| 3314 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3315 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3316 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3317 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3318 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3319 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3320 | return X86EMUL_CONTINUE; |
| 3321 | } |
| 3322 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3323 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3324 | { |
| 3325 | ulong cr0; |
| 3326 | |
| 3327 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3328 | cr0 &= ~X86_CR0_TS; |
| 3329 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3330 | return X86EMUL_CONTINUE; |
| 3331 | } |
| 3332 | |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 3333 | static int em_hypercall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3334 | { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3335 | int rc = ctxt->ops->fix_hypercall(ctxt); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3336 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3337 | if (rc != X86EMUL_CONTINUE) |
| 3338 | return rc; |
| 3339 | |
| 3340 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3341 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3342 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3343 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3344 | return X86EMUL_CONTINUE; |
| 3345 | } |
| 3346 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3347 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3348 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3349 | struct desc_ptr *ptr)) |
| 3350 | { |
| 3351 | struct desc_ptr desc_ptr; |
| 3352 | |
| 3353 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3354 | ctxt->op_bytes = 8; |
| 3355 | get(ctxt, &desc_ptr); |
| 3356 | if (ctxt->op_bytes == 2) { |
| 3357 | ctxt->op_bytes = 4; |
| 3358 | desc_ptr.address &= 0x00ffffff; |
| 3359 | } |
| 3360 | /* Disable writeback. */ |
| 3361 | ctxt->dst.type = OP_NONE; |
| 3362 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3363 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3364 | } |
| 3365 | |
| 3366 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3367 | { |
| 3368 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3369 | } |
| 3370 | |
| 3371 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3372 | { |
| 3373 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3374 | } |
| 3375 | |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3376 | static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3377 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3378 | struct desc_ptr desc_ptr; |
| 3379 | int rc; |
| 3380 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3381 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3382 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3383 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3384 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3385 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3386 | if (rc != X86EMUL_CONTINUE) |
| 3387 | return rc; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 3388 | if (ctxt->mode == X86EMUL_MODE_PROT64 && |
| 3389 | is_noncanonical_address(desc_ptr.address)) |
| 3390 | return emulate_gp(ctxt, 0); |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3391 | if (lgdt) |
| 3392 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3393 | else |
| 3394 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3395 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3396 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3397 | return X86EMUL_CONTINUE; |
| 3398 | } |
| 3399 | |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3400 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3401 | { |
| 3402 | return em_lgdt_lidt(ctxt, true); |
| 3403 | } |
| 3404 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3405 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3406 | { |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3407 | return em_lgdt_lidt(ctxt, false); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3411 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3412 | if (ctxt->dst.type == OP_MEM) |
| 3413 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3414 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3415 | return X86EMUL_CONTINUE; |
| 3416 | } |
| 3417 | |
| 3418 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3419 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3420 | 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] | 3421 | | (ctxt->src.val & 0x0f)); |
| 3422 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3423 | return X86EMUL_CONTINUE; |
| 3424 | } |
| 3425 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3426 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3427 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3428 | int rc = X86EMUL_CONTINUE; |
| 3429 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 3430 | register_address_increment(ctxt, VCPU_REGS_RCX, -1); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3431 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3432 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3433 | rc = jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3434 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3435 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3439 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3440 | int rc = X86EMUL_CONTINUE; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3441 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3442 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
| 3443 | rc = jmp_rel(ctxt, ctxt->src.val); |
| 3444 | |
| 3445 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3446 | } |
| 3447 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3448 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3449 | { |
| 3450 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3451 | &ctxt->dst.val)) |
| 3452 | return X86EMUL_IO_NEEDED; |
| 3453 | |
| 3454 | return X86EMUL_CONTINUE; |
| 3455 | } |
| 3456 | |
| 3457 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3458 | { |
| 3459 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3460 | &ctxt->src.val, 1); |
| 3461 | /* Disable writeback. */ |
| 3462 | ctxt->dst.type = OP_NONE; |
| 3463 | return X86EMUL_CONTINUE; |
| 3464 | } |
| 3465 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3466 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3467 | { |
| 3468 | if (emulator_bad_iopl(ctxt)) |
| 3469 | return emulate_gp(ctxt, 0); |
| 3470 | |
| 3471 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3472 | return X86EMUL_CONTINUE; |
| 3473 | } |
| 3474 | |
| 3475 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3476 | { |
| 3477 | if (emulator_bad_iopl(ctxt)) |
| 3478 | return emulate_gp(ctxt, 0); |
| 3479 | |
| 3480 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3481 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3482 | return X86EMUL_CONTINUE; |
| 3483 | } |
| 3484 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3485 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3486 | { |
| 3487 | u32 eax, ebx, ecx, edx; |
| 3488 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3489 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3490 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3491 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3492 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3493 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3494 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3495 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3496 | return X86EMUL_CONTINUE; |
| 3497 | } |
| 3498 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3499 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3500 | { |
| 3501 | u32 flags; |
| 3502 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 3503 | flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 3504 | X86_EFLAGS_SF; |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3505 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3506 | |
| 3507 | ctxt->eflags &= ~0xffUL; |
| 3508 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3509 | return X86EMUL_CONTINUE; |
| 3510 | } |
| 3511 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3512 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3513 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3514 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3515 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3516 | return X86EMUL_CONTINUE; |
| 3517 | } |
| 3518 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3519 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3520 | { |
| 3521 | switch (ctxt->op_bytes) { |
| 3522 | #ifdef CONFIG_X86_64 |
| 3523 | case 8: |
| 3524 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3525 | break; |
| 3526 | #endif |
| 3527 | default: |
| 3528 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3529 | break; |
| 3530 | } |
| 3531 | return X86EMUL_CONTINUE; |
| 3532 | } |
| 3533 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3534 | static int em_clflush(struct x86_emulate_ctxt *ctxt) |
| 3535 | { |
| 3536 | /* emulating clflush regardless of cpuid */ |
| 3537 | return X86EMUL_CONTINUE; |
| 3538 | } |
| 3539 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 3540 | static int em_movsxd(struct x86_emulate_ctxt *ctxt) |
| 3541 | { |
| 3542 | ctxt->dst.val = (s32) ctxt->src.val; |
| 3543 | return X86EMUL_CONTINUE; |
| 3544 | } |
| 3545 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3546 | static bool valid_cr(int nr) |
| 3547 | { |
| 3548 | switch (nr) { |
| 3549 | case 0: |
| 3550 | case 2 ... 4: |
| 3551 | case 8: |
| 3552 | return true; |
| 3553 | default: |
| 3554 | return false; |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3559 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3560 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3561 | return emulate_ud(ctxt); |
| 3562 | |
| 3563 | return X86EMUL_CONTINUE; |
| 3564 | } |
| 3565 | |
| 3566 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3567 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3568 | u64 new_val = ctxt->src.val64; |
| 3569 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3570 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3571 | |
| 3572 | static u64 cr_reserved_bits[] = { |
| 3573 | 0xffffffff00000000ULL, |
| 3574 | 0, 0, 0, /* CR3 checked later */ |
| 3575 | CR4_RESERVED_BITS, |
| 3576 | 0, 0, 0, |
| 3577 | CR8_RESERVED_BITS, |
| 3578 | }; |
| 3579 | |
| 3580 | if (!valid_cr(cr)) |
| 3581 | return emulate_ud(ctxt); |
| 3582 | |
| 3583 | if (new_val & cr_reserved_bits[cr]) |
| 3584 | return emulate_gp(ctxt, 0); |
| 3585 | |
| 3586 | switch (cr) { |
| 3587 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3588 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3589 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3590 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3591 | return emulate_gp(ctxt, 0); |
| 3592 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3593 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3594 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3595 | |
| 3596 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3597 | !(cr4 & X86_CR4_PAE)) |
| 3598 | return emulate_gp(ctxt, 0); |
| 3599 | |
| 3600 | break; |
| 3601 | } |
| 3602 | case 3: { |
| 3603 | u64 rsvd = 0; |
| 3604 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3605 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3606 | if (efer & EFER_LMA) |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 3607 | rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3608 | |
| 3609 | if (new_val & rsvd) |
| 3610 | return emulate_gp(ctxt, 0); |
| 3611 | |
| 3612 | break; |
| 3613 | } |
| 3614 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3615 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3616 | |
| 3617 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3618 | return emulate_gp(ctxt, 0); |
| 3619 | |
| 3620 | break; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | return X86EMUL_CONTINUE; |
| 3625 | } |
| 3626 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3627 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3628 | { |
| 3629 | unsigned long dr7; |
| 3630 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3631 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3632 | |
| 3633 | /* Check if DR7.Global_Enable is set */ |
| 3634 | return dr7 & (1 << 13); |
| 3635 | } |
| 3636 | |
| 3637 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3638 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3639 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3640 | u64 cr4; |
| 3641 | |
| 3642 | if (dr > 7) |
| 3643 | return emulate_ud(ctxt); |
| 3644 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3645 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3646 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3647 | return emulate_ud(ctxt); |
| 3648 | |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3649 | if (check_dr7_gd(ctxt)) { |
| 3650 | ulong dr6; |
| 3651 | |
| 3652 | ctxt->ops->get_dr(ctxt, 6, &dr6); |
| 3653 | dr6 &= ~15; |
| 3654 | dr6 |= DR6_BD | DR6_RTM; |
| 3655 | ctxt->ops->set_dr(ctxt, 6, dr6); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3656 | return emulate_db(ctxt); |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3657 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3658 | |
| 3659 | return X86EMUL_CONTINUE; |
| 3660 | } |
| 3661 | |
| 3662 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3663 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3664 | u64 new_val = ctxt->src.val64; |
| 3665 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3666 | |
| 3667 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3668 | return emulate_gp(ctxt, 0); |
| 3669 | |
| 3670 | return check_dr_read(ctxt); |
| 3671 | } |
| 3672 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3673 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3674 | { |
| 3675 | u64 efer; |
| 3676 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3677 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3678 | |
| 3679 | if (!(efer & EFER_SVME)) |
| 3680 | return emulate_ud(ctxt); |
| 3681 | |
| 3682 | return X86EMUL_CONTINUE; |
| 3683 | } |
| 3684 | |
| 3685 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3686 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3687 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3688 | |
| 3689 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3690 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3691 | return emulate_gp(ctxt, 0); |
| 3692 | |
| 3693 | return check_svme(ctxt); |
| 3694 | } |
| 3695 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3696 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3697 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3698 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3699 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3700 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3701 | return emulate_ud(ctxt); |
| 3702 | |
| 3703 | return X86EMUL_CONTINUE; |
| 3704 | } |
| 3705 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3706 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3707 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3708 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3709 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3710 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3711 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3712 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3713 | return emulate_gp(ctxt, 0); |
| 3714 | |
| 3715 | return X86EMUL_CONTINUE; |
| 3716 | } |
| 3717 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3718 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3719 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3720 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3721 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3722 | return emulate_gp(ctxt, 0); |
| 3723 | |
| 3724 | return X86EMUL_CONTINUE; |
| 3725 | } |
| 3726 | |
| 3727 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3728 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3729 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3730 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3731 | return emulate_gp(ctxt, 0); |
| 3732 | |
| 3733 | return X86EMUL_CONTINUE; |
| 3734 | } |
| 3735 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3736 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3737 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 3738 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 3739 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3740 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3741 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3742 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3743 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3744 | #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) } |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 3745 | #define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3746 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3747 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3748 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3749 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3750 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3751 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3752 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 3753 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3754 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3755 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3756 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3757 | #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] | 3758 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3759 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3760 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3761 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3762 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3763 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3764 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3765 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3766 | |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3767 | static const struct opcode group7_rm0[] = { |
| 3768 | N, |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 3769 | I(SrcNone | Priv | EmulateOnUD, em_hypercall), |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3770 | N, N, N, N, N, N, |
| 3771 | }; |
| 3772 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3773 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3774 | DI(SrcNone | Priv, monitor), |
| 3775 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3776 | N, N, N, N, N, N, |
| 3777 | }; |
| 3778 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3779 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3780 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 3781 | II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3782 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3783 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3784 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3785 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3786 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3787 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3788 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3789 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3790 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3791 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3792 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3793 | N, N, N, N, N, N, |
| 3794 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3795 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3796 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3797 | F(Lock, em_add), |
| 3798 | F(Lock | PageTable, em_or), |
| 3799 | F(Lock, em_adc), |
| 3800 | F(Lock, em_sbb), |
| 3801 | F(Lock | PageTable, em_and), |
| 3802 | F(Lock, em_sub), |
| 3803 | F(Lock, em_xor), |
| 3804 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3805 | }; |
| 3806 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3807 | static const struct opcode group1A[] = { |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 3808 | I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3809 | }; |
| 3810 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3811 | static const struct opcode group2[] = { |
| 3812 | F(DstMem | ModRM, em_rol), |
| 3813 | F(DstMem | ModRM, em_ror), |
| 3814 | F(DstMem | ModRM, em_rcl), |
| 3815 | F(DstMem | ModRM, em_rcr), |
| 3816 | F(DstMem | ModRM, em_shl), |
| 3817 | F(DstMem | ModRM, em_shr), |
| 3818 | F(DstMem | ModRM, em_shl), |
| 3819 | F(DstMem | ModRM, em_sar), |
| 3820 | }; |
| 3821 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3822 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3823 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3824 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3825 | F(DstMem | SrcNone | Lock, em_not), |
| 3826 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3827 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3828 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3829 | F(DstXacc | Src2Mem, em_div_ex), |
| 3830 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3831 | }; |
| 3832 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3833 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3834 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3835 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3836 | N, N, N, N, N, N, |
| 3837 | }; |
| 3838 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3839 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3840 | F(DstMem | SrcNone | Lock, em_inc), |
| 3841 | F(DstMem | SrcNone | Lock, em_dec), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3842 | I(SrcMem | NearBranch, em_call_near_abs), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3843 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3844 | I(SrcMem | NearBranch, em_jmp_abs), |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 3845 | I(SrcMemFAddr | ImplicitOps, em_jmp_far), |
| 3846 | I(SrcMem | Stack, em_push), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3847 | }; |
| 3848 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3849 | static const struct opcode group6[] = { |
Nadav Amit | 63ea0a4 | 2015-01-08 11:59:03 +0100 | [diff] [blame] | 3850 | DI(Prot | DstMem, sldt), |
| 3851 | DI(Prot | DstMem, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3852 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3853 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3854 | N, N, N, N, |
| 3855 | }; |
| 3856 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3857 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3858 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3859 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3860 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3861 | II(SrcMem | Priv, em_lidt, lidt), |
| 3862 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3863 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3864 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3865 | }, { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3866 | EXT(0, group7_rm0), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3867 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3868 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3869 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3870 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3871 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3872 | } }; |
| 3873 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3874 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3875 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3876 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3877 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3878 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3879 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3880 | }; |
| 3881 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3882 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3883 | 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] | 3884 | }, { |
| 3885 | N, N, N, N, N, N, N, N, |
| 3886 | } }; |
| 3887 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3888 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3889 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3890 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3891 | }; |
| 3892 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3893 | static const struct gprefix pfx_0f_ae_7 = { |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 3894 | I(SrcMem | ByteOp, em_clflush), N, N, N, |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3895 | }; |
| 3896 | |
| 3897 | static const struct group_dual group15 = { { |
| 3898 | N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7), |
| 3899 | }, { |
| 3900 | N, N, N, N, N, N, N, N, |
| 3901 | } }; |
| 3902 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3903 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3904 | 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] | 3905 | }; |
| 3906 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3907 | static const struct instr_dual instr_dual_0f_2b = { |
| 3908 | I(0, em_mov), N |
| 3909 | }; |
| 3910 | |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 3911 | static const struct gprefix pfx_0f_2b = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3912 | ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3913 | }; |
| 3914 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3915 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3916 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3917 | }; |
| 3918 | |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 3919 | static const struct gprefix pfx_0f_e7 = { |
| 3920 | N, I(Sse, em_mov), N, N, |
| 3921 | }; |
| 3922 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3923 | static const struct escape escape_d9 = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3924 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3925 | }, { |
| 3926 | /* 0xC0 - 0xC7 */ |
| 3927 | N, N, N, N, N, N, N, N, |
| 3928 | /* 0xC8 - 0xCF */ |
| 3929 | N, N, N, N, N, N, N, N, |
| 3930 | /* 0xD0 - 0xC7 */ |
| 3931 | N, N, N, N, N, N, N, N, |
| 3932 | /* 0xD8 - 0xDF */ |
| 3933 | N, N, N, N, N, N, N, N, |
| 3934 | /* 0xE0 - 0xE7 */ |
| 3935 | N, N, N, N, N, N, N, N, |
| 3936 | /* 0xE8 - 0xEF */ |
| 3937 | N, N, N, N, N, N, N, N, |
| 3938 | /* 0xF0 - 0xF7 */ |
| 3939 | N, N, N, N, N, N, N, N, |
| 3940 | /* 0xF8 - 0xFF */ |
| 3941 | N, N, N, N, N, N, N, N, |
| 3942 | } }; |
| 3943 | |
| 3944 | static const struct escape escape_db = { { |
| 3945 | N, N, N, N, N, N, N, N, |
| 3946 | }, { |
| 3947 | /* 0xC0 - 0xC7 */ |
| 3948 | N, N, N, N, N, N, N, N, |
| 3949 | /* 0xC8 - 0xCF */ |
| 3950 | N, N, N, N, N, N, N, N, |
| 3951 | /* 0xD0 - 0xC7 */ |
| 3952 | N, N, N, N, N, N, N, N, |
| 3953 | /* 0xD8 - 0xDF */ |
| 3954 | N, N, N, N, N, N, N, N, |
| 3955 | /* 0xE0 - 0xE7 */ |
| 3956 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3957 | /* 0xE8 - 0xEF */ |
| 3958 | N, N, N, N, N, N, N, N, |
| 3959 | /* 0xF0 - 0xF7 */ |
| 3960 | N, N, N, N, N, N, N, N, |
| 3961 | /* 0xF8 - 0xFF */ |
| 3962 | N, N, N, N, N, N, N, N, |
| 3963 | } }; |
| 3964 | |
| 3965 | static const struct escape escape_dd = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3966 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3967 | }, { |
| 3968 | /* 0xC0 - 0xC7 */ |
| 3969 | N, N, N, N, N, N, N, N, |
| 3970 | /* 0xC8 - 0xCF */ |
| 3971 | N, N, N, N, N, N, N, N, |
| 3972 | /* 0xD0 - 0xC7 */ |
| 3973 | N, N, N, N, N, N, N, N, |
| 3974 | /* 0xD8 - 0xDF */ |
| 3975 | N, N, N, N, N, N, N, N, |
| 3976 | /* 0xE0 - 0xE7 */ |
| 3977 | N, N, N, N, N, N, N, N, |
| 3978 | /* 0xE8 - 0xEF */ |
| 3979 | N, N, N, N, N, N, N, N, |
| 3980 | /* 0xF0 - 0xF7 */ |
| 3981 | N, N, N, N, N, N, N, N, |
| 3982 | /* 0xF8 - 0xFF */ |
| 3983 | N, N, N, N, N, N, N, N, |
| 3984 | } }; |
| 3985 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3986 | static const struct instr_dual instr_dual_0f_c3 = { |
| 3987 | I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N |
| 3988 | }; |
| 3989 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 3990 | static const struct mode_dual mode_dual_63 = { |
| 3991 | N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd) |
| 3992 | }; |
| 3993 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3994 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3995 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3996 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3997 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3998 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3999 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4000 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4001 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 4002 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4003 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4004 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4005 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 4006 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4007 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4008 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4009 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 4010 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4011 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4012 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4013 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4014 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4015 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4016 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4017 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4018 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4019 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 4020 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4021 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 4022 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4023 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 4024 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4025 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 4026 | I(ImplicitOps | Stack | No64, em_pusha), |
| 4027 | I(ImplicitOps | Stack | No64, em_popa), |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4028 | N, MD(ModRM, &mode_dual_63), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4029 | N, N, N, N, |
| 4030 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 4031 | I(SrcImm | Mov | Stack, em_push), |
| 4032 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 4033 | I(SrcImmByte | Mov | Stack, em_push), |
| 4034 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4035 | 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] | 4036 | 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] | 4037 | /* 0x70 - 0x7F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4038 | X16(D(SrcImmByte | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4039 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4040 | G(ByteOp | DstMem | SrcImm, group1), |
| 4041 | G(DstMem | SrcImm, group1), |
| 4042 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 4043 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4044 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4045 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4046 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4047 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4048 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4049 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 4050 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 4051 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 4052 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4053 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4054 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4055 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 4056 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 4057 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 4058 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 4059 | II(ImplicitOps | Stack, em_popf, popf), |
| 4060 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4061 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4062 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4063 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4064 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4065 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4066 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4067 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4068 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 4069 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4070 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4071 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4072 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4073 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4074 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4075 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4076 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4077 | I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm), |
| 4078 | I(ImplicitOps | NearBranch, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4079 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 4080 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4081 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4082 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 4083 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Nadav Amit | 16794aa | 2015-01-26 09:32:22 +0200 | [diff] [blame] | 4084 | I(ImplicitOps | SrcImmU16, em_ret_far_imm), |
| 4085 | I(ImplicitOps, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4086 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4087 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4088 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4089 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 4090 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 4091 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 4092 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 4093 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4094 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4095 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4096 | 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] | 4097 | /* 0xE0 - 0xE7 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4098 | X3(I(SrcImmByte | NearBranch, em_loop)), |
| 4099 | I(SrcImmByte | NearBranch, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4100 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 4101 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4102 | /* 0xE8 - 0xEF */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4103 | I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch), |
| 4104 | I(SrcImmFAddr | No64, em_jmp_far), |
| 4105 | D(SrcImmByte | ImplicitOps | NearBranch), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4106 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 4107 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4108 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4109 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4110 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 4111 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4112 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 4113 | D(ImplicitOps), D(ImplicitOps), |
| 4114 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4115 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 4116 | }; |
| 4117 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4118 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4119 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4120 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4121 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4122 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4123 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4124 | N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4125 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4126 | N, N, N, N, N, N, N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4127 | D(ImplicitOps | ModRM | SrcMem | NoAccess), |
| 4128 | N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4129 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 4130 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 4131 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 4132 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 4133 | check_cr_write), |
| 4134 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 4135 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4136 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4137 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 4138 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4139 | N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4140 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4141 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4142 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4143 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4144 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4145 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4146 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 4147 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4148 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4149 | N, N, N, N, N, N, N, N, |
| 4150 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4151 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4152 | /* 0x50 - 0x5F */ |
| 4153 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4154 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4155 | N, N, N, N, |
| 4156 | N, N, N, N, |
| 4157 | N, N, N, N, |
| 4158 | 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] | 4159 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4160 | N, N, N, N, |
| 4161 | N, N, N, N, |
| 4162 | N, N, N, N, |
| 4163 | 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] | 4164 | /* 0x80 - 0x8F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4165 | X16(D(SrcImm | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4166 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4167 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4168 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4169 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4170 | II(ImplicitOps, em_cpuid, cpuid), |
| 4171 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4172 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4173 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4174 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4175 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4176 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4177 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4178 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4179 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4180 | GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4181 | /* 0xB0 - 0xB7 */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 4182 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4183 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4184 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4185 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4186 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4187 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4188 | /* 0xB8 - 0xBF */ |
| 4189 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4190 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4191 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
Nadav Amit | 900efe2 | 2015-03-30 15:39:21 +0300 | [diff] [blame] | 4192 | I(DstReg | SrcMem | ModRM, em_bsf_c), |
| 4193 | I(DstReg | SrcMem | ModRM, em_bsr_c), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4194 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4195 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 4196 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4197 | N, ID(0, &instr_dual_0f_c3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4198 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4199 | /* 0xC8 - 0xCF */ |
| 4200 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4201 | /* 0xD0 - 0xDF */ |
| 4202 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4203 | /* 0xE0 - 0xEF */ |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4204 | N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), |
| 4205 | N, N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4206 | /* 0xF0 - 0xFF */ |
| 4207 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4208 | }; |
| 4209 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4210 | static const struct instr_dual instr_dual_0f_38_f0 = { |
| 4211 | I(DstReg | SrcMem | Mov, em_movbe), N |
| 4212 | }; |
| 4213 | |
| 4214 | static const struct instr_dual instr_dual_0f_38_f1 = { |
| 4215 | I(DstMem | SrcReg | Mov, em_movbe), N |
| 4216 | }; |
| 4217 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4218 | static const struct gprefix three_byte_0f_38_f0 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4219 | ID(0, &instr_dual_0f_38_f0), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4220 | }; |
| 4221 | |
| 4222 | static const struct gprefix three_byte_0f_38_f1 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4223 | ID(0, &instr_dual_0f_38_f1), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4224 | }; |
| 4225 | |
| 4226 | /* |
| 4227 | * Insns below are selected by the prefix which indexed by the third opcode |
| 4228 | * byte. |
| 4229 | */ |
| 4230 | static const struct opcode opcode_map_0f_38[256] = { |
| 4231 | /* 0x00 - 0x7f */ |
| 4232 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4233 | /* 0x80 - 0xef */ |
| 4234 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 4235 | /* 0xf0 - 0xf1 */ |
Nadav Amit | 53bb4f7 | 2014-12-07 11:49:42 +0200 | [diff] [blame] | 4236 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), |
| 4237 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1), |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4238 | /* 0xf2 - 0xff */ |
| 4239 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4240 | }; |
| 4241 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4242 | #undef D |
| 4243 | #undef N |
| 4244 | #undef G |
| 4245 | #undef GD |
| 4246 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4247 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4248 | #undef EXT |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4249 | #undef MD |
Nadav Amit | 2b42fce | 2015-01-26 09:32:25 +0200 | [diff] [blame] | 4250 | #undef ID |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4251 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4252 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4253 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4254 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4255 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4256 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4257 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4258 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4259 | { |
| 4260 | unsigned size; |
| 4261 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4262 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4263 | if (size == 8) |
| 4264 | size = 4; |
| 4265 | return size; |
| 4266 | } |
| 4267 | |
| 4268 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4269 | unsigned size, bool sign_extension) |
| 4270 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4271 | int rc = X86EMUL_CONTINUE; |
| 4272 | |
| 4273 | op->type = OP_IMM; |
| 4274 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4275 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4276 | /* NB. Immediates are sign-extended as necessary. */ |
| 4277 | switch (op->bytes) { |
| 4278 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4279 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4280 | break; |
| 4281 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4282 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4283 | break; |
| 4284 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4285 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4286 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4287 | case 8: |
| 4288 | op->val = insn_fetch(s64, ctxt); |
| 4289 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4290 | } |
| 4291 | if (!sign_extension) { |
| 4292 | switch (op->bytes) { |
| 4293 | case 1: |
| 4294 | op->val &= 0xff; |
| 4295 | break; |
| 4296 | case 2: |
| 4297 | op->val &= 0xffff; |
| 4298 | break; |
| 4299 | case 4: |
| 4300 | op->val &= 0xffffffff; |
| 4301 | break; |
| 4302 | } |
| 4303 | } |
| 4304 | done: |
| 4305 | return rc; |
| 4306 | } |
| 4307 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4308 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4309 | unsigned d) |
| 4310 | { |
| 4311 | int rc = X86EMUL_CONTINUE; |
| 4312 | |
| 4313 | switch (d) { |
| 4314 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4315 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4316 | break; |
| 4317 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4318 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4319 | break; |
| 4320 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4321 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4322 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4323 | *op = ctxt->memop; |
| 4324 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4325 | if (ctxt->d & BitOp) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4326 | fetch_bit_operand(ctxt); |
| 4327 | op->orig_val = op->val; |
| 4328 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4329 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4330 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4331 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4332 | case OpAcc: |
| 4333 | op->type = OP_REG; |
| 4334 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4335 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4336 | fetch_register_operand(op); |
| 4337 | op->orig_val = op->val; |
| 4338 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4339 | case OpAccLo: |
| 4340 | op->type = OP_REG; |
| 4341 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4342 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4343 | fetch_register_operand(op); |
| 4344 | op->orig_val = op->val; |
| 4345 | break; |
| 4346 | case OpAccHi: |
| 4347 | if (ctxt->d & ByteOp) { |
| 4348 | op->type = OP_NONE; |
| 4349 | break; |
| 4350 | } |
| 4351 | op->type = OP_REG; |
| 4352 | op->bytes = ctxt->op_bytes; |
| 4353 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4354 | fetch_register_operand(op); |
| 4355 | op->orig_val = op->val; |
| 4356 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4357 | case OpDI: |
| 4358 | op->type = OP_MEM; |
| 4359 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4360 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4361 | register_address(ctxt, VCPU_REGS_RDI); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4362 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4363 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4364 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4365 | break; |
| 4366 | case OpDX: |
| 4367 | op->type = OP_REG; |
| 4368 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4369 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4370 | fetch_register_operand(op); |
| 4371 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4372 | case OpCL: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4373 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4374 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4375 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4376 | break; |
| 4377 | case OpImmByte: |
| 4378 | rc = decode_imm(ctxt, op, 1, true); |
| 4379 | break; |
| 4380 | case OpOne: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4381 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4382 | op->bytes = 1; |
| 4383 | op->val = 1; |
| 4384 | break; |
| 4385 | case OpImm: |
| 4386 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4387 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4388 | case OpImm64: |
| 4389 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4390 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4391 | case OpMem8: |
| 4392 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4393 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4394 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4395 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4396 | fetch_register_operand(&ctxt->memop); |
| 4397 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4398 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4399 | case OpMem16: |
| 4400 | ctxt->memop.bytes = 2; |
| 4401 | goto mem_common; |
| 4402 | case OpMem32: |
| 4403 | ctxt->memop.bytes = 4; |
| 4404 | goto mem_common; |
| 4405 | case OpImmU16: |
| 4406 | rc = decode_imm(ctxt, op, 2, false); |
| 4407 | break; |
| 4408 | case OpImmU: |
| 4409 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4410 | break; |
| 4411 | case OpSI: |
| 4412 | op->type = OP_MEM; |
| 4413 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4414 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4415 | register_address(ctxt, VCPU_REGS_RSI); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4416 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4417 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4418 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4419 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4420 | case OpXLat: |
| 4421 | op->type = OP_MEM; |
| 4422 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4423 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4424 | address_mask(ctxt, |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4425 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4426 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4427 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4428 | op->val = 0; |
| 4429 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4430 | case OpImmFAddr: |
| 4431 | op->type = OP_IMM; |
| 4432 | op->addr.mem.ea = ctxt->_eip; |
| 4433 | op->bytes = ctxt->op_bytes + 2; |
| 4434 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4435 | break; |
| 4436 | case OpMemFAddr: |
| 4437 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4438 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4439 | case OpES: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4440 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4441 | op->val = VCPU_SREG_ES; |
| 4442 | break; |
| 4443 | case OpCS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4444 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4445 | op->val = VCPU_SREG_CS; |
| 4446 | break; |
| 4447 | case OpSS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4448 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4449 | op->val = VCPU_SREG_SS; |
| 4450 | break; |
| 4451 | case OpDS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4452 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4453 | op->val = VCPU_SREG_DS; |
| 4454 | break; |
| 4455 | case OpFS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4456 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4457 | op->val = VCPU_SREG_FS; |
| 4458 | break; |
| 4459 | case OpGS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4460 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4461 | op->val = VCPU_SREG_GS; |
| 4462 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4463 | case OpImplicit: |
| 4464 | /* Special instructions do their own operand decoding. */ |
| 4465 | default: |
| 4466 | op->type = OP_NONE; /* Disable writeback. */ |
| 4467 | break; |
| 4468 | } |
| 4469 | |
| 4470 | done: |
| 4471 | return rc; |
| 4472 | } |
| 4473 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4474 | 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] | 4475 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4476 | int rc = X86EMUL_CONTINUE; |
| 4477 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4478 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4479 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4480 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4481 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4482 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4483 | ctxt->memop.type = OP_NONE; |
| 4484 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4485 | ctxt->_eip = ctxt->eip; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 4486 | ctxt->fetch.ptr = ctxt->fetch.data; |
| 4487 | ctxt->fetch.end = ctxt->fetch.data + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4488 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4489 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4490 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4491 | else { |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 4492 | rc = __do_insn_fetch_bytes(ctxt, 1); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4493 | if (rc != X86EMUL_CONTINUE) |
| 4494 | return rc; |
| 4495 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4496 | |
| 4497 | switch (mode) { |
| 4498 | case X86EMUL_MODE_REAL: |
| 4499 | case X86EMUL_MODE_VM86: |
| 4500 | case X86EMUL_MODE_PROT16: |
| 4501 | def_op_bytes = def_ad_bytes = 2; |
| 4502 | break; |
| 4503 | case X86EMUL_MODE_PROT32: |
| 4504 | def_op_bytes = def_ad_bytes = 4; |
| 4505 | break; |
| 4506 | #ifdef CONFIG_X86_64 |
| 4507 | case X86EMUL_MODE_PROT64: |
| 4508 | def_op_bytes = 4; |
| 4509 | def_ad_bytes = 8; |
| 4510 | break; |
| 4511 | #endif |
| 4512 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4513 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4514 | } |
| 4515 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4516 | ctxt->op_bytes = def_op_bytes; |
| 4517 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4518 | |
| 4519 | /* Legacy prefixes. */ |
| 4520 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4521 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4522 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4523 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4524 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4525 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4526 | break; |
| 4527 | case 0x67: /* address-size override */ |
| 4528 | if (mode == X86EMUL_MODE_PROT64) |
| 4529 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4530 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4531 | else |
| 4532 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4533 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4534 | break; |
| 4535 | case 0x26: /* ES override */ |
| 4536 | case 0x2e: /* CS override */ |
| 4537 | case 0x36: /* SS override */ |
| 4538 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4539 | has_seg_override = true; |
| 4540 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4541 | break; |
| 4542 | case 0x64: /* FS override */ |
| 4543 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4544 | has_seg_override = true; |
| 4545 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4546 | break; |
| 4547 | case 0x40 ... 0x4f: /* REX */ |
| 4548 | if (mode != X86EMUL_MODE_PROT64) |
| 4549 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4550 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4551 | continue; |
| 4552 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4553 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4554 | break; |
| 4555 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4556 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4557 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4558 | break; |
| 4559 | default: |
| 4560 | goto done_prefixes; |
| 4561 | } |
| 4562 | |
| 4563 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4564 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4565 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | done_prefixes: |
| 4569 | |
| 4570 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4571 | if (ctxt->rex_prefix & 8) |
| 4572 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4573 | |
| 4574 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4575 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4576 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4577 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4578 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4579 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4580 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4581 | |
| 4582 | /* 0F_38 opcode map */ |
| 4583 | if (ctxt->b == 0x38) { |
| 4584 | ctxt->opcode_len = 3; |
| 4585 | ctxt->b = insn_fetch(u8, ctxt); |
| 4586 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4587 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4588 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4589 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4590 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4591 | if (ctxt->d & ModRM) |
| 4592 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4593 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4594 | /* vex-prefix instructions are not implemented */ |
| 4595 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
Nadav Amit | d14cb5d | 2014-11-02 11:54:58 +0200 | [diff] [blame] | 4596 | (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) { |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4597 | ctxt->d = NotImpl; |
| 4598 | } |
| 4599 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4600 | while (ctxt->d & GroupMask) { |
| 4601 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4602 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4603 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4604 | opcode = opcode.u.group[goffset]; |
| 4605 | break; |
| 4606 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4607 | goffset = (ctxt->modrm >> 3) & 7; |
| 4608 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4609 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4610 | else |
| 4611 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4612 | break; |
| 4613 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4614 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4615 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4616 | break; |
| 4617 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4618 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4619 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4620 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4621 | switch (simd_prefix) { |
| 4622 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4623 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4624 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4625 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4626 | } |
| 4627 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4628 | case Escape: |
| 4629 | if (ctxt->modrm > 0xbf) |
| 4630 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4631 | else |
| 4632 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4633 | break; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4634 | case InstrDual: |
| 4635 | if ((ctxt->modrm >> 6) == 3) |
| 4636 | opcode = opcode.u.idual->mod3; |
| 4637 | else |
| 4638 | opcode = opcode.u.idual->mod012; |
| 4639 | break; |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4640 | case ModeDual: |
| 4641 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 4642 | opcode = opcode.u.mdual->mode64; |
| 4643 | else |
| 4644 | opcode = opcode.u.mdual->mode32; |
| 4645 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4646 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4647 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4648 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4649 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4650 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4651 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 4654 | /* Unrecognised? */ |
| 4655 | if (ctxt->d == 0) |
| 4656 | return EMULATION_FAILED; |
| 4657 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4658 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4659 | |
Nadav Amit | 3a6095a | 2014-08-13 16:50:13 +0300 | [diff] [blame] | 4660 | if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) |
| 4661 | return EMULATION_FAILED; |
| 4662 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4663 | if (unlikely(ctxt->d & |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4664 | (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch| |
| 4665 | No16))) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4666 | /* |
| 4667 | * These are copied unconditionally here, and checked unconditionally |
| 4668 | * in x86_emulate_insn. |
| 4669 | */ |
| 4670 | ctxt->check_perm = opcode.check_perm; |
| 4671 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4672 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4673 | if (ctxt->d & NotImpl) |
| 4674 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4675 | |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4676 | if (mode == X86EMUL_MODE_PROT64) { |
| 4677 | if (ctxt->op_bytes == 4 && (ctxt->d & Stack)) |
| 4678 | ctxt->op_bytes = 8; |
| 4679 | else if (ctxt->d & NearBranch) |
| 4680 | ctxt->op_bytes = 8; |
| 4681 | } |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4682 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4683 | if (ctxt->d & Op3264) { |
| 4684 | if (mode == X86EMUL_MODE_PROT64) |
| 4685 | ctxt->op_bytes = 8; |
| 4686 | else |
| 4687 | ctxt->op_bytes = 4; |
| 4688 | } |
| 4689 | |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4690 | if ((ctxt->d & No16) && ctxt->op_bytes == 2) |
| 4691 | ctxt->op_bytes = 4; |
| 4692 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4693 | if (ctxt->d & Sse) |
| 4694 | ctxt->op_bytes = 16; |
| 4695 | else if (ctxt->d & Mmx) |
| 4696 | ctxt->op_bytes = 8; |
| 4697 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4698 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4699 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4700 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4701 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4702 | if (!has_seg_override) { |
| 4703 | has_seg_override = true; |
| 4704 | ctxt->seg_override = ctxt->modrm_seg; |
| 4705 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4706 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4707 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4708 | if (rc != X86EMUL_CONTINUE) |
| 4709 | goto done; |
| 4710 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4711 | if (!has_seg_override) |
| 4712 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4713 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4714 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4715 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4716 | /* |
| 4717 | * Decode and fetch the source operand: register, memory |
| 4718 | * or immediate. |
| 4719 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4720 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4721 | if (rc != X86EMUL_CONTINUE) |
| 4722 | goto done; |
| 4723 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4724 | /* |
| 4725 | * Decode and fetch the second source operand: register, memory |
| 4726 | * or immediate. |
| 4727 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4728 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4729 | if (rc != X86EMUL_CONTINUE) |
| 4730 | goto done; |
| 4731 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4732 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4733 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4734 | |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 4735 | if (ctxt->rip_relative) |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 4736 | ctxt->memopp->addr.mem.ea = address_mask(ctxt, |
| 4737 | ctxt->memopp->addr.mem.ea + ctxt->_eip); |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4738 | |
Paolo Bonzini | a430c91 | 2014-10-23 14:54:14 +0200 | [diff] [blame] | 4739 | done: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4740 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4741 | } |
| 4742 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4743 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4744 | { |
| 4745 | return ctxt->d & PageTable; |
| 4746 | } |
| 4747 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4748 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4749 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4750 | /* The second termination condition only applies for REPE |
| 4751 | * and REPNE. Test if the repeat string operation prefix is |
| 4752 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4753 | * corresponding termination condition according to: |
| 4754 | * - if REPE/REPZ and ZF = 0 then done |
| 4755 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4756 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4757 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4758 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4759 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 4760 | ((ctxt->eflags & X86_EFLAGS_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4761 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 4762 | ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF)))) |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4763 | return true; |
| 4764 | |
| 4765 | return false; |
| 4766 | } |
| 4767 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4768 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4769 | { |
| 4770 | bool fault = false; |
| 4771 | |
| 4772 | ctxt->ops->get_fpu(ctxt); |
| 4773 | asm volatile("1: fwait \n\t" |
| 4774 | "2: \n\t" |
| 4775 | ".pushsection .fixup,\"ax\" \n\t" |
| 4776 | "3: \n\t" |
| 4777 | "movb $1, %[fault] \n\t" |
| 4778 | "jmp 2b \n\t" |
| 4779 | ".popsection \n\t" |
| 4780 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4781 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4782 | ctxt->ops->put_fpu(ctxt); |
| 4783 | |
| 4784 | if (unlikely(fault)) |
| 4785 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4786 | |
| 4787 | return X86EMUL_CONTINUE; |
| 4788 | } |
| 4789 | |
| 4790 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4791 | struct operand *op) |
| 4792 | { |
| 4793 | if (op->type == OP_MM) |
| 4794 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4795 | } |
| 4796 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4797 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4798 | { |
| 4799 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4800 | if (!(ctxt->d & ByteOp)) |
| 4801 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4802 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4803 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4804 | [fastop]"+S"(fop) |
| 4805 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4806 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4807 | if (!fop) /* exception is returned in fop variable */ |
| 4808 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4809 | return X86EMUL_CONTINUE; |
| 4810 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4811 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4812 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 4813 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4814 | memset(&ctxt->rip_relative, 0, |
| 4815 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4816 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4817 | ctxt->io_read.pos = 0; |
| 4818 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4819 | ctxt->mem_read.end = 0; |
| 4820 | } |
| 4821 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4822 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4823 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4824 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4825 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4826 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4827 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4828 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4829 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4830 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4831 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4832 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4833 | goto done; |
| 4834 | } |
| 4835 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4836 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4837 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4838 | goto done; |
| 4839 | } |
| 4840 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4841 | if (unlikely(ctxt->d & |
| 4842 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 4843 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4844 | (ctxt->d & Undefined)) { |
| 4845 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4846 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4847 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4848 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4849 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4850 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 4851 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4852 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4853 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4854 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4855 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 4856 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4857 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4858 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4859 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4860 | if (ctxt->d & Mmx) { |
| 4861 | rc = flush_pending_x87_faults(ctxt); |
| 4862 | if (rc != X86EMUL_CONTINUE) |
| 4863 | goto done; |
| 4864 | /* |
| 4865 | * Now that we know the fpu is exception safe, we can fetch |
| 4866 | * operands from it. |
| 4867 | */ |
| 4868 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4869 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4870 | if (!(ctxt->d & Mov)) |
| 4871 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4872 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4873 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4874 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4875 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4876 | X86_ICPT_PRE_EXCEPT); |
| 4877 | if (rc != X86EMUL_CONTINUE) |
| 4878 | goto done; |
| 4879 | } |
| 4880 | |
Nadav Amit | 64a3829 | 2014-12-10 11:19:04 +0200 | [diff] [blame] | 4881 | /* Instruction can only be executed in protected mode */ |
| 4882 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 4883 | rc = emulate_ud(ctxt); |
| 4884 | goto done; |
| 4885 | } |
| 4886 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4887 | /* Privileged instruction can be executed only in CPL=0 */ |
| 4888 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 4889 | if (ctxt->d & PrivUD) |
| 4890 | rc = emulate_ud(ctxt); |
| 4891 | else |
| 4892 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4893 | goto done; |
| 4894 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4895 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4896 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4897 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4898 | rc = ctxt->check_perm(ctxt); |
| 4899 | if (rc != X86EMUL_CONTINUE) |
| 4900 | goto done; |
| 4901 | } |
| 4902 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4903 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4904 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4905 | X86_ICPT_POST_EXCEPT); |
| 4906 | if (rc != X86EMUL_CONTINUE) |
| 4907 | goto done; |
| 4908 | } |
| 4909 | |
| 4910 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 4911 | /* All REP prefixes have the same first termination condition */ |
| 4912 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
| 4913 | ctxt->eip = ctxt->_eip; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 4914 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4915 | goto done; |
| 4916 | } |
| 4917 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4918 | } |
| 4919 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4920 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4921 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4922 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4923 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4924 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4925 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4926 | } |
| 4927 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4928 | if (ctxt->src2.type == OP_MEM) { |
| 4929 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4930 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4931 | if (rc != X86EMUL_CONTINUE) |
| 4932 | goto done; |
| 4933 | } |
| 4934 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4935 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4936 | goto special_insn; |
| 4937 | |
| 4938 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4939 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4940 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4941 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4942 | &ctxt->dst.val, ctxt->dst.bytes); |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 4943 | if (rc != X86EMUL_CONTINUE) { |
Paolo Bonzini | d44e121 | 2015-02-09 10:02:05 +0100 | [diff] [blame] | 4944 | if (!(ctxt->d & NoWrite) && |
| 4945 | rc == X86EMUL_PROPAGATE_FAULT && |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 4946 | ctxt->exception.vector == PF_VECTOR) |
| 4947 | ctxt->exception.error_code |= PFERR_WRITE_MASK; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4948 | goto done; |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 4949 | } |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4950 | } |
Paolo Bonzini | 4ff6f8e | 2015-02-12 17:04:47 +0100 | [diff] [blame] | 4951 | /* Copy full 64-bit value for CMPXCHG8B. */ |
| 4952 | ctxt->dst.orig_val64 = ctxt->dst.val64; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4953 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4954 | special_insn: |
| 4955 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4956 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4957 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4958 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4959 | if (rc != X86EMUL_CONTINUE) |
| 4960 | goto done; |
| 4961 | } |
| 4962 | |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 4963 | if (ctxt->rep_prefix && (ctxt->d & String)) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 4964 | ctxt->eflags |= X86_EFLAGS_RF; |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 4965 | else |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 4966 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4967 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4968 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4969 | if (ctxt->d & Fastop) { |
| 4970 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4971 | rc = fastop(ctxt, fop); |
| 4972 | if (rc != X86EMUL_CONTINUE) |
| 4973 | goto done; |
| 4974 | goto writeback; |
| 4975 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4976 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4977 | if (rc != X86EMUL_CONTINUE) |
| 4978 | goto done; |
| 4979 | goto writeback; |
| 4980 | } |
| 4981 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4982 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4983 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4984 | else if (ctxt->opcode_len == 3) |
| 4985 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4986 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4987 | switch (ctxt->b) { |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4988 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4989 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4990 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4991 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4992 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4993 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4994 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4995 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4996 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 4997 | ctxt->dst.type = OP_NONE; |
| 4998 | else |
| 4999 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 5000 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 5001 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5002 | switch (ctxt->op_bytes) { |
| 5003 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 5004 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 5005 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 5006 | } |
| 5007 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5008 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 5009 | rc = emulate_int(ctxt, 3); |
| 5010 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5011 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5012 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5013 | break; |
| 5014 | case 0xce: /* into */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5015 | if (ctxt->eflags & X86_EFLAGS_OF) |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 5016 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5017 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 5018 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 5019 | case 0xeb: /* jmp rel short */ |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5020 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5021 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 5022 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5023 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 5024 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 5025 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5026 | case 0xf5: /* cmc */ |
| 5027 | /* complement carry flag from eflags reg */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5028 | ctxt->eflags ^= X86_EFLAGS_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5029 | break; |
| 5030 | case 0xf8: /* clc */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5031 | ctxt->eflags &= ~X86_EFLAGS_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5032 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 5033 | case 0xf9: /* stc */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5034 | ctxt->eflags |= X86_EFLAGS_CF; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 5035 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5036 | case 0xfc: /* cld */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5037 | ctxt->eflags &= ~X86_EFLAGS_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5038 | break; |
| 5039 | case 0xfd: /* std */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5040 | ctxt->eflags |= X86_EFLAGS_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5041 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5042 | default: |
| 5043 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5044 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5045 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5046 | if (rc != X86EMUL_CONTINUE) |
| 5047 | goto done; |
| 5048 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5049 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 5050 | if (ctxt->d & SrcWrite) { |
| 5051 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 5052 | rc = writeback(ctxt, &ctxt->src); |
| 5053 | if (rc != X86EMUL_CONTINUE) |
| 5054 | goto done; |
| 5055 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 5056 | if (!(ctxt->d & NoWrite)) { |
| 5057 | rc = writeback(ctxt, &ctxt->dst); |
| 5058 | if (rc != X86EMUL_CONTINUE) |
| 5059 | goto done; |
| 5060 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5061 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5062 | /* |
| 5063 | * restore dst type in case the decoding will be reused |
| 5064 | * (happens for string instruction ) |
| 5065 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5066 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5067 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5068 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5069 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 5070 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5071 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5072 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 5073 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5074 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5075 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5076 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5077 | if ((ctxt->d & SrcMask) == SrcSI) |
| 5078 | count = ctxt->src.count; |
| 5079 | else |
| 5080 | count = ctxt->dst.count; |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 5081 | register_address_increment(ctxt, VCPU_REGS_RCX, -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5082 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5083 | if (!string_insn_completed(ctxt)) { |
| 5084 | /* |
| 5085 | * Re-enter guest when pio read ahead buffer is empty |
| 5086 | * or, if it is not used, after each 1024 iteration. |
| 5087 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5088 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5089 | (r->end == 0 || r->end != r->pos)) { |
| 5090 | /* |
| 5091 | * Reset read cache. Usually happens before |
| 5092 | * decode, but since instruction is restarted |
| 5093 | * we have to do it here. |
| 5094 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5095 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5096 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5097 | return EMULATION_RESTART; |
| 5098 | } |
| 5099 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 5100 | } |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5101 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5102 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5103 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5104 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5105 | |
| 5106 | done: |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5107 | if (rc == X86EMUL_PROPAGATE_FAULT) { |
| 5108 | WARN_ON(ctxt->exception.vector > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 5109 | ctxt->have_exception = true; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5110 | } |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 5111 | if (rc == X86EMUL_INTERCEPTED) |
| 5112 | return EMULATION_INTERCEPTED; |
| 5113 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5114 | if (rc == X86EMUL_CONTINUE) |
| 5115 | writeback_registers(ctxt); |
| 5116 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5117 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5118 | |
| 5119 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5120 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5121 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 5122 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5123 | break; |
| 5124 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5125 | case 0x0d: /* GrpP (prefetch) */ |
| 5126 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 5127 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5128 | break; |
| 5129 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5130 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5131 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5132 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5133 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5134 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5135 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 5136 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 5137 | ctxt->dst.val = ctxt->src.val; |
Nadav Amit | b91aa14 | 2015-03-30 15:39:19 +0300 | [diff] [blame] | 5138 | else if (ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5139 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5140 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5141 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5142 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5143 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5144 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5145 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5146 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5147 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5148 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5149 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5150 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5151 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5152 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5153 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5154 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5155 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5156 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5157 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5158 | default: |
| 5159 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5160 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5161 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5162 | threebyte_insn: |
| 5163 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5164 | if (rc != X86EMUL_CONTINUE) |
| 5165 | goto done; |
| 5166 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5167 | goto writeback; |
| 5168 | |
| 5169 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 5170 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5171 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5172 | |
| 5173 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5174 | { |
| 5175 | invalidate_registers(ctxt); |
| 5176 | } |
| 5177 | |
| 5178 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5179 | { |
| 5180 | writeback_registers(ctxt); |
| 5181 | } |